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