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