author | zautrix <zautrix> | 2004-09-15 10:22:55 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-09-15 10:22:55 (UTC) |
commit | d4adafe41cf399585b8e35cb600ff35706d55ae0 (patch) (unidiff) | |
tree | 1becc87f6c40390fd73082996d18f71fb5e9c772 /kabc | |
parent | 8889ffd6f6958c3a1d07bafedcc8af5d786a1660 (diff) | |
download | kdepimpi-d4adafe41cf399585b8e35cb600ff35706d55ae0.zip kdepimpi-d4adafe41cf399585b8e35cb600ff35706d55ae0.tar.gz kdepimpi-d4adafe41cf399585b8e35cb600ff35706d55ae0.tar.bz2 |
fixes for desktop version
-rw-r--r-- | kabc/plugins/dir/resourcedir.cpp | 17 | ||||
-rw-r--r-- | kabc/plugins/dir/resourcedir.h | 3 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 20 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.h | 3 |
4 files changed, 28 insertions, 15 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp index 7d96cb7..e58e4f7 100644 --- a/kabc/plugins/dir/resourcedir.cpp +++ b/kabc/plugins/dir/resourcedir.cpp | |||
@@ -35,48 +35,49 @@ $Id$ | |||
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 | #include <kmessagebox.h> | 47 | #include <kmessagebox.h> |
48 | 48 | ||
49 | #include "addressbook.h" | 49 | #include "addressbook.h" |
50 | 50 | ||
51 | #include "formatfactory.h" | 51 | #include "formatfactory.h" |
52 | 52 | ||
53 | #include "resourcedirconfig.h" | 53 | #include "resourcedirconfig.h" |
54 | #include "stdaddressbook.h" | 54 | #include "stdaddressbook.h" |
55 | 55 | ||
56 | //US | 56 | //US |
57 | #include <qdir.h> | 57 | #include <qdir.h> |
58 | 58 | ||
59 | #define NO_DIRWATCH | ||
59 | #include "resourcedir.h" | 60 | #include "resourcedir.h" |
60 | #include "syncprefwidget.h" | 61 | #include "syncprefwidget.h" |
61 | 62 | ||
62 | //#define ALLOW_LOCKING | 63 | //#define ALLOW_LOCKING |
63 | 64 | ||
64 | using namespace KABC; | 65 | using namespace KABC; |
65 | 66 | ||
66 | extern "C" | 67 | extern "C" |
67 | #ifdef _WIN32_ | 68 | #ifdef _WIN32_ |
68 | __declspec(dllexport) | 69 | __declspec(dllexport) |
69 | #else | 70 | #else |
70 | { | 71 | { |
71 | #endif | 72 | #endif |
72 | 73 | ||
73 | //US void *init_kabc_dir() | 74 | //US void *init_kabc_dir() |
74 | void *init_microkabc_dir() | 75 | void *init_microkabc_dir() |
75 | { | 76 | { |
76 | return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>(); | 77 | return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>(); |
77 | } | 78 | } |
78 | #ifndef _WIN32_ | 79 | #ifndef _WIN32_ |
79 | } | 80 | } |
80 | #endif | 81 | #endif |
81 | 82 | ||
82 | ResourceDir::ResourceDir( const KConfig *config, bool syncable ) | 83 | ResourceDir::ResourceDir( const KConfig *config, bool syncable ) |
@@ -92,53 +93,53 @@ ResourceDir::ResourceDir( const KConfig *config, bool syncable ) | |||
92 | mFormatName = cfg->readEntry( "FileFormat", "vcard" ); | 93 | mFormatName = cfg->readEntry( "FileFormat", "vcard" ); |
93 | } else { | 94 | } else { |
94 | path = StdAddressBook::directoryName(); | 95 | path = StdAddressBook::directoryName(); |
95 | mFormatName = "vcard"; | 96 | mFormatName = "vcard"; |
96 | } | 97 | } |
97 | 98 | ||
98 | 99 | ||
99 | FormatFactory *factory = FormatFactory::self(); | 100 | FormatFactory *factory = FormatFactory::self(); |
100 | mFormat = factory->format( mFormatName ); | 101 | mFormat = factory->format( mFormatName ); |
101 | 102 | ||
102 | if ( !mFormat ) { | 103 | if ( !mFormat ) { |
103 | mFormatName = "vcard"; | 104 | mFormatName = "vcard"; |
104 | mFormat = factory->format( mFormatName ); | 105 | mFormat = factory->format( mFormatName ); |
105 | } | 106 | } |
106 | 107 | ||
107 | /*US | 108 | /*US |
108 | //US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); | 109 | //US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); |
109 | if (mFormatName == "vcard") | 110 | if (mFormatName == "vcard") |
110 | mFormat = new VCardFormatPlugin2(); | 111 | mFormat = new VCardFormatPlugin2(); |
111 | else if (mFormatName == "binary") | 112 | else if (mFormatName == "binary") |
112 | mFormat = new BinaryFormat(); | 113 | mFormat = new BinaryFormat(); |
113 | else | 114 | else |
114 | qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); | 115 | qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); |
115 | */ | 116 | */ |
116 | 117 | #ifndef NO_DIRWATCH | |
117 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); | 118 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); |
118 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); | 119 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); |
119 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); | 120 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); |
120 | 121 | #endif | |
121 | setPath( path ); | 122 | setPath( path ); |
122 | } | 123 | } |
123 | 124 | ||
124 | ResourceDir::~ResourceDir() | 125 | ResourceDir::~ResourceDir() |
125 | { | 126 | { |
126 | delete mFormat; | 127 | delete mFormat; |
127 | mFormat = 0; | 128 | mFormat = 0; |
128 | } | 129 | } |
129 | 130 | ||
130 | void ResourceDir::writeConfig( KConfig *config ) | 131 | void ResourceDir::writeConfig( KConfig *config ) |
131 | { | 132 | { |
132 | config->setGroup( "Resource_" + identifier() ); | 133 | config->setGroup( "Resource_" + identifier() ); |
133 | Resource::writeConfig( config ); | 134 | Resource::writeConfig( config ); |
134 | 135 | ||
135 | config->writeEntry( "FilePath", mPath ); | 136 | config->writeEntry( "FilePath", mPath ); |
136 | config->writeEntry( "FileFormat", mFormatName ); | 137 | config->writeEntry( "FileFormat", mFormatName ); |
137 | } | 138 | } |
138 | 139 | ||
139 | Ticket *ResourceDir::requestSaveTicket() | 140 | Ticket *ResourceDir::requestSaveTicket() |
140 | { | 141 | { |
141 | 142 | ||
142 | 143 | ||
143 | if ( !addressBook() ) return 0; | 144 | if ( !addressBook() ) return 0; |
144 | #ifdef ALLOW_LOCKING | 145 | #ifdef ALLOW_LOCKING |
@@ -187,70 +188,71 @@ bool ResourceDir::load() | |||
187 | QStringList::Iterator it; | 188 | QStringList::Iterator it; |
188 | bool ok = true; | 189 | bool ok = true; |
189 | for ( it = files.begin(); it != files.end(); ++it ) { | 190 | for ( it = files.begin(); it != files.end(); ++it ) { |
190 | QFile file( mPath + "/" + (*it) ); | 191 | QFile file( mPath + "/" + (*it) ); |
191 | 192 | ||
192 | if ( !file.open( IO_ReadOnly ) ) { | 193 | if ( !file.open( IO_ReadOnly ) ) { |
193 | addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); | 194 | addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); |
194 | ok = false; | 195 | ok = false; |
195 | continue; | 196 | continue; |
196 | } | 197 | } |
197 | 198 | ||
198 | if ( !mFormat->loadAll( addressBook(), this, &file ) ) | 199 | if ( !mFormat->loadAll( addressBook(), this, &file ) ) |
199 | ok = false; | 200 | ok = false; |
200 | 201 | ||
201 | file.close(); | 202 | file.close(); |
202 | } | 203 | } |
203 | 204 | ||
204 | return ok; | 205 | return ok; |
205 | } | 206 | } |
206 | 207 | ||
207 | bool ResourceDir::save( Ticket *ticket ) | 208 | bool ResourceDir::save( Ticket *ticket ) |
208 | { | 209 | { |
209 | AddressBook::Iterator it; | 210 | AddressBook::Iterator it; |
210 | bool ok = true; | 211 | bool ok = true; |
211 | 212 | #ifndef NO_DIRWATCH | |
212 | mDirWatch.stopScan(); | 213 | mDirWatch.stopScan(); |
213 | 214 | #endif | |
214 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 215 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
215 | if ( (*it).resource() != this || !(*it).changed() ) | 216 | if ( (*it).resource() != this || !(*it).changed() ) |
216 | continue; | 217 | continue; |
217 | 218 | ||
218 | QFile file( mPath + "/" + (*it).uid() ); | 219 | QFile file( mPath + "/" + (*it).uid() ); |
219 | if ( !file.open( IO_WriteOnly ) ) { | 220 | if ( !file.open( IO_WriteOnly ) ) { |
220 | addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); | 221 | addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); |
221 | continue; | 222 | continue; |
222 | } | 223 | } |
223 | 224 | ||
224 | mFormat->save( *it, &file ); | 225 | mFormat->save( *it, &file ); |
225 | 226 | ||
226 | // mark as unchanged | 227 | // mark as unchanged |
227 | (*it).setChanged( false ); | 228 | (*it).setChanged( false ); |
228 | 229 | ||
229 | file.close(); | 230 | file.close(); |
230 | } | 231 | } |
232 | #ifndef NO_DIRWATCH | ||
231 | mDirWatch.startScan(); | 233 | mDirWatch.startScan(); |
232 | 234 | #endif | |
233 | delete ticket; | 235 | delete ticket; |
234 | #ifdef ALLOW_LOCKING | 236 | #ifdef ALLOW_LOCKING |
235 | unlock( mPath ); | 237 | unlock( mPath ); |
236 | #endif | 238 | #endif |
237 | return ok; | 239 | return ok; |
238 | } | 240 | } |
239 | 241 | ||
240 | bool ResourceDir::lock( const QString &path ) | 242 | bool ResourceDir::lock( const QString &path ) |
241 | { | 243 | { |
242 | #ifdef ALLOW_LOCKING | 244 | #ifdef ALLOW_LOCKING |
243 | QString p = path; | 245 | QString p = path; |
244 | //US change the implementation how the lockfilename is getting created | 246 | //US change the implementation how the lockfilename is getting created |
245 | //US p.replace( QRegExp("/"), "_" ); | 247 | //US p.replace( QRegExp("/"), "_" ); |
246 | //US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); | 248 | //US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); |
247 | KURL url(p); | 249 | KURL url(p); |
248 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 250 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
249 | 251 | ||
250 | 252 | ||
251 | if ( QFile::exists( lockName ) ) return false; | 253 | if ( QFile::exists( lockName ) ) return false; |
252 | 254 | ||
253 | QString lockUniqueName; | 255 | QString lockUniqueName; |
254 | lockUniqueName = p + KApplication::randomString( 8 ); | 256 | lockUniqueName = p + KApplication::randomString( 8 ); |
255 | 257 | ||
256 | url = lockUniqueName; | 258 | url = lockUniqueName; |
@@ -280,56 +282,59 @@ bool ResourceDir::lock( const QString &path ) | |||
280 | return true; | 282 | return true; |
281 | #endif | 283 | #endif |
282 | } | 284 | } |
283 | 285 | ||
284 | void ResourceDir::unlock( const QString &path ) | 286 | void ResourceDir::unlock( const QString &path ) |
285 | { | 287 | { |
286 | #ifdef ALLOW_LOCKING | 288 | #ifdef ALLOW_LOCKING |
287 | QString p = path; | 289 | QString p = path; |
288 | //US change the implementation how the lockfilename is getting created | 290 | //US change the implementation how the lockfilename is getting created |
289 | //US p.replace( QRegExp( "/" ), "_" ); | 291 | //US p.replace( QRegExp( "/" ), "_" ); |
290 | //US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); | 292 | //US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); |
291 | KURL url(p); | 293 | KURL url(p); |
292 | QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 294 | QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
293 | 295 | ||
294 | ::unlink( QFile::encodeName( lockName ) ); | 296 | ::unlink( QFile::encodeName( lockName ) ); |
295 | QFile::remove( mLockUniqueName ); | 297 | QFile::remove( mLockUniqueName ); |
296 | addressBook()->emitAddressBookUnlocked(); | 298 | addressBook()->emitAddressBookUnlocked(); |
297 | #else | 299 | #else |
298 | return; | 300 | return; |
299 | #endif | 301 | #endif |
300 | } | 302 | } |
301 | 303 | ||
302 | void ResourceDir::setPath( const QString &path ) | 304 | void ResourceDir::setPath( const QString &path ) |
303 | { | 305 | { |
306 | #ifndef NO_DIRWATCH | ||
304 | mDirWatch.stopScan(); | 307 | mDirWatch.stopScan(); |
305 | mDirWatch.removeDir( mPath ); | 308 | mDirWatch.removeDir( mPath ); |
306 | 309 | ||
307 | mPath = path; | 310 | mPath = path; |
308 | 311 | ||
309 | mDirWatch.addDir( mPath, true ); | 312 | mDirWatch.addDir( mPath, true ); |
310 | mDirWatch.startScan(); | 313 | mDirWatch.startScan(); |
311 | 314 | #else | |
315 | mPath = path; | ||
316 | #endif | ||
312 | //US simulate KDirWatch event | 317 | //US simulate KDirWatch event |
313 | //US pathChanged(); | 318 | //US pathChanged(); |
314 | 319 | ||
315 | } | 320 | } |
316 | 321 | ||
317 | QString ResourceDir::path() const | 322 | QString ResourceDir::path() const |
318 | { | 323 | { |
319 | return mPath; | 324 | return mPath; |
320 | } | 325 | } |
321 | 326 | ||
322 | void ResourceDir::setFormat( const QString &format ) | 327 | void ResourceDir::setFormat( const QString &format ) |
323 | { | 328 | { |
324 | mFormatName = format; | 329 | mFormatName = format; |
325 | 330 | ||
326 | if ( mFormat ) | 331 | if ( mFormat ) |
327 | delete mFormat; | 332 | delete mFormat; |
328 | 333 | ||
329 | FormatFactory *factory = FormatFactory::self(); | 334 | FormatFactory *factory = FormatFactory::self(); |
330 | mFormat = factory->format( mFormatName ); | 335 | mFormat = factory->format( mFormatName ); |
331 | /*US | 336 | /*US |
332 | qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); | 337 | qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); |
333 | if (mFormatName == "vcard") | 338 | if (mFormatName == "vcard") |
334 | mFormat = new VCardFormatPlugin2(); | 339 | mFormat = new VCardFormatPlugin2(); |
335 | else if (mFormatName == "binary") | 340 | else if (mFormatName == "binary") |
diff --git a/kabc/plugins/dir/resourcedir.h b/kabc/plugins/dir/resourcedir.h index c2aedad..77cd18c 100644 --- a/kabc/plugins/dir/resourcedir.h +++ b/kabc/plugins/dir/resourcedir.h | |||
@@ -83,33 +83,34 @@ public: | |||
83 | QString format() const; | 83 | QString format() const; |
84 | 84 | ||
85 | /** | 85 | /** |
86 | * Remove a addressee from its source. | 86 | * Remove a addressee from its source. |
87 | * This method is mainly called by KABC::AddressBook. | 87 | * This method is mainly called by KABC::AddressBook. |
88 | */ | 88 | */ |
89 | virtual void removeAddressee( const Addressee& addr ); | 89 | virtual void removeAddressee( const Addressee& addr ); |
90 | 90 | ||
91 | /** | 91 | /** |
92 | * This method is called by an error handler if the application | 92 | * This method is called by an error handler if the application |
93 | * crashed | 93 | * crashed |
94 | */ | 94 | */ |
95 | virtual void cleanUp(); | 95 | virtual void cleanUp(); |
96 | 96 | ||
97 | protected slots: | 97 | protected slots: |
98 | void pathChanged(); | 98 | void pathChanged(); |
99 | 99 | ||
100 | protected: | 100 | protected: |
101 | bool lock( const QString &path ); | 101 | bool lock( const QString &path ); |
102 | void unlock( const QString &path ); | 102 | void unlock( const QString &path ); |
103 | 103 | ||
104 | private: | 104 | private: |
105 | FormatPlugin *mFormat; | 105 | FormatPlugin *mFormat; |
106 | 106 | ||
107 | #ifndef NO_DIRWATCH | ||
107 | KDirWatch mDirWatch; | 108 | KDirWatch mDirWatch; |
108 | 109 | #endif | |
109 | QString mPath; | 110 | QString mPath; |
110 | QString mFormatName; | 111 | QString mFormatName; |
111 | QString mLockUniqueName; | 112 | QString mLockUniqueName; |
112 | }; | 113 | }; |
113 | 114 | ||
114 | } | 115 | } |
115 | #endif | 116 | #endif |
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index 17263ae..b8c32d5 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp | |||
@@ -28,54 +28,56 @@ $Id$ | |||
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 | #include <kmessagebox.h> | 45 | #include <kmessagebox.h> |
46 | 46 | ||
47 | #include "formatfactory.h" | 47 | #include "formatfactory.h" |
48 | 48 | ||
49 | #include "resource.h" | 49 | #include "resource.h" |
50 | #include "resourcefileconfig.h" | 50 | #include "resourcefileconfig.h" |
51 | #include "stdaddressbook.h" | 51 | #include "stdaddressbook.h" |
52 | 52 | #define NO_DIRWATCH | |
53 | #include "resourcefile.h" | 53 | #include "resourcefile.h" |
54 | #include "syncprefwidget.h" | 54 | #include "syncprefwidget.h" |
55 | 55 | ||
56 | //#define ALLOW_LOCKING | 56 | //#define ALLOW_LOCKING |
57 | 57 | ||
58 | |||
59 | |||
58 | using namespace KABC; | 60 | using namespace KABC; |
59 | 61 | ||
60 | extern "C" | 62 | extern "C" |
61 | #ifdef _WIN32_ | 63 | #ifdef _WIN32_ |
62 | __declspec(dllexport) | 64 | __declspec(dllexport) |
63 | #else | 65 | #else |
64 | { | 66 | { |
65 | #endif | 67 | #endif |
66 | 68 | ||
67 | //US void *init_kabc_file() | 69 | //US void *init_kabc_file() |
68 | void *init_microkabc_file() | 70 | void *init_microkabc_file() |
69 | { | 71 | { |
70 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>(); | 72 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>(); |
71 | } | 73 | } |
72 | #ifndef _WIN32_ | 74 | #ifndef _WIN32_ |
73 | } | 75 | } |
74 | #endif | 76 | #endif |
75 | 77 | ||
76 | ResourceFile::ResourceFile( const KConfig *config, bool syncable ) | 78 | ResourceFile::ResourceFile( const KConfig *config, bool syncable ) |
77 | : Resource( config, syncable ) , mFormat( 0 ) | 79 | : Resource( config, syncable ) , mFormat( 0 ) |
78 | { | 80 | { |
79 | QString fileName, formatName, default_fileName; | 81 | QString fileName, formatName, default_fileName; |
80 | 82 | ||
81 | if (syncable == true) | 83 | if (syncable == true) |
@@ -94,53 +96,53 @@ ResourceFile::ResourceFile( const KConfig *config, bool syncable ) | |||
94 | 96 | ||
95 | init( fileName, formatName ); | 97 | init( fileName, formatName ); |
96 | } | 98 | } |
97 | 99 | ||
98 | ResourceFile::ResourceFile( const QString &fileName, bool syncable , | 100 | ResourceFile::ResourceFile( const QString &fileName, bool syncable , |
99 | const QString &formatName ) | 101 | const QString &formatName ) |
100 | : Resource( 0, syncable ) | 102 | : Resource( 0, syncable ) |
101 | { | 103 | { |
102 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | 104 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); |
103 | init( fileName, formatName ); | 105 | init( fileName, formatName ); |
104 | } | 106 | } |
105 | 107 | ||
106 | void ResourceFile::init( const QString &fileName, const QString &formatName ) | 108 | void ResourceFile::init( const QString &fileName, const QString &formatName ) |
107 | { | 109 | { |
108 | mFormatName = formatName; | 110 | mFormatName = formatName; |
109 | 111 | ||
110 | FormatFactory *factory = FormatFactory::self(); | 112 | FormatFactory *factory = FormatFactory::self(); |
111 | mFormat = factory->format( mFormatName ); | 113 | mFormat = factory->format( mFormatName ); |
112 | 114 | ||
113 | if ( !mFormat ) { | 115 | if ( !mFormat ) { |
114 | mFormatName = "vcard"; | 116 | mFormatName = "vcard"; |
115 | mFormat = factory->format( mFormatName ); | 117 | mFormat = factory->format( mFormatName ); |
116 | } | 118 | } |
117 | 119 | ||
118 | 120 | #ifndef NO_DIRWATCH | |
119 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 121 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
120 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 122 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
121 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 123 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
122 | 124 | #endif | |
123 | setFileName( fileName ); | 125 | setFileName( fileName ); |
124 | } | 126 | } |
125 | 127 | ||
126 | ResourceFile::~ResourceFile() | 128 | ResourceFile::~ResourceFile() |
127 | { | 129 | { |
128 | delete mFormat; | 130 | delete mFormat; |
129 | mFormat = 0; | 131 | mFormat = 0; |
130 | } | 132 | } |
131 | 133 | ||
132 | void ResourceFile::writeConfig( KConfig *config ) | 134 | void ResourceFile::writeConfig( KConfig *config ) |
133 | { | 135 | { |
134 | 136 | ||
135 | config->setGroup( "Resource_" + identifier() ); | 137 | config->setGroup( "Resource_" + identifier() ); |
136 | Resource::writeConfig( config ); | 138 | Resource::writeConfig( config ); |
137 | 139 | ||
138 | config->writeEntry( "FileName", mFileName ); | 140 | config->writeEntry( "FileName", mFileName ); |
139 | config->writeEntry( "FileFormat", mFormatName ); | 141 | config->writeEntry( "FileFormat", mFormatName ); |
140 | 142 | ||
141 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 143 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
142 | 144 | ||
143 | } | 145 | } |
144 | 146 | ||
145 | Ticket *ResourceFile::requestSaveTicket() | 147 | Ticket *ResourceFile::requestSaveTicket() |
146 | { | 148 | { |
@@ -206,68 +208,69 @@ bool ResourceFile::load() | |||
206 | 208 | ||
207 | bool ResourceFile::save( Ticket *ticket ) | 209 | bool ResourceFile::save( Ticket *ticket ) |
208 | { | 210 | { |
209 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 211 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
210 | 212 | ||
211 | 213 | ||
212 | // create backup file | 214 | // create backup file |
213 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | 215 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); |
214 | 216 | ||
215 | /*US we use a simpler method to create a backupfile | 217 | /*US we use a simpler method to create a backupfile |
216 | 218 | ||
217 | (void) KSaveFile::backupFile( mFileName, QString::null | 219 | (void) KSaveFile::backupFile( mFileName, QString::null |
218 | ,extension ); | 220 | ,extension ); |
219 | 221 | ||
220 | KSaveFile saveFile( mFileName ); | 222 | KSaveFile saveFile( mFileName ); |
221 | bool ok = false; | 223 | bool ok = false; |
222 | if ( saveFile.status() == 0 && saveFile.file() ) | 224 | if ( saveFile.status() == 0 && saveFile.file() ) |
223 | { | 225 | { |
224 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | 226 | mFormat->saveAll( addressBook(), this, saveFile.file() ); |
225 | ok = saveFile.close(); | 227 | ok = saveFile.close(); |
226 | } | 228 | } |
227 | */ | 229 | */ |
228 | 230 | ||
229 | //US ToDo: write backupfile | 231 | //US ToDo: write backupfile |
232 | #ifndef NO_DIRWATCH | ||
230 | mDirWatch.stopScan(); | 233 | mDirWatch.stopScan(); |
231 | 234 | #endif | |
232 | QFile info; | 235 | QFile info; |
233 | info.setName( mFileName ); | 236 | info.setName( mFileName ); |
234 | bool ok = info.open( IO_WriteOnly ); | 237 | bool ok = info.open( IO_WriteOnly ); |
235 | if ( ok ) { | 238 | if ( ok ) { |
236 | mFormat->saveAll( addressBook(), this, &info ); | 239 | mFormat->saveAll( addressBook(), this, &info ); |
237 | 240 | ||
238 | info.close(); | 241 | info.close(); |
239 | ok = true; | 242 | ok = true; |
240 | } | 243 | } |
241 | else { | 244 | else { |
242 | 245 | ||
243 | } | 246 | } |
244 | 247 | ||
245 | if ( !ok ) | 248 | if ( !ok ) |
246 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); | 249 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); |
247 | 250 | #ifndef NO_DIRWATCH | |
248 | mDirWatch.startScan(); | 251 | mDirWatch.startScan(); |
249 | 252 | #endif | |
250 | delete ticket; | 253 | delete ticket; |
251 | #ifdef ALLOW_LOCKING | 254 | #ifdef ALLOW_LOCKING |
252 | unlock( mFileName ); | 255 | unlock( mFileName ); |
253 | #endif | 256 | #endif |
254 | 257 | ||
255 | return ok; | 258 | return ok; |
256 | } | 259 | } |
257 | 260 | ||
258 | bool ResourceFile::lock( const QString &fileName ) | 261 | bool ResourceFile::lock( const QString &fileName ) |
259 | { | 262 | { |
260 | #ifdef ALLOW_LOCKING | 263 | #ifdef ALLOW_LOCKING |
261 | 264 | ||
262 | 265 | ||
263 | QString fn = fileName; | 266 | QString fn = fileName; |
264 | 267 | ||
265 | //US change the implementation how the lockfilename is getting created | 268 | //US change the implementation how the lockfilename is getting created |
266 | //US fn.replace( QRegExp("/"), "_" ); | 269 | //US fn.replace( QRegExp("/"), "_" ); |
267 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 270 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
268 | 271 | ||
269 | KURL url(fn); | 272 | KURL url(fn); |
270 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 273 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
271 | 274 | ||
272 | 275 | ||
273 | 276 | ||
@@ -305,56 +308,59 @@ bool ResourceFile::lock( const QString &fileName ) | |||
305 | #endif | 308 | #endif |
306 | } | 309 | } |
307 | 310 | ||
308 | void ResourceFile::unlock( const QString &fileName ) | 311 | void ResourceFile::unlock( const QString &fileName ) |
309 | { | 312 | { |
310 | #ifdef ALLOW_LOCKING | 313 | #ifdef ALLOW_LOCKING |
311 | QString fn = fileName; | 314 | QString fn = fileName; |
312 | //US change the implementation how the lockfilename is getting created | 315 | //US change the implementation how the lockfilename is getting created |
313 | //US fn.replace( QRegExp( "/" ), "_" ); | 316 | //US fn.replace( QRegExp( "/" ), "_" ); |
314 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 317 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
315 | //US QString lockName = fn + ".lock"; | 318 | //US QString lockName = fn + ".lock"; |
316 | KURL url(fn); | 319 | KURL url(fn); |
317 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 320 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
318 | 321 | ||
319 | QFile::remove( lockName ); | 322 | QFile::remove( lockName ); |
320 | QFile::remove( mLockUniqueName ); | 323 | QFile::remove( mLockUniqueName ); |
321 | addressBook()->emitAddressBookUnlocked(); | 324 | addressBook()->emitAddressBookUnlocked(); |
322 | #else | 325 | #else |
323 | return; | 326 | return; |
324 | #endif | 327 | #endif |
325 | } | 328 | } |
326 | 329 | ||
327 | void ResourceFile::setFileName( const QString &fileName ) | 330 | void ResourceFile::setFileName( const QString &fileName ) |
328 | { | 331 | { |
332 | #ifndef NO_DIRWATCH | ||
329 | mDirWatch.stopScan(); | 333 | mDirWatch.stopScan(); |
330 | mDirWatch.removeFile( mFileName ); | 334 | mDirWatch.removeFile( mFileName ); |
331 | |||
332 | mFileName = fileName; | 335 | mFileName = fileName; |
333 | 336 | ||
334 | 337 | ||
335 | mDirWatch.addFile( mFileName ); | 338 | mDirWatch.addFile( mFileName ); |
336 | mDirWatch.startScan(); | 339 | mDirWatch.startScan(); |
340 | #else | ||
341 | mFileName = fileName; | ||
342 | #endif | ||
337 | 343 | ||
338 | //US simulate KDirWatch event | 344 | //US simulate KDirWatch event |
339 | //US fileChanged(); | 345 | //US fileChanged(); |
340 | } | 346 | } |
341 | 347 | ||
342 | QString ResourceFile::fileName() const | 348 | QString ResourceFile::fileName() const |
343 | { | 349 | { |
344 | return mFileName; | 350 | return mFileName; |
345 | } | 351 | } |
346 | 352 | ||
347 | void ResourceFile::setFormat( const QString &format ) | 353 | void ResourceFile::setFormat( const QString &format ) |
348 | { | 354 | { |
349 | mFormatName = format; | 355 | mFormatName = format; |
350 | delete mFormat; | 356 | delete mFormat; |
351 | 357 | ||
352 | FormatFactory *factory = FormatFactory::self(); | 358 | FormatFactory *factory = FormatFactory::self(); |
353 | mFormat = factory->format( mFormatName ); | 359 | mFormat = factory->format( mFormatName ); |
354 | /*US | 360 | /*US |
355 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); | 361 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); |
356 | if (mFormatName == "vcard") { | 362 | if (mFormatName == "vcard") { |
357 | mFormat = new VCardFormatPlugin2(); | 363 | mFormat = new VCardFormatPlugin2(); |
358 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | 364 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); |
359 | } | 365 | } |
360 | else if (mFormatName == "binary") { | 366 | else if (mFormatName == "binary") { |
diff --git a/kabc/plugins/file/resourcefile.h b/kabc/plugins/file/resourcefile.h index 0a3027c..8339340 100644 --- a/kabc/plugins/file/resourcefile.h +++ b/kabc/plugins/file/resourcefile.h | |||
@@ -132,31 +132,32 @@ public: | |||
132 | virtual void removeAddressee( const Addressee& addr ); | 132 | virtual void removeAddressee( const Addressee& addr ); |
133 | 133 | ||
134 | /** | 134 | /** |
135 | * This method is called by an error handler if the application | 135 | * This method is called by an error handler if the application |
136 | * crashed | 136 | * crashed |
137 | */ | 137 | */ |
138 | virtual void cleanUp(); | 138 | virtual void cleanUp(); |
139 | 139 | ||
140 | protected slots: | 140 | protected slots: |
141 | void fileChanged(); | 141 | void fileChanged(); |
142 | 142 | ||
143 | protected: | 143 | protected: |
144 | void init( const QString &fileName, const QString &format ); | 144 | void init( const QString &fileName, const QString &format ); |
145 | 145 | ||
146 | bool lock( const QString &fileName ); | 146 | bool lock( const QString &fileName ); |
147 | void unlock( const QString &fileName ); | 147 | void unlock( const QString &fileName ); |
148 | 148 | ||
149 | private: | 149 | private: |
150 | QString mFileName; | 150 | QString mFileName; |
151 | QString mFormatName; | 151 | QString mFormatName; |
152 | 152 | ||
153 | FormatPlugin *mFormat; | 153 | FormatPlugin *mFormat; |
154 | 154 | ||
155 | QString mLockUniqueName; | 155 | QString mLockUniqueName; |
156 | 156 | #ifndef NO_DIRWATCH | |
157 | KDirWatch mDirWatch; | 157 | KDirWatch mDirWatch; |
158 | #endif | ||
158 | }; | 159 | }; |
159 | 160 | ||
160 | } | 161 | } |
161 | 162 | ||
162 | #endif | 163 | #endif |