summaryrefslogtreecommitdiffabout
path: root/kabc
authorzautrix <zautrix>2004-09-11 23:25:03 (UTC)
committer zautrix <zautrix>2004-09-11 23:25:03 (UTC)
commit0a2900e71fd3d80393d67c81aab3aae19d2d680c (patch) (unidiff)
tree1f2926eca6bee600819932603eaaf28c7f267d6b /kabc
parenta040c9d56282dae396b02627fafb602e44323837 (diff)
downloadkdepimpi-0a2900e71fd3d80393d67c81aab3aae19d2d680c.zip
kdepimpi-0a2900e71fd3d80393d67c81aab3aae19d2d680c.tar.gz
kdepimpi-0a2900e71fd3d80393d67c81aab3aae19d2d680c.tar.bz2
Bugfixes
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/dir/resourcedir.cpp32
-rw-r--r--kabc/plugins/file/resourcefile.cpp16
-rw-r--r--kabc/plugins/sharpdtm/resourcesharpdtm.cpp15
3 files changed, 42 insertions, 21 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp
index fac06a1..7d96cb7 100644
--- a/kabc/plugins/dir/resourcedir.cpp
+++ b/kabc/plugins/dir/resourcedir.cpp
@@ -1,361 +1,363 @@
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
62//#define ALLOW_LOCKING
63
62using namespace KABC; 64using namespace KABC;
63 65
64extern "C" 66extern "C"
65#ifdef _WIN32_ 67#ifdef _WIN32_
66__declspec(dllexport) 68__declspec(dllexport)
67#else 69#else
68{ 70{
69#endif 71#endif
70 72
71//US void *init_kabc_dir() 73//US void *init_kabc_dir()
72 void *init_microkabc_dir() 74 void *init_microkabc_dir()
73 { 75 {
74 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>(); 76 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, SyncPrefWidgetContainer>();
75 } 77 }
76#ifndef _WIN32_ 78#ifndef _WIN32_
77} 79}
78#endif 80#endif
79 81
80ResourceDir::ResourceDir( const KConfig *config, bool syncable ) 82ResourceDir::ResourceDir( const KConfig *config, bool syncable )
81 : Resource( config, syncable ) 83 : Resource( config, syncable )
82{ 84{
83 QString path; 85 QString path;
84 86
85 KConfig *cfg = (KConfig *)config; 87 KConfig *cfg = (KConfig *)config;
86 if ( cfg ) { 88 if ( cfg ) {
87//US path = config->readEntry( "FilePath" ); 89//US path = config->readEntry( "FilePath" );
88 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() ); 90 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() );
89//US mFormatName = config->readEntry( "FileFormat" ); 91//US mFormatName = config->readEntry( "FileFormat" );
90 mFormatName = cfg->readEntry( "FileFormat", "vcard" ); 92 mFormatName = cfg->readEntry( "FileFormat", "vcard" );
91 } else { 93 } else {
92 path = StdAddressBook::directoryName(); 94 path = StdAddressBook::directoryName();
93 mFormatName = "vcard"; 95 mFormatName = "vcard";
94 } 96 }
95 97
96 98
97 FormatFactory *factory = FormatFactory::self(); 99 FormatFactory *factory = FormatFactory::self();
98 mFormat = factory->format( mFormatName ); 100 mFormat = factory->format( mFormatName );
99 101
100 if ( !mFormat ) { 102 if ( !mFormat ) {
101 mFormatName = "vcard"; 103 mFormatName = "vcard";
102 mFormat = factory->format( mFormatName ); 104 mFormat = factory->format( mFormatName );
103 } 105 }
104 106
105/*US 107/*US
106//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); 108//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1());
107 if (mFormatName == "vcard") 109 if (mFormatName == "vcard")
108 mFormat = new VCardFormatPlugin2(); 110 mFormat = new VCardFormatPlugin2();
109 else if (mFormatName == "binary") 111 else if (mFormatName == "binary")
110 mFormat = new BinaryFormat(); 112 mFormat = new BinaryFormat();
111 else 113 else
112 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); 114 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1());
113*/ 115*/
114 116
115 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); 117 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) );
116 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); 118 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) );
117 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); 119 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) );
118 120
119 setPath( path ); 121 setPath( path );
120} 122}
121 123
122ResourceDir::~ResourceDir() 124ResourceDir::~ResourceDir()
123{ 125{
124 delete mFormat; 126 delete mFormat;
125 mFormat = 0; 127 mFormat = 0;
126} 128}
127 129
128void ResourceDir::writeConfig( KConfig *config ) 130void ResourceDir::writeConfig( KConfig *config )
129{ 131{
130 config->setGroup( "Resource_" + identifier() ); 132 config->setGroup( "Resource_" + identifier() );
131 Resource::writeConfig( config ); 133 Resource::writeConfig( config );
132 134
133 config->writeEntry( "FilePath", mPath ); 135 config->writeEntry( "FilePath", mPath );
134 config->writeEntry( "FileFormat", mFormatName ); 136 config->writeEntry( "FileFormat", mFormatName );
135} 137}
136 138
137Ticket *ResourceDir::requestSaveTicket() 139Ticket *ResourceDir::requestSaveTicket()
138{ 140{
139 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl; 141
140 142
141 if ( !addressBook() ) return 0; 143 if ( !addressBook() ) return 0;
142 144#ifdef ALLOW_LOCKING
143 if ( !lock( mPath ) ) { 145 if ( !lock( mPath ) ) {
144 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '" 146
145 << mPath << "'" << endl;
146 return 0; 147 return 0;
147 } 148 }
149#endif
148 return createTicket( this ); 150 return createTicket( this );
151
149} 152}
150 153
151 154
152bool ResourceDir::doOpen() 155bool ResourceDir::doOpen()
153{ 156{
154 QDir dir( mPath ); 157 QDir dir( mPath );
155 if ( !dir.exists() ) { // no directory available 158 if ( !dir.exists() ) { // no directory available
156 return dir.mkdir( dir.path() ); 159 return dir.mkdir( dir.path() );
157 } else { 160 } else {
158 QString testName = dir.entryList( QDir::Files )[0]; 161 QString testName = dir.entryList( QDir::Files )[0];
159 if ( testName.isNull() || testName.isEmpty() ) // no file in directory 162 if ( testName.isNull() || testName.isEmpty() ) // no file in directory
160 return true; 163 return true;
161 164
162 QFile file( mPath + "/" + testName ); 165 QFile file( mPath + "/" + testName );
163 if ( file.open( IO_ReadOnly ) ) 166 if ( file.open( IO_ReadOnly ) )
164 return true; 167 return true;
165 168
166 if ( file.size() == 0 ) 169 if ( file.size() == 0 )
167 return true; 170 return true;
168 171
169 bool ok = mFormat->checkFormat( &file ); 172 bool ok = mFormat->checkFormat( &file );
170 file.close(); 173 file.close();
171 return ok; 174 return ok;
172 } 175 }
173} 176}
174 177
175void ResourceDir::doClose() 178void ResourceDir::doClose()
176{ 179{
177} 180}
178 181
179bool ResourceDir::load() 182bool ResourceDir::load()
180{ 183{
181 kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl;
182
183 QDir dir( mPath ); 184 QDir dir( mPath );
184 QStringList files = dir.entryList( QDir::Files ); 185 QStringList files = dir.entryList( QDir::Files );
185 186
186 QStringList::Iterator it; 187 QStringList::Iterator it;
187 bool ok = true; 188 bool ok = true;
188 for ( it = files.begin(); it != files.end(); ++it ) { 189 for ( it = files.begin(); it != files.end(); ++it ) {
189 QFile file( mPath + "/" + (*it) ); 190 QFile file( mPath + "/" + (*it) );
190 191
191 if ( !file.open( IO_ReadOnly ) ) { 192 if ( !file.open( IO_ReadOnly ) ) {
192 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); 193 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) );
193 ok = false; 194 ok = false;
194 continue; 195 continue;
195 } 196 }
196 197
197 if ( !mFormat->loadAll( addressBook(), this, &file ) ) 198 if ( !mFormat->loadAll( addressBook(), this, &file ) )
198 ok = false; 199 ok = false;
199 200
200 file.close(); 201 file.close();
201 } 202 }
202 203
203 return ok; 204 return ok;
204} 205}
205 206
206bool ResourceDir::save( Ticket *ticket ) 207bool ResourceDir::save( Ticket *ticket )
207{ 208{
208 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl;
209
210 AddressBook::Iterator it; 209 AddressBook::Iterator it;
211 bool ok = true; 210 bool ok = true;
212 211
213 mDirWatch.stopScan(); 212 mDirWatch.stopScan();
214 213
215 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 214 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
216 if ( (*it).resource() != this || !(*it).changed() ) 215 if ( (*it).resource() != this || !(*it).changed() )
217 continue; 216 continue;
218 217
219 QFile file( mPath + "/" + (*it).uid() ); 218 QFile file( mPath + "/" + (*it).uid() );
220 if ( !file.open( IO_WriteOnly ) ) { 219 if ( !file.open( IO_WriteOnly ) ) {
221 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); 220 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) );
222 continue; 221 continue;
223 } 222 }
224 223
225 mFormat->save( *it, &file ); 224 mFormat->save( *it, &file );
226 225
227 // mark as unchanged 226 // mark as unchanged
228 (*it).setChanged( false ); 227 (*it).setChanged( false );
229 228
230 file.close(); 229 file.close();
231 } 230 }
232 mDirWatch.startScan(); 231 mDirWatch.startScan();
233 232
234 delete ticket; 233 delete ticket;
234#ifdef ALLOW_LOCKING
235 unlock( mPath ); 235 unlock( mPath );
236 236#endif
237 return ok; 237 return ok;
238} 238}
239 239
240bool ResourceDir::lock( const QString &path ) 240bool ResourceDir::lock( const QString &path )
241{ 241{
242 kdDebug(5700) << "ResourceDir::lock()" << endl; 242#ifdef ALLOW_LOCKING
243
244 QString p = path; 243 QString p = path;
245//US change the implementation how the lockfilename is getting created 244//US change the implementation how the lockfilename is getting created
246//US p.replace( QRegExp("/"), "_" ); 245//US p.replace( QRegExp("/"), "_" );
247//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); 246//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" );
248 KURL url(p); 247 KURL url(p);
249 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 248 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
250 249
251 250
252 kdDebug(5700) << "-- lock name: " << lockName << endl;
253
254 if ( QFile::exists( lockName ) ) return false; 251 if ( QFile::exists( lockName ) ) return false;
255 252
256 QString lockUniqueName; 253 QString lockUniqueName;
257 lockUniqueName = p + KApplication::randomString( 8 ); 254 lockUniqueName = p + KApplication::randomString( 8 );
258 255
259 url = lockUniqueName; 256 url = lockUniqueName;
260//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 257//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
261 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 258 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
262 259
263 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
264
265 // Create unique file 260 // Create unique file
266 QFile file( mLockUniqueName ); 261 QFile file( mLockUniqueName );
267 file.open( IO_WriteOnly ); 262 file.open( IO_WriteOnly );
268 file.close(); 263 file.close();
269 264
270 // Create lock file 265 // Create lock file
271 int result = 0; 266 int result = 0;
272#ifndef _WIN32_ 267#ifndef _WIN32_
273 result = ::link( QFile::encodeName( mLockUniqueName ), 268 result = ::link( QFile::encodeName( mLockUniqueName ),
274 QFile::encodeName( lockName ) ); 269 QFile::encodeName( lockName ) );
275#endif 270#endif
276 if ( result == 0 ) { 271 if ( result == 0 ) {
277 addressBook()->emitAddressBookLocked(); 272 addressBook()->emitAddressBookLocked();
278 return true; 273 return true;
279 } 274 }
280 275
281 // TODO: check stat 276 // TODO: check stat
282 277
283 return false; 278 return false;
279#else
280 return true;
281#endif
284} 282}
285 283
286void ResourceDir::unlock( const QString &path ) 284void ResourceDir::unlock( const QString &path )
287{ 285{
286#ifdef ALLOW_LOCKING
288 QString p = path; 287 QString p = path;
289//US change the implementation how the lockfilename is getting created 288//US change the implementation how the lockfilename is getting created
290//US p.replace( QRegExp( "/" ), "_" ); 289//US p.replace( QRegExp( "/" ), "_" );
291//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); 290//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" );
292 KURL url(p); 291 KURL url(p);
293 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); 292 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" );
294 293
295 ::unlink( QFile::encodeName( lockName ) ); 294 ::unlink( QFile::encodeName( lockName ) );
296 QFile::remove( mLockUniqueName ); 295 QFile::remove( mLockUniqueName );
297 addressBook()->emitAddressBookUnlocked(); 296 addressBook()->emitAddressBookUnlocked();
297#else
298 return;
299#endif
298} 300}
299 301
300void ResourceDir::setPath( const QString &path ) 302void ResourceDir::setPath( const QString &path )
301{ 303{
302 mDirWatch.stopScan(); 304 mDirWatch.stopScan();
303 mDirWatch.removeDir( mPath ); 305 mDirWatch.removeDir( mPath );
304 306
305 mPath = path; 307 mPath = path;
306 308
307 mDirWatch.addDir( mPath, true ); 309 mDirWatch.addDir( mPath, true );
308 mDirWatch.startScan(); 310 mDirWatch.startScan();
309 311
310//US simulate KDirWatch event 312//US simulate KDirWatch event
311//US pathChanged(); 313//US pathChanged();
312 314
313} 315}
314 316
315QString ResourceDir::path() const 317QString ResourceDir::path() const
316{ 318{
317 return mPath; 319 return mPath;
318} 320}
319 321
320void ResourceDir::setFormat( const QString &format ) 322void ResourceDir::setFormat( const QString &format )
321{ 323{
322 mFormatName = format; 324 mFormatName = format;
323 325
324 if ( mFormat ) 326 if ( mFormat )
325 delete mFormat; 327 delete mFormat;
326 328
327 FormatFactory *factory = FormatFactory::self(); 329 FormatFactory *factory = FormatFactory::self();
328 mFormat = factory->format( mFormatName ); 330 mFormat = factory->format( mFormatName );
329/*US 331/*US
330qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); 332qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1());
331 if (mFormatName == "vcard") 333 if (mFormatName == "vcard")
332 mFormat = new VCardFormatPlugin2(); 334 mFormat = new VCardFormatPlugin2();
333 else if (mFormatName == "binary") 335 else if (mFormatName == "binary")
334 mFormat = new BinaryFormat(); 336 mFormat = new BinaryFormat();
335 else 337 else
336 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1()); 338 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1());
337*/ 339*/
338 340
339} 341}
340 342
341QString ResourceDir::format() const 343QString ResourceDir::format() const
342{ 344{
343 return mFormatName; 345 return mFormatName;
344} 346}
345 347
346void ResourceDir::pathChanged() 348void ResourceDir::pathChanged()
347{ 349{
348 if ( !addressBook() ) 350 if ( !addressBook() )
349 return; 351 return;
350 352
351 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) ); 353 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) );
352 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 354 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
353 load(); 355 load();
354 addressBook()->emitAddressBookChanged(); 356 addressBook()->emitAddressBookChanged();
355 } 357 }
356} 358}
357 359
358void ResourceDir::removeAddressee( const Addressee& addr ) 360void ResourceDir::removeAddressee( const Addressee& addr )
359{ 361{
360 QFile::remove( mPath + "/" + addr.uid() ); 362 QFile::remove( mPath + "/" + addr.uid() );
361} 363}
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index fa6bc49..17263ae 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -1,376 +1,388 @@
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//#define ALLOW_LOCKING
56 57
57using namespace KABC; 58using namespace KABC;
58 59
59extern "C" 60extern "C"
60#ifdef _WIN32_ 61#ifdef _WIN32_
61__declspec(dllexport) 62__declspec(dllexport)
62#else 63#else
63{ 64{
64#endif 65#endif
65 66
66//US void *init_kabc_file() 67//US void *init_kabc_file()
67 void *init_microkabc_file() 68 void *init_microkabc_file()
68 { 69 {
69 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>(); 70 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, SyncPrefWidgetContainer>();
70 } 71 }
71#ifndef _WIN32_ 72#ifndef _WIN32_
72} 73}
73#endif 74#endif
74 75
75ResourceFile::ResourceFile( const KConfig *config, bool syncable ) 76ResourceFile::ResourceFile( const KConfig *config, bool syncable )
76 : Resource( config, syncable ) , mFormat( 0 ) 77 : Resource( config, syncable ) , mFormat( 0 )
77{ 78{
78 QString fileName, formatName, default_fileName; 79 QString fileName, formatName, default_fileName;
79 80
80 if (syncable == true) 81 if (syncable == true)
81 default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf"; 82 default_fileName = "/home/polo/kdepim/apps/kabc/localfile.vcf";
82 else 83 else
83 default_fileName = StdAddressBook::fileName(); 84 default_fileName = StdAddressBook::fileName();
84 85
85 KConfig *cfg = (KConfig *)config; 86 KConfig *cfg = (KConfig *)config;
86 if ( cfg ) { 87 if ( cfg ) {
87 fileName = cfg->readEntry( "FileName", default_fileName ); 88 fileName = cfg->readEntry( "FileName", default_fileName );
88 formatName = cfg->readEntry( "FileFormat", "vcard" ); 89 formatName = cfg->readEntry( "FileFormat", "vcard" );
89 } else { 90 } else {
90 fileName = default_fileName; 91 fileName = default_fileName;
91 formatName = "vcard"; 92 formatName = "vcard";
92 } 93 }
93 94
94 init( fileName, formatName ); 95 init( fileName, formatName );
95} 96}
96 97
97ResourceFile::ResourceFile( const QString &fileName, bool syncable , 98ResourceFile::ResourceFile( const QString &fileName, bool syncable ,
98 const QString &formatName ) 99 const QString &formatName )
99 : Resource( 0, syncable ) 100 : Resource( 0, syncable )
100{ 101{
101// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 102// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
102 init( fileName, formatName ); 103 init( fileName, formatName );
103} 104}
104 105
105void ResourceFile::init( const QString &fileName, const QString &formatName ) 106void ResourceFile::init( const QString &fileName, const QString &formatName )
106{ 107{
107 mFormatName = formatName; 108 mFormatName = formatName;
108 109
109 FormatFactory *factory = FormatFactory::self(); 110 FormatFactory *factory = FormatFactory::self();
110 mFormat = factory->format( mFormatName ); 111 mFormat = factory->format( mFormatName );
111 112
112 if ( !mFormat ) { 113 if ( !mFormat ) {
113 mFormatName = "vcard"; 114 mFormatName = "vcard";
114 mFormat = factory->format( mFormatName ); 115 mFormat = factory->format( mFormatName );
115 } 116 }
116 117
117 118
118 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 119 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
119 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 120 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
120 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 121 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
121 122
122 setFileName( fileName ); 123 setFileName( fileName );
123} 124}
124 125
125ResourceFile::~ResourceFile() 126ResourceFile::~ResourceFile()
126{ 127{
127 delete mFormat; 128 delete mFormat;
128 mFormat = 0; 129 mFormat = 0;
129} 130}
130 131
131void ResourceFile::writeConfig( KConfig *config ) 132void ResourceFile::writeConfig( KConfig *config )
132{ 133{
133 134
134 config->setGroup( "Resource_" + identifier() ); 135 config->setGroup( "Resource_" + identifier() );
135 Resource::writeConfig( config ); 136 Resource::writeConfig( config );
136 137
137 config->writeEntry( "FileName", mFileName ); 138 config->writeEntry( "FileName", mFileName );
138 config->writeEntry( "FileFormat", mFormatName ); 139 config->writeEntry( "FileFormat", mFormatName );
139 140
140// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); 141// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
141 142
142} 143}
143 144
144Ticket *ResourceFile::requestSaveTicket() 145Ticket *ResourceFile::requestSaveTicket()
145{ 146{
146 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; 147 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
147 148
148 if ( !addressBook() ) return 0; 149 if ( !addressBook() ) return 0;
149 150
151#ifdef ALLOW_LOCKING
150 if ( !lock( mFileName ) ) { 152 if ( !lock( mFileName ) ) {
151 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" 153 qDebug("unablt to lock file ");
152 << mFileName << "'" << endl;
153 return 0; 154 return 0;
154 } 155 }
156#endif
155 return createTicket( this ); 157 return createTicket( this );
156} 158}
157 159
158 160
159bool ResourceFile::doOpen() 161bool ResourceFile::doOpen()
160{ 162{
161 QFile file( mFileName ); 163 QFile file( mFileName );
162 164
163 if ( !file.exists() ) { 165 if ( !file.exists() ) {
164 // try to create the file 166 // try to create the file
165 bool ok = file.open( IO_WriteOnly ); 167 bool ok = file.open( IO_WriteOnly );
166 if ( ok ) 168 if ( ok )
167 file.close(); 169 file.close();
168 170
169 return ok; 171 return ok;
170 } else { 172 } else {
171 if ( !file.open( IO_ReadWrite ) ) 173 if ( !file.open( IO_ReadWrite ) )
172 return false; 174 return false;
173 175
174 if ( file.size() == 0 ) { 176 if ( file.size() == 0 ) {
175 file.close(); 177 file.close();
176 return true; 178 return true;
177 } 179 }
178 180
179 bool ok = mFormat->checkFormat( &file ); 181 bool ok = mFormat->checkFormat( &file );
180 file.close(); 182 file.close();
181 183
182 return ok; 184 return ok;
183 } 185 }
184} 186}
185 187
186void ResourceFile::doClose() 188void ResourceFile::doClose()
187{ 189{
188} 190}
189 191
190bool ResourceFile::load() 192bool ResourceFile::load()
191{ 193{
192 194
193 195
194 QFile file( mFileName ); 196 QFile file( mFileName );
195 if ( !file.open( IO_ReadOnly ) ) { 197 if ( !file.open( IO_ReadOnly ) ) {
196 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 198 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
197 return false; 199 return false;
198 } 200 }
199 201
200// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 202// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
201 203
202 return mFormat->loadAll( addressBook(), this, &file ); 204 return mFormat->loadAll( addressBook(), this, &file );
203} 205}
204 206
205bool ResourceFile::save( Ticket *ticket ) 207bool ResourceFile::save( Ticket *ticket )
206{ 208{
207// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 209// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
208 210
209 211
210 // create backup file 212 // create backup file
211 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 213 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
212 214
213/*US we use a simpler method to create a backupfile 215/*US we use a simpler method to create a backupfile
214 216
215 (void) KSaveFile::backupFile( mFileName, QString::null 217 (void) KSaveFile::backupFile( mFileName, QString::null
216 ,extension ); 218 ,extension );
217 219
218 KSaveFile saveFile( mFileName ); 220 KSaveFile saveFile( mFileName );
219 bool ok = false; 221 bool ok = false;
220 if ( saveFile.status() == 0 && saveFile.file() ) 222 if ( saveFile.status() == 0 && saveFile.file() )
221 { 223 {
222 mFormat->saveAll( addressBook(), this, saveFile.file() ); 224 mFormat->saveAll( addressBook(), this, saveFile.file() );
223 ok = saveFile.close(); 225 ok = saveFile.close();
224 } 226 }
225*/ 227*/
226 228
227//US ToDo: write backupfile 229//US ToDo: write backupfile
228 mDirWatch.stopScan(); 230 mDirWatch.stopScan();
229 231
230 QFile info; 232 QFile info;
231 info.setName( mFileName ); 233 info.setName( mFileName );
232 bool ok = info.open( IO_WriteOnly ); 234 bool ok = info.open( IO_WriteOnly );
233 if ( ok ) { 235 if ( ok ) {
234 mFormat->saveAll( addressBook(), this, &info ); 236 mFormat->saveAll( addressBook(), this, &info );
235 237
236 info.close(); 238 info.close();
237 ok = true; 239 ok = true;
238 } 240 }
239 else { 241 else {
240 242
241 } 243 }
242 244
243 if ( !ok ) 245 if ( !ok )
244 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 246 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
245 247
246 mDirWatch.startScan(); 248 mDirWatch.startScan();
247 249
248 delete ticket; 250 delete ticket;
251#ifdef ALLOW_LOCKING
249 unlock( mFileName ); 252 unlock( mFileName );
253#endif
250 254
251 return ok; 255 return ok;
252} 256}
253 257
254bool ResourceFile::lock( const QString &fileName ) 258bool ResourceFile::lock( const QString &fileName )
255{ 259{
260#ifdef ALLOW_LOCKING
256 261
257 262
258 QString fn = fileName; 263 QString fn = fileName;
259 264
260//US change the implementation how the lockfilename is getting created 265//US change the implementation how the lockfilename is getting created
261//US fn.replace( QRegExp("/"), "_" ); 266//US fn.replace( QRegExp("/"), "_" );
262//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 267//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
263 268
264 KURL url(fn); 269 KURL url(fn);
265 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 270 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
266 271
267 272
268 273
269 if (QFile::exists( lockName )) return false; 274 if (QFile::exists( lockName )) return false;
270 275
271 QString lockUniqueName; 276 QString lockUniqueName;
272 lockUniqueName = fn + KApplication::randomString( 8 ); 277 lockUniqueName = fn + KApplication::randomString( 8 );
273 278
274 url = lockUniqueName; 279 url = lockUniqueName;
275//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 280//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
276 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 281 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
277 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 282 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
278 283
279 // Create unique file 284 // Create unique file
280 QFile file( mLockUniqueName ); 285 QFile file( mLockUniqueName );
281 file.open( IO_WriteOnly ); 286 file.open( IO_WriteOnly );
282 file.close(); 287 file.close();
283 288
284 // Create lock file 289 // Create lock file
285 int result = 0; 290 int result = 0;
286#ifndef _WIN32_ 291#ifndef _WIN32_
287 result = ::link( QFile::encodeName( mLockUniqueName ), 292 result = ::link( QFile::encodeName( mLockUniqueName ),
288 QFile::encodeName( lockName ) ); 293 QFile::encodeName( lockName ) );
289#endif 294#endif
290 if ( result == 0 ) { 295 if ( result == 0 ) {
291 addressBook()->emitAddressBookLocked(); 296 addressBook()->emitAddressBookLocked();
292 return true; 297 return true;
293 } 298 }
294 299
295 // TODO: check stat 300 // TODO: check stat
296 301
297 return false; 302 return false;
303#else
304 return true;
305#endif
298} 306}
299 307
300void ResourceFile::unlock( const QString &fileName ) 308void ResourceFile::unlock( const QString &fileName )
301{ 309{
310#ifdef ALLOW_LOCKING
302 QString fn = fileName; 311 QString fn = fileName;
303//US change the implementation how the lockfilename is getting created 312//US change the implementation how the lockfilename is getting created
304//US fn.replace( QRegExp( "/" ), "_" ); 313//US fn.replace( QRegExp( "/" ), "_" );
305//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 314//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
306//US QString lockName = fn + ".lock"; 315//US QString lockName = fn + ".lock";
307 KURL url(fn); 316 KURL url(fn);
308 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 317 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
309 318
310 QFile::remove( lockName ); 319 QFile::remove( lockName );
311 QFile::remove( mLockUniqueName ); 320 QFile::remove( mLockUniqueName );
312 addressBook()->emitAddressBookUnlocked(); 321 addressBook()->emitAddressBookUnlocked();
322#else
323 return;
324#endif
313} 325}
314 326
315void ResourceFile::setFileName( const QString &fileName ) 327void ResourceFile::setFileName( const QString &fileName )
316{ 328{
317 mDirWatch.stopScan(); 329 mDirWatch.stopScan();
318 mDirWatch.removeFile( mFileName ); 330 mDirWatch.removeFile( mFileName );
319 331
320 mFileName = fileName; 332 mFileName = fileName;
321 333
322 334
323 mDirWatch.addFile( mFileName ); 335 mDirWatch.addFile( mFileName );
324 mDirWatch.startScan(); 336 mDirWatch.startScan();
325 337
326//US simulate KDirWatch event 338//US simulate KDirWatch event
327//US fileChanged(); 339//US fileChanged();
328} 340}
329 341
330QString ResourceFile::fileName() const 342QString ResourceFile::fileName() const
331{ 343{
332 return mFileName; 344 return mFileName;
333} 345}
334 346
335void ResourceFile::setFormat( const QString &format ) 347void ResourceFile::setFormat( const QString &format )
336{ 348{
337 mFormatName = format; 349 mFormatName = format;
338 delete mFormat; 350 delete mFormat;
339 351
340 FormatFactory *factory = FormatFactory::self(); 352 FormatFactory *factory = FormatFactory::self();
341 mFormat = factory->format( mFormatName ); 353 mFormat = factory->format( mFormatName );
342/*US 354/*US
343//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); 355//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1());
344 if (mFormatName == "vcard") { 356 if (mFormatName == "vcard") {
345 mFormat = new VCardFormatPlugin2(); 357 mFormat = new VCardFormatPlugin2();
346// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 358// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
347 } 359 }
348 else if (mFormatName == "binary") { 360 else if (mFormatName == "binary") {
349 mFormat = new BinaryFormat(); 361 mFormat = new BinaryFormat();
350// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 362// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
351 } 363 }
352 else 364 else
353 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 365 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
354*/ 366*/
355 367
356} 368}
357 369
358QString ResourceFile::format() const 370QString ResourceFile::format() const
359{ 371{
360 return mFormatName; 372 return mFormatName;
361} 373}
362 374
363void ResourceFile::fileChanged() 375void ResourceFile::fileChanged()
364{ 376{
365 // There is a small theoretical chance that KDirWatch calls us before 377 // There is a small theoretical chance that KDirWatch calls us before
366 // we are fully constructed 378 // we are fully constructed
367 if (!addressBook()) 379 if (!addressBook())
368 return; 380 return;
369 381
370 382
371 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 383 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
372 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 384 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
373 load(); 385 load();
374 addressBook()->emitAddressBookChanged(); 386 addressBook()->emitAddressBookChanged();
375 } 387 }
376} 388}
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
index 8a795e9..867bc07 100644
--- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
+++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
@@ -1,339 +1,346 @@
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//#define ALLOW_LOCKING
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
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#ifdef ALLOW_LOCKING
114 if ( !lock( fileName() ) ) { 115 if ( !lock( fileName() ) ) {
115 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket(): Unable to lock file '" 116 qDebug("ResourceSharpDTM::requestSaveTicket(): Unable to lock file ");
116 << fileName() << "'" << endl;
117 return 0; 117 return 0;
118 } 118 }
119#endif
119 return createTicket( this ); 120 return createTicket( this );
120} 121}
121 122
122 123
123bool ResourceSharpDTM::doOpen() 124bool ResourceSharpDTM::doOpen()
124{ 125{
125 qDebug("ResourceSharpDTM::doOpen: %s", fileName().latin1()); 126 qDebug("ResourceSharpDTM::doOpen: %s", fileName().latin1());
126 127
127 // the last parameter in the SlZDataBase constructor means "readonly" 128 // the last parameter in the SlZDataBase constructor means "readonly"
128 mAccess = new SlZDataBase(fileName(), 129 mAccess = new SlZDataBase(fileName(),
129 SlZDataBase::addressbookItems(), 130 SlZDataBase::addressbookItems(),
130 NULL, true); 131 NULL, true);
131 132
132 if ( !mAccess ) { 133 if ( !mAccess ) {
133 qDebug("Unable to load file() %s", fileName().latin1()); 134 qDebug("Unable to load file() %s", fileName().latin1());
134 return false; 135 return false;
135 } 136 }
136 137
137 if (mConverter == 0) 138 if (mConverter == 0)
138 { 139 {
139 mConverter = new SharpDTMConverter(); 140 mConverter = new SharpDTMConverter();
140 bool res = mConverter->init(); 141 bool res = mConverter->init();
141 if ( !res ) 142 if ( !res )
142 { 143 {
143 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file"); 144 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file");
144 145
145 qDebug(msg); 146 qDebug(msg);
146 delete mAccess; 147 delete mAccess;
147 mAccess = 0; 148 mAccess = 0;
148 return false; 149 return false;
149 } 150 }
150 } 151 }
151 152
152 return true; 153 return true;
153} 154}
154 155
155void ResourceSharpDTM::doClose() 156void ResourceSharpDTM::doClose()
156{ 157{
157 qDebug("ResourceSharpDTM::doClose: %s", fileName().latin1()); 158 qDebug("ResourceSharpDTM::doClose: %s", fileName().latin1());
158 159
159 if(mAccess) 160 if(mAccess)
160 { 161 {
161 delete mAccess; 162 delete mAccess;
162 mAccess = 0; 163 mAccess = 0;
163 } 164 }
164 // it seems so, that deletion of access deletes backend as well 165 // it seems so, that deletion of access deletes backend as well
165 //delete backend; 166 //delete backend;
166 167
167 return; 168 return;
168} 169}
169 170
170bool ResourceSharpDTM::load() 171bool ResourceSharpDTM::load()
171{ 172{
172 qDebug("ResourceSharpDTM::load: %s", fileName().latin1()); 173 qDebug("ResourceSharpDTM::load: %s", fileName().latin1());
173 174
174 bool res = false; 175 bool res = false;
175 176
176 CardId id; 177 CardId id;
177 178
178 for (bool res=mAccess->first(); res == true; res=mAccess->next()) 179 for (bool res=mAccess->first(); res == true; res=mAccess->next())
179 { 180 {
180 id = mAccess->cardId(); 181 id = mAccess->cardId();
181 182
182 KABC::Addressee addressee; 183 KABC::Addressee addressee;
183 184
184 res = mConverter->sharpToAddressee( id, mAccess, addressee ); 185 res = mConverter->sharpToAddressee( id, mAccess, addressee );
185 186
186 if ( !addressee.isEmpty() && res ) 187 if ( !addressee.isEmpty() && res )
187 { 188 {
188 addressee.setResource( this ); 189 addressee.setResource( this );
189 addressBook()->insertAddressee( addressee ); 190 addressBook()->insertAddressee( addressee );
190 } 191 }
191 } 192 }
192 193
193 return true; 194 return true;
194} 195}
195 196
196bool ResourceSharpDTM::save( Ticket *ticket ) 197bool ResourceSharpDTM::save( Ticket *ticket )
197{ 198{
198 qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); 199 qDebug("ResourceSharpDTM::save: %s", fileName().latin1());
199 200
200 mDirWatch.stopScan(); 201 mDirWatch.stopScan();
201 202
202 KABC::AddressBook::Iterator it; 203 KABC::AddressBook::Iterator it;
203 bool res; 204 bool res;
204 205
205 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 206 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
206 CardId id = 0;// 0 means new card 207 CardId id = 0;// 0 means new card
207 KABC::Addressee addressee = (*it); 208 KABC::Addressee addressee = (*it);
208 209
209 res = mAccess->startEditCard(id); 210 res = mAccess->startEditCard(id);
210 if (res == true) 211 if (res == true)
211 { 212 {
212 res = mConverter->addresseeToSharp( *it, mAccess, id ); 213 res = mConverter->addresseeToSharp( *it, mAccess, id );
213 if (res == true) 214 if (res == true)
214 { 215 {
215 res = mAccess->finishEditCard(&id); 216 res = mAccess->finishEditCard(&id);
216 if (res == false) 217 if (res == false)
217 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); 218 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1());
218 } 219 }
219 else 220 else
220 { 221 {
221 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); 222 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1());
222 mAccess->cancelEditCard(); 223 mAccess->cancelEditCard();
223 } 224 }
224 } 225 }
225 else 226 else
226 { 227 {
227 qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1()); 228 qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1());
228 } 229 }
229 230
230 } 231 }
231 232
232//US mAccess->save(); 233//US mAccess->save();
233 234
234 mDirWatch.startScan(); 235 mDirWatch.startScan();
235 delete ticket; 236 delete ticket;
236 unlock( fileName() ); 237 unlock( fileName() );
237 238
238 return true; 239 return true;
239} 240}
240 241
241bool ResourceSharpDTM::lock( const QString &lockfileName ) 242bool ResourceSharpDTM::lock( const QString &lockfileName )
242{ 243{
244#ifdef ALLOW_LOCKING
243 qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); 245 qDebug("ResourceSharpDTM::lock: %s", fileName().latin1());
244 246
245 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; 247 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl;
246 248
247 QString fn = lockfileName; 249 QString fn = lockfileName;
248 250
249 KURL url(fn); 251 KURL url(fn);
250 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 252 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
251 253
252 kdDebug(5700) << "-- lock name: " << lockName << endl; 254 kdDebug(5700) << "-- lock name: " << lockName << endl;
253 255
254 if (QFile::exists( lockName )) 256 if (QFile::exists( lockName ))
255 { 257 {
256 qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); 258 qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName());
257 return false; 259 return false;
258 } 260 }
259 261
260 262
261 QString lockUniqueName; 263 QString lockUniqueName;
262 lockUniqueName = fn + KApplication::randomString( 8 ); 264 lockUniqueName = fn + KApplication::randomString( 8 );
263 265
264 url = lockUniqueName; 266 url = lockUniqueName;
265//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 267//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
266 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 268 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
267 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 269 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
268 270
269 // Create unique file 271 // Create unique file
270 QFile file( mLockUniqueName ); 272 QFile file( mLockUniqueName );
271 file.open( IO_WriteOnly ); 273 file.open( IO_WriteOnly );
272 file.close(); 274 file.close();
273 275
274 // Create lock file 276 // Create lock file
275 int result = 0; 277 int result = 0;
276#ifndef _WIN32_ 278#ifndef _WIN32_
277 result = ::link( QFile::encodeName( mLockUniqueName ), 279 result = ::link( QFile::encodeName( mLockUniqueName ),
278 QFile::encodeName( lockName ) ); 280 QFile::encodeName( lockName ) );
279#endif 281#endif
280 if ( result == 0 ) { 282 if ( result == 0 ) {
281 addressBook()->emitAddressBookLocked(); 283 addressBook()->emitAddressBookLocked();
282 return true; 284 return true;
283 } 285 }
284 286
285 // TODO: check stat 287 // TODO: check stat
286 288
287 return false; 289 return false;
290#else
291 return true;
292#endif
288} 293}
289 294
290void ResourceSharpDTM::unlock( const QString &fileName ) 295void ResourceSharpDTM::unlock( const QString &fileName )
291{ 296{
297#ifdef ALLOW_LOCKING
292 qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); 298 qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1());
293 299
294 QString fn = fileName; 300 QString fn = fileName;
295 KURL url(fn); 301 KURL url(fn);
296 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 302 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
297 303
298 QFile::remove( lockName ); 304 QFile::remove( lockName );
299 QFile::remove( mLockUniqueName ); 305 QFile::remove( mLockUniqueName );
300 addressBook()->emitAddressBookUnlocked(); 306 addressBook()->emitAddressBookUnlocked();
307#endif
301} 308}
302 309
303void ResourceSharpDTM::setFileName( const QString &newFileName ) 310void ResourceSharpDTM::setFileName( const QString &newFileName )
304{ 311{
305 mDirWatch.stopScan(); 312 mDirWatch.stopScan();
306 mDirWatch.removeFile( fileName() ); 313 mDirWatch.removeFile( fileName() );
307 314
308 Resource::setFileName( newFileName ); 315 Resource::setFileName( newFileName );
309 316
310 mDirWatch.addFile( fileName() ); 317 mDirWatch.addFile( fileName() );
311 mDirWatch.startScan(); 318 mDirWatch.startScan();
312 319
313} 320}
314 321
315void ResourceSharpDTM::fileChanged() 322void ResourceSharpDTM::fileChanged()
316{ 323{
317 // There is a small theoretical chance that KDirWatch calls us before 324 // There is a small theoretical chance that KDirWatch calls us before
318 // we are fully constructed 325 // we are fully constructed
319 if (!addressBook()) 326 if (!addressBook())
320 return; 327 return;
321 328
322 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); 329 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
323 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 330 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
324 load(); 331 load();
325 addressBook()->emitAddressBookChanged(); 332 addressBook()->emitAddressBookChanged();
326 } 333 }
327} 334}
328 335
329void ResourceSharpDTM::removeAddressee( const Addressee &addr ) 336void ResourceSharpDTM::removeAddressee( const Addressee &addr )
330{ 337{
331} 338}
332 339
333void ResourceSharpDTM::cleanUp() 340void ResourceSharpDTM::cleanUp()
334{ 341{
335 unlock( fileName() ); 342 unlock( fileName() );
336} 343}
337 344
338 345
339 346