summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/dir/resourcedir.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/dir/resourcedir.cpp') (more/less context) (show whitespace changes)
-rw-r--r--kabc/plugins/dir/resourcedir.cpp32
1 files changed, 17 insertions, 15 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
@@ -56,12 +56,14 @@ $Id$
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
@@ -133,22 +135,23 @@ void ResourceDir::writeConfig( KConfig *config )
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;
140 141
141 if ( !addressBook() ) return 0;
142 142
143 if ( !addressBook() ) return 0;
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 );
@@ -175,14 +178,12 @@ bool ResourceDir::doOpen()
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 ) {
@@ -202,14 +203,12 @@ bool ResourceDir::load()
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 ) {
@@ -229,42 +228,38 @@ bool ResourceDir::save( Ticket *ticket )
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
@@ -278,26 +273,33 @@ bool ResourceDir::lock( const QString &path )
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 );