summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/dir/resourcedir.cpp33
-rw-r--r--kabc/plugins/file/resourcefile.cpp36
2 files changed, 32 insertions, 37 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp
index f354a9e..be19821 100644
--- a/kabc/plugins/dir/resourcedir.cpp
+++ b/kabc/plugins/dir/resourcedir.cpp
@@ -1,172 +1,171 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28 28
29#include <sys/types.h> 29#include <sys/types.h>
30#include <sys/stat.h> 30#include <sys/stat.h>
31#ifndef _WIN32_ 31#ifndef _WIN32_
32#include <unistd.h> 32#include <unistd.h>
33#endif 33#endif
34 34
35#include <qregexp.h> 35#include <qregexp.h>
36#include <qtimer.h> 36#include <qtimer.h>
37#include <qwidget.h> 37#include <qwidget.h>
38 38
39#include <kapplication.h> 39#include <kapplication.h>
40#include <kconfig.h> 40#include <kconfig.h>
41#include <kdebug.h> 41#include <kdebug.h>
42//US #include <kgenericfactory.h> 42//US #include <kgenericfactory.h>
43#include <kglobal.h> 43#include <kglobal.h>
44#include <klocale.h> 44#include <klocale.h>
45#include <kstandarddirs.h> 45#include <kstandarddirs.h>
46#include <kurlrequester.h> 46#include <kurlrequester.h>
47 47
48#include "addressbook.h" 48#include "addressbook.h"
49 49
50//US #include "formatfactory.h" 50#include "formatfactory.h"
51 51
52#include "resourcedirconfig.h" 52#include "resourcedirconfig.h"
53#include "stdaddressbook.h" 53#include "stdaddressbook.h"
54 54
55//US 55//US
56#include <qdir.h> 56#include <qdir.h>
57#include <formats/vcardformatplugin2.h> 57//US #include "../../formats/vcardformatplugin2.h"
58#include <formats/binaryformat.h> 58//US #include "../../formats/binaryformat.h"
59 59
60#include "resourcedir.h" 60#include "resourcedir.h"
61 61
62using namespace KABC; 62using namespace KABC;
63 63
64extern "C" 64extern "C"
65{ 65{
66 void *init_kabc_dir() 66//US void *init_kabc_dir()
67 void *init_microkabc_dir()
67 { 68 {
68 qDebug("resourcedir.cpp : init_kabc_dir has to be changed"); 69 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>();
69//US return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>();
70 return 0;
71 } 70 }
72} 71}
73 72
74 73
75ResourceDir::ResourceDir( const KConfig *config ) 74ResourceDir::ResourceDir( const KConfig *config )
76 : Resource( config ) 75 : Resource( config )
77{ 76{
78 QString path; 77 QString path;
79 78
80 KConfig *cfg = (KConfig *)config; 79 KConfig *cfg = (KConfig *)config;
81 if ( cfg ) { 80 if ( cfg ) {
82//US path = config->readEntry( "FilePath" ); 81//US path = config->readEntry( "FilePath" );
83 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() ); 82 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() );
84//US mFormatName = config->readEntry( "FileFormat" ); 83//US mFormatName = config->readEntry( "FileFormat" );
85 mFormatName = cfg->readEntry( "FileFormat", "vcard" ); 84 mFormatName = cfg->readEntry( "FileFormat", "vcard" );
86 } else { 85 } else {
87 path = StdAddressBook::directoryName(); 86 path = StdAddressBook::directoryName();
88 mFormatName = "vcard"; 87 mFormatName = "vcard";
89 } 88 }
90 89
91 90
92/*US FormatFactory *factory = FormatFactory::self(); 91 FormatFactory *factory = FormatFactory::self();
93 mFormat = factory->format( mFormatName ); 92 mFormat = factory->format( mFormatName );
94 93
95 if ( !mFormat ) { 94 if ( !mFormat ) {
96 mFormatName = "vcard"; 95 mFormatName = "vcard";
97 mFormat = factory->format( mFormatName ); 96 mFormat = factory->format( mFormatName );
98 } 97 }
99*/
100 98
99/*US
101//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); 100//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1());
102 if (mFormatName == "vcard") 101 if (mFormatName == "vcard")
103 mFormat = new VCardFormatPlugin2(); 102 mFormat = new VCardFormatPlugin2();
104 else if (mFormatName == "binary") 103 else if (mFormatName == "binary")
105 mFormat = new BinaryFormat(); 104 mFormat = new BinaryFormat();
106 else 105 else
107 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); 106 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1());
108 107*/
109 108
110/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods 109/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods
111 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); 110 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) );
112 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); 111 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) );
113 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); 112 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) );
114*/ 113*/
115 114
116 setPath( path ); 115 setPath( path );
117} 116}
118 117
119ResourceDir::~ResourceDir() 118ResourceDir::~ResourceDir()
120{ 119{
121 delete mFormat; 120 delete mFormat;
122 mFormat = 0; 121 mFormat = 0;
123} 122}
124 123
125void ResourceDir::writeConfig( KConfig *config ) 124void ResourceDir::writeConfig( KConfig *config )
126{ 125{
127 Resource::writeConfig( config ); 126 Resource::writeConfig( config );
128 127
129 config->writeEntry( "FilePath", mPath ); 128 config->writeEntry( "FilePath", mPath );
130 config->writeEntry( "FileFormat", mFormatName ); 129 config->writeEntry( "FileFormat", mFormatName );
131} 130}
132 131
133Ticket *ResourceDir::requestSaveTicket() 132Ticket *ResourceDir::requestSaveTicket()
134{ 133{
135 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl; 134 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl;
136 135
137 if ( !addressBook() ) return 0; 136 if ( !addressBook() ) return 0;
138 137
139 if ( !lock( mPath ) ) { 138 if ( !lock( mPath ) ) {
140 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '" 139 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '"
141 << mPath << "'" << endl; 140 << mPath << "'" << endl;
142 return 0; 141 return 0;
143 } 142 }
144 return createTicket( this ); 143 return createTicket( this );
145} 144}
146 145
147 146
148bool ResourceDir::doOpen() 147bool ResourceDir::doOpen()
149{ 148{
150 QDir dir( mPath ); 149 QDir dir( mPath );
151 if ( !dir.exists() ) { // no directory available 150 if ( !dir.exists() ) { // no directory available
152 return dir.mkdir( dir.path() ); 151 return dir.mkdir( dir.path() );
153 } else { 152 } else {
154 QString testName = dir.entryList( QDir::Files )[0]; 153 QString testName = dir.entryList( QDir::Files )[0];
155 if ( testName.isNull() || testName.isEmpty() ) // no file in directory 154 if ( testName.isNull() || testName.isEmpty() ) // no file in directory
156 return true; 155 return true;
157 156
158 QFile file( mPath + "/" + testName ); 157 QFile file( mPath + "/" + testName );
159 if ( file.open( IO_ReadOnly ) ) 158 if ( file.open( IO_ReadOnly ) )
160 return true; 159 return true;
161 160
162 if ( file.size() == 0 ) 161 if ( file.size() == 0 )
163 return true; 162 return true;
164 163
165 bool ok = mFormat->checkFormat( &file ); 164 bool ok = mFormat->checkFormat( &file );
166 file.close(); 165 file.close();
167 return ok; 166 return ok;
168 } 167 }
169} 168}
170 169
171void ResourceDir::doClose() 170void ResourceDir::doClose()
172{ 171{
@@ -200,164 +199,160 @@ bool ResourceDir::load()
200} 199}
201 200
202bool ResourceDir::save( Ticket *ticket ) 201bool ResourceDir::save( Ticket *ticket )
203{ 202{
204 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl; 203 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl;
205 204
206 AddressBook::Iterator it; 205 AddressBook::Iterator it;
207 bool ok = true; 206 bool ok = true;
208 207
209 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 208 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
210 if ( (*it).resource() != this || !(*it).changed() ) 209 if ( (*it).resource() != this || !(*it).changed() )
211 continue; 210 continue;
212 211
213 QFile file( mPath + "/" + (*it).uid() ); 212 QFile file( mPath + "/" + (*it).uid() );
214 if ( !file.open( IO_WriteOnly ) ) { 213 if ( !file.open( IO_WriteOnly ) ) {
215 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); 214 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) );
216 continue; 215 continue;
217 } 216 }
218 217
219 mFormat->save( *it, &file ); 218 mFormat->save( *it, &file );
220 219
221 // mark as unchanged 220 // mark as unchanged
222 (*it).setChanged( false ); 221 (*it).setChanged( false );
223 222
224 file.close(); 223 file.close();
225 } 224 }
226 225
227 delete ticket; 226 delete ticket;
228 unlock( mPath ); 227 unlock( mPath );
229 228
230 return ok; 229 return ok;
231} 230}
232 231
233bool ResourceDir::lock( const QString &path ) 232bool ResourceDir::lock( const QString &path )
234{ 233{
235 kdDebug(5700) << "ResourceDir::lock()" << endl; 234 kdDebug(5700) << "ResourceDir::lock()" << endl;
236 235
237 QString p = path; 236 QString p = path;
238//US change the implementation how the lockfilename is getting created 237//US change the implementation how the lockfilename is getting created
239//US p.replace( QRegExp("/"), "_" ); 238//US p.replace( QRegExp("/"), "_" );
240//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); 239//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" );
241 KURL url(p); 240 KURL url(p);
242 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 241 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
243 242
244 243
245 kdDebug(5700) << "-- lock name: " << lockName << endl; 244 kdDebug(5700) << "-- lock name: " << lockName << endl;
246 245
247 if ( QFile::exists( lockName ) ) return false; 246 if ( QFile::exists( lockName ) ) return false;
248 247
249 QString lockUniqueName; 248 QString lockUniqueName;
250 lockUniqueName = p + KApplication::randomString( 8 ); 249 lockUniqueName = p + KApplication::randomString( 8 );
251 250
252 url = lockUniqueName; 251 url = lockUniqueName;
253//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 252//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
254 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 253 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
255 254
256 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 255 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
257 256
258 // Create unique file 257 // Create unique file
259 QFile file( mLockUniqueName ); 258 QFile file( mLockUniqueName );
260 file.open( IO_WriteOnly ); 259 file.open( IO_WriteOnly );
261 file.close(); 260 file.close();
262 261
263 // Create lock file 262 // Create lock file
264#ifdef _WIN32_
265 int result = 0;
266 qDebug("WARNING: ResourceDir::lock cannot link ");
267#else
268 int result = ::link( QFile::encodeName( mLockUniqueName ), 263 int result = ::link( QFile::encodeName( mLockUniqueName ),
269 QFile::encodeName( lockName ) ); 264 QFile::encodeName( lockName ) );
270#endif 265
271 if ( result == 0 ) { 266 if ( result == 0 ) {
272 addressBook()->emitAddressBookLocked(); 267 addressBook()->emitAddressBookLocked();
273 return true; 268 return true;
274 } 269 }
275 270
276 // TODO: check stat 271 // TODO: check stat
277 272
278 return false; 273 return false;
279} 274}
280 275
281void ResourceDir::unlock( const QString &path ) 276void ResourceDir::unlock( const QString &path )
282{ 277{
283 QString p = path; 278 QString p = path;
284//US change the implementation how the lockfilename is getting created 279//US change the implementation how the lockfilename is getting created
285//US p.replace( QRegExp( "/" ), "_" ); 280//US p.replace( QRegExp( "/" ), "_" );
286//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); 281//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" );
287 KURL url(p); 282 KURL url(p);
288 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); 283 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" );
289 284
290 ::unlink( QFile::encodeName( lockName ) ); 285 ::unlink( QFile::encodeName( lockName ) );
291 QFile::remove( mLockUniqueName ); 286 QFile::remove( mLockUniqueName );
292 addressBook()->emitAddressBookUnlocked(); 287 addressBook()->emitAddressBookUnlocked();
293} 288}
294 289
295void ResourceDir::setPath( const QString &path ) 290void ResourceDir::setPath( const QString &path )
296{ 291{
297/*US ToDo: no synchronization so far. Has to be changed in the future 292/*US ToDo: no synchronization so far. Has to be changed in the future
298 mDirWatch.stopScan(); 293 mDirWatch.stopScan();
299 mDirWatch.removeDir( mPath ); 294 mDirWatch.removeDir( mPath );
300*/ 295*/
301 mPath = path; 296 mPath = path;
302 297
303/*US ToDo: no synchronization so far. Has to be changed in the future 298/*US ToDo: no synchronization so far. Has to be changed in the future
304 mDirWatch.addDir( mPath, true ); 299 mDirWatch.addDir( mPath, true );
305 mDirWatch.startScan(); 300 mDirWatch.startScan();
306*/ 301*/
307 302
308//US simulate KDirWatch event 303//US simulate KDirWatch event
309 pathChanged(); 304 pathChanged();
310 305
311} 306}
312 307
313QString ResourceDir::path() const 308QString ResourceDir::path() const
314{ 309{
315 return mPath; 310 return mPath;
316} 311}
317 312
318void ResourceDir::setFormat( const QString &format ) 313void ResourceDir::setFormat( const QString &format )
319{ 314{
320 mFormatName = format; 315 mFormatName = format;
321 316
322 if ( mFormat ) 317 if ( mFormat )
323 delete mFormat; 318 delete mFormat;
324 319
325//US FormatFactory *factory = FormatFactory::self(); 320 FormatFactory *factory = FormatFactory::self();
326//US mFormat = factory->format( mFormatName ); 321 mFormat = factory->format( mFormatName );
327 322/*US
328qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); 323qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1());
329 if (mFormatName == "vcard") 324 if (mFormatName == "vcard")
330 mFormat = new VCardFormatPlugin2(); 325 mFormat = new VCardFormatPlugin2();
331 else if (mFormatName == "binary") 326 else if (mFormatName == "binary")
332 mFormat = new BinaryFormat(); 327 mFormat = new BinaryFormat();
333 else 328 else
334 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1()); 329 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1());
335 330*/
336 331
337} 332}
338 333
339QString ResourceDir::format() const 334QString ResourceDir::format() const
340{ 335{
341 return mFormatName; 336 return mFormatName;
342} 337}
343 338
344void ResourceDir::pathChanged() 339void ResourceDir::pathChanged()
345{ 340{
346 if ( !addressBook() ) 341 if ( !addressBook() )
347 return; 342 return;
348 343
349 load(); 344 load();
350 addressBook()->emitAddressBookChanged(); 345 addressBook()->emitAddressBookChanged();
351} 346}
352 347
353void ResourceDir::removeAddressee( const Addressee& addr ) 348void ResourceDir::removeAddressee( const Addressee& addr )
354{ 349{
355 QFile::remove( mPath + "/" + addr.uid() ); 350 QFile::remove( mPath + "/" + addr.uid() );
356} 351}
357 352
358void ResourceDir::cleanUp() 353void ResourceDir::cleanUp()
359{ 354{
360 unlock( mPath ); 355 unlock( mPath );
361} 356}
362 357
363//US #include "resourcedir.moc" 358//US #include "resourcedir.moc"
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index d30ed2f..80af841 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -1,389 +1,389 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28#include <sys/types.h> 28#include <sys/types.h>
29#include <sys/stat.h> 29#include <sys/stat.h>
30#ifndef _WIN32_ 30#ifndef _WIN32_
31#include <unistd.h> 31#include <unistd.h>
32#endif 32#endif
33 33
34#include <qfile.h> 34#include <qfile.h>
35#include <qfileinfo.h> 35#include <qfileinfo.h>
36#include <qregexp.h> 36#include <qregexp.h>
37#include <qtimer.h> 37#include <qtimer.h>
38 38
39#include <kapplication.h> 39#include <kapplication.h>
40#include <kconfig.h> 40#include <kconfig.h>
41#include <kdebug.h> 41#include <kdebug.h>
42#include <klocale.h> 42#include <klocale.h>
43//US #include <ksavefile.h> 43//US #include <ksavefile.h>
44#include <kstandarddirs.h> 44#include <kstandarddirs.h>
45 45
46//US #include "formatfactory.h" 46#include "formatfactory.h"
47 47
48#include "resource.h" 48#include "resource.h"
49#include "resourcefileconfig.h" 49#include "resourcefileconfig.h"
50#include "stdaddressbook.h" 50#include "stdaddressbook.h"
51 51
52#include <formats/vcardformatplugin2.h> 52//US #include "../../formats/vcardformatplugin2.h"
53#include <formats/binaryformat.h> 53//US #include "../../formats/binaryformat.h"
54 54
55 55
56#include "resourcefile.h" 56#include "resourcefile.h"
57 57
58using namespace KABC; 58using namespace KABC;
59 59
60extern "C" 60extern "C"
61{ 61{
62 void *init_kabc_file() 62//US void *init_kabc_file()
63 void *init_microkabc_file()
63 { 64 {
64 qDebug("!!!resourcefile.cpp : init_kabc_file has to be changed"); 65 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>();
65//US return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>();
66 return 0;
67 } 66 }
68} 67}
69 68
70 69
71ResourceFile::ResourceFile( const KConfig *config ) 70ResourceFile::ResourceFile( const KConfig *config )
72 : Resource( config ) , mFormat( 0 ) 71 : Resource( config ) , mFormat( 0 )
73{ 72{
74 QString fileName, formatName; 73 QString fileName, formatName;
75 74
76 KConfig *cfg = (KConfig *)config; 75 KConfig *cfg = (KConfig *)config;
77 if ( cfg ) { 76 if ( cfg ) {
78 fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() ); 77 fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() );
79 formatName = cfg->readEntry( "FileFormat", "vcard" ); 78 formatName = cfg->readEntry( "FileFormat", "vcard" );
80// qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() ); 79// qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() );
81 } else { 80 } else {
82 fileName = StdAddressBook::fileName(); 81 fileName = StdAddressBook::fileName();
83 formatName = "vcard"; 82 formatName = "vcard";
84// qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() ); 83// qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() );
85 } 84 }
86 85
87 init( fileName, formatName ); 86 init( fileName, formatName );
88} 87}
89 88
90ResourceFile::ResourceFile( const QString &fileName, 89ResourceFile::ResourceFile( const QString &fileName,
91 const QString &formatName ) 90 const QString &formatName )
92 : Resource( 0 ) 91 : Resource( 0 )
93{ 92{
94// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 93// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
95 init( fileName, formatName ); 94 init( fileName, formatName );
96} 95}
97 96
98void ResourceFile::init( const QString &fileName, const QString &formatName ) 97void ResourceFile::init( const QString &fileName, const QString &formatName )
99{ 98{
100 mFormatName = formatName; 99 mFormatName = formatName;
101 100
102/*US FormatFactory *factory = FormatFactory::self(); 101 FormatFactory *factory = FormatFactory::self();
103 mFormat = factory->format( mFormatName ); 102 mFormat = factory->format( mFormatName );
104 103
105 if ( !mFormat ) { 104 if ( !mFormat ) {
106 mFormatName = "vcard"; 105 mFormatName = "vcard";
107 mFormat = factory->format( mFormatName ); 106 mFormat = factory->format( mFormatName );
108 } 107 }
109*/
110 108
109/*US
111//US qDebug("ResourceFile::init initialized with format %s ", formatName.latin1()); 110//US qDebug("ResourceFile::init initialized with format %s ", formatName.latin1());
112 if (mFormatName == "vcard") { 111 if (mFormatName == "vcard") {
113 mFormat = new VCardFormatPlugin2(); 112 mFormat = new VCardFormatPlugin2();
114// qDebug("ResourceFile::init format VCardFormatPlugin2"); 113// qDebug("ResourceFile::init format VCardFormatPlugin2");
115 } 114 }
116 else if (mFormatName == "binary") { 115 else if (mFormatName == "binary") {
117 mFormat = new BinaryFormat(); 116 mFormat = new BinaryFormat();
118// qDebug("ResourceFile::init format BinaryFormat"); 117// qDebug("ResourceFile::init format BinaryFormat");
119 } 118 }
120 else 119 else
121 qDebug("ResourceFile::init format unknown !!! %s ", formatName.latin1()); 120 qDebug("ResourceFile::init format unknown !!! %s ", formatName.latin1());
121*/
122 122
123/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods 123/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods
124 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 124 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
125 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 125 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
126 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 126 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
127*/ 127*/
128 128
129 setFileName( fileName ); 129 setFileName( fileName );
130} 130}
131 131
132ResourceFile::~ResourceFile() 132ResourceFile::~ResourceFile()
133{ 133{
134 delete mFormat; 134 delete mFormat;
135 mFormat = 0; 135 mFormat = 0;
136} 136}
137 137
138void ResourceFile::writeConfig( KConfig *config ) 138void ResourceFile::writeConfig( KConfig *config )
139{ 139{
140 Resource::writeConfig( config ); 140 Resource::writeConfig( config );
141 141
142 config->writeEntry( "FileName", mFileName ); 142 config->writeEntry( "FileName", mFileName );
143 config->writeEntry( "FileFormat", mFormatName ); 143 config->writeEntry( "FileFormat", mFormatName );
144 144
145// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); 145// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
146 146
147} 147}
148 148
149Ticket *ResourceFile::requestSaveTicket() 149Ticket *ResourceFile::requestSaveTicket()
150{ 150{
151 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; 151 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
152 152
153 if ( !addressBook() ) return 0; 153 if ( !addressBook() ) return 0;
154 154
155 if ( !lock( mFileName ) ) { 155 if ( !lock( mFileName ) ) {
156 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" 156 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '"
157 << mFileName << "'" << endl; 157 << mFileName << "'" << endl;
158 return 0; 158 return 0;
159 } 159 }
160 return createTicket( this ); 160 return createTicket( this );
161} 161}
162 162
163 163
164bool ResourceFile::doOpen() 164bool ResourceFile::doOpen()
165{ 165{
166 QFile file( mFileName ); 166 QFile file( mFileName );
167 167
168 if ( !file.exists() ) { 168 if ( !file.exists() ) {
169 // try to create the file 169 // try to create the file
170 bool ok = file.open( IO_WriteOnly ); 170 bool ok = file.open( IO_WriteOnly );
171 if ( ok ) 171 if ( ok )
172 file.close(); 172 file.close();
173 173
174 return ok; 174 return ok;
175 } else { 175 } else {
176 if ( !file.open( IO_ReadWrite ) ) 176 if ( !file.open( IO_ReadWrite ) )
177 return false; 177 return false;
178 178
179 if ( file.size() == 0 ) { 179 if ( file.size() == 0 ) {
180 file.close(); 180 file.close();
181 return true; 181 return true;
182 } 182 }
183 183
184 bool ok = mFormat->checkFormat( &file ); 184 bool ok = mFormat->checkFormat( &file );
185 file.close(); 185 file.close();
186 186
187 return ok; 187 return ok;
188 } 188 }
189} 189}
190 190
191void ResourceFile::doClose() 191void ResourceFile::doClose()
192{ 192{
193} 193}
194 194
195bool ResourceFile::load() 195bool ResourceFile::load()
196{ 196{
197 kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl; 197 kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl;
198 198
199 QFile file( mFileName ); 199 QFile file( mFileName );
200 if ( !file.open( IO_ReadOnly ) ) { 200 if ( !file.open( IO_ReadOnly ) ) {
201 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 201 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
202 return false; 202 return false;
203 } 203 }
204 204
205// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 205// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
206 206
207 return mFormat->loadAll( addressBook(), this, &file ); 207 return mFormat->loadAll( addressBook(), this, &file );
208} 208}
209 209
210bool ResourceFile::save( Ticket *ticket ) 210bool ResourceFile::save( Ticket *ticket )
211{ 211{
212// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 212// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
213 kdDebug(5700) << "ResourceFile::save()" << endl; 213 kdDebug(5700) << "ResourceFile::save()" << endl;
214 214
215 // create backup file 215 // create backup file
216 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 216 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
217 217
218/*US we use a simpler method to create a backupfile 218/*US we use a simpler method to create a backupfile
219 219
220 (void) KSaveFile::backupFile( mFileName, QString::null /*directory*/ 220 (void) KSaveFile::backupFile( mFileName, QString::null
221/*US ,extension ); 221 ,extension );
222 222
223 KSaveFile saveFile( mFileName ); 223 KSaveFile saveFile( mFileName );
224 bool ok = false; 224 bool ok = false;
225 if ( saveFile.status() == 0 && saveFile.file() ) 225 if ( saveFile.status() == 0 && saveFile.file() )
226 { 226 {
227 mFormat->saveAll( addressBook(), this, saveFile.file() ); 227 mFormat->saveAll( addressBook(), this, saveFile.file() );
228 ok = saveFile.close(); 228 ok = saveFile.close();
229 } 229 }
230*/ 230*/
231 231
232//US ToDo: write backupfile 232//US ToDo: write backupfile
233 QFile info; 233 QFile info;
234 info.setName( mFileName ); 234 info.setName( mFileName );
235 bool ok = info.open( IO_WriteOnly ); 235 bool ok = info.open( IO_WriteOnly );
236 if ( ok ) { 236 if ( ok ) {
237 mFormat->saveAll( addressBook(), this, &info ); 237 mFormat->saveAll( addressBook(), this, &info );
238 238
239 info.close(); 239 info.close();
240 ok = true; 240 ok = true;
241 } 241 }
242 else { 242 else {
243 243
244 } 244 }
245 245
246 if ( !ok ) 246 if ( !ok )
247 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 247 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
248 248
249 delete ticket; 249 delete ticket;
250 unlock( mFileName ); 250 unlock( mFileName );
251 251
252 return ok; 252 return ok;
253 253
254 qDebug("ResourceFile::save has to be changed"); 254 qDebug("ResourceFile::save has to be changed");
255 return true; 255 return true;
256} 256}
257 257
258bool ResourceFile::lock( const QString &fileName ) 258bool ResourceFile::lock( const QString &fileName )
259{ 259{
260 kdDebug(5700) << "ResourceFile::lock()" << endl; 260 kdDebug(5700) << "ResourceFile::lock()" << endl;
261 261
262 QString fn = fileName; 262 QString fn = fileName;
263 263
264//US change the implementation how the lockfilename is getting created 264//US change the implementation how the lockfilename is getting created
265//US fn.replace( QRegExp("/"), "_" ); 265//US fn.replace( QRegExp("/"), "_" );
266//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 266//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
267 267
268 KURL url(fn); 268 KURL url(fn);
269 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 269 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
270 270
271 kdDebug(5700) << "-- lock name: " << lockName << endl; 271 kdDebug(5700) << "-- lock name: " << lockName << endl;
272 272
273 if (QFile::exists( lockName )) return false; 273 if (QFile::exists( lockName )) return false;
274 274
275 QString lockUniqueName; 275 QString lockUniqueName;
276 lockUniqueName = fn + KApplication::randomString( 8 ); 276 lockUniqueName = fn + KApplication::randomString( 8 );
277 277
278 url = lockUniqueName; 278 url = lockUniqueName;
279//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 279//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
280 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 280 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
281 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 281 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
282 282
283 // Create unique file 283 // Create unique file
284 QFile file( mLockUniqueName ); 284 QFile file( mLockUniqueName );
285 file.open( IO_WriteOnly ); 285 file.open( IO_WriteOnly );
286 file.close(); 286 file.close();
287 287
288 // Create lock file 288 // Create lock file
289 int result = 0;//::link( QFile::encodeName( mLockUniqueName ), 289 int result = ::link( QFile::encodeName( mLockUniqueName ),
290 // QFile::encodeName( lockName ) ); 290 QFile::encodeName( lockName ) );
291 qDebug("lock files %s, %s needs to be fixed", mLockUniqueName.latin1(), lockName.latin1() );
292 291
293 if ( result == 0 ) { 292 if ( result == 0 ) {
294 addressBook()->emitAddressBookLocked(); 293 addressBook()->emitAddressBookLocked();
295 return true; 294 return true;
296 } 295 }
297 296
298 // TODO: check stat 297 // TODO: check stat
299 298
300 return false; 299 return false;
301} 300}
302 301
303void ResourceFile::unlock( const QString &fileName ) 302void ResourceFile::unlock( const QString &fileName )
304{ 303{
305 QString fn = fileName; 304 QString fn = fileName;
306//US change the implementation how the lockfilename is getting created 305//US change the implementation how the lockfilename is getting created
307//US fn.replace( QRegExp( "/" ), "_" ); 306//US fn.replace( QRegExp( "/" ), "_" );
308//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 307//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
309//US QString lockName = fn + ".lock"; 308//US QString lockName = fn + ".lock";
310 KURL url(fn); 309 KURL url(fn);
311 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 310 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
312 311
313 QFile::remove( lockName ); 312 QFile::remove( lockName );
314 QFile::remove( mLockUniqueName ); 313 QFile::remove( mLockUniqueName );
315 addressBook()->emitAddressBookUnlocked(); 314 addressBook()->emitAddressBookUnlocked();
316} 315}
317 316
318void ResourceFile::setFileName( const QString &fileName ) 317void ResourceFile::setFileName( const QString &fileName )
319{ 318{
320/*US ToDo: no synchronization so far. Has to be changed in the future 319/*US ToDo: no synchronization so far. Has to be changed in the future
321 mDirWatch.stopScan(); 320 mDirWatch.stopScan();
322 mDirWatch.removeFile( mFileName ); 321 mDirWatch.removeFile( mFileName );
323*/ 322*/
324 mFileName = fileName; 323 mFileName = fileName;
325 324
326 325
327/*US ToDo: no synchronization so far. Has to be changed in the future 326/*US ToDo: no synchronization so far. Has to be changed in the future
328 mDirWatch.addFile( mFileName ); 327 mDirWatch.addFile( mFileName );
329 mDirWatch.startScan(); 328 mDirWatch.startScan();
330*/ 329*/
331//US simulate KDirWatch event 330//US simulate KDirWatch event
332 fileChanged(); 331 fileChanged();
333} 332}
334 333
335QString ResourceFile::fileName() const 334QString ResourceFile::fileName() const
336{ 335{
337 return mFileName; 336 return mFileName;
338} 337}
339 338
340void ResourceFile::setFormat( const QString &format ) 339void ResourceFile::setFormat( const QString &format )
341{ 340{
342 mFormatName = format; 341 mFormatName = format;
343 delete mFormat; 342 delete mFormat;
344 343
345//US FormatFactory *factory = FormatFactory::self(); 344 FormatFactory *factory = FormatFactory::self();
346//US mFormat = factory->format( mFormatName ); 345 mFormat = factory->format( mFormatName );
347 346/*US
348//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); 347//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1());
349 if (mFormatName == "vcard") { 348 if (mFormatName == "vcard") {
350 mFormat = new VCardFormatPlugin2(); 349 mFormat = new VCardFormatPlugin2();
351// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 350// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
352 } 351 }
353 else if (mFormatName == "binary") { 352 else if (mFormatName == "binary") {
354 mFormat = new BinaryFormat(); 353 mFormat = new BinaryFormat();
355// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 354// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
356 } 355 }
357 else 356 else
358 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 357 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
358*/
359 359
360} 360}
361 361
362QString ResourceFile::format() const 362QString ResourceFile::format() const
363{ 363{
364 return mFormatName; 364 return mFormatName;
365} 365}
366 366
367void ResourceFile::fileChanged() 367void ResourceFile::fileChanged()
368{ 368{
369 // There is a small theoretical chance that KDirWatch calls us before 369 // There is a small theoretical chance that KDirWatch calls us before
370 // we are fully constructed 370 // we are fully constructed
371 if (!addressBook()) 371 if (!addressBook())
372 return; 372 return;
373 load(); 373 load();
374 addressBook()->emitAddressBookChanged(); 374 addressBook()->emitAddressBookChanged();
375} 375}
376 376
377void ResourceFile::removeAddressee( const Addressee &addr ) 377void ResourceFile::removeAddressee( const Addressee &addr )
378{ 378{
379 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); 379 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
380 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); 380 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
381 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); 381 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
382} 382}
383 383
384void ResourceFile::cleanUp() 384void ResourceFile::cleanUp()
385{ 385{
386 unlock( mFileName ); 386 unlock( mFileName );
387} 387}
388 388
389//US #include "resourcefile.moc" 389//US #include "resourcefile.moc"