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.cpp74
1 files changed, 38 insertions, 36 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index 1d3acec..361b36b 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -50,29 +50,31 @@ $Id$
50#include "resourcefileconfig.h" 50#include "resourcefileconfig.h"
51#include "stdaddressbook.h" 51#include "stdaddressbook.h"
52 52
53#include "resourcefile.h" 53#include "resourcefile.h"
54#include "syncwidget.h"
55
54 56
55using namespace KABC; 57using namespace KABC;
56 58
57extern "C" 59extern "C"
58#ifdef _WIN32_ 60#ifdef _WIN32_
59__declspec(dllexport) 61__declspec(dllexport)
60#else 62#else
61{ 63{
62#endif 64#endif
63 65
64//US void *init_kabc_file() 66//US void *init_kabc_file()
65 void *init_microkabc_file() 67 void *init_microkabc_file()
66 { 68 {
67 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); 69 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, KRES::SyncWidget>();
68 } 70 }
69#ifndef _WIN32_ 71#ifndef _WIN32_
70} 72}
71#endif 73#endif
72 74
73ResourceFile::ResourceFile( const KConfig *config ) 75ResourceFile::ResourceFile( const KConfig *config, bool syncable )
74 : Resource( config ) , mFormat( 0 ) 76 : Resource( config, syncable ) , mFormat( 0 )
75{ 77{
76 QString fileName, formatName; 78 QString fileName, formatName;
77 79
78 KConfig *cfg = (KConfig *)config; 80 KConfig *cfg = (KConfig *)config;
@@ -88,11 +90,11 @@ ResourceFile::ResourceFile( const KConfig *config )
88 90
89 init( fileName, formatName ); 91 init( fileName, formatName );
90} 92}
91 93
92ResourceFile::ResourceFile( const QString &fileName, 94ResourceFile::ResourceFile( const QString &fileName, bool syncable ,
93 const QString &formatName ) 95 const QString &formatName )
94 : Resource( 0 ) 96 : Resource( 0, syncable )
95{ 97{
96// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 98// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
97 init( fileName, formatName ); 99 init( fileName, formatName );
98} 100}
@@ -138,16 +140,16 @@ ResourceFile::~ResourceFile()
138 140
139void ResourceFile::writeConfig( KConfig *config ) 141void ResourceFile::writeConfig( KConfig *config )
140{ 142{
141 143
142 config->setGroup( "Resource_" + identifier() ); 144 config->setGroup( "Resource_" + identifier() );
143 Resource::writeConfig( config ); 145 Resource::writeConfig( config );
144 146
145 config->writeEntry( "FileName", mFileName ); 147 config->writeEntry( "FileName", mFileName );
146 config->writeEntry( "FileFormat", mFormatName ); 148 config->writeEntry( "FileFormat", mFormatName );
147 149
148// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); 150// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
149 151
150} 152}
151 153
152Ticket *ResourceFile::requestSaveTicket() 154Ticket *ResourceFile::requestSaveTicket()
153{ 155{
@@ -196,31 +198,31 @@ void ResourceFile::doClose()
196} 198}
197 199
198bool ResourceFile::load() 200bool ResourceFile::load()
199{ 201{
200 202
201 203
202 QFile file( mFileName ); 204 QFile file( mFileName );
203 if ( !file.open( IO_ReadOnly ) ) { 205 if ( !file.open( IO_ReadOnly ) ) {
204 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 206 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
205 return false; 207 return false;
206 } 208 }
207 209
208// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 210// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
209 211
210 return mFormat->loadAll( addressBook(), this, &file ); 212 return mFormat->loadAll( addressBook(), this, &file );
211} 213}
212 214
213bool ResourceFile::save( Ticket *ticket ) 215bool ResourceFile::save( Ticket *ticket )
214{ 216{
215// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 217// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
216 218
217 219
218 // create backup file 220 // create backup file
219 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 221 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
220 222
221/*US we use a simpler method to create a backupfile 223/*US we use a simpler method to create a backupfile
222 224
223 (void) KSaveFile::backupFile( mFileName, QString::null 225 (void) KSaveFile::backupFile( mFileName, QString::null
224 ,extension ); 226 ,extension );
225 227
226 KSaveFile saveFile( mFileName ); 228 KSaveFile saveFile( mFileName );
@@ -231,55 +233,55 @@ bool ResourceFile::save( Ticket *ticket )
231 ok = saveFile.close(); 233 ok = saveFile.close();
232 } 234 }
233*/ 235*/
234 236
235//US ToDo: write backupfile 237//US ToDo: write backupfile
236 QFile info; 238 QFile info;
237 info.setName( mFileName ); 239 info.setName( mFileName );
238 bool ok = info.open( IO_WriteOnly ); 240 bool ok = info.open( IO_WriteOnly );
239 if ( ok ) { 241 if ( ok ) {
240 mFormat->saveAll( addressBook(), this, &info ); 242 mFormat->saveAll( addressBook(), this, &info );
241 243
242 info.close(); 244 info.close();
243 ok = true; 245 ok = true;
244 } 246 }
245 else { 247 else {
246 248
247 } 249 }
248 250
249 if ( !ok ) 251 if ( !ok )
250 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 252 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
251 253
252 delete ticket; 254 delete ticket;
253 unlock( mFileName ); 255 unlock( mFileName );
254 256
255 return ok; 257 return ok;
256 258
257 qDebug("ResourceFile::save has to be changed"); 259 qDebug("ResourceFile::save has to be changed");
258 return true; 260 return true;
259} 261}
260 262
261bool ResourceFile::lock( const QString &fileName ) 263bool ResourceFile::lock( const QString &fileName )
262{ 264{
263 265
264 266
265 QString fn = fileName; 267 QString fn = fileName;
266 268
267//US change the implementation how the lockfilename is getting created 269//US change the implementation how the lockfilename is getting created
268//US fn.replace( QRegExp("/"), "_" ); 270//US fn.replace( QRegExp("/"), "_" );
269//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 271//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
270 272
271 KURL url(fn); 273 KURL url(fn);
272 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 274 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
273 275
274 276
275 277
276 if (QFile::exists( lockName )) return false; 278 if (QFile::exists( lockName )) return false;
277 279
278 QString lockUniqueName; 280 QString lockUniqueName;
279 lockUniqueName = fn + KApplication::randomString( 8 ); 281 lockUniqueName = fn + KApplication::randomString( 8 );
280 282
281 url = lockUniqueName; 283 url = lockUniqueName;
282//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 284//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
283 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 285 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
284 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 286 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
285 287
@@ -310,11 +312,11 @@ void ResourceFile::unlock( const QString &fileName )
310//US change the implementation how the lockfilename is getting created 312//US change the implementation how the lockfilename is getting created
311//US fn.replace( QRegExp( "/" ), "_" ); 313//US fn.replace( QRegExp( "/" ), "_" );
312//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 314//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
313//US QString lockName = fn + ".lock"; 315//US QString lockName = fn + ".lock";
314 KURL url(fn); 316 KURL url(fn);
315 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 317 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
316 318
317 QFile::remove( lockName ); 319 QFile::remove( lockName );
318 QFile::remove( mLockUniqueName ); 320 QFile::remove( mLockUniqueName );
319 addressBook()->emitAddressBookUnlocked(); 321 addressBook()->emitAddressBookUnlocked();
320} 322}
@@ -322,16 +324,16 @@ void ResourceFile::unlock( const QString &fileName )
322void ResourceFile::setFileName( const QString &fileName ) 324void ResourceFile::setFileName( const QString &fileName )
323{ 325{
324 mDirWatch.stopScan(); 326 mDirWatch.stopScan();
325 mDirWatch.removeFile( mFileName ); 327 mDirWatch.removeFile( mFileName );
326 328
327 mFileName = fileName; 329 mFileName = fileName;
328 330
329 331
330 mDirWatch.addFile( mFileName ); 332 mDirWatch.addFile( mFileName );
331 mDirWatch.startScan(); 333 mDirWatch.startScan();
332 334
333//US simulate KDirWatch event 335//US simulate KDirWatch event
334//US fileChanged(); 336//US fileChanged();
335} 337}
336 338
337QString ResourceFile::fileName() const 339QString ResourceFile::fileName() const
@@ -345,9 +347,9 @@ void ResourceFile::setFormat( const QString &format )
345 delete mFormat; 347 delete mFormat;
346 348
347 FormatFactory *factory = FormatFactory::self(); 349 FormatFactory *factory = FormatFactory::self();
348 mFormat = factory->format( mFormatName ); 350 mFormat = factory->format( mFormatName );
349/*US 351/*US
350//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); 352//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1());
351 if (mFormatName == "vcard") { 353 if (mFormatName == "vcard") {
352 mFormat = new VCardFormatPlugin2(); 354 mFormat = new VCardFormatPlugin2();
353// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 355// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
@@ -358,9 +360,9 @@ void ResourceFile::setFormat( const QString &format )
358 } 360 }
359 else 361 else
360 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 362 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
361*/ 363*/
362 364
363} 365}
364 366
365QString ResourceFile::format() const 367QString ResourceFile::format() const
366{ 368{
@@ -372,10 +374,10 @@ void ResourceFile::fileChanged()
372 // There is a small theoretical chance that KDirWatch calls us before 374 // There is a small theoretical chance that KDirWatch calls us before
373 // we are fully constructed 375 // we are fully constructed
374 if (!addressBook()) 376 if (!addressBook())
375 return; 377 return;
376 378
377 379
378 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 380 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
379 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 381 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
380 load(); 382 load();
381 addressBook()->emitAddressBookChanged(); 383 addressBook()->emitAddressBookChanged();