summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/file/resourcefile.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/file/resourcefile.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/file/resourcefile.cpp10
1 files changed, 5 insertions, 5 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index dad4571..3ed850c 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -108,355 +108,355 @@ ResourceFile::ResourceFile( const QString &fileName ,
108{ 108{
109// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 109// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
110 110
111 111
112 mLastBackupDate = -1; 112 mLastBackupDate = -1;
113 init( fileName, formatName ); 113 init( fileName, formatName );
114} 114}
115 115
116void ResourceFile::init( const QString &fileName, const QString &formatName ) 116void ResourceFile::init( const QString &fileName, const QString &formatName )
117{ 117{
118 mFormatName = formatName; 118 mFormatName = formatName;
119 119
120 FormatFactory *factory = FormatFactory::self(); 120 FormatFactory *factory = FormatFactory::self();
121 mFormat = factory->format( mFormatName ); 121 mFormat = factory->format( mFormatName );
122 122
123 if ( !mFormat ) { 123 if ( !mFormat ) {
124 mFormatName = "vcard"; 124 mFormatName = "vcard";
125 mFormat = factory->format( mFormatName ); 125 mFormat = factory->format( mFormatName );
126 } 126 }
127 127
128#ifndef NO_DIRWATCH 128#ifndef NO_DIRWATCH
129 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 129 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
130 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 130 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
131 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 131 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
132#endif 132#endif
133 133
134 QString localKdeDir; 134 QString localKdeDir;
135 localKdeDir = readEnvPath("LOCALMICROKDEHOME"); 135 localKdeDir = readEnvPath("LOCALMICROKDEHOME");
136 if ( ! localKdeDir.isEmpty() ) { 136 if ( ! localKdeDir.isEmpty() ) {
137 qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); 137 qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() );
138 QFileInfo fi ( fileName ); 138 QFileInfo fi ( fileName );
139 QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); 139 QString localname = localKdeDir + "/apps/kabc/" + fi.fileName ();
140 QFileInfo fi2 ( localname ); 140 QFileInfo fi2 ( localname );
141 if ( ! fi2.exists() || mFamily == "sync_res" ) { 141 if ( ! fi2.exists() || mFamily == "sync_res" ) {
142 if ( fi.exists() && mFamily == "sync_res") { 142 if ( fi.exists() && mFamily == "sync_res") {
143 qDebug("LOCAL mode SYNC mode using absolute file path "); 143 qDebug("LOCAL mode SYNC mode using absolute file path ");
144 setFileName( fileName ); 144 setFileName( fileName );
145 return; 145 return;
146 } else { 146 } else {
147 KMessageBox::error(0,i18n("Addressbook resource file not found:\n '%1'.\nIn LOCAL mode only resource files in\n'%2'\nare supported.\n(i.e. in the dir ./apps/kabc/ relative to the kapi(.exe) binary.)\n\nIf you are starting KA/Pi for the very first time\nyou will get this error message as well.\nIt will create the missing file automatically for you.").arg(localname).arg(localKdeDir+"/apps/kabc/") ); 147 KMessageBox::error(0,i18n("Addressbook resource file not found:\n '%1'.\nIn LOCAL mode only resource files in\n'%2'\nare supported.\n(i.e. in the dir ./apps/kabc/ relative to the kapi(.exe) binary.)\n\nIf you are starting KA/Pi for the very first time\nyou will get this error message as well.\nIt will create the missing file automatically for you.").arg(localname).arg(localKdeDir+"/apps/kabc/") );
148 setFileName( localname ); 148 setFileName( localname );
149 return; 149 return;
150 } 150 }
151 151
152 } else { 152 } else {
153 qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); 153 qDebug("Local resource file found. Changing filename to: %s",localname.latin1() );
154 setFileName( localname ); 154 setFileName( localname );
155 return; 155 return;
156 } 156 }
157 157
158 } 158 }
159 setFileName( fileName ); 159 setFileName( fileName );
160} 160}
161 161
162ResourceFile::~ResourceFile() 162ResourceFile::~ResourceFile()
163{ 163{
164 delete mFormat; 164 delete mFormat;
165 mFormat = 0; 165 mFormat = 0;
166} 166}
167 167
168void ResourceFile::writeConfig( KConfig *config ) 168void ResourceFile::writeConfig( KConfig *config )
169{ 169{
170 170
171 config->setGroup( "Resource_" + identifier() ); 171 config->setGroup( "Resource_" + identifier() );
172 Resource::writeConfig( config ); 172 Resource::writeConfig( config );
173 173
174 config->writeEntry( "FileName", fileName() ); 174 config->writeEntry( "FileName", fileName() );
175 config->writeEntry( "FileFormat", mFormatName ); 175 config->writeEntry( "FileFormat", mFormatName );
176 176
177// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); 177// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
178 178
179} 179}
180 180
181Ticket *ResourceFile::requestSaveTicket() 181Ticket *ResourceFile::requestSaveTicket()
182{ 182{
183 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; 183 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
184 184
185 if ( !addressBook() ) return 0; 185 if ( !addressBook() ) return 0;
186 186
187#ifdef ALLOW_LOCKING 187#ifdef ALLOW_LOCKING
188 if ( !lock( mFileName ) ) { 188 if ( !lock( mFileName ) ) {
189 qDebug("unablt to lock file "); 189 qDebug("unablt to lock file ");
190 return 0; 190 return 0;
191 } 191 }
192#endif 192#endif
193 return createTicket( this ); 193 return createTicket( this );
194} 194}
195 195
196 196
197bool ResourceFile::doOpen() 197bool ResourceFile::doOpen()
198{ 198{
199 QFile file( fileName() ); 199 QFile file( fileName() );
200 qDebug("ResourceFile::openfile %s ", fileName().latin1()); 200 qDebug("ResourceFile::openfile %s ", fileName().latin1());
201 201
202 if ( !file.exists() ) { 202 if ( !file.exists() ) {
203 // try to create the file 203 // try to create the file
204 bool ok = file.open( IO_WriteOnly ); 204 bool ok = file.open( QIODevice::WriteOnly );
205 if ( ok ) 205 if ( ok )
206 file.close(); 206 file.close();
207 207
208 return ok; 208 return ok;
209 } else { 209 } else {
210 if ( !file.open( IO_ReadWrite ) ) 210 if ( !file.open( QIODevice::ReadWrite ) )
211 return false; 211 return false;
212 212
213 if ( file.size() < 10 ) { 213 if ( file.size() < 10 ) {
214 file.close(); 214 file.close();
215 return true; 215 return true;
216 } 216 }
217 217
218 bool ok = mFormat->checkFormat( &file ); 218 bool ok = mFormat->checkFormat( &file );
219 file.close(); 219 file.close();
220 220
221 return ok; 221 return ok;
222 } 222 }
223} 223}
224 224
225void ResourceFile::doClose() 225void ResourceFile::doClose()
226{ 226{
227} 227}
228 228
229bool ResourceFile::load() 229bool ResourceFile::load()
230{ 230{
231 231
232 QFile file( fileName() ); 232 QFile file( fileName() );
233 if ( !file.open( IO_ReadOnly ) ) { 233 if ( !file.open( QIODevice::ReadOnly ) ) {
234 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); 234 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) );
235 return false; 235 return false;
236 } 236 }
237 237
238// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 238// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
239 239
240 return mFormat->loadAll( addressBook(), this, &file ); 240 return mFormat->loadAll( addressBook(), this, &file );
241} 241}
242 242
243bool ResourceFile::save( Ticket *ticket ) 243bool ResourceFile::save( Ticket *ticket )
244{ 244{
245// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 245// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
246 246
247 247
248 // create backup file 248 // create backup file
249 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 249 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
250 250
251/*US we use a simpler method to create a backupfile 251/*US we use a simpler method to create a backupfile
252 252
253 (void) KSaveFile::backupFile( mFileName, QString::null 253 (void) KSaveFile::backupFile( mFileName, QString::null
254 ,extension ); 254 ,extension );
255 255
256 KSaveFile saveFile( mFileName ); 256 KSaveFile saveFile( mFileName );
257 bool ok = false; 257 bool ok = false;
258 if ( saveFile.status() == 0 && saveFile.file() ) 258 if ( saveFile.status() == 0 && saveFile.file() )
259 { 259 {
260 mFormat->saveAll( addressBook(), this, saveFile.file() ); 260 mFormat->saveAll( addressBook(), this, saveFile.file() );
261 ok = saveFile.close(); 261 ok = saveFile.close();
262 } 262 }
263*/ 263*/
264 264
265//US ToDo: write backupfile 265//US ToDo: write backupfile
266#ifndef NO_DIRWATCH 266#ifndef NO_DIRWATCH
267 mDirWatch.stopScan(); 267 mDirWatch.stopScan();
268#endif 268#endif
269 if ( mLastBackupDate >= 0 && mFamily != "sync_res") { 269 if ( mLastBackupDate >= 0 && mFamily != "sync_res") {
270 KConfig conf (locateLocal("config","microkdeglobalrc")); 270 KConfig conf (locateLocal("config","microkdeglobalrc"));
271 conf.setGroup( "BackupSettings" ); 271 conf.setGroup( "BackupSettings" );
272 bool b_enabled = conf.readBoolEntry( "BackupEnabled" ); 272 bool b_enabled = conf.readBoolEntry( "BackupEnabled" );
273 if ( b_enabled ) { 273 if ( b_enabled ) {
274 int num = conf.readNumEntry( "BackupNumbers" ); 274 int num = conf.readNumEntry( "BackupNumbers" );
275 int d_count = conf.readNumEntry( "BackupDayCount" ); 275 int d_count = conf.readNumEntry( "BackupDayCount" );
276 bool stdDir = conf.readBoolEntry( "BackupUseDefaultDir" ); 276 bool stdDir = conf.readBoolEntry( "BackupUseDefaultDir" );
277 QString bupDir = conf.readEntry( "BackupDatadir" ); 277 QString bupDir = conf.readEntry( "BackupDatadir" );
278 QDate reference ( 2000,1,1 ); 278 QDate reference ( 2000,1,1 );
279 int daysTo = reference.daysTo ( QDate::currentDate() ); 279 int daysTo = reference.daysTo ( QDate::currentDate() );
280 bool saveDate = false; 280 bool saveDate = false;
281 if ( daysTo - d_count >= mLastBackupDate ) { 281 if ( daysTo - d_count >= mLastBackupDate ) {
282 qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate ); 282 qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate );
283 if ( stdDir ) 283 if ( stdDir )
284 bupDir = KGlobalSettings::backupDataDir(); 284 bupDir = KGlobalSettings::backupDataDir();
285 int retval = KApplication::createBackup( fileName(), bupDir, num ); 285 int retval = KApplication::createBackup( fileName(), bupDir, num );
286 if ( retval == 0 ) { 286 if ( retval == 0 ) {
287 qDebug("KO: Backup cancelled. Will try again tomorrow "); 287 qDebug("KO: Backup cancelled. Will try again tomorrow ");
288 // retval == 0 : backup skipped for today, try again tomorrow 288 // retval == 0 : backup skipped for today, try again tomorrow
289 mLastBackupDate = daysTo - d_count+1; 289 mLastBackupDate = daysTo - d_count+1;
290 saveDate = true; 290 saveDate = true;
291 } else if ( retval == 1 ){ 291 } else if ( retval == 1 ){
292 qDebug("KO: Backup created."); 292 qDebug("KO: Backup created.");
293 // backup ok 293 // backup ok
294 mLastBackupDate = daysTo; 294 mLastBackupDate = daysTo;
295 saveDate = true; 295 saveDate = true;
296 } else if ( retval == 2 ){ 296 } else if ( retval == 2 ){
297 qDebug("KO: Backup globally cancelled."); 297 qDebug("KO: Backup globally cancelled.");
298 // backup globally cancelled 298 // backup globally cancelled
299 b_enabled = false; 299 b_enabled = false;
300 } 300 }
301 if ( !b_enabled ) { 301 if ( !b_enabled ) {
302 conf.writeEntry( "mBackupEnabled", false ); 302 conf.writeEntry( "mBackupEnabled", false );
303 } 303 }
304 if ( saveDate ) { 304 if ( saveDate ) {
305 KConfig config ( locateLocal("config","kabcrc") ); 305 KConfig config ( locateLocal("config","kabcrc") );
306 config.setGroup( "Resource_" + identifier() ); 306 config.setGroup( "Resource_" + identifier() );
307 config.writeEntry( "LastBackupDate", mLastBackupDate ); 307 config.writeEntry( "LastBackupDate", mLastBackupDate );
308 } 308 }
309 } 309 }
310 } 310 }
311 } 311 }
312 QFile info; 312 QFile info;
313 info.setName( fileName() ); 313 info.setName( fileName() );
314 bool ok = info.open( IO_WriteOnly ); 314 bool ok = info.open( QIODevice::WriteOnly );
315 if ( ok ) { 315 if ( ok ) {
316 mFormat->saveAll( addressBook(), this, &info ); 316 mFormat->saveAll( addressBook(), this, &info );
317 317
318 info.close(); 318 info.close();
319 ok = true; 319 ok = true;
320 } 320 }
321 else { 321 else {
322 322
323 } 323 }
324 324
325 if ( !ok ) 325 if ( !ok )
326 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); 326 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) );
327#ifndef NO_DIRWATCH 327#ifndef NO_DIRWATCH
328 mDirWatch.startScan(); 328 mDirWatch.startScan();
329#endif 329#endif
330 delete ticket; 330 delete ticket;
331#ifdef ALLOW_LOCKING 331#ifdef ALLOW_LOCKING
332 unlock( mFileName ); 332 unlock( mFileName );
333#endif 333#endif
334 334
335 return ok; 335 return ok;
336} 336}
337 337
338bool ResourceFile::lock( const QString &fileName ) 338bool ResourceFile::lock( const QString &fileName )
339{ 339{
340#ifdef ALLOW_LOCKING 340#ifdef ALLOW_LOCKING
341 341
342 342
343 QString fn = fileName; 343 QString fn = fileName;
344 344
345//US change the implementation how the lockfilename is getting created 345//US change the implementation how the lockfilename is getting created
346//US fn.replace( QRegExp("/"), "_" ); 346//US fn.replace( QRegExp("/"), "_" );
347//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 347//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
348 348
349 KURL url(fn); 349 KURL url(fn);
350 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 350 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
351 351
352 352
353 353
354 if (QFile::exists( lockName )) return false; 354 if (QFile::exists( lockName )) return false;
355 355
356 QString lockUniqueName; 356 QString lockUniqueName;
357 lockUniqueName = fn + KApplication::randomString( 8 ); 357 lockUniqueName = fn + KApplication::randomString( 8 );
358 358
359 url = lockUniqueName; 359 url = lockUniqueName;
360//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 360//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
361 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 361 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
362 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 362 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
363 363
364 // Create unique file 364 // Create unique file
365 QFile file( mLockUniqueName ); 365 QFile file( mLockUniqueName );
366 file.open( IO_WriteOnly ); 366 file.open( QIODevice::WriteOnly );
367 file.close(); 367 file.close();
368 368
369 // Create lock file 369 // Create lock file
370 int result = 0; 370 int result = 0;
371#ifndef _WIN32_ 371#ifndef _WIN32_
372 result = ::link( QFile::encodeName( mLockUniqueName ), 372 result = ::link( QFile::encodeName( mLockUniqueName ),
373 QFile::encodeName( lockName ) ); 373 QFile::encodeName( lockName ) );
374#endif 374#endif
375 if ( result == 0 ) { 375 if ( result == 0 ) {
376 addressBook()->emitAddressBookLocked(); 376 addressBook()->emitAddressBookLocked();
377 return true; 377 return true;
378 } 378 }
379 379
380 // TODO: check stat 380 // TODO: check stat
381 381
382 return false; 382 return false;
383#else 383#else
384 return true; 384 return true;
385#endif 385#endif
386} 386}
387 387
388void ResourceFile::unlock( const QString &fileName ) 388void ResourceFile::unlock( const QString &fileName )
389{ 389{
390#ifdef ALLOW_LOCKING 390#ifdef ALLOW_LOCKING
391 QString fn = fileName; 391 QString fn = fileName;
392//US change the implementation how the lockfilename is getting created 392//US change the implementation how the lockfilename is getting created
393//US fn.replace( QRegExp( "/" ), "_" ); 393//US fn.replace( QRegExp( "/" ), "_" );
394//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 394//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
395//US QString lockName = fn + ".lock"; 395//US QString lockName = fn + ".lock";
396 KURL url(fn); 396 KURL url(fn);
397 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 397 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
398 398
399 QFile::remove( lockName ); 399 QFile::remove( lockName );
400 QFile::remove( mLockUniqueName ); 400 QFile::remove( mLockUniqueName );
401 addressBook()->emitAddressBookUnlocked(); 401 addressBook()->emitAddressBookUnlocked();
402#else 402#else
403 return; 403 return;
404#endif 404#endif
405} 405}
406 406
407void ResourceFile::setFileName( const QString &fileName ) 407void ResourceFile::setFileName( const QString &fileName )
408{ 408{
409#ifndef NO_DIRWATCH 409#ifndef NO_DIRWATCH
410 mDirWatch.stopScan(); 410 mDirWatch.stopScan();
411 mDirWatch.removeFile( mFileName ); 411 mDirWatch.removeFile( mFileName );
412 mFileName = fileName; 412 mFileName = fileName;
413 413
414 414
415 mDirWatch.addFile( mFileName ); 415 mDirWatch.addFile( mFileName );
416 mDirWatch.startScan(); 416 mDirWatch.startScan();
417#else 417#else
418 mFileName2 = fileName; 418 mFileName2 = fileName;
419#endif 419#endif
420 420
421//US simulate KDirWatch event 421//US simulate KDirWatch event
422//US fileChanged(); 422//US fileChanged();
423} 423}
424 424
425QString ResourceFile::fileName() const 425QString ResourceFile::fileName() const
426{ 426{
427 return mFileName2; 427 return mFileName2;
428} 428}
429 429
430void ResourceFile::setFormat( const QString &format ) 430void ResourceFile::setFormat( const QString &format )
431{ 431{
432 mFormatName = format; 432 mFormatName = format;
433 delete mFormat; 433 delete mFormat;
434 434
435 FormatFactory *factory = FormatFactory::self(); 435 FormatFactory *factory = FormatFactory::self();
436 mFormat = factory->format( mFormatName ); 436 mFormat = factory->format( mFormatName );
437/*US 437/*US
438//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); 438//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1());
439 if (mFormatName == "vcard") { 439 if (mFormatName == "vcard") {
440 mFormat = new VCardFormatPlugin2(); 440 mFormat = new VCardFormatPlugin2();
441// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 441// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
442 } 442 }
443 else if (mFormatName == "binary") { 443 else if (mFormatName == "binary") {
444 mFormat = new BinaryFormat(); 444 mFormat = new BinaryFormat();
445// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 445// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
446 } 446 }
447 else 447 else
448 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 448 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
449*/ 449*/
450 450
451} 451}
452 452
453QString ResourceFile::format() const 453QString ResourceFile::format() const
454{ 454{
455 return mFormatName; 455 return mFormatName;
456} 456}
457 457
458void ResourceFile::fileChanged() 458void ResourceFile::fileChanged()
459{ 459{
460 // There is a small theoretical chance that KDirWatch calls us before 460 // There is a small theoretical chance that KDirWatch calls us before
461 // we are fully constructed 461 // we are fully constructed
462 if (!addressBook()) 462 if (!addressBook())