author | zautrix <zautrix> | 2005-03-30 13:24:56 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-03-30 13:24:56 (UTC) |
commit | 056f171723a9301aea5a65340dffeda34e078abf (patch) (unidiff) | |
tree | 7930525c801dd9c5d9a910b71bca7dc79e10f666 /kabc/plugins | |
parent | b5222dd7a607f78235b1ea39fea0f95a9c08ccd3 (diff) | |
download | kdepimpi-056f171723a9301aea5a65340dffeda34e078abf.zip kdepimpi-056f171723a9301aea5a65340dffeda34e078abf.tar.gz kdepimpi-056f171723a9301aea5a65340dffeda34e078abf.tar.bz2 |
fixes
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index 4ab7f02..0e6c2a3 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp | |||
@@ -1,421 +1,427 @@ | |||
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 | /* |
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 | #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 | #define NO_DIRWATCH | 52 | #define NO_DIRWATCH |
53 | #include "resourcefile.h" | 53 | #include "resourcefile.h" |
54 | 54 | ||
55 | //#define ALLOW_LOCKING | 55 | //#define ALLOW_LOCKING |
56 | 56 | ||
57 | 57 | ||
58 | using namespace KABC; | 58 | using namespace KABC; |
59 | 59 | ||
60 | extern "C" | 60 | extern "C" |
61 | #ifdef _WIN32_ | 61 | #ifdef _WIN32_ |
62 | __declspec(dllexport) | 62 | __declspec(dllexport) |
63 | #else | 63 | #else |
64 | { | 64 | { |
65 | #endif | 65 | #endif |
66 | 66 | ||
67 | //US void *init_kabc_file() | 67 | //US void *init_kabc_file() |
68 | void *init_microkabc_file() | 68 | void *init_microkabc_file() |
69 | { | 69 | { |
70 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); | 70 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); |
71 | } | 71 | } |
72 | #ifndef _WIN32_ | 72 | #ifndef _WIN32_ |
73 | } | 73 | } |
74 | #endif | 74 | #endif |
75 | 75 | ||
76 | ResourceFile::ResourceFile( const KConfig *config ) | 76 | ResourceFile::ResourceFile( const KConfig *config ) |
77 | : Resource( config ) , mFormat( 0 ) | 77 | : Resource( config ) , mFormat( 0 ) |
78 | { | 78 | { |
79 | QString fileName, formatName, default_fileName; | 79 | QString fileName, formatName, default_fileName; |
80 | 80 | ||
81 | default_fileName = StdAddressBook::fileName(); | 81 | default_fileName = StdAddressBook::fileName(); |
82 | 82 | ||
83 | KConfig *cfg = (KConfig *)config; | 83 | KConfig *cfg = (KConfig *)config; |
84 | if ( cfg ) { | 84 | if ( cfg ) { |
85 | fileName = cfg->readEntry( "FileName", default_fileName ); | 85 | fileName = cfg->readEntry( "FileName", default_fileName ); |
86 | formatName = cfg->readEntry( "FileFormat", "vcard" ); | 86 | formatName = cfg->readEntry( "FileFormat", "vcard" ); |
87 | } else { | 87 | } else { |
88 | fileName = default_fileName; | 88 | fileName = default_fileName; |
89 | formatName = "vcard"; | 89 | formatName = "vcard"; |
90 | } | 90 | } |
91 | 91 | ||
92 | init( fileName, formatName ); | 92 | init( fileName, formatName ); |
93 | } | 93 | } |
94 | 94 | ||
95 | ResourceFile::ResourceFile( const QString &fileName , | 95 | ResourceFile::ResourceFile( const QString &fileName , |
96 | const QString &formatName ) | 96 | const QString &formatName ) |
97 | : Resource( 0 ) | 97 | : Resource( 0 ) |
98 | { | 98 | { |
99 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | 99 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); |
100 | 100 | ||
101 | 101 | ||
102 | 102 | ||
103 | init( fileName, formatName ); | 103 | init( fileName, formatName ); |
104 | } | 104 | } |
105 | 105 | ||
106 | void ResourceFile::init( const QString &fileName, const QString &formatName ) | 106 | void ResourceFile::init( const QString &fileName, const QString &formatName ) |
107 | { | 107 | { |
108 | mFormatName = formatName; | 108 | mFormatName = formatName; |
109 | 109 | ||
110 | FormatFactory *factory = FormatFactory::self(); | 110 | FormatFactory *factory = FormatFactory::self(); |
111 | mFormat = factory->format( mFormatName ); | 111 | mFormat = factory->format( mFormatName ); |
112 | 112 | ||
113 | if ( !mFormat ) { | 113 | if ( !mFormat ) { |
114 | mFormatName = "vcard"; | 114 | mFormatName = "vcard"; |
115 | mFormat = factory->format( mFormatName ); | 115 | mFormat = factory->format( mFormatName ); |
116 | } | 116 | } |
117 | 117 | ||
118 | #ifndef NO_DIRWATCH | 118 | #ifndef NO_DIRWATCH |
119 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 119 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
120 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 120 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
121 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 121 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
122 | #endif | 122 | #endif |
123 | 123 | ||
124 | 124 | ||
125 | QString localKdeDir; | 125 | QString localKdeDir; |
126 | localKdeDir = readEnvPath("LOCALMICROKDEHOME"); | 126 | localKdeDir = readEnvPath("LOCALMICROKDEHOME"); |
127 | if ( ! localKdeDir.isEmpty() ) { | 127 | if ( ! localKdeDir.isEmpty() ) { |
128 | qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); | 128 | qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); |
129 | 129 | QFileInfo fi ( fileName ); | |
130 | 130 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); | |
131 | 131 | QFileInfo fi2 ( localname ); | |
132 | if ( ! fi2.exists() ) { | ||
133 | KMessageBox::error(0,i18n("Addressbook resource file not found:\n '%1'.\nIn LOCAL mode only resource files in\n'%2'\nare supported.\n(i.e. in the dir ./apps/kabc/ relative to the kapi(.exe) binary.)\n\nIf you are starting KA/Pi for the very first time\nyou will get this error message as well.\nIt will create the missing file automatically for you.").arg(localname).arg(localKdeDir+"/apps/kabc/") ); | ||
134 | setFileName( localname ); | ||
135 | return; | ||
136 | |||
137 | } else { | ||
138 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); | ||
139 | setFileName( localname ); | ||
140 | return; | ||
141 | } | ||
132 | 142 | ||
133 | } | 143 | } |
134 | qDebug("RESOURCE: %s ", fileName.latin1()); | 144 | setFileName( fileName ); |
135 | |||
136 | |||
137 | |||
138 | setFileName( fileName ); | ||
139 | } | 145 | } |
140 | 146 | ||
141 | ResourceFile::~ResourceFile() | 147 | ResourceFile::~ResourceFile() |
142 | { | 148 | { |
143 | delete mFormat; | 149 | delete mFormat; |
144 | mFormat = 0; | 150 | mFormat = 0; |
145 | } | 151 | } |
146 | 152 | ||
147 | void ResourceFile::writeConfig( KConfig *config ) | 153 | void ResourceFile::writeConfig( KConfig *config ) |
148 | { | 154 | { |
149 | 155 | ||
150 | config->setGroup( "Resource_" + identifier() ); | 156 | config->setGroup( "Resource_" + identifier() ); |
151 | Resource::writeConfig( config ); | 157 | Resource::writeConfig( config ); |
152 | 158 | ||
153 | config->writeEntry( "FileName", fileName() ); | 159 | config->writeEntry( "FileName", fileName() ); |
154 | config->writeEntry( "FileFormat", mFormatName ); | 160 | config->writeEntry( "FileFormat", mFormatName ); |
155 | 161 | ||
156 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 162 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
157 | 163 | ||
158 | } | 164 | } |
159 | 165 | ||
160 | Ticket *ResourceFile::requestSaveTicket() | 166 | Ticket *ResourceFile::requestSaveTicket() |
161 | { | 167 | { |
162 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; | 168 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; |
163 | 169 | ||
164 | if ( !addressBook() ) return 0; | 170 | if ( !addressBook() ) return 0; |
165 | 171 | ||
166 | #ifdef ALLOW_LOCKING | 172 | #ifdef ALLOW_LOCKING |
167 | if ( !lock( mFileName ) ) { | 173 | if ( !lock( mFileName ) ) { |
168 | qDebug("unablt to lock file "); | 174 | qDebug("unablt to lock file "); |
169 | return 0; | 175 | return 0; |
170 | } | 176 | } |
171 | #endif | 177 | #endif |
172 | return createTicket( this ); | 178 | return createTicket( this ); |
173 | } | 179 | } |
174 | 180 | ||
175 | 181 | ||
176 | bool ResourceFile::doOpen() | 182 | bool ResourceFile::doOpen() |
177 | { | 183 | { |
178 | QFile file( fileName() ); | 184 | QFile file( fileName() ); |
179 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); | 185 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); |
180 | 186 | ||
181 | if ( !file.exists() ) { | 187 | if ( !file.exists() ) { |
182 | // try to create the file | 188 | // try to create the file |
183 | bool ok = file.open( IO_WriteOnly ); | 189 | bool ok = file.open( IO_WriteOnly ); |
184 | if ( ok ) | 190 | if ( ok ) |
185 | file.close(); | 191 | file.close(); |
186 | 192 | ||
187 | return ok; | 193 | return ok; |
188 | } else { | 194 | } else { |
189 | if ( !file.open( IO_ReadWrite ) ) | 195 | if ( !file.open( IO_ReadWrite ) ) |
190 | return false; | 196 | return false; |
191 | 197 | ||
192 | if ( file.size() == 0 ) { | 198 | if ( file.size() == 0 ) { |
193 | file.close(); | 199 | file.close(); |
194 | return true; | 200 | return true; |
195 | } | 201 | } |
196 | 202 | ||
197 | bool ok = mFormat->checkFormat( &file ); | 203 | bool ok = mFormat->checkFormat( &file ); |
198 | file.close(); | 204 | file.close(); |
199 | 205 | ||
200 | return ok; | 206 | return ok; |
201 | } | 207 | } |
202 | } | 208 | } |
203 | 209 | ||
204 | void ResourceFile::doClose() | 210 | void ResourceFile::doClose() |
205 | { | 211 | { |
206 | } | 212 | } |
207 | 213 | ||
208 | bool ResourceFile::load() | 214 | bool ResourceFile::load() |
209 | { | 215 | { |
210 | 216 | ||
211 | QFile file( fileName() ); | 217 | QFile file( fileName() ); |
212 | if ( !file.open( IO_ReadOnly ) ) { | 218 | if ( !file.open( IO_ReadOnly ) ) { |
213 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); | 219 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); |
214 | return false; | 220 | return false; |
215 | } | 221 | } |
216 | 222 | ||
217 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 223 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
218 | 224 | ||
219 | return mFormat->loadAll( addressBook(), this, &file ); | 225 | return mFormat->loadAll( addressBook(), this, &file ); |
220 | } | 226 | } |
221 | 227 | ||
222 | bool ResourceFile::save( Ticket *ticket ) | 228 | bool ResourceFile::save( Ticket *ticket ) |
223 | { | 229 | { |
224 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 230 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
225 | 231 | ||
226 | 232 | ||
227 | // create backup file | 233 | // create backup file |
228 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | 234 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); |
229 | 235 | ||
230 | /*US we use a simpler method to create a backupfile | 236 | /*US we use a simpler method to create a backupfile |
231 | 237 | ||
232 | (void) KSaveFile::backupFile( mFileName, QString::null | 238 | (void) KSaveFile::backupFile( mFileName, QString::null |
233 | ,extension ); | 239 | ,extension ); |
234 | 240 | ||
235 | KSaveFile saveFile( mFileName ); | 241 | KSaveFile saveFile( mFileName ); |
236 | bool ok = false; | 242 | bool ok = false; |
237 | if ( saveFile.status() == 0 && saveFile.file() ) | 243 | if ( saveFile.status() == 0 && saveFile.file() ) |
238 | { | 244 | { |
239 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | 245 | mFormat->saveAll( addressBook(), this, saveFile.file() ); |
240 | ok = saveFile.close(); | 246 | ok = saveFile.close(); |
241 | } | 247 | } |
242 | */ | 248 | */ |
243 | 249 | ||
244 | //US ToDo: write backupfile | 250 | //US ToDo: write backupfile |
245 | #ifndef NO_DIRWATCH | 251 | #ifndef NO_DIRWATCH |
246 | mDirWatch.stopScan(); | 252 | mDirWatch.stopScan(); |
247 | #endif | 253 | #endif |
248 | QFile info; | 254 | QFile info; |
249 | info.setName( fileName() ); | 255 | info.setName( fileName() ); |
250 | bool ok = info.open( IO_WriteOnly ); | 256 | bool ok = info.open( IO_WriteOnly ); |
251 | if ( ok ) { | 257 | if ( ok ) { |
252 | mFormat->saveAll( addressBook(), this, &info ); | 258 | mFormat->saveAll( addressBook(), this, &info ); |
253 | 259 | ||
254 | info.close(); | 260 | info.close(); |
255 | ok = true; | 261 | ok = true; |
256 | } | 262 | } |
257 | else { | 263 | else { |
258 | 264 | ||
259 | } | 265 | } |
260 | 266 | ||
261 | if ( !ok ) | 267 | if ( !ok ) |
262 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); | 268 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); |
263 | #ifndef NO_DIRWATCH | 269 | #ifndef NO_DIRWATCH |
264 | mDirWatch.startScan(); | 270 | mDirWatch.startScan(); |
265 | #endif | 271 | #endif |
266 | delete ticket; | 272 | delete ticket; |
267 | #ifdef ALLOW_LOCKING | 273 | #ifdef ALLOW_LOCKING |
268 | unlock( mFileName ); | 274 | unlock( mFileName ); |
269 | #endif | 275 | #endif |
270 | 276 | ||
271 | return ok; | 277 | return ok; |
272 | } | 278 | } |
273 | 279 | ||
274 | bool ResourceFile::lock( const QString &fileName ) | 280 | bool ResourceFile::lock( const QString &fileName ) |
275 | { | 281 | { |
276 | #ifdef ALLOW_LOCKING | 282 | #ifdef ALLOW_LOCKING |
277 | 283 | ||
278 | 284 | ||
279 | QString fn = fileName; | 285 | QString fn = fileName; |
280 | 286 | ||
281 | //US change the implementation how the lockfilename is getting created | 287 | //US change the implementation how the lockfilename is getting created |
282 | //US fn.replace( QRegExp("/"), "_" ); | 288 | //US fn.replace( QRegExp("/"), "_" ); |
283 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 289 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
284 | 290 | ||
285 | KURL url(fn); | 291 | KURL url(fn); |
286 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 292 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
287 | 293 | ||
288 | 294 | ||
289 | 295 | ||
290 | if (QFile::exists( lockName )) return false; | 296 | if (QFile::exists( lockName )) return false; |
291 | 297 | ||
292 | QString lockUniqueName; | 298 | QString lockUniqueName; |
293 | lockUniqueName = fn + KApplication::randomString( 8 ); | 299 | lockUniqueName = fn + KApplication::randomString( 8 ); |
294 | 300 | ||
295 | url = lockUniqueName; | 301 | url = lockUniqueName; |
296 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 302 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
297 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 303 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
298 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 304 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
299 | 305 | ||
300 | // Create unique file | 306 | // Create unique file |
301 | QFile file( mLockUniqueName ); | 307 | QFile file( mLockUniqueName ); |
302 | file.open( IO_WriteOnly ); | 308 | file.open( IO_WriteOnly ); |
303 | file.close(); | 309 | file.close(); |
304 | 310 | ||
305 | // Create lock file | 311 | // Create lock file |
306 | int result = 0; | 312 | int result = 0; |
307 | #ifndef _WIN32_ | 313 | #ifndef _WIN32_ |
308 | result = ::link( QFile::encodeName( mLockUniqueName ), | 314 | result = ::link( QFile::encodeName( mLockUniqueName ), |
309 | QFile::encodeName( lockName ) ); | 315 | QFile::encodeName( lockName ) ); |
310 | #endif | 316 | #endif |
311 | if ( result == 0 ) { | 317 | if ( result == 0 ) { |
312 | addressBook()->emitAddressBookLocked(); | 318 | addressBook()->emitAddressBookLocked(); |
313 | return true; | 319 | return true; |
314 | } | 320 | } |
315 | 321 | ||
316 | // TODO: check stat | 322 | // TODO: check stat |
317 | 323 | ||
318 | return false; | 324 | return false; |
319 | #else | 325 | #else |
320 | return true; | 326 | return true; |
321 | #endif | 327 | #endif |
322 | } | 328 | } |
323 | 329 | ||
324 | void ResourceFile::unlock( const QString &fileName ) | 330 | void ResourceFile::unlock( const QString &fileName ) |
325 | { | 331 | { |
326 | #ifdef ALLOW_LOCKING | 332 | #ifdef ALLOW_LOCKING |
327 | QString fn = fileName; | 333 | QString fn = fileName; |
328 | //US change the implementation how the lockfilename is getting created | 334 | //US change the implementation how the lockfilename is getting created |
329 | //US fn.replace( QRegExp( "/" ), "_" ); | 335 | //US fn.replace( QRegExp( "/" ), "_" ); |
330 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 336 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
331 | //US QString lockName = fn + ".lock"; | 337 | //US QString lockName = fn + ".lock"; |
332 | KURL url(fn); | 338 | KURL url(fn); |
333 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 339 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
334 | 340 | ||
335 | QFile::remove( lockName ); | 341 | QFile::remove( lockName ); |
336 | QFile::remove( mLockUniqueName ); | 342 | QFile::remove( mLockUniqueName ); |
337 | addressBook()->emitAddressBookUnlocked(); | 343 | addressBook()->emitAddressBookUnlocked(); |
338 | #else | 344 | #else |
339 | return; | 345 | return; |
340 | #endif | 346 | #endif |
341 | } | 347 | } |
342 | 348 | ||
343 | void ResourceFile::setFileName( const QString &fileName ) | 349 | void ResourceFile::setFileName( const QString &fileName ) |
344 | { | 350 | { |
345 | #ifndef NO_DIRWATCH | 351 | #ifndef NO_DIRWATCH |
346 | mDirWatch.stopScan(); | 352 | mDirWatch.stopScan(); |
347 | mDirWatch.removeFile( mFileName ); | 353 | mDirWatch.removeFile( mFileName ); |
348 | mFileName = fileName; | 354 | mFileName = fileName; |
349 | 355 | ||
350 | 356 | ||
351 | mDirWatch.addFile( mFileName ); | 357 | mDirWatch.addFile( mFileName ); |
352 | mDirWatch.startScan(); | 358 | mDirWatch.startScan(); |
353 | #else | 359 | #else |
354 | mFileName2 = fileName; | 360 | mFileName2 = fileName; |
355 | #endif | 361 | #endif |
356 | 362 | ||
357 | //US simulate KDirWatch event | 363 | //US simulate KDirWatch event |
358 | //US fileChanged(); | 364 | //US fileChanged(); |
359 | } | 365 | } |
360 | 366 | ||
361 | QString ResourceFile::fileName() const | 367 | QString ResourceFile::fileName() const |
362 | { | 368 | { |
363 | return mFileName2; | 369 | return mFileName2; |
364 | } | 370 | } |
365 | 371 | ||
366 | void ResourceFile::setFormat( const QString &format ) | 372 | void ResourceFile::setFormat( const QString &format ) |
367 | { | 373 | { |
368 | mFormatName = format; | 374 | mFormatName = format; |
369 | delete mFormat; | 375 | delete mFormat; |
370 | 376 | ||
371 | FormatFactory *factory = FormatFactory::self(); | 377 | FormatFactory *factory = FormatFactory::self(); |
372 | mFormat = factory->format( mFormatName ); | 378 | mFormat = factory->format( mFormatName ); |
373 | /*US | 379 | /*US |
374 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); | 380 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); |
375 | if (mFormatName == "vcard") { | 381 | if (mFormatName == "vcard") { |
376 | mFormat = new VCardFormatPlugin2(); | 382 | mFormat = new VCardFormatPlugin2(); |
377 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | 383 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); |
378 | } | 384 | } |
379 | else if (mFormatName == "binary") { | 385 | else if (mFormatName == "binary") { |
380 | mFormat = new BinaryFormat(); | 386 | mFormat = new BinaryFormat(); |
381 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | 387 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); |
382 | } | 388 | } |
383 | else | 389 | else |
384 | qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); | 390 | qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); |
385 | */ | 391 | */ |
386 | 392 | ||
387 | } | 393 | } |
388 | 394 | ||
389 | QString ResourceFile::format() const | 395 | QString ResourceFile::format() const |
390 | { | 396 | { |
391 | return mFormatName; | 397 | return mFormatName; |
392 | } | 398 | } |
393 | 399 | ||
394 | void ResourceFile::fileChanged() | 400 | void ResourceFile::fileChanged() |
395 | { | 401 | { |
396 | // There is a small theoretical chance that KDirWatch calls us before | 402 | // There is a small theoretical chance that KDirWatch calls us before |
397 | // we are fully constructed | 403 | // we are fully constructed |
398 | if (!addressBook()) | 404 | if (!addressBook()) |
399 | return; | 405 | return; |
400 | 406 | ||
401 | 407 | ||
402 | QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 408 | QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
403 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 409 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
404 | load(); | 410 | load(); |
405 | addressBook()->emitAddressBookChanged(); | 411 | addressBook()->emitAddressBookChanged(); |
406 | } | 412 | } |
407 | } | 413 | } |
408 | 414 | ||
409 | void ResourceFile::removeAddressee( const Addressee &addr ) | 415 | void ResourceFile::removeAddressee( const Addressee &addr ) |
410 | { | 416 | { |
411 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); | 417 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); |
412 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); | 418 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); |
413 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); | 419 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); |
414 | } | 420 | } |
415 | 421 | ||
416 | void ResourceFile::cleanUp() | 422 | void ResourceFile::cleanUp() |
417 | { | 423 | { |
418 | unlock( fileName() ); | 424 | unlock( fileName() ); |
419 | } | 425 | } |
420 | 426 | ||
421 | //US #include "resourcefile.moc" | 427 | //US #include "resourcefile.moc" |