summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/qtopia/resourceqtopia.cpp
authorulf69 <ulf69>2004-07-12 23:34:33 (UTC)
committer ulf69 <ulf69>2004-07-12 23:34:33 (UTC)
commitbce030bdee5c381d7937ef3d2b1b49ce24bbd1ae (patch) (unidiff)
treee92c85b28da2db2552f64be567e511c13106cc82 /kabc/plugins/qtopia/resourceqtopia.cpp
parentc4d51e8f5ecf7be146ee53ea2bbe3af6fca25d4d (diff)
downloadkdepimpi-bce030bdee5c381d7937ef3d2b1b49ce24bbd1ae.zip
kdepimpi-bce030bdee5c381d7937ef3d2b1b49ce24bbd1ae.tar.gz
kdepimpi-bce030bdee5c381d7937ef3d2b1b49ce24bbd1ae.tar.bz2
added first version of the qtopia resourcefiles. The content is of no meaning
Diffstat (limited to 'kabc/plugins/qtopia/resourceqtopia.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.cpp410
1 files changed, 410 insertions, 0 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp
new file mode 100644
index 0000000..3d9d28b
--- a/dev/null
+++ b/kabc/plugins/qtopia/resourceqtopia.cpp
@@ -0,0 +1,410 @@
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#include <unistd.h>
31
32#include <qdir.h>
33#include <qfile.h>
34#include <qfileinfo.h>
35#include <qregexp.h>
36#include <qtimer.h>
37
38#include <kapplication.h>
39#include <kconfig.h>
40#include <kdebug.h>
41#include <klocale.h>
42//US #include <ksavefile.h>
43#include <kstandarddirs.h>
44
45//US #include "formatfactory.h"
46//US #include <qpe/qpeapplication.h>
47
48#include <opie/ocontactaccess.h>
49#include <opie/ocontactaccessbackend_xml.h>
50
51#include "resource.h"
52#include "resourceqtopiaconfig.h"
53#include "stdaddressbook.h"
54
55#include "opieconverter.h"
56
57#include "resourceqtopia.h"
58
59using namespace KABC;
60extern "C"
61{
62 void *init_microkabc_qtopia()
63 {
64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>();
65 }
66}
67
68ResourceOpie::ResourceOpie( const KConfig *config )
69 : Resource( config ), mConverter (0)
70{
71 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
72
73 KConfig *cfg = (KConfig *)config;
74 if ( cfg ) {
75 fileName = cfg->readEntry( "FileName", fileName );
76
77 }
78
79// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
80
81 init( fileName );
82}
83
84ResourceOpie::ResourceOpie( const QString &fileName )
85 : Resource( 0 )
86{
87// qDebug("ResourceOpie::ResourceOpie : 3 %s", fileName.latin1());
88 init( fileName );
89}
90
91void ResourceOpie::init( const QString &fileName )
92{
93
94/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods
95 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
96 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
97 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
98*/
99 //US opie addressbook is always readonly
100//US setReadOnly( true );
101
102 setFileName( fileName );
103
104}
105
106ResourceOpie::~ResourceOpie()
107{
108 if (mConverter != 0)
109 delete mConverter;
110}
111
112void ResourceOpie::writeConfig( KConfig *config )
113{
114 //US opie addressbook is always readonly
115//US setReadOnly( true );
116
117 Resource::writeConfig( config );
118
119 config->writeEntry( "FileName", mFileName );
120
121// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
122
123}
124
125Ticket *ResourceOpie::requestSaveTicket()
126{
127 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl;
128
129 if ( !addressBook() ) return 0;
130
131 if ( !lock( mFileName ) ) {
132 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '"
133 << mFileName << "'" << endl;
134 return 0;
135 }
136 return createTicket( this );
137}
138
139
140bool ResourceOpie::doOpen()
141{
142// qDebug("ResourceOpie::doOpen() %s", mFileName.latin1());
143/*US
144 QFile file( mFileName );
145
146 if ( !file.exists() ) {
147 // try to create the file
148 bool ok = file.open( IO_WriteOnly );
149 if ( ok )
150 file.close();
151
152 return ok;
153 } else {
154 if ( !file.open( IO_ReadWrite ) )
155 return false;
156
157 if ( file.size() == 0 ) {
158 file.close();
159 return true;
160 }
161
162//US bool ok = mFormat->checkFormat( &file );
163 bool ok = true;
164
165 file.close();
166
167 return ok;
168 }
169*/
170 qDebug("ResourceOpie::doOpen() has to be fixed - %s", mFileName.latin1());
171 return true;
172}
173
174void ResourceOpie::doClose()
175{
176// qDebug("ResourceOpie::doClose() %s", mFileName.latin1());
177}
178
179bool ResourceOpie::load()
180{
181// qDebug("ResourceOpie::load() %s", mFileName.latin1());
182 kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl;
183
184 qDebug("ResourceOpie::load: Try to load file() %s", mFileName.latin1());
185
186 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", mFileName );
187 OContactAccess* access = new OContactAccess("KDEPim/Pi", 0l, backend, false);
188
189 if ( !access ) {
190 qDebug("Unable to load file() %s", mFileName.latin1());
191 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
192 return false;
193 }
194
195 access -> setReadAhead( 32 ); // Use ReadAhead-Cache if available
196
197 bool res = false;
198 if (mConverter == 0)
199 {
200 mConverter = new OpieConverter();
201 res = mConverter->init();
202 if ( !res )
203 {
204 qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
205 addressBook()->error( i18n( "Unable to initialize opie converter. Most likely a problem with the category file" ) );
206 delete access;
207 return false;
208 }
209 }
210
211
212 OContactAccess::List::Iterator it;
213 OContactAccess::List allList = access->allRecords();
214 for ( it = allList.begin(); it != allList.end(); ++it )
215 {
216 OContact c = (*it);
217
218 KABC::Addressee addressee;
219
220 res = mConverter->opieToAddressee( c, addressee );
221
222 if ( !addressee.isEmpty() && res )
223 {
224 addressee.setResource( this );
225 addressBook()->insertAddressee( addressee );
226 }
227
228// qDebug("found %s", c.fullName().latin1());
229 }
230
231 delete access;
232 // it seems so, that deletion of access deletes backend as well
233 //delete backend;
234
235 return true;
236}
237
238bool ResourceOpie::save( Ticket *ticket )
239{
240 qDebug("ResourceOpie::save() has to be fixed - %s", mFileName.latin1());
241/*US
242
243 qDebug("ResourceOpie::save %s", mFileName.latin1());
244 kdDebug(5700) << "ResourceOpie::save()" << endl;
245
246 // create backup file
247 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
248*/
249/*US we use a simpler method to create a backupfile
250
251 (void) KSaveFile::backupFile( mFileName, QString::null
252 ,extension );
253
254 KSaveFile saveFile( mFileName );
255 bool ok = false;
256 if ( saveFile.status() == 0 && saveFile.file() )
257 {
258 mFormat->saveAll( addressBook(), this, saveFile.file() );
259 ok = saveFile.close();
260 }
261*/
262/*US
263//US ToDo: write backupfile
264 QFile info;
265 info.setName( mFileName );
266 bool ok = info.open( IO_WriteOnly );
267 if ( ok ) {
268//US mFormat->saveAll( addressBook(), this, &info );
269
270 info.close();
271 ok = true;
272 }
273 else {
274
275 }
276
277 if ( !ok )
278 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
279
280 delete ticket;
281 unlock( mFileName );
282
283 return ok;
284
285 qDebug("ResourceOpie::save has to be changed");
286*/
287 return true;
288
289}
290
291bool ResourceOpie::lock( const QString &fileName )
292{
293 qDebug("ResourceOpie::lock() has to be fixed - %s", mFileName.latin1());
294
295/*US
296// qDebug("ResourceOpie::lock() %s", fileName.latin1());
297 kdDebug(5700) << "ResourceOpie::lock()" << endl;
298
299 QString fn = fileName;
300
301//US change the implementation how the lockfilename is getting created
302//US fn.replace( QRegExp("/"), "_" );
303//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
304
305 KURL url(fn);
306 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
307
308 kdDebug(5700) << "-- lock name: " << lockName << endl;
309
310 if (QFile::exists( lockName )) return false;
311
312 QString lockUniqueName;
313 lockUniqueName = fn + KApplication::randomString( 8 );
314
315 url = lockUniqueName;
316//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
317 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
318 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
319
320 // Create unique file
321 QFile file( mLockUniqueName );
322 file.open( IO_WriteOnly );
323 file.close();
324
325 // Create lock file
326 int result = ::link( QFile::encodeName( mLockUniqueName ),
327 QFile::encodeName( lockName ) );
328
329 if ( result == 0 ) {
330 addressBook()->emitAddressBookLocked();
331 return true;
332 }
333
334 // TODO: check stat
335
336 return false;
337*/
338
339 return true;
340}
341
342void ResourceOpie::unlock( const QString &fileName )
343{
344 qDebug("ResourceOpie::unlock() has to be fixed - %s", mFileName.latin1());
345/*US
346// qDebug("ResourceOpie::unlock() %s", fileName.latin1());
347
348 QString fn = fileName;
349//US change the implementation how the lockfilename is getting created
350//US fn.replace( QRegExp( "/" ), "_" );
351//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
352//US QString lockName = fn + ".lock";
353 KURL url(fn);
354 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
355
356 QFile::remove( lockName );
357 QFile::remove( mLockUniqueName );
358 addressBook()->emitAddressBookUnlocked();
359*/
360}
361
362void ResourceOpie::setFileName( const QString &fileName )
363{
364/*US ToDo: no synchronization so far. Has to be changed in the future
365 mDirWatch.stopScan();
366 mDirWatch.removeFile( mFileName );
367*/
368 mFileName = fileName;
369
370
371/*US ToDo: no synchronization so far. Has to be changed in the future
372 mDirWatch.addFile( mFileName );
373 mDirWatch.startScan();
374*/
375//US simulate KDirWatch event
376 fileChanged();
377}
378
379QString ResourceOpie::fileName() const
380{
381 return mFileName;
382}
383
384void ResourceOpie::fileChanged()
385{
386 // There is a small theoretical chance that KDirWatch calls us before
387 // we are fully constructed
388 if (!addressBook())
389 return;
390 load();
391 addressBook()->emitAddressBookChanged();
392}
393
394void ResourceOpie::removeAddressee( const Addressee &addr )
395{
396/*US
397 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
398 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
399 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
400*/
401}
402
403void ResourceOpie::cleanUp()
404{
405// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1());
406
407 unlock( mFileName );
408}
409
410//US #include "resourceopie.moc"