-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp index 2cdf4bf..ba17c50 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp | |||
@@ -1,406 +1,406 @@ | |||
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 | /* |
22 | Enhanced Version of the file for platform independent KDE tools. | 22 | Enhanced Version of the file for platform independent KDE tools. |
23 | Copyright (c) 2004 Ulf Schenk | 23 | Copyright (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 | #include <libkdepim/ksyncprofile.h> | 48 | #include <libkdepim/ksyncprofile.h> |
49 | 49 | ||
50 | #include "resourcesharpdtmconfig.h" | 50 | #include "resourcesharpdtmconfig.h" |
51 | #include "resourcesharpdtm.h" | 51 | #include "resourcesharpdtm.h" |
52 | 52 | ||
53 | #include "stdaddressbook.h" | 53 | #include "stdaddressbook.h" |
54 | 54 | ||
55 | #include "sharpdtmconverter.h" | 55 | #include "sharpdtmconverter.h" |
56 | //#define ALLOW_LOCKING | 56 | //#define ALLOW_LOCKING |
57 | using namespace KABC; | 57 | using namespace KABC; |
58 | extern "C" | 58 | extern "C" |
59 | { | 59 | { |
60 | void *init_microkabc_sharpdtm() | 60 | void *init_microkabc_sharpdtm() |
61 | { | 61 | { |
62 | return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>(); | 62 | return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>(); |
63 | } | 63 | } |
64 | } | 64 | } |
65 | 65 | ||
66 | ResourceSharpDTM::ResourceSharpDTM( const KConfig *config ) | 66 | ResourceSharpDTM::ResourceSharpDTM( const KConfig *config ) |
67 | : Resource( config ), mConverter (0) | 67 | : Resource( config ), mConverter (0) |
68 | { | 68 | { |
69 | // we can not choose the filename. Therefore use the default to display | 69 | // we can not choose the filename. Therefore use the default to display |
70 | 70 | ||
71 | QString fileName = SlZDataBase::addressbookFileName(); | 71 | QString fileName = SlZDataBase::addressbookFileName(); |
72 | init( fileName ); | 72 | init( fileName ); |
73 | } | 73 | } |
74 | 74 | ||
75 | ResourceSharpDTM::ResourceSharpDTM( const QString &fileName, bool syncable ) | 75 | ResourceSharpDTM::ResourceSharpDTM( const QString &fileName ) |
76 | : Resource( 0, syncable ) | 76 | : Resource( 0 ) |
77 | { | 77 | { |
78 | init( fileName ); | 78 | init( fileName ); |
79 | } | 79 | } |
80 | 80 | ||
81 | void ResourceSharpDTM::init( const QString &fileName ) | 81 | void ResourceSharpDTM::init( const QString &fileName ) |
82 | { | 82 | { |
83 | 83 | ||
84 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 84 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
85 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 85 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
86 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 86 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
87 | 87 | ||
88 | setFileName( fileName ); | 88 | setFileName( fileName ); |
89 | } | 89 | } |
90 | 90 | ||
91 | ResourceSharpDTM::~ResourceSharpDTM() | 91 | ResourceSharpDTM::~ResourceSharpDTM() |
92 | { | 92 | { |
93 | if (mConverter != 0) | 93 | if (mConverter != 0) |
94 | delete mConverter; | 94 | delete mConverter; |
95 | 95 | ||
96 | if(mAccess != 0) | 96 | if(mAccess != 0) |
97 | delete mAccess; | 97 | delete mAccess; |
98 | } | 98 | } |
99 | 99 | ||
100 | void ResourceSharpDTM::writeConfig( KConfig *config ) | 100 | void ResourceSharpDTM::writeConfig( KConfig *config ) |
101 | { | 101 | { |
102 | Resource::writeConfig( config ); | 102 | Resource::writeConfig( config ); |
103 | } | 103 | } |
104 | 104 | ||
105 | Ticket *ResourceSharpDTM::requestSaveTicket() | 105 | Ticket *ResourceSharpDTM::requestSaveTicket() |
106 | { | 106 | { |
107 | 107 | ||
108 | 108 | ||
109 | qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1()); | 109 | qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1()); |
110 | 110 | ||
111 | if ( !addressBook() ) return 0; | 111 | if ( !addressBook() ) return 0; |
112 | 112 | ||
113 | #ifdef ALLOW_LOCKING | 113 | #ifdef ALLOW_LOCKING |
114 | if ( !lock( fileName() ) ) { | 114 | if ( !lock( fileName() ) ) { |
115 | qDebug("ResourceSharpDTM::requestSaveTicket(): Unable to lock file "); | 115 | qDebug("ResourceSharpDTM::requestSaveTicket(): Unable to lock file "); |
116 | return 0; | 116 | return 0; |
117 | } | 117 | } |
118 | #endif | 118 | #endif |
119 | return createTicket( this ); | 119 | return createTicket( this ); |
120 | } | 120 | } |
121 | 121 | ||
122 | 122 | ||
123 | bool ResourceSharpDTM::doOpen() | 123 | bool 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, false); | 130 | NULL, false); |
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 | ||
155 | void ResourceSharpDTM::doClose() | 155 | void 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 | ||
170 | bool ResourceSharpDTM::load() | 170 | bool 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 | ||
196 | bool ResourceSharpDTM::save( Ticket *ticket ) | 196 | bool 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 | mDirWatch.stopScan(); |
201 | 201 | ||
202 | KABC::AddressBook::Iterator it; | 202 | KABC::AddressBook::Iterator it; |
203 | bool res; | 203 | bool res; |
204 | KABC::Addressee::List changedAddressees; | 204 | KABC::Addressee::List changedAddressees; |
205 | typedef QMap<int,QString> AddresseeMap; | 205 | typedef QMap<int,QString> AddresseeMap; |
206 | AddresseeMap map; | 206 | AddresseeMap map; |
207 | CardId id ; | 207 | CardId id ; |
208 | 208 | ||
209 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 209 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
210 | 210 | ||
211 | if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { | 211 | if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { |
212 | QString uid = (*it).originalExternalUID(); | 212 | QString uid = (*it).originalExternalUID(); |
213 | bool res; | 213 | bool res; |
214 | if ( uid.isEmpty() ) | 214 | if ( uid.isEmpty() ) |
215 | id = 0; | 215 | id = 0; |
216 | else | 216 | else |
217 | id = uid.toUInt(); | 217 | id = uid.toUInt(); |
218 | KABC::Addressee addressee = (*it); | 218 | KABC::Addressee addressee = (*it); |
219 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL ) { | 219 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL ) { |
220 | res = mAccess->startEditCard(id); | 220 | res = mAccess->startEditCard(id); |
221 | if (res == true) | 221 | if (res == true) |
222 | { | 222 | { |
223 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); | 223 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); |
224 | if (res == true) | 224 | if (res == true) |
225 | { | 225 | { |
226 | res = mAccess->finishEditCard(&id); | 226 | res = mAccess->finishEditCard(&id); |
227 | //(*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | 227 | //(*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); |
228 | //(*it).setExternalUID( QString::number( id ) ); | 228 | //(*it).setExternalUID( QString::number( id ) ); |
229 | //(*it).setOriginalExternalUID( QString::number( id ) ); | 229 | //(*it).setOriginalExternalUID( QString::number( id ) ); |
230 | map.insert(id,(*it).uid()); | 230 | map.insert(id,(*it).uid()); |
231 | if (res == false) | 231 | if (res == false) |
232 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); | 232 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); |
233 | 233 | ||
234 | } | 234 | } |
235 | else | 235 | else |
236 | { | 236 | { |
237 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); | 237 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); |
238 | mAccess->cancelEditCard(); | 238 | mAccess->cancelEditCard(); |
239 | } | 239 | } |
240 | } | 240 | } |
241 | 241 | ||
242 | } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { | 242 | } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { |
243 | res = mAccess->deleteCard(&id); | 243 | res = mAccess->deleteCard(&id); |
244 | if ( !res ) | 244 | if ( !res ) |
245 | qDebug("delete error "); | 245 | qDebug("delete error "); |
246 | 246 | ||
247 | 247 | ||
248 | } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { | 248 | } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { |
249 | //changed | 249 | //changed |
250 | res = mAccess->startEditCard(id); | 250 | res = mAccess->startEditCard(id); |
251 | if (res == true) | 251 | if (res == true) |
252 | { | 252 | { |
253 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); | 253 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); |
254 | if (res == true) | 254 | if (res == true) |
255 | { | 255 | { |
256 | res = mAccess->finishEditCard(&id); | 256 | res = mAccess->finishEditCard(&id); |
257 | //(*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_CSUM ); | 257 | //(*it).setTempSyncStat( SYNC_TEMPSTATE_NEW_CSUM ); |
258 | map.insert(id,(*it).uid()); | 258 | map.insert(id,(*it).uid()); |
259 | if (res == false) | 259 | if (res == false) |
260 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); | 260 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); |
261 | 261 | ||
262 | } | 262 | } |
263 | else | 263 | else |
264 | { | 264 | { |
265 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); | 265 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); |
266 | mAccess->cancelEditCard(); | 266 | mAccess->cancelEditCard(); |
267 | } | 267 | } |
268 | } | 268 | } |
269 | } | 269 | } |
270 | } | 270 | } |
271 | 271 | ||
272 | } | 272 | } |
273 | AddresseeMap::Iterator itam; | 273 | AddresseeMap::Iterator itam; |
274 | for ( res=mAccess->first(); res == true; res=mAccess->next()) | 274 | for ( res=mAccess->first(); res == true; res=mAccess->next()) |
275 | { | 275 | { |
276 | id = mAccess->cardId(); | 276 | id = mAccess->cardId(); |
277 | int idint = id; | 277 | int idint = id; |
278 | itam = map.find( idint ); | 278 | itam = map.find( idint ); |
279 | if ( itam != map.end() ) { | 279 | if ( itam != map.end() ) { |
280 | KABC::Addressee addressee; | 280 | KABC::Addressee addressee; |
281 | res = mConverter->sharpToAddressee( id, mAccess, addressee ); | 281 | res = mConverter->sharpToAddressee( id, mAccess, addressee ); |
282 | 282 | ||
283 | if ( !addressee.isEmpty() && res ) | 283 | if ( !addressee.isEmpty() && res ) |
284 | { | 284 | { |
285 | addressee.setResource( this ); | 285 | addressee.setResource( this ); |
286 | addressee.setUid( itam.data() ); | 286 | addressee.setUid( itam.data() ); |
287 | addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | 287 | addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); |
288 | addressBook()->insertAddressee( addressee , false ); | 288 | addressBook()->insertAddressee( addressee , false ); |
289 | } | 289 | } |
290 | } | 290 | } |
291 | } | 291 | } |
292 | 292 | ||
293 | //US mAccess->save(); | 293 | //US mAccess->save(); |
294 | 294 | ||
295 | mDirWatch.startScan(); | 295 | mDirWatch.startScan(); |
296 | delete ticket; | 296 | delete ticket; |
297 | unlock( fileName() ); | 297 | unlock( fileName() ); |
298 | 298 | ||
299 | return true; | 299 | return true; |
300 | } | 300 | } |
301 | 301 | ||
302 | bool ResourceSharpDTM::lock( const QString &lockfileName ) | 302 | bool ResourceSharpDTM::lock( const QString &lockfileName ) |
303 | { | 303 | { |
304 | #ifdef ALLOW_LOCKING | 304 | #ifdef ALLOW_LOCKING |
305 | qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); | 305 | qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); |
306 | 306 | ||
307 | kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; | 307 | kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; |
308 | 308 | ||
309 | QString fn = lockfileName; | 309 | QString fn = lockfileName; |
310 | 310 | ||
311 | KURL url(fn); | 311 | KURL url(fn); |
312 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 312 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
313 | 313 | ||
314 | kdDebug(5700) << "-- lock name: " << lockName << endl; | 314 | kdDebug(5700) << "-- lock name: " << lockName << endl; |
315 | 315 | ||
316 | if (QFile::exists( lockName )) | 316 | if (QFile::exists( lockName )) |
317 | { | 317 | { |
318 | qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); | 318 | qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); |
319 | return false; | 319 | return false; |
320 | } | 320 | } |
321 | 321 | ||
322 | 322 | ||
323 | QString lockUniqueName; | 323 | QString lockUniqueName; |
324 | lockUniqueName = fn + KApplication::randomString( 8 ); | 324 | lockUniqueName = fn + KApplication::randomString( 8 ); |
325 | 325 | ||
326 | url = lockUniqueName; | 326 | url = lockUniqueName; |
327 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 327 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
328 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 328 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
329 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 329 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
330 | 330 | ||
331 | // Create unique file | 331 | // Create unique file |
332 | QFile file( mLockUniqueName ); | 332 | QFile file( mLockUniqueName ); |
333 | file.open( IO_WriteOnly ); | 333 | file.open( IO_WriteOnly ); |
334 | file.close(); | 334 | file.close(); |
335 | 335 | ||
336 | // Create lock file | 336 | // Create lock file |
337 | int result = 0; | 337 | int result = 0; |
338 | #ifndef _WIN32_ | 338 | #ifndef _WIN32_ |
339 | result = ::link( QFile::encodeName( mLockUniqueName ), | 339 | result = ::link( QFile::encodeName( mLockUniqueName ), |
340 | QFile::encodeName( lockName ) ); | 340 | QFile::encodeName( lockName ) ); |
341 | #endif | 341 | #endif |
342 | if ( result == 0 ) { | 342 | if ( result == 0 ) { |
343 | addressBook()->emitAddressBookLocked(); | 343 | addressBook()->emitAddressBookLocked(); |
344 | return true; | 344 | return true; |
345 | } | 345 | } |
346 | 346 | ||
347 | // TODO: check stat | 347 | // TODO: check stat |
348 | 348 | ||
349 | return false; | 349 | return false; |
350 | #else | 350 | #else |
351 | return true; | 351 | return true; |
352 | #endif | 352 | #endif |
353 | } | 353 | } |
354 | 354 | ||
355 | void ResourceSharpDTM::unlock( const QString &fileName ) | 355 | void ResourceSharpDTM::unlock( const QString &fileName ) |
356 | { | 356 | { |
357 | #ifdef ALLOW_LOCKING | 357 | #ifdef ALLOW_LOCKING |
358 | qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); | 358 | qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); |
359 | 359 | ||
360 | QString fn = fileName; | 360 | QString fn = fileName; |
361 | KURL url(fn); | 361 | KURL url(fn); |
362 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 362 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
363 | 363 | ||
364 | QFile::remove( lockName ); | 364 | QFile::remove( lockName ); |
365 | QFile::remove( mLockUniqueName ); | 365 | QFile::remove( mLockUniqueName ); |
366 | addressBook()->emitAddressBookUnlocked(); | 366 | addressBook()->emitAddressBookUnlocked(); |
367 | #endif | 367 | #endif |
368 | } | 368 | } |
369 | 369 | ||
370 | void ResourceSharpDTM::setFileName( const QString &newFileName ) | 370 | void ResourceSharpDTM::setFileName( const QString &newFileName ) |
371 | { | 371 | { |
372 | mDirWatch.stopScan(); | 372 | mDirWatch.stopScan(); |
373 | mDirWatch.removeFile( fileName() ); | 373 | mDirWatch.removeFile( fileName() ); |
374 | 374 | ||
375 | Resource::setFileName( newFileName ); | 375 | Resource::setFileName( newFileName ); |
376 | 376 | ||
377 | mDirWatch.addFile( fileName() ); | 377 | mDirWatch.addFile( fileName() ); |
378 | mDirWatch.startScan(); | 378 | mDirWatch.startScan(); |
379 | 379 | ||
380 | } | 380 | } |
381 | 381 | ||
382 | void ResourceSharpDTM::fileChanged() | 382 | void ResourceSharpDTM::fileChanged() |
383 | { | 383 | { |
384 | // There is a small theoretical chance that KDirWatch calls us before | 384 | // There is a small theoretical chance that KDirWatch calls us before |
385 | // we are fully constructed | 385 | // we are fully constructed |
386 | if (!addressBook()) | 386 | if (!addressBook()) |
387 | return; | 387 | return; |
388 | 388 | ||
389 | QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 389 | QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
390 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 390 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
391 | load(); | 391 | load(); |
392 | addressBook()->emitAddressBookChanged(); | 392 | addressBook()->emitAddressBookChanged(); |
393 | } | 393 | } |
394 | } | 394 | } |
395 | 395 | ||
396 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) | 396 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) |
397 | { | 397 | { |
398 | } | 398 | } |
399 | 399 | ||
400 | void ResourceSharpDTM::cleanUp() | 400 | void ResourceSharpDTM::cleanUp() |
401 | { | 401 | { |
402 | unlock( fileName() ); | 402 | unlock( fileName() ); |
403 | } | 403 | } |
404 | 404 | ||
405 | 405 | ||
406 | 406 | ||