summaryrefslogtreecommitdiffabout
path: root/kabc
Unidiff
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/dir/resourcedir.cpp3
-rw-r--r--kabc/plugins/file/resourcefile.cpp9
-rw-r--r--kabc/plugins/opie/resourceopie.cpp4
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.cpp3
-rw-r--r--kabc/plugins/sharpdtm/resourcesharpdtm.cpp2
5 files changed, 16 insertions, 5 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp
index eccd482..fac06a1 100644
--- a/kabc/plugins/dir/resourcedir.cpp
+++ b/kabc/plugins/dir/resourcedir.cpp
@@ -1,365 +1,368 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2002 Tobias Koenig <tokoe@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 28
29#include <sys/types.h> 29#include <sys/types.h>
30#include <sys/stat.h> 30#include <sys/stat.h>
31#ifndef _WIN32_ 31#ifndef _WIN32_
32#include <unistd.h> 32#include <unistd.h>
33#endif 33#endif
34 34
35#include <qregexp.h> 35#include <qregexp.h>
36#include <qtimer.h> 36#include <qtimer.h>
37#include <qwidget.h> 37#include <qwidget.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//US #include <kgenericfactory.h> 42//US #include <kgenericfactory.h>
43#include <kglobal.h> 43#include <kglobal.h>
44#include <klocale.h> 44#include <klocale.h>
45#include <kstandarddirs.h> 45#include <kstandarddirs.h>
46#include <kurlrequester.h> 46#include <kurlrequester.h>
47#include <kmessagebox.h> 47#include <kmessagebox.h>
48 48
49#include "addressbook.h" 49#include "addressbook.h"
50 50
51#include "formatfactory.h" 51#include "formatfactory.h"
52 52
53#include "resourcedirconfig.h" 53#include "resourcedirconfig.h"
54#include "stdaddressbook.h" 54#include "stdaddressbook.h"
55 55
56//US 56//US
57#include <qdir.h> 57#include <qdir.h>
58 58
59#include "resourcedir.h" 59#include "resourcedir.h"
60#include "syncprefwidget.h" 60#include "syncprefwidget.h"
61 61
62using namespace KABC; 62using namespace KABC;
63 63
64extern "C" 64extern "C"
65#ifdef _WIN32_ 65#ifdef _WIN32_
66__declspec(dllexport) 66__declspec(dllexport)
67#else 67#else
68{ 68{
69#endif 69#endif
70 70
71//US void *init_kabc_dir() 71//US void *init_kabc_dir()
72 void *init_microkabc_dir() 72 void *init_microkabc_dir()
73 { 73 {
74 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>(); 74 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>();
75 } 75 }
76#ifndef _WIN32_ 76#ifndef _WIN32_
77} 77}
78#endif 78#endif
79 79
80ResourceDir::ResourceDir( const KConfig *config, bool syncable ) 80ResourceDir::ResourceDir( const KConfig *config, bool syncable )
81 : Resource( config, syncable ) 81 : Resource( config, syncable )
82{ 82{
83 QString path; 83 QString path;
84 84
85 KConfig *cfg = (KConfig *)config; 85 KConfig *cfg = (KConfig *)config;
86 if ( cfg ) { 86 if ( cfg ) {
87//US path = config->readEntry( "FilePath" ); 87//US path = config->readEntry( "FilePath" );
88 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() ); 88 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() );
89//US mFormatName = config->readEntry( "FileFormat" ); 89//US mFormatName = config->readEntry( "FileFormat" );
90 mFormatName = cfg->readEntry( "FileFormat", "vcard" ); 90 mFormatName = cfg->readEntry( "FileFormat", "vcard" );
91 } else { 91 } else {
92 path = StdAddressBook::directoryName(); 92 path = StdAddressBook::directoryName();
93 mFormatName = "vcard"; 93 mFormatName = "vcard";
94 } 94 }
95 95
96 96
97 FormatFactory *factory = FormatFactory::self(); 97 FormatFactory *factory = FormatFactory::self();
98 mFormat = factory->format( mFormatName ); 98 mFormat = factory->format( mFormatName );
99 99
100 if ( !mFormat ) { 100 if ( !mFormat ) {
101 mFormatName = "vcard"; 101 mFormatName = "vcard";
102 mFormat = factory->format( mFormatName ); 102 mFormat = factory->format( mFormatName );
103 } 103 }
104 104
105/*US 105/*US
106//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); 106//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1());
107 if (mFormatName == "vcard") 107 if (mFormatName == "vcard")
108 mFormat = new VCardFormatPlugin2(); 108 mFormat = new VCardFormatPlugin2();
109 else if (mFormatName == "binary") 109 else if (mFormatName == "binary")
110 mFormat = new BinaryFormat(); 110 mFormat = new BinaryFormat();
111 else 111 else
112 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); 112 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1());
113*/ 113*/
114 114
115 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); 115 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) );
116 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); 116 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) );
117 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); 117 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) );
118 118
119 setPath( path ); 119 setPath( path );
120} 120}
121 121
122ResourceDir::~ResourceDir() 122ResourceDir::~ResourceDir()
123{ 123{
124 delete mFormat; 124 delete mFormat;
125 mFormat = 0; 125 mFormat = 0;
126} 126}
127 127
128void ResourceDir::writeConfig( KConfig *config ) 128void ResourceDir::writeConfig( KConfig *config )
129{ 129{
130 config->setGroup( "Resource_" + identifier() ); 130 config->setGroup( "Resource_" + identifier() );
131 Resource::writeConfig( config ); 131 Resource::writeConfig( config );
132 132
133 config->writeEntry( "FilePath", mPath ); 133 config->writeEntry( "FilePath", mPath );
134 config->writeEntry( "FileFormat", mFormatName ); 134 config->writeEntry( "FileFormat", mFormatName );
135} 135}
136 136
137Ticket *ResourceDir::requestSaveTicket() 137Ticket *ResourceDir::requestSaveTicket()
138{ 138{
139 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl; 139 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl;
140 140
141 if ( !addressBook() ) return 0; 141 if ( !addressBook() ) return 0;
142 142
143 if ( !lock( mPath ) ) { 143 if ( !lock( mPath ) ) {
144 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '" 144 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '"
145 << mPath << "'" << endl; 145 << mPath << "'" << endl;
146 return 0; 146 return 0;
147 } 147 }
148 return createTicket( this ); 148 return createTicket( this );
149} 149}
150 150
151 151
152bool ResourceDir::doOpen() 152bool ResourceDir::doOpen()
153{ 153{
154 QDir dir( mPath ); 154 QDir dir( mPath );
155 if ( !dir.exists() ) { // no directory available 155 if ( !dir.exists() ) { // no directory available
156 return dir.mkdir( dir.path() ); 156 return dir.mkdir( dir.path() );
157 } else { 157 } else {
158 QString testName = dir.entryList( QDir::Files )[0]; 158 QString testName = dir.entryList( QDir::Files )[0];
159 if ( testName.isNull() || testName.isEmpty() ) // no file in directory 159 if ( testName.isNull() || testName.isEmpty() ) // no file in directory
160 return true; 160 return true;
161 161
162 QFile file( mPath + "/" + testName ); 162 QFile file( mPath + "/" + testName );
163 if ( file.open( IO_ReadOnly ) ) 163 if ( file.open( IO_ReadOnly ) )
164 return true; 164 return true;
165 165
166 if ( file.size() == 0 ) 166 if ( file.size() == 0 )
167 return true; 167 return true;
168 168
169 bool ok = mFormat->checkFormat( &file ); 169 bool ok = mFormat->checkFormat( &file );
170 file.close(); 170 file.close();
171 return ok; 171 return ok;
172 } 172 }
173} 173}
174 174
175void ResourceDir::doClose() 175void ResourceDir::doClose()
176{ 176{
177} 177}
178 178
179bool ResourceDir::load() 179bool ResourceDir::load()
180{ 180{
181 kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl; 181 kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl;
182 182
183 QDir dir( mPath ); 183 QDir dir( mPath );
184 QStringList files = dir.entryList( QDir::Files ); 184 QStringList files = dir.entryList( QDir::Files );
185 185
186 QStringList::Iterator it; 186 QStringList::Iterator it;
187 bool ok = true; 187 bool ok = true;
188 for ( it = files.begin(); it != files.end(); ++it ) { 188 for ( it = files.begin(); it != files.end(); ++it ) {
189 QFile file( mPath + "/" + (*it) ); 189 QFile file( mPath + "/" + (*it) );
190 190
191 if ( !file.open( IO_ReadOnly ) ) { 191 if ( !file.open( IO_ReadOnly ) ) {
192 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); 192 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) );
193 ok = false; 193 ok = false;
194 continue; 194 continue;
195 } 195 }
196 196
197 if ( !mFormat->loadAll( addressBook(), this, &file ) ) 197 if ( !mFormat->loadAll( addressBook(), this, &file ) )
198 ok = false; 198 ok = false;
199 199
200 file.close(); 200 file.close();
201 } 201 }
202 202
203 return ok; 203 return ok;
204} 204}
205 205
206bool ResourceDir::save( Ticket *ticket ) 206bool ResourceDir::save( Ticket *ticket )
207{ 207{
208 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl; 208 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl;
209 209
210 AddressBook::Iterator it; 210 AddressBook::Iterator it;
211 bool ok = true; 211 bool ok = true;
212 212
213 mDirWatch.stopScan();
214
213 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 215 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
214 if ( (*it).resource() != this || !(*it).changed() ) 216 if ( (*it).resource() != this || !(*it).changed() )
215 continue; 217 continue;
216 218
217 QFile file( mPath + "/" + (*it).uid() ); 219 QFile file( mPath + "/" + (*it).uid() );
218 if ( !file.open( IO_WriteOnly ) ) { 220 if ( !file.open( IO_WriteOnly ) ) {
219 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); 221 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) );
220 continue; 222 continue;
221 } 223 }
222 224
223 mFormat->save( *it, &file ); 225 mFormat->save( *it, &file );
224 226
225 // mark as unchanged 227 // mark as unchanged
226 (*it).setChanged( false ); 228 (*it).setChanged( false );
227 229
228 file.close(); 230 file.close();
229 } 231 }
232 mDirWatch.startScan();
230 233
231 delete ticket; 234 delete ticket;
232 unlock( mPath ); 235 unlock( mPath );
233 236
234 return ok; 237 return ok;
235} 238}
236 239
237bool ResourceDir::lock( const QString &path ) 240bool ResourceDir::lock( const QString &path )
238{ 241{
239 kdDebug(5700) << "ResourceDir::lock()" << endl; 242 kdDebug(5700) << "ResourceDir::lock()" << endl;
240 243
241 QString p = path; 244 QString p = path;
242//US change the implementation how the lockfilename is getting created 245//US change the implementation how the lockfilename is getting created
243//US p.replace( QRegExp("/"), "_" ); 246//US p.replace( QRegExp("/"), "_" );
244//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); 247//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" );
245 KURL url(p); 248 KURL url(p);
246 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 249 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
247 250
248 251
249 kdDebug(5700) << "-- lock name: " << lockName << endl; 252 kdDebug(5700) << "-- lock name: " << lockName << endl;
250 253
251 if ( QFile::exists( lockName ) ) return false; 254 if ( QFile::exists( lockName ) ) return false;
252 255
253 QString lockUniqueName; 256 QString lockUniqueName;
254 lockUniqueName = p + KApplication::randomString( 8 ); 257 lockUniqueName = p + KApplication::randomString( 8 );
255 258
256 url = lockUniqueName; 259 url = lockUniqueName;
257//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 260//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
258 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 261 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
259 262
260 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 263 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
261 264
262 // Create unique file 265 // Create unique file
263 QFile file( mLockUniqueName ); 266 QFile file( mLockUniqueName );
264 file.open( IO_WriteOnly ); 267 file.open( IO_WriteOnly );
265 file.close(); 268 file.close();
266 269
267 // Create lock file 270 // Create lock file
268 int result = 0; 271 int result = 0;
269#ifndef _WIN32_ 272#ifndef _WIN32_
270 result = ::link( QFile::encodeName( mLockUniqueName ), 273 result = ::link( QFile::encodeName( mLockUniqueName ),
271 QFile::encodeName( lockName ) ); 274 QFile::encodeName( lockName ) );
272#endif 275#endif
273 if ( result == 0 ) { 276 if ( result == 0 ) {
274 addressBook()->emitAddressBookLocked(); 277 addressBook()->emitAddressBookLocked();
275 return true; 278 return true;
276 } 279 }
277 280
278 // TODO: check stat 281 // TODO: check stat
279 282
280 return false; 283 return false;
281} 284}
282 285
283void ResourceDir::unlock( const QString &path ) 286void ResourceDir::unlock( const QString &path )
284{ 287{
285 QString p = path; 288 QString p = path;
286//US change the implementation how the lockfilename is getting created 289//US change the implementation how the lockfilename is getting created
287//US p.replace( QRegExp( "/" ), "_" ); 290//US p.replace( QRegExp( "/" ), "_" );
288//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); 291//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" );
289 KURL url(p); 292 KURL url(p);
290 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); 293 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" );
291 294
292 ::unlink( QFile::encodeName( lockName ) ); 295 ::unlink( QFile::encodeName( lockName ) );
293 QFile::remove( mLockUniqueName ); 296 QFile::remove( mLockUniqueName );
294 addressBook()->emitAddressBookUnlocked(); 297 addressBook()->emitAddressBookUnlocked();
295} 298}
296 299
297void ResourceDir::setPath( const QString &path ) 300void ResourceDir::setPath( const QString &path )
298{ 301{
299 mDirWatch.stopScan(); 302 mDirWatch.stopScan();
300 mDirWatch.removeDir( mPath ); 303 mDirWatch.removeDir( mPath );
301 304
302 mPath = path; 305 mPath = path;
303 306
304 mDirWatch.addDir( mPath, true ); 307 mDirWatch.addDir( mPath, true );
305 mDirWatch.startScan(); 308 mDirWatch.startScan();
306 309
307//US simulate KDirWatch event 310//US simulate KDirWatch event
308//US pathChanged(); 311//US pathChanged();
309 312
310} 313}
311 314
312QString ResourceDir::path() const 315QString ResourceDir::path() const
313{ 316{
314 return mPath; 317 return mPath;
315} 318}
316 319
317void ResourceDir::setFormat( const QString &format ) 320void ResourceDir::setFormat( const QString &format )
318{ 321{
319 mFormatName = format; 322 mFormatName = format;
320 323
321 if ( mFormat ) 324 if ( mFormat )
322 delete mFormat; 325 delete mFormat;
323 326
324 FormatFactory *factory = FormatFactory::self(); 327 FormatFactory *factory = FormatFactory::self();
325 mFormat = factory->format( mFormatName ); 328 mFormat = factory->format( mFormatName );
326/*US 329/*US
327qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); 330qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1());
328 if (mFormatName == "vcard") 331 if (mFormatName == "vcard")
329 mFormat = new VCardFormatPlugin2(); 332 mFormat = new VCardFormatPlugin2();
330 else if (mFormatName == "binary") 333 else if (mFormatName == "binary")
331 mFormat = new BinaryFormat(); 334 mFormat = new BinaryFormat();
332 else 335 else
333 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1()); 336 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1());
334*/ 337*/
335 338
336} 339}
337 340
338QString ResourceDir::format() const 341QString ResourceDir::format() const
339{ 342{
340 return mFormatName; 343 return mFormatName;
341} 344}
342 345
343void ResourceDir::pathChanged() 346void ResourceDir::pathChanged()
344{ 347{
345 if ( !addressBook() ) 348 if ( !addressBook() )
346 return; 349 return;
347 350
348 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) ); 351 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) );
349 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 352 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
350 load(); 353 load();
351 addressBook()->emitAddressBookChanged(); 354 addressBook()->emitAddressBookChanged();
352 } 355 }
353} 356}
354 357
355void ResourceDir::removeAddressee( const Addressee& addr ) 358void ResourceDir::removeAddressee( const Addressee& addr )
356{ 359{
357 QFile::remove( mPath + "/" + addr.uid() ); 360 QFile::remove( mPath + "/" + addr.uid() );
358} 361}
359 362
360void ResourceDir::cleanUp() 363void ResourceDir::cleanUp()
361{ 364{
362 unlock( mPath ); 365 unlock( mPath );
363} 366}
364 367
365//US #include "resourcedir.moc" 368//US #include "resourcedir.moc"
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index da2e4d7..fa6bc49 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -1,389 +1,390 @@
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#include <kmessagebox.h> 45#include <kmessagebox.h>
46 46
47#include "formatfactory.h" 47#include "formatfactory.h"
48 48
49#include "resource.h" 49#include "resource.h"
50#include "resourcefileconfig.h" 50#include "resourcefileconfig.h"
51#include "stdaddressbook.h" 51#include "stdaddressbook.h"
52 52
53#include "resourcefile.h" 53#include "resourcefile.h"
54#include "syncprefwidget.h" 54#include "syncprefwidget.h"
55 55
56 56
57using namespace KABC; 57using namespace KABC;
58 58
59extern "C" 59extern "C"
60#ifdef _WIN32_ 60#ifdef _WIN32_
61__declspec(dllexport) 61__declspec(dllexport)
62#else 62#else
63{ 63{
64#endif 64#endif
65 65
66//US void *init_kabc_file() 66//US void *init_kabc_file()
67 void *init_microkabc_file() 67 void *init_microkabc_file()
68 { 68 {
69 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>(); 69 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>();
70 } 70 }
71#ifndef _WIN32_ 71#ifndef _WIN32_
72} 72}
73#endif 73#endif
74 74
75ResourceFile::ResourceFile( const KConfig *config, bool syncable ) 75ResourceFile::ResourceFile( const KConfig *config, bool syncable )
76 : Resource( config, syncable ) , mFormat( 0 ) 76 : Resource( config, syncable ) , mFormat( 0 )
77{ 77{
78 QString fileName, formatName, default_fileName; 78 QString fileName, formatName, default_fileName;
79 79
80 if (syncable == true) 80 if (syncable == true)
81 default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf"; 81 default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf";
82 else 82 else
83 default_fileName = StdAddressBook::fileName(); 83 default_fileName = StdAddressBook::fileName();
84 84
85 KConfig *cfg = (KConfig *)config; 85 KConfig *cfg = (KConfig *)config;
86 if ( cfg ) { 86 if ( cfg ) {
87 fileName = cfg->readEntry( "FileName", default_fileName ); 87 fileName = cfg->readEntry( "FileName", default_fileName );
88 formatName = cfg->readEntry( "FileFormat", "vcard" ); 88 formatName = cfg->readEntry( "FileFormat", "vcard" );
89 } else { 89 } else {
90 fileName = default_fileName; 90 fileName = default_fileName;
91 formatName = "vcard"; 91 formatName = "vcard";
92 } 92 }
93 93
94 init( fileName, formatName ); 94 init( fileName, formatName );
95} 95}
96 96
97ResourceFile::ResourceFile( const QString &fileName, bool syncable , 97ResourceFile::ResourceFile( const QString &fileName, bool syncable ,
98 const QString &formatName ) 98 const QString &formatName )
99 : Resource( 0, syncable ) 99 : Resource( 0, syncable )
100{ 100{
101// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 101// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
102 init( fileName, formatName ); 102 init( fileName, formatName );
103} 103}
104 104
105void ResourceFile::init( const QString &fileName, const QString &formatName ) 105void ResourceFile::init( const QString &fileName, const QString &formatName )
106{ 106{
107 mFormatName = formatName; 107 mFormatName = formatName;
108 108
109 FormatFactory *factory = FormatFactory::self(); 109 FormatFactory *factory = FormatFactory::self();
110 mFormat = factory->format( mFormatName ); 110 mFormat = factory->format( mFormatName );
111 111
112 if ( !mFormat ) { 112 if ( !mFormat ) {
113 mFormatName = "vcard"; 113 mFormatName = "vcard";
114 mFormat = factory->format( mFormatName ); 114 mFormat = factory->format( mFormatName );
115 } 115 }
116 116
117 117
118 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 118 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
119 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 119 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
120 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 120 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
121 121
122 setFileName( fileName ); 122 setFileName( fileName );
123} 123}
124 124
125ResourceFile::~ResourceFile() 125ResourceFile::~ResourceFile()
126{ 126{
127 delete mFormat; 127 delete mFormat;
128 mFormat = 0; 128 mFormat = 0;
129} 129}
130 130
131void ResourceFile::writeConfig( KConfig *config ) 131void ResourceFile::writeConfig( KConfig *config )
132{ 132{
133 133
134 config->setGroup( "Resource_" + identifier() ); 134 config->setGroup( "Resource_" + identifier() );
135 Resource::writeConfig( config ); 135 Resource::writeConfig( config );
136 136
137 config->writeEntry( "FileName", mFileName ); 137 config->writeEntry( "FileName", mFileName );
138 config->writeEntry( "FileFormat", mFormatName ); 138 config->writeEntry( "FileFormat", mFormatName );
139 139
140// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); 140// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
141 141
142} 142}
143 143
144Ticket *ResourceFile::requestSaveTicket() 144Ticket *ResourceFile::requestSaveTicket()
145{ 145{
146 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; 146 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
147 147
148 if ( !addressBook() ) return 0; 148 if ( !addressBook() ) return 0;
149 149
150 if ( !lock( mFileName ) ) { 150 if ( !lock( mFileName ) ) {
151 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" 151 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '"
152 << mFileName << "'" << endl; 152 << mFileName << "'" << endl;
153 return 0; 153 return 0;
154 } 154 }
155 return createTicket( this ); 155 return createTicket( this );
156} 156}
157 157
158 158
159bool ResourceFile::doOpen() 159bool ResourceFile::doOpen()
160{ 160{
161 QFile file( mFileName ); 161 QFile file( mFileName );
162 162
163 if ( !file.exists() ) { 163 if ( !file.exists() ) {
164 // try to create the file 164 // try to create the file
165 bool ok = file.open( IO_WriteOnly ); 165 bool ok = file.open( IO_WriteOnly );
166 if ( ok ) 166 if ( ok )
167 file.close(); 167 file.close();
168 168
169 return ok; 169 return ok;
170 } else { 170 } else {
171 if ( !file.open( IO_ReadWrite ) ) 171 if ( !file.open( IO_ReadWrite ) )
172 return false; 172 return false;
173 173
174 if ( file.size() == 0 ) { 174 if ( file.size() == 0 ) {
175 file.close(); 175 file.close();
176 return true; 176 return true;
177 } 177 }
178 178
179 bool ok = mFormat->checkFormat( &file ); 179 bool ok = mFormat->checkFormat( &file );
180 file.close(); 180 file.close();
181 181
182 return ok; 182 return ok;
183 } 183 }
184} 184}
185 185
186void ResourceFile::doClose() 186void ResourceFile::doClose()
187{ 187{
188} 188}
189 189
190bool ResourceFile::load() 190bool ResourceFile::load()
191{ 191{
192 192
193 193
194 QFile file( mFileName ); 194 QFile file( mFileName );
195 if ( !file.open( IO_ReadOnly ) ) { 195 if ( !file.open( IO_ReadOnly ) ) {
196 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 196 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
197 return false; 197 return false;
198 } 198 }
199 199
200// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 200// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
201 201
202 return mFormat->loadAll( addressBook(), this, &file ); 202 return mFormat->loadAll( addressBook(), this, &file );
203} 203}
204 204
205bool ResourceFile::save( Ticket *ticket ) 205bool ResourceFile::save( Ticket *ticket )
206{ 206{
207// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 207// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
208 208
209 209
210 // create backup file 210 // create backup file
211 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 211 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
212 212
213/*US we use a simpler method to create a backupfile 213/*US we use a simpler method to create a backupfile
214 214
215 (void) KSaveFile::backupFile( mFileName, QString::null 215 (void) KSaveFile::backupFile( mFileName, QString::null
216 ,extension ); 216 ,extension );
217 217
218 KSaveFile saveFile( mFileName ); 218 KSaveFile saveFile( mFileName );
219 bool ok = false; 219 bool ok = false;
220 if ( saveFile.status() == 0 && saveFile.file() ) 220 if ( saveFile.status() == 0 && saveFile.file() )
221 { 221 {
222 mFormat->saveAll( addressBook(), this, saveFile.file() ); 222 mFormat->saveAll( addressBook(), this, saveFile.file() );
223 ok = saveFile.close(); 223 ok = saveFile.close();
224 } 224 }
225*/ 225*/
226 226
227//US ToDo: write backupfile 227//US ToDo: write backupfile
228 mDirWatch.stopScan();
229
228 QFile info; 230 QFile info;
229 info.setName( mFileName ); 231 info.setName( mFileName );
230 bool ok = info.open( IO_WriteOnly ); 232 bool ok = info.open( IO_WriteOnly );
231 if ( ok ) { 233 if ( ok ) {
232 mFormat->saveAll( addressBook(), this, &info ); 234 mFormat->saveAll( addressBook(), this, &info );
233 235
234 info.close(); 236 info.close();
235 ok = true; 237 ok = true;
236 } 238 }
237 else { 239 else {
238 240
239 } 241 }
240 242
241 if ( !ok ) 243 if ( !ok )
242 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 244 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
243 245
246 mDirWatch.startScan();
247
244 delete ticket; 248 delete ticket;
245 unlock( mFileName ); 249 unlock( mFileName );
246 250
247 return ok; 251 return ok;
248
249 qDebug("ResourceFile::save has to be changed");
250 return true;
251} 252}
252 253
253bool ResourceFile::lock( const QString &fileName ) 254bool ResourceFile::lock( const QString &fileName )
254{ 255{
255 256
256 257
257 QString fn = fileName; 258 QString fn = fileName;
258 259
259//US change the implementation how the lockfilename is getting created 260//US change the implementation how the lockfilename is getting created
260//US fn.replace( QRegExp("/"), "_" ); 261//US fn.replace( QRegExp("/"), "_" );
261//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 262//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
262 263
263 KURL url(fn); 264 KURL url(fn);
264 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 265 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
265 266
266 267
267 268
268 if (QFile::exists( lockName )) return false; 269 if (QFile::exists( lockName )) return false;
269 270
270 QString lockUniqueName; 271 QString lockUniqueName;
271 lockUniqueName = fn + KApplication::randomString( 8 ); 272 lockUniqueName = fn + KApplication::randomString( 8 );
272 273
273 url = lockUniqueName; 274 url = lockUniqueName;
274//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 275//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
275 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 276 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
276 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 277 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
277 278
278 // Create unique file 279 // Create unique file
279 QFile file( mLockUniqueName ); 280 QFile file( mLockUniqueName );
280 file.open( IO_WriteOnly ); 281 file.open( IO_WriteOnly );
281 file.close(); 282 file.close();
282 283
283 // Create lock file 284 // Create lock file
284 int result = 0; 285 int result = 0;
285#ifndef _WIN32_ 286#ifndef _WIN32_
286 result = ::link( QFile::encodeName( mLockUniqueName ), 287 result = ::link( QFile::encodeName( mLockUniqueName ),
287 QFile::encodeName( lockName ) ); 288 QFile::encodeName( lockName ) );
288#endif 289#endif
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 ResourceFile::unlock( const QString &fileName ) 300void ResourceFile::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 ResourceFile::setFileName( const QString &fileName ) 315void ResourceFile::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 322
322 mDirWatch.addFile( mFileName ); 323 mDirWatch.addFile( mFileName );
323 mDirWatch.startScan(); 324 mDirWatch.startScan();
324 325
325//US simulate KDirWatch event 326//US simulate KDirWatch event
326//US fileChanged(); 327//US fileChanged();
327} 328}
328 329
329QString ResourceFile::fileName() const 330QString ResourceFile::fileName() const
330{ 331{
331 return mFileName; 332 return mFileName;
332} 333}
333 334
334void ResourceFile::setFormat( const QString &format ) 335void ResourceFile::setFormat( const QString &format )
335{ 336{
336 mFormatName = format; 337 mFormatName = format;
337 delete mFormat; 338 delete mFormat;
338 339
339 FormatFactory *factory = FormatFactory::self(); 340 FormatFactory *factory = FormatFactory::self();
340 mFormat = factory->format( mFormatName ); 341 mFormat = factory->format( mFormatName );
341/*US 342/*US
342//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); 343//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1());
343 if (mFormatName == "vcard") { 344 if (mFormatName == "vcard") {
344 mFormat = new VCardFormatPlugin2(); 345 mFormat = new VCardFormatPlugin2();
345// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 346// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
346 } 347 }
347 else if (mFormatName == "binary") { 348 else if (mFormatName == "binary") {
348 mFormat = new BinaryFormat(); 349 mFormat = new BinaryFormat();
349// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 350// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
350 } 351 }
351 else 352 else
352 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 353 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
353*/ 354*/
354 355
355} 356}
356 357
357QString ResourceFile::format() const 358QString ResourceFile::format() const
358{ 359{
359 return mFormatName; 360 return mFormatName;
360} 361}
361 362
362void ResourceFile::fileChanged() 363void ResourceFile::fileChanged()
363{ 364{
364 // There is a small theoretical chance that KDirWatch calls us before 365 // There is a small theoretical chance that KDirWatch calls us before
365 // we are fully constructed 366 // we are fully constructed
366 if (!addressBook()) 367 if (!addressBook())
367 return; 368 return;
368 369
369 370
370 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 371 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
371 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 372 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
372 load(); 373 load();
373 addressBook()->emitAddressBookChanged(); 374 addressBook()->emitAddressBookChanged();
374 } 375 }
375} 376}
376 377
377void ResourceFile::removeAddressee( const Addressee &addr ) 378void ResourceFile::removeAddressee( const Addressee &addr )
378{ 379{
379 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); 380 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/logos/" ) + addr.uid() ) );
381 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); 382 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
382} 383}
383 384
384void ResourceFile::cleanUp() 385void ResourceFile::cleanUp()
385{ 386{
386 unlock( mFileName ); 387 unlock( mFileName );
387} 388}
388 389
389//US #include "resourcefile.moc" 390//US #include "resourcefile.moc"
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp
index 4b811e9..22237dc 100644
--- a/kabc/plugins/opie/resourceopie.cpp
+++ b/kabc/plugins/opie/resourceopie.cpp
@@ -1,348 +1,350 @@
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#include <unistd.h> 30#include <unistd.h>
31 31
32#include <qdir.h> 32#include <qdir.h>
33#include <qfile.h> 33#include <qfile.h>
34#include <qfileinfo.h> 34#include <qfileinfo.h>
35#include <qregexp.h> 35#include <qregexp.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//US #include "formatfactory.h" 45//US #include "formatfactory.h"
46//US #include <qpe/qpeapplication.h> 46//US #include <qpe/qpeapplication.h>
47 47
48#include <opie/ocontactaccess.h> 48#include <opie/ocontactaccess.h>
49#include <opie/ocontactaccessbackend_xml.h> 49#include <opie/ocontactaccessbackend_xml.h>
50 50
51#include "resourceopieconfig.h" 51#include "resourceopieconfig.h"
52#include "stdaddressbook.h" 52#include "stdaddressbook.h"
53#include "syncprefwidget.h" 53#include "syncprefwidget.h"
54 54
55#include "opieconverter.h" 55#include "opieconverter.h"
56 56
57#include "resourceopie.h" 57#include "resourceopie.h"
58 58
59using namespace KABC; 59using namespace KABC;
60extern "C" 60extern "C"
61{ 61{
62 void *init_microkabc_opie() 62 void *init_microkabc_opie()
63 { 63 {
64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, SyncPrefWidgetContainer>(); 64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, SyncPrefWidgetContainer>();
65 } 65 }
66} 66}
67 67
68ResourceOpie::ResourceOpie( const KConfig *config, bool syncable ) 68ResourceOpie::ResourceOpie( const KConfig *config, bool syncable )
69 : Resource( config, syncable ), mAccess(0), mConverter (0) 69 : Resource( config, syncable ), mAccess(0), mConverter (0)
70{ 70{
71 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; 71 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
72 72
73 KConfig *cfg = (KConfig *)config; 73 KConfig *cfg = (KConfig *)config;
74 if ( cfg ) { 74 if ( cfg ) {
75 fileName = cfg->readEntry( "FileName", fileName ); 75 fileName = cfg->readEntry( "FileName", fileName );
76 76
77 } 77 }
78 78
79 init( fileName ); 79 init( fileName );
80} 80}
81 81
82ResourceOpie::ResourceOpie( const QString &fileName, bool syncable ) 82ResourceOpie::ResourceOpie( const QString &fileName, bool syncable )
83 : Resource( 0, syncable ) 83 : Resource( 0, syncable )
84{ 84{
85 init( fileName ); 85 init( fileName );
86} 86}
87 87
88void ResourceOpie::init( const QString &fileName ) 88void ResourceOpie::init( const QString &fileName )
89{ 89{
90 qDebug("ResourceOpie::init()"); 90 qDebug("ResourceOpie::init()");
91 91
92 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 92 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
93 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 93 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
94 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 94 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
95 95
96 setFileName( fileName ); 96 setFileName( fileName );
97} 97}
98 98
99ResourceOpie::~ResourceOpie() 99ResourceOpie::~ResourceOpie()
100{ 100{
101 if (mConverter != 0) 101 if (mConverter != 0)
102 delete mConverter; 102 delete mConverter;
103 103
104 if(mAccess != 0) 104 if(mAccess != 0)
105 delete mAccess; 105 delete mAccess;
106} 106}
107 107
108void ResourceOpie::writeConfig( KConfig *config ) 108void ResourceOpie::writeConfig( KConfig *config )
109{ 109{
110 Resource::writeConfig( config ); 110 Resource::writeConfig( config );
111 111
112 config->writeEntry( "FileName", fileName() ); 112 config->writeEntry( "FileName", fileName() );
113} 113}
114 114
115Ticket *ResourceOpie::requestSaveTicket() 115Ticket *ResourceOpie::requestSaveTicket()
116{ 116{
117 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; 117 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl;
118 118
119 qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1()); 119 qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1());
120 120
121 if ( !addressBook() ) return 0; 121 if ( !addressBook() ) return 0;
122 122
123 if ( !lock( fileName() ) ) { 123 if ( !lock( fileName() ) ) {
124 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" 124 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '"
125 << fileName() << "'" << endl; 125 << fileName() << "'" << endl;
126 return 0; 126 return 0;
127 } 127 }
128 return createTicket( this ); 128 return createTicket( this );
129} 129}
130 130
131 131
132bool ResourceOpie::doOpen() 132bool ResourceOpie::doOpen()
133{ 133{
134 qDebug("ResourceOpie::doOpen: %s", fileName().latin1()); 134 qDebug("ResourceOpie::doOpen: %s", fileName().latin1());
135 135
136 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() ); 136 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() );
137 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false); 137 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false);
138 138
139 if ( !mAccess ) { 139 if ( !mAccess ) {
140 qDebug("Unable to load file() %s", fileName().latin1()); 140 qDebug("Unable to load file() %s", fileName().latin1());
141 return false; 141 return false;
142 } 142 }
143 143
144 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available 144 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available
145 145
146 146
147 if (mConverter == 0) 147 if (mConverter == 0)
148 { 148 {
149 mConverter = new OpieConverter(); 149 mConverter = new OpieConverter();
150 bool res = mConverter->init(); 150 bool res = mConverter->init();
151 if ( !res ) 151 if ( !res )
152 { 152 {
153 qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); 153 qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
154 delete mAccess; 154 delete mAccess;
155 mAccess = 0; 155 mAccess = 0;
156 return false; 156 return false;
157 } 157 }
158 } 158 }
159 159
160 160
161 161
162 return true; 162 return true;
163} 163}
164 164
165void ResourceOpie::doClose() 165void ResourceOpie::doClose()
166{ 166{
167 qDebug("ResourceOpie::doClose: %s", fileName().latin1()); 167 qDebug("ResourceOpie::doClose: %s", fileName().latin1());
168 168
169 if(mAccess) 169 if(mAccess)
170 { 170 {
171 delete mAccess; 171 delete mAccess;
172 mAccess = 0; 172 mAccess = 0;
173 } 173 }
174 // it seems so, that deletion of access deletes backend as well 174 // it seems so, that deletion of access deletes backend as well
175 //delete backend; 175 //delete backend;
176 176
177 return; 177 return;
178 178
179} 179}
180 180
181 181
182bool ResourceOpie::load() 182bool ResourceOpie::load()
183{ 183{
184 qDebug("ResourceOpie::load: %s", fileName().latin1()); 184 qDebug("ResourceOpie::load: %s", fileName().latin1());
185 185
186 bool res = false; 186 bool res = false;
187 187
188 OContactAccess::List::Iterator it; 188 OContactAccess::List::Iterator it;
189 OContactAccess::List allList = mAccess->allRecords(); 189 OContactAccess::List allList = mAccess->allRecords();
190 for ( it = allList.begin(); it != allList.end(); ++it ) 190 for ( it = allList.begin(); it != allList.end(); ++it )
191 { 191 {
192 const OContact c = (*it); 192 const OContact c = (*it);
193 193
194 KABC::Addressee addressee; 194 KABC::Addressee addressee;
195 195
196 res = mConverter->opieToAddressee( c, addressee ); 196 res = mConverter->opieToAddressee( c, addressee );
197 197
198 if ( !addressee.isEmpty() && res ) 198 if ( !addressee.isEmpty() && res )
199 { 199 {
200 addressee.setResource( this ); 200 addressee.setResource( this );
201 addressBook()->insertAddressee( addressee ); 201 addressBook()->insertAddressee( addressee );
202 } 202 }
203 203
204 } 204 }
205 205
206 206
207 return true; 207 return true;
208} 208}
209 209
210bool ResourceOpie::save( Ticket *ticket ) 210bool ResourceOpie::save( Ticket *ticket )
211{ 211{
212 qDebug("ResourceOpie::save: %s", fileName().latin1()); 212 qDebug("ResourceOpie::save: %s", fileName().latin1());
213 213
214 214 mDirWatch.stopScan();
215 215
216 KABC::AddressBook::Iterator it; 216 KABC::AddressBook::Iterator it;
217 bool res; 217 bool res;
218 218
219 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 219 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
220 OContact c; 220 OContact c;
221 KABC::Addressee addressee = (*it); 221 KABC::Addressee addressee = (*it);
222 222
223 res = mConverter->addresseeToOpie( *it, c ); 223 res = mConverter->addresseeToOpie( *it, c );
224 if (res == true) 224 if (res == true)
225 { 225 {
226 res = mAccess->add(c); 226 res = mAccess->add(c);
227 if (res == false) 227 if (res == false)
228 qDebug("Unable to append Contact %s", c.fullName().latin1()); 228 qDebug("Unable to append Contact %s", c.fullName().latin1());
229 } 229 }
230 else 230 else
231 { 231 {
232 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); 232 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
233 } 233 }
234 } 234 }
235 235
236 mAccess->save(); 236 mAccess->save();
237 237
238 mDirWatch.startScan();
239
238 delete ticket; 240 delete ticket;
239 unlock( fileName() ); 241 unlock( fileName() );
240 242
241 return true; 243 return true;
242 244
243} 245}
244 246
245bool ResourceOpie::lock( const QString &lockfileName ) 247bool ResourceOpie::lock( const QString &lockfileName )
246{ 248{
247 qDebug("ResourceOpie::lock: %s", fileName().latin1()); 249 qDebug("ResourceOpie::lock: %s", fileName().latin1());
248 250
249 kdDebug(5700) << "ResourceOpie::lock()" << endl; 251 kdDebug(5700) << "ResourceOpie::lock()" << endl;
250 252
251 QString fn = lockfileName; 253 QString fn = lockfileName;
252 254
253 KURL url(fn); 255 KURL url(fn);
254 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 256 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
255 257
256 kdDebug(5700) << "-- lock name: " << lockName << endl; 258 kdDebug(5700) << "-- lock name: " << lockName << endl;
257 259
258 if (QFile::exists( lockName )) 260 if (QFile::exists( lockName ))
259 { 261 {
260 qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); 262 qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1());
261 return false; 263 return false;
262 } 264 }
263 265
264 QString lockUniqueName; 266 QString lockUniqueName;
265 lockUniqueName = fn + KApplication::randomString( 8 ); 267 lockUniqueName = fn + KApplication::randomString( 8 );
266 268
267 url = lockUniqueName; 269 url = lockUniqueName;
268//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 270//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
269 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 271 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
270 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 272 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
271 273
272 // Create unique file 274 // Create unique file
273 QFile file( mLockUniqueName ); 275 QFile file( mLockUniqueName );
274 file.open( IO_WriteOnly ); 276 file.open( IO_WriteOnly );
275 file.close(); 277 file.close();
276 278
277 // Create lock file 279 // Create lock file
278 int result = 0; 280 int result = 0;
279#ifndef _WIN32_ 281#ifndef _WIN32_
280 result = ::link( QFile::encodeName( mLockUniqueName ), 282 result = ::link( QFile::encodeName( mLockUniqueName ),
281 QFile::encodeName( lockName ) ); 283 QFile::encodeName( lockName ) );
282#endif 284#endif
283 if ( result == 0 ) { 285 if ( result == 0 ) {
284 addressBook()->emitAddressBookLocked(); 286 addressBook()->emitAddressBookLocked();
285 return true; 287 return true;
286 } 288 }
287 289
288 // TODO: check stat 290 // TODO: check stat
289 291
290 return false; 292 return false;
291} 293}
292 294
293void ResourceOpie::unlock( const QString &fileName ) 295void ResourceOpie::unlock( const QString &fileName )
294{ 296{
295 qDebug("ResourceOpie::unlock() %s", fileName.latin1()); 297 qDebug("ResourceOpie::unlock() %s", fileName.latin1());
296 298
297 QString fn = fileName; 299 QString fn = fileName;
298 KURL url(fn); 300 KURL url(fn);
299 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 301 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
300 302
301 QFile::remove( lockName ); 303 QFile::remove( lockName );
302 QFile::remove( mLockUniqueName ); 304 QFile::remove( mLockUniqueName );
303 addressBook()->emitAddressBookUnlocked(); 305 addressBook()->emitAddressBookUnlocked();
304} 306}
305 307
306void ResourceOpie::setFileName( const QString &newFileName ) 308void ResourceOpie::setFileName( const QString &newFileName )
307{ 309{
308 mDirWatch.stopScan(); 310 mDirWatch.stopScan();
309 mDirWatch.removeFile( fileName() ); 311 mDirWatch.removeFile( fileName() );
310 312
311 Resource::setFileName( newFileName ); 313 Resource::setFileName( newFileName );
312 314
313 mDirWatch.addFile( fileName() ); 315 mDirWatch.addFile( fileName() );
314 mDirWatch.startScan(); 316 mDirWatch.startScan();
315 317
316} 318}
317 319
318 320
319void ResourceOpie::fileChanged() 321void ResourceOpie::fileChanged()
320{ 322{
321 // There is a small theoretical chance that KDirWatch calls us before 323 // There is a small theoretical chance that KDirWatch calls us before
322 // we are fully constructed 324 // we are fully constructed
323 if (!addressBook()) 325 if (!addressBook())
324 return; 326 return;
325 327
326 QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); 328 QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
327 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 329 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
328 load(); 330 load();
329 addressBook()->emitAddressBookChanged(); 331 addressBook()->emitAddressBookChanged();
330 } 332 }
331 333
332 load(); 334 load();
333 addressBook()->emitAddressBookChanged(); 335 addressBook()->emitAddressBookChanged();
334} 336}
335 337
336void ResourceOpie::removeAddressee( const Addressee &addr ) 338void ResourceOpie::removeAddressee( const Addressee &addr )
337{ 339{
338} 340}
339 341
340void ResourceOpie::cleanUp() 342void ResourceOpie::cleanUp()
341{ 343{
342// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1()); 344// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1());
343 345
344 unlock( fileName() ); 346 unlock( fileName() );
345} 347}
346 348
347 349
348//US #include "resourceopie.moc" 350//US #include "resourceopie.moc"
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp
index 93a4d33..48a9f22 100644
--- a/kabc/plugins/qtopia/resourceqtopia.cpp
+++ b/kabc/plugins/qtopia/resourceqtopia.cpp
@@ -1,319 +1,322 @@
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#include "syncprefwidget.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, SyncPrefWidgetContainer>(); 61 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidgetContainer>();
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 init( fileName ); 70 init( fileName );
71} 71}
72 72
73ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable ) 73ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable )
74 : Resource( 0, syncable ) 74 : Resource( 0, syncable )
75{ 75{
76 init( fileName ); 76 init( fileName );
77} 77}
78 78
79void ResourceQtopia::init( const QString &fileName ) 79void ResourceQtopia::init( const QString &fileName )
80{ 80{
81 81
82 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 82 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
83 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 83 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
84 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 84 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
85 85
86 setFileName( fileName ); 86 setFileName( fileName );
87} 87}
88 88
89ResourceQtopia::~ResourceQtopia() 89ResourceQtopia::~ResourceQtopia()
90{ 90{
91 if (mConverter != 0) 91 if (mConverter != 0)
92 delete mConverter; 92 delete mConverter;
93 93
94 if(mAccess != 0) 94 if(mAccess != 0)
95 delete mAccess; 95 delete mAccess;
96} 96}
97 97
98void ResourceQtopia::writeConfig( KConfig *config ) 98void ResourceQtopia::writeConfig( KConfig *config )
99{ 99{
100 Resource::writeConfig( config ); 100 Resource::writeConfig( config );
101} 101}
102 102
103Ticket *ResourceQtopia::requestSaveTicket() 103Ticket *ResourceQtopia::requestSaveTicket()
104{ 104{
105 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; 105 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl;
106 106
107 qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1()); 107 qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1());
108 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 131
132 if (mConverter == 0) 132 if (mConverter == 0)
133 { 133 {
134 mConverter = new QtopiaConverter(); 134 mConverter = new QtopiaConverter();
135 bool 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 qDebug("ResourceQtopia::save: %s", fileName().latin1()); 192 qDebug("ResourceQtopia::save: %s", fileName().latin1());
193 193
194 mDirWatch.stopScan();
194 195
195 KABC::AddressBook::Iterator it; 196 KABC::AddressBook::Iterator it;
196 bool res; 197 bool res;
197 198
198 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 199 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
199 PimContact c; 200 PimContact c;
200 KABC::Addressee addressee = (*it); 201 KABC::Addressee addressee = (*it);
201 202
202 res = mConverter->addresseeToQtopia( *it, c ); 203 res = mConverter->addresseeToQtopia( *it, c );
203 if (res == true) 204 if (res == true)
204 { 205 {
205 mAccess->addContact(c); 206 mAccess->addContact(c);
206// if (res == false) 207// if (res == false)
207// qDebug("Unable to append Contact %s", c.fullName().latin1()); 208// qDebug("Unable to append Contact %s", c.fullName().latin1());
208 } 209 }
209 else 210 else
210 { 211 {
211 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); 212 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
212 } 213 }
213 } 214 }
214 215
215// mAccess->addressBookUpdated(); 216// mAccess->addressBookUpdated();
216 217
218 mDirWatch.startScan();
219
217 delete ticket; 220 delete ticket;
218 unlock( fileName() ); 221 unlock( fileName() );
219 222
220 return true; 223 return true;
221} 224}
222 225
223bool ResourceQtopia::lock( const QString &lockfileName ) 226bool ResourceQtopia::lock( const QString &lockfileName )
224{ 227{
225 qDebug("ResourceQtopia::lock: %s", fileName().latin1()); 228 qDebug("ResourceQtopia::lock: %s", fileName().latin1());
226 229
227 kdDebug(5700) << "ResourceQtopia::lock()" << endl; 230 kdDebug(5700) << "ResourceQtopia::lock()" << endl;
228 231
229 QString fn = lockfileName; 232 QString fn = lockfileName;
230 233
231 KURL url(fn); 234 KURL url(fn);
232 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 235 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
233 236
234 kdDebug(5700) << "-- lock name: " << lockName << endl; 237 kdDebug(5700) << "-- lock name: " << lockName << endl;
235 238
236 if (QFile::exists( lockName )) 239 if (QFile::exists( lockName ))
237 { 240 {
238 qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); 241 qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1());
239 return false; 242 return false;
240 } 243 }
241 244
242 QString lockUniqueName; 245 QString lockUniqueName;
243 lockUniqueName = fn + KApplication::randomString( 8 ); 246 lockUniqueName = fn + KApplication::randomString( 8 );
244 247
245 url = lockUniqueName; 248 url = lockUniqueName;
246//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 249//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
247 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 250 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
248 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 251 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
249 252
250 // Create unique file 253 // Create unique file
251 QFile file( mLockUniqueName ); 254 QFile file( mLockUniqueName );
252 file.open( IO_WriteOnly ); 255 file.open( IO_WriteOnly );
253 file.close(); 256 file.close();
254 257
255 // Create lock file 258 // Create lock file
256 int result = 0; 259 int result = 0;
257#ifndef _WIN32_ 260#ifndef _WIN32_
258 result = ::link( QFile::encodeName( mLockUniqueName ), 261 result = ::link( QFile::encodeName( mLockUniqueName ),
259 QFile::encodeName( lockName ) ); 262 QFile::encodeName( lockName ) );
260#endif 263#endif
261 if ( result == 0 ) { 264 if ( result == 0 ) {
262 addressBook()->emitAddressBookLocked(); 265 addressBook()->emitAddressBookLocked();
263 return true; 266 return true;
264 } 267 }
265 268
266 // TODO: check stat 269 // TODO: check stat
267 270
268 return false; 271 return false;
269} 272}
270 273
271void ResourceQtopia::unlock( const QString &fileName ) 274void ResourceQtopia::unlock( const QString &fileName )
272{ 275{
273 qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); 276 qDebug("ResourceQtopia::unlock() %s", fileName.latin1());
274 277
275 QString fn = fileName; 278 QString fn = fileName;
276 KURL url(fn); 279 KURL url(fn);
277 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 280 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
278 281
279 QFile::remove( lockName ); 282 QFile::remove( lockName );
280 QFile::remove( mLockUniqueName ); 283 QFile::remove( mLockUniqueName );
281 addressBook()->emitAddressBookUnlocked(); 284 addressBook()->emitAddressBookUnlocked();
282} 285}
283 286
284void ResourceQtopia::setFileName( const QString &newFileName ) 287void ResourceQtopia::setFileName( const QString &newFileName )
285{ 288{
286 mDirWatch.stopScan(); 289 mDirWatch.stopScan();
287 mDirWatch.removeFile( fileName() ); 290 mDirWatch.removeFile( fileName() );
288 291
289 Resource::setFileName( newFileName ); 292 Resource::setFileName( newFileName );
290 293
291 mDirWatch.addFile( fileName() ); 294 mDirWatch.addFile( fileName() );
292 mDirWatch.startScan(); 295 mDirWatch.startScan();
293} 296}
294 297
295 298
296void ResourceQtopia::fileChanged() 299void ResourceQtopia::fileChanged()
297{ 300{
298 // There is a small theoretical chance that KDirWatch calls us before 301 // There is a small theoretical chance that KDirWatch calls us before
299 // we are fully constructed 302 // we are fully constructed
300 if (!addressBook()) 303 if (!addressBook())
301 return; 304 return;
302 305
303 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); 306 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
304 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 307 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
305 load(); 308 load();
306 addressBook()->emitAddressBookChanged(); 309 addressBook()->emitAddressBookChanged();
307 } 310 }
308} 311}
309 312
310void ResourceQtopia::removeAddressee( const Addressee &addr ) 313void ResourceQtopia::removeAddressee( const Addressee &addr )
311{ 314{
312} 315}
313 316
314void ResourceQtopia::cleanUp() 317void ResourceQtopia::cleanUp()
315{ 318{
316 unlock( fileName() ); 319 unlock( fileName() );
317} 320}
318 321
319//US #include "resourceqtopia.moc" 322//US #include "resourceqtopia.moc"
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
index b63df0c..8a795e9 100644
--- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
+++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
@@ -1,337 +1,339 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2004 Ulf Schenk 3 Copyright (c) 2004 Ulf Schenk
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#include <unistd.h> 30#include <unistd.h>
31 31
32#include <qdir.h> 32#include <qdir.h>
33#include <qfile.h> 33#include <qfile.h>
34#include <qfileinfo.h> 34#include <qfileinfo.h>
35#include <qregexp.h> 35#include <qregexp.h>
36//US #include <qtimer.h> 36//US #include <qtimer.h>
37 37
38#include <kapplication.h> 38#include <kapplication.h>
39#include <kconfig.h> 39#include <kconfig.h>
40#include <kdebug.h> 40#include <kdebug.h>
41#include <klocale.h> 41#include <klocale.h>
42//US #include <ksavefile.h> 42//US #include <ksavefile.h>
43#include <kstandarddirs.h> 43#include <kstandarddirs.h>
44#include <kmessagebox.h> 44#include <kmessagebox.h>
45 45
46#include <sl/slzdb.h> 46#include <sl/slzdb.h>
47 47
48 48
49#include "resourcesharpdtmconfig.h" 49#include "resourcesharpdtmconfig.h"
50#include "resourcesharpdtm.h" 50#include "resourcesharpdtm.h"
51 51
52#include "syncprefwidget.h" 52#include "syncprefwidget.h"
53 53
54#include "stdaddressbook.h" 54#include "stdaddressbook.h"
55 55
56#include "sharpdtmconverter.h" 56#include "sharpdtmconverter.h"
57 57
58using namespace KABC; 58using namespace KABC;
59extern "C" 59extern "C"
60{ 60{
61 void *init_microkabc_sharpdtm() 61 void *init_microkabc_sharpdtm()
62 { 62 {
63 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig,SyncPrefWidgetContainer>(); 63 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig,SyncPrefWidgetContainer>();
64 } 64 }
65} 65}
66 66
67ResourceSharpDTM::ResourceSharpDTM( const KConfig *config, bool syncable ) 67ResourceSharpDTM::ResourceSharpDTM( const KConfig *config, bool syncable )
68 : Resource( config, syncable ), mConverter (0) 68 : Resource( config, syncable ), mConverter (0)
69{ 69{
70 // we can not choose the filename. Therefore use the default to display 70 // we can not choose the filename. Therefore use the default to display
71 71
72 QString fileName = SlZDataBase::addressbookFileName(); 72 QString fileName = SlZDataBase::addressbookFileName();
73 init( fileName ); 73 init( fileName );
74} 74}
75 75
76ResourceSharpDTM::ResourceSharpDTM( const QString &fileName, bool syncable ) 76ResourceSharpDTM::ResourceSharpDTM( const QString &fileName, bool syncable )
77 : Resource( 0, syncable ) 77 : Resource( 0, syncable )
78{ 78{
79 init( fileName ); 79 init( fileName );
80} 80}
81 81
82void ResourceSharpDTM::init( const QString &fileName ) 82void ResourceSharpDTM::init( const QString &fileName )
83{ 83{
84 84
85 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 85 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
86 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 86 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
87 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 87 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
88 88
89 setFileName( fileName ); 89 setFileName( fileName );
90} 90}
91 91
92ResourceSharpDTM::~ResourceSharpDTM() 92ResourceSharpDTM::~ResourceSharpDTM()
93{ 93{
94 if (mConverter != 0) 94 if (mConverter != 0)
95 delete mConverter; 95 delete mConverter;
96 96
97 if(mAccess != 0) 97 if(mAccess != 0)
98 delete mAccess; 98 delete mAccess;
99} 99}
100 100
101void ResourceSharpDTM::writeConfig( KConfig *config ) 101void ResourceSharpDTM::writeConfig( KConfig *config )
102{ 102{
103 Resource::writeConfig( config ); 103 Resource::writeConfig( config );
104} 104}
105 105
106Ticket *ResourceSharpDTM::requestSaveTicket() 106Ticket *ResourceSharpDTM::requestSaveTicket()
107{ 107{
108 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket()" << endl; 108 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket()" << endl;
109 109
110 qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1()); 110 qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1());
111 111
112 if ( !addressBook() ) return 0; 112 if ( !addressBook() ) return 0;
113 113
114 if ( !lock( fileName() ) ) { 114 if ( !lock( fileName() ) ) {
115 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket(): Unable to lock file '" 115 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket(): Unable to lock file '"
116 << fileName() << "'" << endl; 116 << fileName() << "'" << endl;
117 return 0; 117 return 0;
118 } 118 }
119 return createTicket( this ); 119 return createTicket( this );
120} 120}
121 121
122 122
123bool ResourceSharpDTM::doOpen() 123bool ResourceSharpDTM::doOpen()
124{ 124{
125 qDebug("ResourceSharpDTM::doOpen: %s", fileName().latin1()); 125 qDebug("ResourceSharpDTM::doOpen: %s", fileName().latin1());
126 126
127 // the last parameter in the SlZDataBase constructor means "readonly" 127 // the last parameter in the SlZDataBase constructor means "readonly"
128 mAccess = new SlZDataBase(fileName(), 128 mAccess = new SlZDataBase(fileName(),
129 SlZDataBase::addressbookItems(), 129 SlZDataBase::addressbookItems(),
130 NULL, true); 130 NULL, true);
131 131
132 if ( !mAccess ) { 132 if ( !mAccess ) {
133 qDebug("Unable to load file() %s", fileName().latin1()); 133 qDebug("Unable to load file() %s", fileName().latin1());
134 return false; 134 return false;
135 } 135 }
136 136
137 if (mConverter == 0) 137 if (mConverter == 0)
138 { 138 {
139 mConverter = new SharpDTMConverter(); 139 mConverter = new SharpDTMConverter();
140 bool res = mConverter->init(); 140 bool res = mConverter->init();
141 if ( !res ) 141 if ( !res )
142 { 142 {
143 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file"); 143 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file");
144 144
145 qDebug(msg); 145 qDebug(msg);
146 delete mAccess; 146 delete mAccess;
147 mAccess = 0; 147 mAccess = 0;
148 return false; 148 return false;
149 } 149 }
150 } 150 }
151 151
152 return true; 152 return true;
153} 153}
154 154
155void ResourceSharpDTM::doClose() 155void ResourceSharpDTM::doClose()
156{ 156{
157 qDebug("ResourceSharpDTM::doClose: %s", fileName().latin1()); 157 qDebug("ResourceSharpDTM::doClose: %s", fileName().latin1());
158 158
159 if(mAccess) 159 if(mAccess)
160 { 160 {
161 delete mAccess; 161 delete mAccess;
162 mAccess = 0; 162 mAccess = 0;
163 } 163 }
164 // it seems so, that deletion of access deletes backend as well 164 // it seems so, that deletion of access deletes backend as well
165 //delete backend; 165 //delete backend;
166 166
167 return; 167 return;
168} 168}
169 169
170bool ResourceSharpDTM::load() 170bool ResourceSharpDTM::load()
171{ 171{
172 qDebug("ResourceSharpDTM::load: %s", fileName().latin1()); 172 qDebug("ResourceSharpDTM::load: %s", fileName().latin1());
173 173
174 bool res = false; 174 bool res = false;
175 175
176 CardId id; 176 CardId id;
177 177
178 for (bool res=mAccess->first(); res == true; res=mAccess->next()) 178 for (bool res=mAccess->first(); res == true; res=mAccess->next())
179 { 179 {
180 id = mAccess->cardId(); 180 id = mAccess->cardId();
181 181
182 KABC::Addressee addressee; 182 KABC::Addressee addressee;
183 183
184 res = mConverter->sharpToAddressee( id, mAccess, addressee ); 184 res = mConverter->sharpToAddressee( id, mAccess, addressee );
185 185
186 if ( !addressee.isEmpty() && res ) 186 if ( !addressee.isEmpty() && res )
187 { 187 {
188 addressee.setResource( this ); 188 addressee.setResource( this );
189 addressBook()->insertAddressee( addressee ); 189 addressBook()->insertAddressee( addressee );
190 } 190 }
191 } 191 }
192 192
193 return true; 193 return true;
194} 194}
195 195
196bool ResourceSharpDTM::save( Ticket *ticket ) 196bool ResourceSharpDTM::save( Ticket *ticket )
197{ 197{
198 qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); 198 qDebug("ResourceSharpDTM::save: %s", fileName().latin1());
199 199
200 mDirWatch.stopScan();
200 201
201 KABC::AddressBook::Iterator it; 202 KABC::AddressBook::Iterator it;
202 bool res; 203 bool res;
203 204
204 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 205 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
205 CardId id = 0;// 0 means new card 206 CardId id = 0;// 0 means new card
206 KABC::Addressee addressee = (*it); 207 KABC::Addressee addressee = (*it);
207 208
208 res = mAccess->startEditCard(id); 209 res = mAccess->startEditCard(id);
209 if (res == true) 210 if (res == true)
210 { 211 {
211 res = mConverter->addresseeToSharp( *it, mAccess, id ); 212 res = mConverter->addresseeToSharp( *it, mAccess, id );
212 if (res == true) 213 if (res == true)
213 { 214 {
214 res = mAccess->finishEditCard(&id); 215 res = mAccess->finishEditCard(&id);
215 if (res == false) 216 if (res == false)
216 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); 217 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1());
217 } 218 }
218 else 219 else
219 { 220 {
220 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); 221 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1());
221 mAccess->cancelEditCard(); 222 mAccess->cancelEditCard();
222 } 223 }
223 } 224 }
224 else 225 else
225 { 226 {
226 qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1()); 227 qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1());
227 } 228 }
228 229
229 } 230 }
230 231
231//US mAccess->save(); 232//US mAccess->save();
232 233
234 mDirWatch.startScan();
233 delete ticket; 235 delete ticket;
234 unlock( fileName() ); 236 unlock( fileName() );
235 237
236 return true; 238 return true;
237} 239}
238 240
239bool ResourceSharpDTM::lock( const QString &lockfileName ) 241bool ResourceSharpDTM::lock( const QString &lockfileName )
240{ 242{
241 qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); 243 qDebug("ResourceSharpDTM::lock: %s", fileName().latin1());
242 244
243 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; 245 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl;
244 246
245 QString fn = lockfileName; 247 QString fn = lockfileName;
246 248
247 KURL url(fn); 249 KURL url(fn);
248 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 250 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
249 251
250 kdDebug(5700) << "-- lock name: " << lockName << endl; 252 kdDebug(5700) << "-- lock name: " << lockName << endl;
251 253
252 if (QFile::exists( lockName )) 254 if (QFile::exists( lockName ))
253 { 255 {
254 qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); 256 qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName());
255 return false; 257 return false;
256 } 258 }
257 259
258 260
259 QString lockUniqueName; 261 QString lockUniqueName;
260 lockUniqueName = fn + KApplication::randomString( 8 ); 262 lockUniqueName = fn + KApplication::randomString( 8 );
261 263
262 url = lockUniqueName; 264 url = lockUniqueName;
263//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 265//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
264 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 266 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
265 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 267 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
266 268
267 // Create unique file 269 // Create unique file
268 QFile file( mLockUniqueName ); 270 QFile file( mLockUniqueName );
269 file.open( IO_WriteOnly ); 271 file.open( IO_WriteOnly );
270 file.close(); 272 file.close();
271 273
272 // Create lock file 274 // Create lock file
273 int result = 0; 275 int result = 0;
274#ifndef _WIN32_ 276#ifndef _WIN32_
275 result = ::link( QFile::encodeName( mLockUniqueName ), 277 result = ::link( QFile::encodeName( mLockUniqueName ),
276 QFile::encodeName( lockName ) ); 278 QFile::encodeName( lockName ) );
277#endif 279#endif
278 if ( result == 0 ) { 280 if ( result == 0 ) {
279 addressBook()->emitAddressBookLocked(); 281 addressBook()->emitAddressBookLocked();
280 return true; 282 return true;
281 } 283 }
282 284
283 // TODO: check stat 285 // TODO: check stat
284 286
285 return false; 287 return false;
286} 288}
287 289
288void ResourceSharpDTM::unlock( const QString &fileName ) 290void ResourceSharpDTM::unlock( const QString &fileName )
289{ 291{
290 qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); 292 qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1());
291 293
292 QString fn = fileName; 294 QString fn = fileName;
293 KURL url(fn); 295 KURL url(fn);
294 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 296 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
295 297
296 QFile::remove( lockName ); 298 QFile::remove( lockName );
297 QFile::remove( mLockUniqueName ); 299 QFile::remove( mLockUniqueName );
298 addressBook()->emitAddressBookUnlocked(); 300 addressBook()->emitAddressBookUnlocked();
299} 301}
300 302
301void ResourceSharpDTM::setFileName( const QString &newFileName ) 303void ResourceSharpDTM::setFileName( const QString &newFileName )
302{ 304{
303 mDirWatch.stopScan(); 305 mDirWatch.stopScan();
304 mDirWatch.removeFile( fileName() ); 306 mDirWatch.removeFile( fileName() );
305 307
306 Resource::setFileName( newFileName ); 308 Resource::setFileName( newFileName );
307 309
308 mDirWatch.addFile( fileName() ); 310 mDirWatch.addFile( fileName() );
309 mDirWatch.startScan(); 311 mDirWatch.startScan();
310 312
311} 313}
312 314
313void ResourceSharpDTM::fileChanged() 315void ResourceSharpDTM::fileChanged()
314{ 316{
315 // There is a small theoretical chance that KDirWatch calls us before 317 // There is a small theoretical chance that KDirWatch calls us before
316 // we are fully constructed 318 // we are fully constructed
317 if (!addressBook()) 319 if (!addressBook())
318 return; 320 return;
319 321
320 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); 322 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
321 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 323 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
322 load(); 324 load();
323 addressBook()->emitAddressBookChanged(); 325 addressBook()->emitAddressBookChanged();
324 } 326 }
325} 327}
326 328
327void ResourceSharpDTM::removeAddressee( const Addressee &addr ) 329void ResourceSharpDTM::removeAddressee( const Addressee &addr )
328{ 330{
329} 331}
330 332
331void ResourceSharpDTM::cleanUp() 333void ResourceSharpDTM::cleanUp()
332{ 334{
333 unlock( fileName() ); 335 unlock( fileName() );
334} 336}
335 337
336 338
337 339