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