summaryrefslogtreecommitdiffabout
path: root/kabc
authorzautrix <zautrix>2004-09-15 10:22:55 (UTC)
committer zautrix <zautrix>2004-09-15 10:22:55 (UTC)
commitd4adafe41cf399585b8e35cb600ff35706d55ae0 (patch) (unidiff)
tree1becc87f6c40390fd73082996d18f71fb5e9c772 /kabc
parent8889ffd6f6958c3a1d07bafedcc8af5d786a1660 (diff)
downloadkdepimpi-d4adafe41cf399585b8e35cb600ff35706d55ae0.zip
kdepimpi-d4adafe41cf399585b8e35cb600ff35706d55ae0.tar.gz
kdepimpi-d4adafe41cf399585b8e35cb600ff35706d55ae0.tar.bz2
fixes for desktop version
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/dir/resourcedir.cpp17
-rw-r--r--kabc/plugins/dir/resourcedir.h3
-rw-r--r--kabc/plugins/file/resourcefile.cpp20
-rw-r--r--kabc/plugins/file/resourcefile.h3
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
@@ -11,349 +11,354 @@
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#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
64using namespace KABC; 65using namespace KABC;
65 66
66extern "C" 67extern "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
82ResourceDir::ResourceDir( const KConfig *config, bool syncable ) 83ResourceDir::ResourceDir( const KConfig *config, bool syncable )
83 : Resource( config, syncable ) 84 : Resource( config, syncable )
84{ 85{
85 QString path; 86 QString path;
86 87
87 KConfig *cfg = (KConfig *)config; 88 KConfig *cfg = (KConfig *)config;
88 if ( cfg ) { 89 if ( cfg ) {
89//US path = config->readEntry( "FilePath" ); 90//US path = config->readEntry( "FilePath" );
90 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() ); 91 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() );
91//US mFormatName = config->readEntry( "FileFormat" ); 92//US mFormatName = config->readEntry( "FileFormat" );
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
124ResourceDir::~ResourceDir() 125ResourceDir::~ResourceDir()
125{ 126{
126 delete mFormat; 127 delete mFormat;
127 mFormat = 0; 128 mFormat = 0;
128} 129}
129 130
130void ResourceDir::writeConfig( KConfig *config ) 131void 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
139Ticket *ResourceDir::requestSaveTicket() 140Ticket *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
145 if ( !lock( mPath ) ) { 146 if ( !lock( mPath ) ) {
146 147
147 return 0; 148 return 0;
148 } 149 }
149#endif 150#endif
150 return createTicket( this ); 151 return createTicket( this );
151 152
152} 153}
153 154
154 155
155bool ResourceDir::doOpen() 156bool ResourceDir::doOpen()
156{ 157{
157 QDir dir( mPath ); 158 QDir dir( mPath );
158 if ( !dir.exists() ) { // no directory available 159 if ( !dir.exists() ) { // no directory available
159 return dir.mkdir( dir.path() ); 160 return dir.mkdir( dir.path() );
160 } else { 161 } else {
161 QString testName = dir.entryList( QDir::Files )[0]; 162 QString testName = dir.entryList( QDir::Files )[0];
162 if ( testName.isNull() || testName.isEmpty() ) // no file in directory 163 if ( testName.isNull() || testName.isEmpty() ) // no file in directory
163 return true; 164 return true;
164 165
165 QFile file( mPath + "/" + testName ); 166 QFile file( mPath + "/" + testName );
166 if ( file.open( IO_ReadOnly ) ) 167 if ( file.open( IO_ReadOnly ) )
167 return true; 168 return true;
168 169
169 if ( file.size() == 0 ) 170 if ( file.size() == 0 )
170 return true; 171 return true;
171 172
172 bool ok = mFormat->checkFormat( &file ); 173 bool ok = mFormat->checkFormat( &file );
173 file.close(); 174 file.close();
174 return ok; 175 return ok;
175 } 176 }
176} 177}
177 178
178void ResourceDir::doClose() 179void ResourceDir::doClose()
179{ 180{
180} 181}
181 182
182bool ResourceDir::load() 183bool ResourceDir::load()
183{ 184{
184 QDir dir( mPath ); 185 QDir dir( mPath );
185 QStringList files = dir.entryList( QDir::Files ); 186 QStringList files = dir.entryList( QDir::Files );
186 187
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
207bool ResourceDir::save( Ticket *ticket ) 208bool 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
240bool ResourceDir::lock( const QString &path ) 242bool 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;
257//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 259//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
258 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 260 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
259 261
260 // Create unique file 262 // Create unique file
261 QFile file( mLockUniqueName ); 263 QFile file( mLockUniqueName );
262 file.open( IO_WriteOnly ); 264 file.open( IO_WriteOnly );
263 file.close(); 265 file.close();
264 266
265 // Create lock file 267 // Create lock file
266 int result = 0; 268 int result = 0;
267#ifndef _WIN32_ 269#ifndef _WIN32_
268 result = ::link( QFile::encodeName( mLockUniqueName ), 270 result = ::link( QFile::encodeName( mLockUniqueName ),
269 QFile::encodeName( lockName ) ); 271 QFile::encodeName( lockName ) );
270#endif 272#endif
271 if ( result == 0 ) { 273 if ( result == 0 ) {
272 addressBook()->emitAddressBookLocked(); 274 addressBook()->emitAddressBookLocked();
273 return true; 275 return true;
274 } 276 }
275 277
276 // TODO: check stat 278 // TODO: check stat
277 279
278 return false; 280 return false;
279#else 281#else
280 return true; 282 return true;
281#endif 283#endif
282} 284}
283 285
284void ResourceDir::unlock( const QString &path ) 286void 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
302void ResourceDir::setPath( const QString &path ) 304void 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
317QString ResourceDir::path() const 322QString ResourceDir::path() const
318{ 323{
319 return mPath; 324 return mPath;
320} 325}
321 326
322void ResourceDir::setFormat( const QString &format ) 327void 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
332qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); 337qDebug("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")
336 mFormat = new BinaryFormat(); 341 mFormat = new BinaryFormat();
337 else 342 else
338 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1()); 343 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1());
339*/ 344*/
340 345
341} 346}
342 347
343QString ResourceDir::format() const 348QString ResourceDir::format() const
344{ 349{
345 return mFormatName; 350 return mFormatName;
346} 351}
347 352
348void ResourceDir::pathChanged() 353void ResourceDir::pathChanged()
349{ 354{
350 if ( !addressBook() ) 355 if ( !addressBook() )
351 return; 356 return;
352 357
353 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) ); 358 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) );
354 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 359 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
355 load(); 360 load();
356 addressBook()->emitAddressBookChanged(); 361 addressBook()->emitAddressBookChanged();
357 } 362 }
358} 363}
359 364
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
@@ -59,57 +59,58 @@ public:
59 59
60 virtual Ticket *requestSaveTicket(); 60 virtual Ticket *requestSaveTicket();
61 61
62 virtual bool load(); 62 virtual bool load();
63 virtual bool save( Ticket * ); 63 virtual bool save( Ticket * );
64 64
65 /** 65 /**
66 * Set path to be used for saving. 66 * Set path to be used for saving.
67 */ 67 */
68 void setPath( const QString & ); 68 void setPath( const QString & );
69 69
70 /** 70 /**
71 * Return path used for loading and saving the address book. 71 * Return path used for loading and saving the address book.
72 */ 72 */
73 QString path() const; 73 QString path() const;
74 74
75 /** 75 /**
76 * Set the format by name. 76 * Set the format by name.
77 */ 77 */
78 void setFormat( const QString &format ); 78 void setFormat( const QString &format );
79 79
80 /** 80 /**
81 * Returns the format name. 81 * Returns the format name.
82 */ 82 */
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
97protected slots: 97protected slots:
98 void pathChanged(); 98 void pathChanged();
99 99
100protected: 100protected:
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
104private: 104private:
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
@@ -4,167 +4,169 @@
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#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
58using namespace KABC; 60using namespace KABC;
59 61
60extern "C" 62extern "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
76ResourceFile::ResourceFile( const KConfig *config, bool syncable ) 78ResourceFile::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)
82 default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf"; 84 default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf";
83 else 85 else
84 default_fileName = StdAddressBook::fileName(); 86 default_fileName = StdAddressBook::fileName();
85 87
86 KConfig *cfg = (KConfig *)config; 88 KConfig *cfg = (KConfig *)config;
87 if ( cfg ) { 89 if ( cfg ) {
88 fileName = cfg->readEntry( "FileName", default_fileName ); 90 fileName = cfg->readEntry( "FileName", default_fileName );
89 formatName = cfg->readEntry( "FileFormat", "vcard" ); 91 formatName = cfg->readEntry( "FileFormat", "vcard" );
90 } else { 92 } else {
91 fileName = default_fileName; 93 fileName = default_fileName;
92 formatName = "vcard"; 94 formatName = "vcard";
93 } 95 }
94 96
95 init( fileName, formatName ); 97 init( fileName, formatName );
96} 98}
97 99
98ResourceFile::ResourceFile( const QString &fileName, bool syncable , 100ResourceFile::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
106void ResourceFile::init( const QString &fileName, const QString &formatName ) 108void 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
126ResourceFile::~ResourceFile() 128ResourceFile::~ResourceFile()
127{ 129{
128 delete mFormat; 130 delete mFormat;
129 mFormat = 0; 131 mFormat = 0;
130} 132}
131 133
132void ResourceFile::writeConfig( KConfig *config ) 134void 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
145Ticket *ResourceFile::requestSaveTicket() 147Ticket *ResourceFile::requestSaveTicket()
146{ 148{
147 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; 149 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
148 150
149 if ( !addressBook() ) return 0; 151 if ( !addressBook() ) return 0;
150 152
151#ifdef ALLOW_LOCKING 153#ifdef ALLOW_LOCKING
152 if ( !lock( mFileName ) ) { 154 if ( !lock( mFileName ) ) {
153 qDebug("unablt to lock file "); 155 qDebug("unablt to lock file ");
154 return 0; 156 return 0;
155 } 157 }
156#endif 158#endif
157 return createTicket( this ); 159 return createTicket( this );
158} 160}
159 161
160 162
161bool ResourceFile::doOpen() 163bool ResourceFile::doOpen()
162{ 164{
163 QFile file( mFileName ); 165 QFile file( mFileName );
164 166
165 if ( !file.exists() ) { 167 if ( !file.exists() ) {
166 // try to create the file 168 // try to create the file
167 bool ok = file.open( IO_WriteOnly ); 169 bool ok = file.open( IO_WriteOnly );
168 if ( ok ) 170 if ( ok )
169 file.close(); 171 file.close();
170 172
@@ -182,203 +184,207 @@ bool ResourceFile::doOpen()
182 file.close(); 184 file.close();
183 185
184 return ok; 186 return ok;
185 } 187 }
186} 188}
187 189
188void ResourceFile::doClose() 190void ResourceFile::doClose()
189{ 191{
190} 192}
191 193
192bool ResourceFile::load() 194bool ResourceFile::load()
193{ 195{
194 196
195 197
196 QFile file( mFileName ); 198 QFile file( mFileName );
197 if ( !file.open( IO_ReadOnly ) ) { 199 if ( !file.open( IO_ReadOnly ) ) {
198 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 200 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
199 return false; 201 return false;
200 } 202 }
201 203
202// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 204// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
203 205
204 return mFormat->loadAll( addressBook(), this, &file ); 206 return mFormat->loadAll( addressBook(), this, &file );
205} 207}
206 208
207bool ResourceFile::save( Ticket *ticket ) 209bool 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
258bool ResourceFile::lock( const QString &fileName ) 261bool 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
274 if (QFile::exists( lockName )) return false; 277 if (QFile::exists( lockName )) return false;
275 278
276 QString lockUniqueName; 279 QString lockUniqueName;
277 lockUniqueName = fn + KApplication::randomString( 8 ); 280 lockUniqueName = fn + KApplication::randomString( 8 );
278 281
279 url = lockUniqueName; 282 url = lockUniqueName;
280//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 283//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
281 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 284 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
282 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 285 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
283 286
284 // Create unique file 287 // Create unique file
285 QFile file( mLockUniqueName ); 288 QFile file( mLockUniqueName );
286 file.open( IO_WriteOnly ); 289 file.open( IO_WriteOnly );
287 file.close(); 290 file.close();
288 291
289 // Create lock file 292 // Create lock file
290 int result = 0; 293 int result = 0;
291#ifndef _WIN32_ 294#ifndef _WIN32_
292 result = ::link( QFile::encodeName( mLockUniqueName ), 295 result = ::link( QFile::encodeName( mLockUniqueName ),
293 QFile::encodeName( lockName ) ); 296 QFile::encodeName( lockName ) );
294#endif 297#endif
295 if ( result == 0 ) { 298 if ( result == 0 ) {
296 addressBook()->emitAddressBookLocked(); 299 addressBook()->emitAddressBookLocked();
297 return true; 300 return true;
298 } 301 }
299 302
300 // TODO: check stat 303 // TODO: check stat
301 304
302 return false; 305 return false;
303#else 306#else
304 return true; 307 return true;
305#endif 308#endif
306} 309}
307 310
308void ResourceFile::unlock( const QString &fileName ) 311void 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
327void ResourceFile::setFileName( const QString &fileName ) 330void 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
342QString ResourceFile::fileName() const 348QString ResourceFile::fileName() const
343{ 349{
344 return mFileName; 350 return mFileName;
345} 351}
346 352
347void ResourceFile::setFormat( const QString &format ) 353void 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") {
361 mFormat = new BinaryFormat(); 367 mFormat = new BinaryFormat();
362// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 368// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
363 } 369 }
364 else 370 else
365 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 371 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
366*/ 372*/
367 373
368} 374}
369 375
370QString ResourceFile::format() const 376QString ResourceFile::format() const
371{ 377{
372 return mFormatName; 378 return mFormatName;
373} 379}
374 380
375void ResourceFile::fileChanged() 381void ResourceFile::fileChanged()
376{ 382{
377 // There is a small theoretical chance that KDirWatch calls us before 383 // There is a small theoretical chance that KDirWatch calls us before
378 // we are fully constructed 384 // we are fully constructed
379 if (!addressBook()) 385 if (!addressBook())
380 return; 386 return;
381 387
382 388
383 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 389 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
384 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 390 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
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
@@ -108,55 +108,56 @@ public:
108 /** 108 /**
109 * Set name of file to be used for saving. 109 * Set name of file to be used for saving.
110 */ 110 */
111 void setFileName( const QString & ); 111 void setFileName( const QString & );
112 112
113 /** 113 /**
114 * Return name of file used for loading and saving the address book. 114 * Return name of file used for loading and saving the address book.
115 */ 115 */
116 QString fileName() const; 116 QString fileName() const;
117 117
118 /** 118 /**
119 Sets a new format by name. 119 Sets a new format by name.
120 */ 120 */
121 void setFormat( const QString &name ); 121 void setFormat( const QString &name );
122 122
123 /** 123 /**
124 Returns the format name. 124 Returns the format name.
125 */ 125 */
126 QString format() const; 126 QString format() const;
127 127
128 /** 128 /**
129 * Remove a addressee from its source. 129 * Remove a addressee from its source.
130 * This method is mainly called by KABC::AddressBook. 130 * This method is mainly called by KABC::AddressBook.
131 */ 131 */
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
140protected slots: 140protected slots:
141 void fileChanged(); 141 void fileChanged();
142 142
143protected: 143protected:
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
149private: 149private:
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