author | zautrix <zautrix> | 2005-04-21 22:07:10 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-04-21 22:07:10 (UTC) |
commit | 6284b1d7d597463347b471ed8ec2770f4e2d449b (patch) (unidiff) | |
tree | 5444a13ed788191733fec406791ddf03ec246b59 /kabc | |
parent | fd38343ef53c9b2a48208f747100579703cc1814 (diff) | |
download | kdepimpi-6284b1d7d597463347b471ed8ec2770f4e2d449b.zip kdepimpi-6284b1d7d597463347b471ed8ec2770f4e2d449b.tar.gz kdepimpi-6284b1d7d597463347b471ed8ec2770f4e2d449b.tar.bz2 |
fixes
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 49 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.h | 1 |
2 files changed, 48 insertions, 2 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index 2bd9e71..c89939d 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp | |||
@@ -1,433 +1,478 @@ | |||
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 | #include <kglobalsettings.h> | ||
46 | 47 | ||
47 | #include "formatfactory.h" | 48 | #include "formatfactory.h" |
48 | 49 | ||
49 | #include "resource.h" | 50 | #include "resource.h" |
50 | #include "resourcefileconfig.h" | 51 | #include "resourcefileconfig.h" |
51 | #include "stdaddressbook.h" | 52 | #include "stdaddressbook.h" |
52 | #define NO_DIRWATCH | 53 | #define NO_DIRWATCH |
53 | #include "resourcefile.h" | 54 | #include "resourcefile.h" |
54 | 55 | ||
55 | //#define ALLOW_LOCKING | 56 | //#define ALLOW_LOCKING |
56 | 57 | ||
57 | 58 | ||
58 | using namespace KABC; | 59 | using namespace KABC; |
59 | 60 | ||
60 | extern "C" | 61 | extern "C" |
61 | #ifdef _WIN32_ | 62 | #ifdef _WIN32_ |
62 | __declspec(dllexport) | 63 | __declspec(dllexport) |
63 | #else | 64 | #else |
64 | { | 65 | { |
65 | #endif | 66 | #endif |
66 | 67 | ||
67 | //US void *init_kabc_file() | 68 | //US void *init_kabc_file() |
68 | void *init_microkabc_file() | 69 | void *init_microkabc_file() |
69 | { | 70 | { |
70 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); | 71 | return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); |
71 | } | 72 | } |
72 | #ifndef _WIN32_ | 73 | #ifndef _WIN32_ |
73 | } | 74 | } |
74 | #endif | 75 | #endif |
75 | 76 | ||
76 | ResourceFile::ResourceFile( const KConfig *config ) | 77 | ResourceFile::ResourceFile( const KConfig *config ) |
77 | : Resource( config ) , mFormat( 0 ) | 78 | : Resource( config ) , mFormat( 0 ) |
78 | { | 79 | { |
79 | QString fileName, formatName, default_fileName; | 80 | QString fileName, formatName, default_fileName; |
80 | 81 | ||
81 | default_fileName = StdAddressBook::fileName(); | 82 | default_fileName = StdAddressBook::fileName(); |
82 | 83 | mLastBackupDate = -1; | |
83 | KConfig *cfg = (KConfig *)config; | 84 | KConfig *cfg = (KConfig *)config; |
84 | if ( cfg ) { | 85 | if ( cfg ) { |
85 | fileName = cfg->readEntry( "FileName", default_fileName ); | 86 | fileName = cfg->readEntry( "FileName", default_fileName ); |
86 | formatName = cfg->readEntry( "FileFormat", "vcard" ); | 87 | formatName = cfg->readEntry( "FileFormat", "vcard" ); |
87 | mFamily = cfg->readEntry( "ResourceName", "std" ); | 88 | mFamily = cfg->readEntry( "ResourceName", "std" ); |
89 | mLastBackupDate = cfg->readNumEntry( "LastBackupDate", 0 ); | ||
88 | } else { | 90 | } else { |
89 | fileName = default_fileName; | 91 | fileName = default_fileName; |
90 | formatName = "vcard"; | 92 | formatName = "vcard"; |
91 | } | 93 | } |
92 | 94 | ||
93 | init( fileName, formatName ); | 95 | init( fileName, formatName ); |
94 | } | 96 | } |
95 | 97 | ||
96 | ResourceFile::ResourceFile( const QString &fileName , | 98 | ResourceFile::ResourceFile( const QString &fileName , |
97 | const QString &formatName ) | 99 | const QString &formatName ) |
98 | : Resource( 0 ) | 100 | : Resource( 0 ) |
99 | { | 101 | { |
100 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | 102 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); |
101 | 103 | ||
102 | 104 | ||
103 | 105 | mLastBackupDate = -1; | |
104 | init( fileName, formatName ); | 106 | init( fileName, formatName ); |
105 | } | 107 | } |
106 | 108 | ||
107 | void ResourceFile::init( const QString &fileName, const QString &formatName ) | 109 | void ResourceFile::init( const QString &fileName, const QString &formatName ) |
108 | { | 110 | { |
109 | mFormatName = formatName; | 111 | mFormatName = formatName; |
110 | 112 | ||
111 | FormatFactory *factory = FormatFactory::self(); | 113 | FormatFactory *factory = FormatFactory::self(); |
112 | mFormat = factory->format( mFormatName ); | 114 | mFormat = factory->format( mFormatName ); |
113 | 115 | ||
114 | if ( !mFormat ) { | 116 | if ( !mFormat ) { |
115 | mFormatName = "vcard"; | 117 | mFormatName = "vcard"; |
116 | mFormat = factory->format( mFormatName ); | 118 | mFormat = factory->format( mFormatName ); |
117 | } | 119 | } |
118 | 120 | ||
119 | #ifndef NO_DIRWATCH | 121 | #ifndef NO_DIRWATCH |
120 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 122 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
121 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 123 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
122 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 124 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
123 | #endif | 125 | #endif |
124 | 126 | ||
125 | QString localKdeDir; | 127 | QString localKdeDir; |
126 | localKdeDir = readEnvPath("LOCALMICROKDEHOME"); | 128 | localKdeDir = readEnvPath("LOCALMICROKDEHOME"); |
127 | if ( ! localKdeDir.isEmpty() ) { | 129 | if ( ! localKdeDir.isEmpty() ) { |
128 | qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); | 130 | qDebug("LOCALMICROKDEHOME is set to: %s",localKdeDir.latin1() ); |
129 | QFileInfo fi ( fileName ); | 131 | QFileInfo fi ( fileName ); |
130 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); | 132 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); |
131 | QFileInfo fi2 ( localname ); | 133 | QFileInfo fi2 ( localname ); |
132 | if ( ! fi2.exists() || mFamily == "sync_res" ) { | 134 | if ( ! fi2.exists() || mFamily == "sync_res" ) { |
133 | if ( fi.exists() && mFamily == "sync_res") { | 135 | if ( fi.exists() && mFamily == "sync_res") { |
134 | qDebug("LOCAL mode SYNC mode using absolute file path "); | 136 | qDebug("LOCAL mode SYNC mode using absolute file path "); |
135 | setFileName( fileName ); | 137 | setFileName( fileName ); |
136 | return; | 138 | return; |
137 | } else { | 139 | } else { |
138 | 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/") ); | 140 | 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/") ); |
139 | setFileName( localname ); | 141 | setFileName( localname ); |
140 | return; | 142 | return; |
141 | } | 143 | } |
142 | 144 | ||
143 | } else { | 145 | } else { |
144 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); | 146 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); |
145 | setFileName( localname ); | 147 | setFileName( localname ); |
146 | return; | 148 | return; |
147 | } | 149 | } |
148 | 150 | ||
149 | } | 151 | } |
150 | setFileName( fileName ); | 152 | setFileName( fileName ); |
151 | } | 153 | } |
152 | 154 | ||
153 | ResourceFile::~ResourceFile() | 155 | ResourceFile::~ResourceFile() |
154 | { | 156 | { |
155 | delete mFormat; | 157 | delete mFormat; |
156 | mFormat = 0; | 158 | mFormat = 0; |
157 | } | 159 | } |
158 | 160 | ||
159 | void ResourceFile::writeConfig( KConfig *config ) | 161 | void ResourceFile::writeConfig( KConfig *config ) |
160 | { | 162 | { |
161 | 163 | ||
162 | config->setGroup( "Resource_" + identifier() ); | 164 | config->setGroup( "Resource_" + identifier() ); |
163 | Resource::writeConfig( config ); | 165 | Resource::writeConfig( config ); |
164 | 166 | ||
165 | config->writeEntry( "FileName", fileName() ); | 167 | config->writeEntry( "FileName", fileName() ); |
166 | config->writeEntry( "FileFormat", mFormatName ); | 168 | config->writeEntry( "FileFormat", mFormatName ); |
167 | 169 | ||
168 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 170 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
169 | 171 | ||
170 | } | 172 | } |
171 | 173 | ||
172 | Ticket *ResourceFile::requestSaveTicket() | 174 | Ticket *ResourceFile::requestSaveTicket() |
173 | { | 175 | { |
174 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; | 176 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; |
175 | 177 | ||
176 | if ( !addressBook() ) return 0; | 178 | if ( !addressBook() ) return 0; |
177 | 179 | ||
178 | #ifdef ALLOW_LOCKING | 180 | #ifdef ALLOW_LOCKING |
179 | if ( !lock( mFileName ) ) { | 181 | if ( !lock( mFileName ) ) { |
180 | qDebug("unablt to lock file "); | 182 | qDebug("unablt to lock file "); |
181 | return 0; | 183 | return 0; |
182 | } | 184 | } |
183 | #endif | 185 | #endif |
184 | return createTicket( this ); | 186 | return createTicket( this ); |
185 | } | 187 | } |
186 | 188 | ||
187 | 189 | ||
188 | bool ResourceFile::doOpen() | 190 | bool ResourceFile::doOpen() |
189 | { | 191 | { |
190 | QFile file( fileName() ); | 192 | QFile file( fileName() ); |
191 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); | 193 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); |
192 | 194 | ||
193 | if ( !file.exists() ) { | 195 | if ( !file.exists() ) { |
194 | // try to create the file | 196 | // try to create the file |
195 | bool ok = file.open( IO_WriteOnly ); | 197 | bool ok = file.open( IO_WriteOnly ); |
196 | if ( ok ) | 198 | if ( ok ) |
197 | file.close(); | 199 | file.close(); |
198 | 200 | ||
199 | return ok; | 201 | return ok; |
200 | } else { | 202 | } else { |
201 | if ( !file.open( IO_ReadWrite ) ) | 203 | if ( !file.open( IO_ReadWrite ) ) |
202 | return false; | 204 | return false; |
203 | 205 | ||
204 | if ( file.size() < 10 ) { | 206 | if ( file.size() < 10 ) { |
205 | file.close(); | 207 | file.close(); |
206 | return true; | 208 | return true; |
207 | } | 209 | } |
208 | 210 | ||
209 | bool ok = mFormat->checkFormat( &file ); | 211 | bool ok = mFormat->checkFormat( &file ); |
210 | file.close(); | 212 | file.close(); |
211 | 213 | ||
212 | return ok; | 214 | return ok; |
213 | } | 215 | } |
214 | } | 216 | } |
215 | 217 | ||
216 | void ResourceFile::doClose() | 218 | void ResourceFile::doClose() |
217 | { | 219 | { |
218 | } | 220 | } |
219 | 221 | ||
220 | bool ResourceFile::load() | 222 | bool ResourceFile::load() |
221 | { | 223 | { |
222 | 224 | ||
223 | QFile file( fileName() ); | 225 | QFile file( fileName() ); |
224 | if ( !file.open( IO_ReadOnly ) ) { | 226 | if ( !file.open( IO_ReadOnly ) ) { |
225 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); | 227 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); |
226 | return false; | 228 | return false; |
227 | } | 229 | } |
228 | 230 | ||
229 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 231 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
230 | 232 | ||
231 | return mFormat->loadAll( addressBook(), this, &file ); | 233 | return mFormat->loadAll( addressBook(), this, &file ); |
232 | } | 234 | } |
233 | 235 | ||
234 | bool ResourceFile::save( Ticket *ticket ) | 236 | bool ResourceFile::save( Ticket *ticket ) |
235 | { | 237 | { |
236 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 238 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
237 | 239 | ||
238 | 240 | ||
239 | // create backup file | 241 | // create backup file |
240 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | 242 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); |
241 | 243 | ||
242 | /*US we use a simpler method to create a backupfile | 244 | /*US we use a simpler method to create a backupfile |
243 | 245 | ||
244 | (void) KSaveFile::backupFile( mFileName, QString::null | 246 | (void) KSaveFile::backupFile( mFileName, QString::null |
245 | ,extension ); | 247 | ,extension ); |
246 | 248 | ||
247 | KSaveFile saveFile( mFileName ); | 249 | KSaveFile saveFile( mFileName ); |
248 | bool ok = false; | 250 | bool ok = false; |
249 | if ( saveFile.status() == 0 && saveFile.file() ) | 251 | if ( saveFile.status() == 0 && saveFile.file() ) |
250 | { | 252 | { |
251 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | 253 | mFormat->saveAll( addressBook(), this, saveFile.file() ); |
252 | ok = saveFile.close(); | 254 | ok = saveFile.close(); |
253 | } | 255 | } |
254 | */ | 256 | */ |
255 | 257 | ||
256 | //US ToDo: write backupfile | 258 | //US ToDo: write backupfile |
257 | #ifndef NO_DIRWATCH | 259 | #ifndef NO_DIRWATCH |
258 | mDirWatch.stopScan(); | 260 | mDirWatch.stopScan(); |
259 | #endif | 261 | #endif |
262 | if ( mLastBackupDate >= 0 && mFamily != "sync_res") { | ||
263 | KConfig conf (locateLocal("config","microkdeglobalrc")); | ||
264 | conf.setGroup( "BackupSettings" ); | ||
265 | bool b_enabled = conf.readBoolEntry( "BackupEnabled" ); | ||
266 | if ( b_enabled ) { | ||
267 | int num = conf.readNumEntry( "BackupNumbers" ); | ||
268 | int d_count = conf.readNumEntry( "BackupDayCount" ); | ||
269 | bool stdDir = conf.readBoolEntry( "BackupUseDefaultDir" ); | ||
270 | QString bupDir = conf.readEntry( "BackupDatadir" ); | ||
271 | QDate reference ( 2000,1,1 ); | ||
272 | int daysTo = reference.daysTo ( QDate::currentDate() ); | ||
273 | bool saveDate = false; | ||
274 | if ( daysTo - d_count >= mLastBackupDate ) { | ||
275 | qDebug("KA: Last backup was %d days ago ", daysTo - mLastBackupDate ); | ||
276 | if ( stdDir ) | ||
277 | bupDir = KGlobalSettings::backupDataDir(); | ||
278 | int retval = KApplication::createBackup( fileName(), bupDir, num ); | ||
279 | if ( retval == 0 ) { | ||
280 | qDebug("KO: Backup cancelled. Will try again tomorrow "); | ||
281 | // retval == 0 : backup skipped for today, try again tomorrow | ||
282 | mLastBackupDate = daysTo - d_count+1; | ||
283 | saveDate = true; | ||
284 | } else if ( retval == 1 ){ | ||
285 | qDebug("KO: Backup created."); | ||
286 | // backup ok | ||
287 | mLastBackupDate = daysTo; | ||
288 | saveDate = true; | ||
289 | } else if ( retval == 2 ){ | ||
290 | qDebug("KO: Backup globally cancelled."); | ||
291 | // backup globally cancelled | ||
292 | b_enabled = false; | ||
293 | } | ||
294 | if ( !b_enabled ) { | ||
295 | conf.writeEntry( "mBackupEnabled", false ); | ||
296 | } | ||
297 | if ( saveDate ) { | ||
298 | KConfig config ( locateLocal("config","kabcrc") ); | ||
299 | config.setGroup( "Resource_" + identifier() ); | ||
300 | config.writeEntry( "LastBackupDate", mLastBackupDate ); | ||
301 | } | ||
302 | } | ||
303 | } | ||
304 | } | ||
260 | QFile info; | 305 | QFile info; |
261 | info.setName( fileName() ); | 306 | info.setName( fileName() ); |
262 | bool ok = info.open( IO_WriteOnly ); | 307 | bool ok = info.open( IO_WriteOnly ); |
263 | if ( ok ) { | 308 | if ( ok ) { |
264 | mFormat->saveAll( addressBook(), this, &info ); | 309 | mFormat->saveAll( addressBook(), this, &info ); |
265 | 310 | ||
266 | info.close(); | 311 | info.close(); |
267 | ok = true; | 312 | ok = true; |
268 | } | 313 | } |
269 | else { | 314 | else { |
270 | 315 | ||
271 | } | 316 | } |
272 | 317 | ||
273 | if ( !ok ) | 318 | if ( !ok ) |
274 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); | 319 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); |
275 | #ifndef NO_DIRWATCH | 320 | #ifndef NO_DIRWATCH |
276 | mDirWatch.startScan(); | 321 | mDirWatch.startScan(); |
277 | #endif | 322 | #endif |
278 | delete ticket; | 323 | delete ticket; |
279 | #ifdef ALLOW_LOCKING | 324 | #ifdef ALLOW_LOCKING |
280 | unlock( mFileName ); | 325 | unlock( mFileName ); |
281 | #endif | 326 | #endif |
282 | 327 | ||
283 | return ok; | 328 | return ok; |
284 | } | 329 | } |
285 | 330 | ||
286 | bool ResourceFile::lock( const QString &fileName ) | 331 | bool ResourceFile::lock( const QString &fileName ) |
287 | { | 332 | { |
288 | #ifdef ALLOW_LOCKING | 333 | #ifdef ALLOW_LOCKING |
289 | 334 | ||
290 | 335 | ||
291 | QString fn = fileName; | 336 | QString fn = fileName; |
292 | 337 | ||
293 | //US change the implementation how the lockfilename is getting created | 338 | //US change the implementation how the lockfilename is getting created |
294 | //US fn.replace( QRegExp("/"), "_" ); | 339 | //US fn.replace( QRegExp("/"), "_" ); |
295 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 340 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
296 | 341 | ||
297 | KURL url(fn); | 342 | KURL url(fn); |
298 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 343 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
299 | 344 | ||
300 | 345 | ||
301 | 346 | ||
302 | if (QFile::exists( lockName )) return false; | 347 | if (QFile::exists( lockName )) return false; |
303 | 348 | ||
304 | QString lockUniqueName; | 349 | QString lockUniqueName; |
305 | lockUniqueName = fn + KApplication::randomString( 8 ); | 350 | lockUniqueName = fn + KApplication::randomString( 8 ); |
306 | 351 | ||
307 | url = lockUniqueName; | 352 | url = lockUniqueName; |
308 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 353 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
309 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 354 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
310 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 355 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
311 | 356 | ||
312 | // Create unique file | 357 | // Create unique file |
313 | QFile file( mLockUniqueName ); | 358 | QFile file( mLockUniqueName ); |
314 | file.open( IO_WriteOnly ); | 359 | file.open( IO_WriteOnly ); |
315 | file.close(); | 360 | file.close(); |
316 | 361 | ||
317 | // Create lock file | 362 | // Create lock file |
318 | int result = 0; | 363 | int result = 0; |
319 | #ifndef _WIN32_ | 364 | #ifndef _WIN32_ |
320 | result = ::link( QFile::encodeName( mLockUniqueName ), | 365 | result = ::link( QFile::encodeName( mLockUniqueName ), |
321 | QFile::encodeName( lockName ) ); | 366 | QFile::encodeName( lockName ) ); |
322 | #endif | 367 | #endif |
323 | if ( result == 0 ) { | 368 | if ( result == 0 ) { |
324 | addressBook()->emitAddressBookLocked(); | 369 | addressBook()->emitAddressBookLocked(); |
325 | return true; | 370 | return true; |
326 | } | 371 | } |
327 | 372 | ||
328 | // TODO: check stat | 373 | // TODO: check stat |
329 | 374 | ||
330 | return false; | 375 | return false; |
331 | #else | 376 | #else |
332 | return true; | 377 | return true; |
333 | #endif | 378 | #endif |
334 | } | 379 | } |
335 | 380 | ||
336 | void ResourceFile::unlock( const QString &fileName ) | 381 | void ResourceFile::unlock( const QString &fileName ) |
337 | { | 382 | { |
338 | #ifdef ALLOW_LOCKING | 383 | #ifdef ALLOW_LOCKING |
339 | QString fn = fileName; | 384 | QString fn = fileName; |
340 | //US change the implementation how the lockfilename is getting created | 385 | //US change the implementation how the lockfilename is getting created |
341 | //US fn.replace( QRegExp( "/" ), "_" ); | 386 | //US fn.replace( QRegExp( "/" ), "_" ); |
342 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 387 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
343 | //US QString lockName = fn + ".lock"; | 388 | //US QString lockName = fn + ".lock"; |
344 | KURL url(fn); | 389 | KURL url(fn); |
345 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 390 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
346 | 391 | ||
347 | QFile::remove( lockName ); | 392 | QFile::remove( lockName ); |
348 | QFile::remove( mLockUniqueName ); | 393 | QFile::remove( mLockUniqueName ); |
349 | addressBook()->emitAddressBookUnlocked(); | 394 | addressBook()->emitAddressBookUnlocked(); |
350 | #else | 395 | #else |
351 | return; | 396 | return; |
352 | #endif | 397 | #endif |
353 | } | 398 | } |
354 | 399 | ||
355 | void ResourceFile::setFileName( const QString &fileName ) | 400 | void ResourceFile::setFileName( const QString &fileName ) |
356 | { | 401 | { |
357 | #ifndef NO_DIRWATCH | 402 | #ifndef NO_DIRWATCH |
358 | mDirWatch.stopScan(); | 403 | mDirWatch.stopScan(); |
359 | mDirWatch.removeFile( mFileName ); | 404 | mDirWatch.removeFile( mFileName ); |
360 | mFileName = fileName; | 405 | mFileName = fileName; |
361 | 406 | ||
362 | 407 | ||
363 | mDirWatch.addFile( mFileName ); | 408 | mDirWatch.addFile( mFileName ); |
364 | mDirWatch.startScan(); | 409 | mDirWatch.startScan(); |
365 | #else | 410 | #else |
366 | mFileName2 = fileName; | 411 | mFileName2 = fileName; |
367 | #endif | 412 | #endif |
368 | 413 | ||
369 | //US simulate KDirWatch event | 414 | //US simulate KDirWatch event |
370 | //US fileChanged(); | 415 | //US fileChanged(); |
371 | } | 416 | } |
372 | 417 | ||
373 | QString ResourceFile::fileName() const | 418 | QString ResourceFile::fileName() const |
374 | { | 419 | { |
375 | return mFileName2; | 420 | return mFileName2; |
376 | } | 421 | } |
377 | 422 | ||
378 | void ResourceFile::setFormat( const QString &format ) | 423 | void ResourceFile::setFormat( const QString &format ) |
379 | { | 424 | { |
380 | mFormatName = format; | 425 | mFormatName = format; |
381 | delete mFormat; | 426 | delete mFormat; |
382 | 427 | ||
383 | FormatFactory *factory = FormatFactory::self(); | 428 | FormatFactory *factory = FormatFactory::self(); |
384 | mFormat = factory->format( mFormatName ); | 429 | mFormat = factory->format( mFormatName ); |
385 | /*US | 430 | /*US |
386 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); | 431 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); |
387 | if (mFormatName == "vcard") { | 432 | if (mFormatName == "vcard") { |
388 | mFormat = new VCardFormatPlugin2(); | 433 | mFormat = new VCardFormatPlugin2(); |
389 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | 434 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); |
390 | } | 435 | } |
391 | else if (mFormatName == "binary") { | 436 | else if (mFormatName == "binary") { |
392 | mFormat = new BinaryFormat(); | 437 | mFormat = new BinaryFormat(); |
393 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | 438 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); |
394 | } | 439 | } |
395 | else | 440 | else |
396 | qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); | 441 | qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); |
397 | */ | 442 | */ |
398 | 443 | ||
399 | } | 444 | } |
400 | 445 | ||
401 | QString ResourceFile::format() const | 446 | QString ResourceFile::format() const |
402 | { | 447 | { |
403 | return mFormatName; | 448 | return mFormatName; |
404 | } | 449 | } |
405 | 450 | ||
406 | void ResourceFile::fileChanged() | 451 | void ResourceFile::fileChanged() |
407 | { | 452 | { |
408 | // There is a small theoretical chance that KDirWatch calls us before | 453 | // There is a small theoretical chance that KDirWatch calls us before |
409 | // we are fully constructed | 454 | // we are fully constructed |
410 | if (!addressBook()) | 455 | if (!addressBook()) |
411 | return; | 456 | return; |
412 | 457 | ||
413 | 458 | ||
414 | QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 459 | QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
415 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 460 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
416 | load(); | 461 | load(); |
417 | addressBook()->emitAddressBookChanged(); | 462 | addressBook()->emitAddressBookChanged(); |
418 | } | 463 | } |
419 | } | 464 | } |
420 | 465 | ||
421 | void ResourceFile::removeAddressee( const Addressee &addr ) | 466 | void ResourceFile::removeAddressee( const Addressee &addr ) |
422 | { | 467 | { |
423 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); | 468 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); |
424 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); | 469 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); |
425 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); | 470 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); |
426 | } | 471 | } |
427 | 472 | ||
428 | void ResourceFile::cleanUp() | 473 | void ResourceFile::cleanUp() |
429 | { | 474 | { |
430 | unlock( fileName() ); | 475 | unlock( fileName() ); |
431 | } | 476 | } |
432 | 477 | ||
433 | //US #include "resourcefile.moc" | 478 | //US #include "resourcefile.moc" |
diff --git a/kabc/plugins/file/resourcefile.h b/kabc/plugins/file/resourcefile.h index 3e9edfc..61da154 100644 --- a/kabc/plugins/file/resourcefile.h +++ b/kabc/plugins/file/resourcefile.h | |||
@@ -1,164 +1,165 @@ | |||
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 | 28 | ||
29 | #ifndef KABC_RESOURCEFILE_H | 29 | #ifndef KABC_RESOURCEFILE_H |
30 | #define KABC_RESOURCEFILE_H | 30 | #define KABC_RESOURCEFILE_H |
31 | 31 | ||
32 | #include <kconfig.h> | 32 | #include <kconfig.h> |
33 | #include <kdirwatch.h> | 33 | #include <kdirwatch.h> |
34 | 34 | ||
35 | #include <sys/types.h> | 35 | #include <sys/types.h> |
36 | 36 | ||
37 | #include <resource.h> | 37 | #include <resource.h> |
38 | 38 | ||
39 | class QTimer; | 39 | class QTimer; |
40 | class FormatPlugin; | 40 | class FormatPlugin; |
41 | 41 | ||
42 | namespace KABC { | 42 | namespace KABC { |
43 | 43 | ||
44 | //US class FormatPlugin; | 44 | //US class FormatPlugin; |
45 | class ResourceConfigWidget; | 45 | class ResourceConfigWidget; |
46 | 46 | ||
47 | /** | 47 | /** |
48 | @internal | 48 | @internal |
49 | */ | 49 | */ |
50 | class ResourceFile : public Resource | 50 | class ResourceFile : public Resource |
51 | { | 51 | { |
52 | Q_OBJECT | 52 | Q_OBJECT |
53 | 53 | ||
54 | public: | 54 | public: |
55 | 55 | ||
56 | /** | 56 | /** |
57 | Constructor. | 57 | Constructor. |
58 | 58 | ||
59 | @param cfg The config object where custom resource settings are stored. | 59 | @param cfg The config object where custom resource settings are stored. |
60 | */ | 60 | */ |
61 | ResourceFile( const KConfig *cfg ); | 61 | ResourceFile( const KConfig *cfg ); |
62 | 62 | ||
63 | /** | 63 | /** |
64 | Construct file resource on file @arg fileName using format @arg formatName. | 64 | Construct file resource on file @arg fileName using format @arg formatName. |
65 | */ | 65 | */ |
66 | ResourceFile( const QString &fileName , const QString &formatName = "vcard" ); | 66 | ResourceFile( const QString &fileName , const QString &formatName = "vcard" ); |
67 | 67 | ||
68 | /** | 68 | /** |
69 | * Destructor. | 69 | * Destructor. |
70 | */ | 70 | */ |
71 | ~ResourceFile(); | 71 | ~ResourceFile(); |
72 | 72 | ||
73 | /** | 73 | /** |
74 | Writes the config back. | 74 | Writes the config back. |
75 | */ | 75 | */ |
76 | virtual void writeConfig( KConfig *cfg ); | 76 | virtual void writeConfig( KConfig *cfg ); |
77 | 77 | ||
78 | /** | 78 | /** |
79 | * Tries to open the file and checks for the proper format. | 79 | * Tries to open the file and checks for the proper format. |
80 | * This method should be called before @ref load(). | 80 | * This method should be called before @ref load(). |
81 | */ | 81 | */ |
82 | virtual bool doOpen(); | 82 | virtual bool doOpen(); |
83 | 83 | ||
84 | /** | 84 | /** |
85 | * Closes the file again. | 85 | * Closes the file again. |
86 | */ | 86 | */ |
87 | virtual void doClose(); | 87 | virtual void doClose(); |
88 | 88 | ||
89 | /** | 89 | /** |
90 | * Requests a save ticket, that is used by @ref save() | 90 | * Requests a save ticket, that is used by @ref save() |
91 | */ | 91 | */ |
92 | virtual Ticket *requestSaveTicket(); | 92 | virtual Ticket *requestSaveTicket(); |
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Loads all addressees from file to the address book. | 95 | * Loads all addressees from file to the address book. |
96 | * Returns true if all addressees could be loaded otherwise false. | 96 | * Returns true if all addressees could be loaded otherwise false. |
97 | */ | 97 | */ |
98 | virtual bool load(); | 98 | virtual bool load(); |
99 | 99 | ||
100 | /** | 100 | /** |
101 | * Saves all addresses from address book to file. | 101 | * Saves all addresses from address book to file. |
102 | * Returns true if all addressees could be saved otherwise false. | 102 | * Returns true if all addressees could be saved otherwise false. |
103 | * | 103 | * |
104 | * @param ticket The ticket returned by @ref requestSaveTicket() | 104 | * @param ticket The ticket returned by @ref requestSaveTicket() |
105 | */ | 105 | */ |
106 | virtual bool save( Ticket *ticket ); | 106 | virtual bool save( Ticket *ticket ); |
107 | 107 | ||
108 | /** | 108 | /** |
109 | * Set name of file to be used for saving. | 109 | * Set name of file to be used for saving. |
110 | */ | 110 | */ |
111 | void setFileName( const QString & ); | 111 | void setFileName( const QString & ); |
112 | 112 | ||
113 | /** | 113 | /** |
114 | * Return name of file used for loading and saving the address book. | 114 | * Return name of file used for loading and saving the address book. |
115 | */ | 115 | */ |
116 | QString fileName() const; | 116 | QString fileName() const; |
117 | 117 | ||
118 | /** | 118 | /** |
119 | Sets a new format by name. | 119 | Sets a new format by name. |
120 | */ | 120 | */ |
121 | void setFormat( const QString &name ); | 121 | void setFormat( const QString &name ); |
122 | 122 | ||
123 | /** | 123 | /** |
124 | Returns the format name. | 124 | Returns the format name. |
125 | */ | 125 | */ |
126 | QString format() const; | 126 | QString format() const; |
127 | 127 | ||
128 | /** | 128 | /** |
129 | * Remove a addressee from its source. | 129 | * Remove a addressee from its source. |
130 | * This method is mainly called by KABC::AddressBook. | 130 | * This method is mainly called by KABC::AddressBook. |
131 | */ | 131 | */ |
132 | virtual void removeAddressee( const Addressee& addr ); | 132 | virtual void removeAddressee( const Addressee& addr ); |
133 | 133 | ||
134 | /** | 134 | /** |
135 | * This method is called by an error handler if the application | 135 | * This method is called by an error handler if the application |
136 | * crashed | 136 | * crashed |
137 | */ | 137 | */ |
138 | virtual void cleanUp(); | 138 | virtual void cleanUp(); |
139 | 139 | ||
140 | protected slots: | 140 | protected slots: |
141 | void fileChanged(); | 141 | void fileChanged(); |
142 | 142 | ||
143 | protected: | 143 | protected: |
144 | void init( const QString &fileName, const QString &format ); | 144 | void init( const QString &fileName, const QString &format ); |
145 | 145 | ||
146 | bool lock( const QString &fileName ); | 146 | bool lock( const QString &fileName ); |
147 | void unlock( const QString &fileName ); | 147 | void unlock( const QString &fileName ); |
148 | 148 | ||
149 | private: | 149 | private: |
150 | int mLastBackupDate; | ||
150 | QString mFamily; | 151 | QString mFamily; |
151 | QString mFileName2; | 152 | QString mFileName2; |
152 | QString mFormatName; | 153 | QString mFormatName; |
153 | 154 | ||
154 | FormatPlugin *mFormat; | 155 | FormatPlugin *mFormat; |
155 | 156 | ||
156 | QString mLockUniqueName; | 157 | QString mLockUniqueName; |
157 | #ifndef NO_DIRWATCH | 158 | #ifndef NO_DIRWATCH |
158 | KDirWatch mDirWatch; | 159 | KDirWatch mDirWatch; |
159 | #endif | 160 | #endif |
160 | }; | 161 | }; |
161 | 162 | ||
162 | } | 163 | } |
163 | 164 | ||
164 | #endif | 165 | #endif |