summaryrefslogtreecommitdiffabout
path: root/kabc
Unidiff
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.cpp67
1 files changed, 34 insertions, 33 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp
index 7475685..7e10e46 100644
--- a/kabc/plugins/qtopia/resourceqtopia.cpp
+++ b/kabc/plugins/qtopia/resourceqtopia.cpp
@@ -1,356 +1,357 @@
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#include <sys/types.h> 27#include <sys/types.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <unistd.h> 29#include <unistd.h>
30 30
31#include <qdir.h> 31#include <qdir.h>
32#include <qfile.h> 32#include <qfile.h>
33#include <qfileinfo.h> 33#include <qfileinfo.h>
34#include <qregexp.h> 34#include <qregexp.h>
35//US #include <qtimer.h> 35//US #include <qtimer.h>
36 36
37#include <kapplication.h> 37#include <kapplication.h>
38#include <kconfig.h> 38#include <kconfig.h>
39#include <kdebug.h> 39#include <kdebug.h>
40#include <klocale.h> 40#include <klocale.h>
41//US #include <ksavefile.h> 41//US #include <ksavefile.h>
42#include <kstandarddirs.h> 42#include <kstandarddirs.h>
43#include <kmessagebox.h> 43#include <kmessagebox.h>
44 44
45#include <qpe/pim/addressbookaccess.h> 45#include <qpe/pim/addressbookaccess.h>
46 46
47 47
48#include "resourceqtopiaconfig.h" 48#include "resourceqtopiaconfig.h"
49#include "stdaddressbook.h" 49#include "stdaddressbook.h"
50 50
51#include "qtopiaconverter.h" 51#include "qtopiaconverter.h"
52 52
53#include "resourceqtopia.h" 53#include "resourceqtopia.h"
54 54
55using namespace KABC; 55using namespace KABC;
56extern "C" 56extern "C"
57{ 57{
58 void *init_microkabc_qtopia() 58 void *init_microkabc_qtopia()
59 { 59 {
60 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig>(); 60 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig>();
61 } 61 }
62} 62}
63 63
64ResourceQtopia::ResourceQtopia( const KConfig *config ) 64ResourceQtopia::ResourceQtopia( const KConfig *config )
65 : Resource( config ), mConverter (0) 65 : Resource( config ), mConverter (0)
66{ 66{
67 // we can not choose the filename. Therefore use the default to display 67 // we can not choose the filename. Therefore use the default to display
68 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; 68 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
69 69
70 init( fileName ); 70 init( fileName );
71} 71}
72 72
73ResourceQtopia::ResourceQtopia( const QString &fileName ) 73ResourceQtopia::ResourceQtopia( const QString &fileName )
74 : Resource( 0 ) 74 : Resource( 0 )
75{ 75{
76// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 76// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
77 init( fileName ); 77 init( fileName );
78} 78}
79 79
80void ResourceQtopia::init( const QString &fileName ) 80void ResourceQtopia::init( const QString &fileName )
81{ 81{
82 82
83 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 83 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
84 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 84 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
85 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 85 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
86 86
87 setFileName( fileName ); 87 setFileName( fileName );
88} 88}
89 89
90ResourceQtopia::~ResourceQtopia() 90ResourceQtopia::~ResourceQtopia()
91{ 91{
92 if (mConverter != 0) 92 if (mConverter != 0)
93 delete mConverter; 93 delete mConverter;
94 94
95} 95}
96 96
97void ResourceQtopia::writeConfig( KConfig *config ) 97void ResourceQtopia::writeConfig( KConfig *config )
98{ 98{
99 Resource::writeConfig( config ); 99 Resource::writeConfig( config );
100} 100}
101 101
102Ticket *ResourceQtopia::requestSaveTicket() 102Ticket *ResourceQtopia::requestSaveTicket()
103{ 103{
104 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; 104 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl;
105 105
106 if ( !addressBook() ) return 0; 106 if ( !addressBook() ) return 0;
107 107
108 if ( !lock( mFileName ) ) { 108 if ( !lock( mFileName ) ) {
109 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" 109 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '"
110 << mFileName << "'" << endl; 110 << mFileName << "'" << endl;
111 return 0; 111 return 0;
112 } 112 }
113 return createTicket( this ); 113 return createTicket( this );
114} 114}
115 115
116 116
117bool ResourceQtopia::doOpen() 117bool ResourceQtopia::doOpen()
118{ 118{
119/*US 119/*US
120 QFile file( mFileName ); 120 QFile file( mFileName );
121 121
122 if ( !file.exists() ) { 122 if ( !file.exists() ) {
123 // try to create the file 123 // try to create the file
124 bool ok = file.open( IO_WriteOnly ); 124 bool ok = file.open( IO_WriteOnly );
125 if ( ok ) 125 if ( ok )
126 file.close(); 126 file.close();
127 127
128 return ok; 128 return ok;
129 } else { 129 } else {
130 if ( !file.open( IO_ReadWrite ) ) 130 if ( !file.open( IO_ReadWrite ) )
131 return false; 131 return false;
132 132
133 if ( file.size() == 0 ) { 133 if ( file.size() == 0 ) {
134 file.close(); 134 file.close();
135 return true; 135 return true;
136 } 136 }
137 137
138//US bool ok = mFormat->checkFormat( &file ); 138//US bool ok = mFormat->checkFormat( &file );
139 bool ok = true; 139 bool ok = true;
140 140
141 file.close(); 141 file.close();
142 142
143 return ok; 143 return ok;
144 } 144 }
145*/ 145*/
146 return true; 146 return true;
147} 147}
148 148
149void ResourceQtopia::doClose() 149void ResourceQtopia::doClose()
150{ 150{
151} 151}
152 152
153bool ResourceQtopia::load() 153bool ResourceQtopia::load()
154{ 154{
155 kdDebug(5700) << "ResourceQtopia::load(): '" << mFileName << "'" << endl; 155 kdDebug(5700) << "ResourceQtopia::load(): '" << mFileName << "'" << endl;
156 156
157// qDebug("ResourceQtopia::load: Try to load file() %s", mFileName.latin1()); 157// qDebug("ResourceQtopia::load: Try to load file() %s", mFileName.latin1());
158 158
159 AddressBookAccess* access = new AddressBookAccess(); 159 AddressBookAccess* access = new AddressBookAccess();
160 160
161 if ( !access ) { 161 if ( !access ) {
162 qDebug("Unable to load file() %s", mFileName.latin1()); 162 qDebug("Unable to load file() %s", mFileName.latin1());
163 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) ); 163 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
164 return false; 164 return false;
165 } 165 }
166 166
167 bool res = false; 167 bool res = false;
168 if (mConverter == 0) 168 if (mConverter == 0)
169 { 169 {
170 mConverter = new QtopiaConverter(); 170 mConverter = new QtopiaConverter();
171 res = mConverter->init(); 171 res = mConverter->init();
172 if ( !res ) 172 if ( !res )
173 { 173 {
174 qDebug("Unable to initialize qtopia converter. Most likely a problem with the category file"); 174 QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file");
175 addressBook()->error( i18n( "Unable to initialize qtopia converter. Most likely a problem with the category file" ) ); 175 qDebug(msg);
176 addressBook()->error( i18n( msg ) );
176 delete access; 177 delete access;
177 return false; 178 return false;
178 } 179 }
179 } 180 }
180 181
181 { //create a new scope 182 { //create a new scope
182 AddressBookIterator it(*access); 183 AddressBookIterator it(*access);
183 const PimContact* contact; 184 const PimContact* contact;
184 185
185 for (contact=it.toFirst(); it.current(); ++it) { 186 for (contact=it.toFirst(); it.current(); ++it) {
186 contact = it.current(); 187 contact = it.current();
187 188
188 KABC::Addressee addressee; 189 KABC::Addressee addressee;
189 190
190 res = mConverter->qtopiaToAddressee( (*contact), addressee ); 191 res = mConverter->qtopiaToAddressee( (*contact), addressee );
191 192
192 if ( !addressee.isEmpty() && res ) 193 if ( !addressee.isEmpty() && res )
193 { 194 {
194 addressee.setResource( this ); 195 addressee.setResource( this );
195 addressBook()->insertAddressee( addressee ); 196 addressBook()->insertAddressee( addressee );
196 } 197 }
197 } 198 }
198 } 199 }
199 200
200 delete access; 201 delete access;
201 return true; 202 return true;
202} 203}
203 204
204bool ResourceQtopia::save( Ticket *ticket ) 205bool ResourceQtopia::save( Ticket *ticket )
205{ 206{
206/*US 207/*US
207// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 208// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
208 kdDebug(5700) << "ResourceQtopia::save()" << endl; 209 kdDebug(5700) << "ResourceQtopia::save()" << endl;
209 210
210 // create backup file 211 // create backup file
211 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 212 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
212 213
213/*US we use a simpler method to create a backupfile 214/*US we use a simpler method to create a backupfile
214 215
215 (void) KSaveFile::backupFile( mFileName, QString::null 216 (void) KSaveFile::backupFile( mFileName, QString::null
216 ,extension ); 217 ,extension );
217 218
218 KSaveFile saveFile( mFileName ); 219 KSaveFile saveFile( mFileName );
219 bool ok = false; 220 bool ok = false;
220 if ( saveFile.status() == 0 && saveFile.file() ) 221 if ( saveFile.status() == 0 && saveFile.file() )
221 { 222 {
222 mFormat->saveAll( addressBook(), this, saveFile.file() ); 223 mFormat->saveAll( addressBook(), this, saveFile.file() );
223 ok = saveFile.close(); 224 ok = saveFile.close();
224 } 225 }
225*/ 226*/
226 227
227/*US 228/*US
228//US ToDo: write backupfile 229//US ToDo: write backupfile
229 QFile info; 230 QFile info;
230 info.setName( mFileName ); 231 info.setName( mFileName );
231 bool ok = info.open( IO_WriteOnly ); 232 bool ok = info.open( IO_WriteOnly );
232 if ( ok ) { 233 if ( ok ) {
233//US mFormat->saveAll( addressBook(), this, &info ); 234//US mFormat->saveAll( addressBook(), this, &info );
234 235
235 info.close(); 236 info.close();
236 ok = true; 237 ok = true;
237 } 238 }
238 else { 239 else {
239 240
240 } 241 }
241 242
242 if ( !ok ) 243 if ( !ok )
243 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 244 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
244 245
245 delete ticket; 246 delete ticket;
246 unlock( mFileName ); 247 unlock( mFileName );
247 248
248 return ok; 249 return ok;
249 250
250 qDebug("ResourceQtopia::save has to be changed"); 251 qDebug("ResourceQtopia::save has to be changed");
251*/ 252*/
252 return true; 253 return true;
253} 254}
254 255
255bool ResourceQtopia::lock( const QString &fileName ) 256bool ResourceQtopia::lock( const QString &fileName )
256{ 257{
257 kdDebug(5700) << "ResourceQtopia::lock()" << endl; 258 kdDebug(5700) << "ResourceQtopia::lock()" << endl;
258 259
259 QString fn = fileName; 260 QString fn = fileName;
260 261
261//US change the implementation how the lockfilename is getting created 262//US change the implementation how the lockfilename is getting created
262//US fn.replace( QRegExp("/"), "_" ); 263//US fn.replace( QRegExp("/"), "_" );
263//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 264//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
264 265
265 KURL url(fn); 266 KURL url(fn);
266 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 267 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
267 268
268 kdDebug(5700) << "-- lock name: " << lockName << endl; 269 kdDebug(5700) << "-- lock name: " << lockName << endl;
269 270
270 if (QFile::exists( lockName )) return false; 271 if (QFile::exists( lockName )) return false;
271 272
272 QString lockUniqueName; 273 QString lockUniqueName;
273 lockUniqueName = fn + KApplication::randomString( 8 ); 274 lockUniqueName = fn + KApplication::randomString( 8 );
274 275
275 url = lockUniqueName; 276 url = lockUniqueName;
276//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 277//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
277 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 278 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
278 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 279 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
279 280
280 // Create unique file 281 // Create unique file
281 QFile file( mLockUniqueName ); 282 QFile file( mLockUniqueName );
282 file.open( IO_WriteOnly ); 283 file.open( IO_WriteOnly );
283 file.close(); 284 file.close();
284 285
285 // Create lock file 286 // Create lock file
286 int result = ::link( QFile::encodeName( mLockUniqueName ), 287 int result = ::link( QFile::encodeName( mLockUniqueName ),
287 QFile::encodeName( lockName ) ); 288 QFile::encodeName( lockName ) );
288 289
289 if ( result == 0 ) { 290 if ( result == 0 ) {
290 addressBook()->emitAddressBookLocked(); 291 addressBook()->emitAddressBookLocked();
291 return true; 292 return true;
292 } 293 }
293 294
294 // TODO: check stat 295 // TODO: check stat
295 296
296 return false; 297 return false;
297} 298}
298 299
299void ResourceQtopia::unlock( const QString &fileName ) 300void ResourceQtopia::unlock( const QString &fileName )
300{ 301{
301 QString fn = fileName; 302 QString fn = fileName;
302//US change the implementation how the lockfilename is getting created 303//US change the implementation how the lockfilename is getting created
303//US fn.replace( QRegExp( "/" ), "_" ); 304//US fn.replace( QRegExp( "/" ), "_" );
304//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 305//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
305//US QString lockName = fn + ".lock"; 306//US QString lockName = fn + ".lock";
306 KURL url(fn); 307 KURL url(fn);
307 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 308 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
308 309
309 QFile::remove( lockName ); 310 QFile::remove( lockName );
310 QFile::remove( mLockUniqueName ); 311 QFile::remove( mLockUniqueName );
311 addressBook()->emitAddressBookUnlocked(); 312 addressBook()->emitAddressBookUnlocked();
312} 313}
313 314
314void ResourceQtopia::setFileName( const QString &fileName ) 315void ResourceQtopia::setFileName( const QString &fileName )
315{ 316{
316 mDirWatch.stopScan(); 317 mDirWatch.stopScan();
317 mDirWatch.removeFile( mFileName ); 318 mDirWatch.removeFile( mFileName );
318 319
319 mFileName = fileName; 320 mFileName = fileName;
320 321
321 mDirWatch.addFile( mFileName ); 322 mDirWatch.addFile( mFileName );
322 mDirWatch.startScan(); 323 mDirWatch.startScan();
323 324
324//US simulate KDirWatch event 325//US simulate KDirWatch event
325//US fileChanged(); 326//US fileChanged();
326} 327}
327 328
328QString ResourceQtopia::fileName() const 329QString ResourceQtopia::fileName() const
329{ 330{
330 return mFileName; 331 return mFileName;
331} 332}
332 333
333void ResourceQtopia::fileChanged() 334void ResourceQtopia::fileChanged()
334{ 335{
335 // There is a small theoretical chance that KDirWatch calls us before 336 // There is a small theoretical chance that KDirWatch calls us before
336 // we are fully constructed 337 // we are fully constructed
337 if (!addressBook()) 338 if (!addressBook())
338 return; 339 return;
339 340
340 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 341 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
341 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 342 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
342 load(); 343 load();
343 addressBook()->emitAddressBookChanged(); 344 addressBook()->emitAddressBookChanged();
344 } 345 }
345} 346}
346 347
347void ResourceQtopia::removeAddressee( const Addressee &addr ) 348void ResourceQtopia::removeAddressee( const Addressee &addr )
348{ 349{
349} 350}
350 351
351void ResourceQtopia::cleanUp() 352void ResourceQtopia::cleanUp()
352{ 353{
353 unlock( mFileName ); 354 unlock( mFileName );
354} 355}
355 356
356//US #include "resourceqtopia.moc" 357//US #include "resourceqtopia.moc"