-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp index af76558..2bd9e71 100644 --- a/kabc/plugins/file/resourcefile.cpp +++ b/kabc/plugins/file/resourcefile.cpp | |||
@@ -76,257 +76,257 @@ __declspec(dllexport) | |||
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 | mFamily = cfg->readEntry( "ResourceName", "std" ); | 87 | mFamily = cfg->readEntry( "ResourceName", "std" ); |
88 | } else { | 88 | } else { |
89 | fileName = default_fileName; | 89 | fileName = default_fileName; |
90 | formatName = "vcard"; | 90 | formatName = "vcard"; |
91 | } | 91 | } |
92 | 92 | ||
93 | init( fileName, formatName ); | 93 | init( fileName, formatName ); |
94 | } | 94 | } |
95 | 95 | ||
96 | ResourceFile::ResourceFile( const QString &fileName , | 96 | ResourceFile::ResourceFile( const QString &fileName , |
97 | const QString &formatName ) | 97 | const QString &formatName ) |
98 | : Resource( 0 ) | 98 | : Resource( 0 ) |
99 | { | 99 | { |
100 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | 100 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); |
101 | 101 | ||
102 | 102 | ||
103 | 103 | ||
104 | init( fileName, formatName ); | 104 | init( fileName, formatName ); |
105 | } | 105 | } |
106 | 106 | ||
107 | void ResourceFile::init( const QString &fileName, const QString &formatName ) | 107 | void ResourceFile::init( const QString &fileName, const QString &formatName ) |
108 | { | 108 | { |
109 | mFormatName = formatName; | 109 | mFormatName = formatName; |
110 | 110 | ||
111 | FormatFactory *factory = FormatFactory::self(); | 111 | FormatFactory *factory = FormatFactory::self(); |
112 | mFormat = factory->format( mFormatName ); | 112 | mFormat = factory->format( mFormatName ); |
113 | 113 | ||
114 | if ( !mFormat ) { | 114 | if ( !mFormat ) { |
115 | mFormatName = "vcard"; | 115 | mFormatName = "vcard"; |
116 | mFormat = factory->format( mFormatName ); | 116 | mFormat = factory->format( mFormatName ); |
117 | } | 117 | } |
118 | 118 | ||
119 | #ifndef NO_DIRWATCH | 119 | #ifndef NO_DIRWATCH |
120 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 120 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
121 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 121 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
122 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 122 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
123 | #endif | 123 | #endif |
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 | QFileInfo fi ( fileName ); | 129 | QFileInfo fi ( fileName ); |
130 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); | 130 | QString localname = localKdeDir + "/apps/kabc/" + fi.fileName (); |
131 | QFileInfo fi2 ( localname ); | 131 | QFileInfo fi2 ( localname ); |
132 | if ( ! fi2.exists() || mFamily == "sync_res" ) { | 132 | if ( ! fi2.exists() || mFamily == "sync_res" ) { |
133 | if ( fi.exists() && mFamily == "sync_res") { | 133 | if ( fi.exists() && mFamily == "sync_res") { |
134 | qDebug("LOCAL mode SYNC mode using absolute file path "); | 134 | qDebug("LOCAL mode SYNC mode using absolute file path "); |
135 | setFileName( fileName ); | 135 | setFileName( fileName ); |
136 | return; | 136 | return; |
137 | } else { | 137 | } 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/") ); | 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/") ); |
139 | setFileName( localname ); | 139 | setFileName( localname ); |
140 | return; | 140 | return; |
141 | } | 141 | } |
142 | 142 | ||
143 | } else { | 143 | } else { |
144 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); | 144 | qDebug("Local resource file found. Changing filename to: %s",localname.latin1() ); |
145 | setFileName( localname ); | 145 | setFileName( localname ); |
146 | return; | 146 | return; |
147 | } | 147 | } |
148 | 148 | ||
149 | } | 149 | } |
150 | setFileName( fileName ); | 150 | setFileName( fileName ); |
151 | } | 151 | } |
152 | 152 | ||
153 | ResourceFile::~ResourceFile() | 153 | ResourceFile::~ResourceFile() |
154 | { | 154 | { |
155 | delete mFormat; | 155 | delete mFormat; |
156 | mFormat = 0; | 156 | mFormat = 0; |
157 | } | 157 | } |
158 | 158 | ||
159 | void ResourceFile::writeConfig( KConfig *config ) | 159 | void ResourceFile::writeConfig( KConfig *config ) |
160 | { | 160 | { |
161 | 161 | ||
162 | config->setGroup( "Resource_" + identifier() ); | 162 | config->setGroup( "Resource_" + identifier() ); |
163 | Resource::writeConfig( config ); | 163 | Resource::writeConfig( config ); |
164 | 164 | ||
165 | config->writeEntry( "FileName", fileName() ); | 165 | config->writeEntry( "FileName", fileName() ); |
166 | config->writeEntry( "FileFormat", mFormatName ); | 166 | config->writeEntry( "FileFormat", mFormatName ); |
167 | 167 | ||
168 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 168 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
169 | 169 | ||
170 | } | 170 | } |
171 | 171 | ||
172 | Ticket *ResourceFile::requestSaveTicket() | 172 | Ticket *ResourceFile::requestSaveTicket() |
173 | { | 173 | { |
174 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; | 174 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; |
175 | 175 | ||
176 | if ( !addressBook() ) return 0; | 176 | if ( !addressBook() ) return 0; |
177 | 177 | ||
178 | #ifdef ALLOW_LOCKING | 178 | #ifdef ALLOW_LOCKING |
179 | if ( !lock( mFileName ) ) { | 179 | if ( !lock( mFileName ) ) { |
180 | qDebug("unablt to lock file "); | 180 | qDebug("unablt to lock file "); |
181 | return 0; | 181 | return 0; |
182 | } | 182 | } |
183 | #endif | 183 | #endif |
184 | return createTicket( this ); | 184 | return createTicket( this ); |
185 | } | 185 | } |
186 | 186 | ||
187 | 187 | ||
188 | bool ResourceFile::doOpen() | 188 | bool ResourceFile::doOpen() |
189 | { | 189 | { |
190 | QFile file( fileName() ); | 190 | QFile file( fileName() ); |
191 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); | 191 | qDebug("ResourceFile::openfile %s ", fileName().latin1()); |
192 | 192 | ||
193 | if ( !file.exists() ) { | 193 | if ( !file.exists() ) { |
194 | // try to create the file | 194 | // try to create the file |
195 | bool ok = file.open( IO_WriteOnly ); | 195 | bool ok = file.open( IO_WriteOnly ); |
196 | if ( ok ) | 196 | if ( ok ) |
197 | file.close(); | 197 | file.close(); |
198 | 198 | ||
199 | return ok; | 199 | return ok; |
200 | } else { | 200 | } else { |
201 | if ( !file.open( IO_ReadWrite ) ) | 201 | if ( !file.open( IO_ReadWrite ) ) |
202 | return false; | 202 | return false; |
203 | 203 | ||
204 | if ( file.size() == 0 ) { | 204 | if ( file.size() < 10 ) { |
205 | file.close(); | 205 | file.close(); |
206 | return true; | 206 | return true; |
207 | } | 207 | } |
208 | 208 | ||
209 | bool ok = mFormat->checkFormat( &file ); | 209 | bool ok = mFormat->checkFormat( &file ); |
210 | file.close(); | 210 | file.close(); |
211 | 211 | ||
212 | return ok; | 212 | return ok; |
213 | } | 213 | } |
214 | } | 214 | } |
215 | 215 | ||
216 | void ResourceFile::doClose() | 216 | void ResourceFile::doClose() |
217 | { | 217 | { |
218 | } | 218 | } |
219 | 219 | ||
220 | bool ResourceFile::load() | 220 | bool ResourceFile::load() |
221 | { | 221 | { |
222 | 222 | ||
223 | QFile file( fileName() ); | 223 | QFile file( fileName() ); |
224 | if ( !file.open( IO_ReadOnly ) ) { | 224 | if ( !file.open( IO_ReadOnly ) ) { |
225 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); | 225 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( fileName() ) ); |
226 | return false; | 226 | return false; |
227 | } | 227 | } |
228 | 228 | ||
229 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 229 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
230 | 230 | ||
231 | return mFormat->loadAll( addressBook(), this, &file ); | 231 | return mFormat->loadAll( addressBook(), this, &file ); |
232 | } | 232 | } |
233 | 233 | ||
234 | bool ResourceFile::save( Ticket *ticket ) | 234 | bool ResourceFile::save( Ticket *ticket ) |
235 | { | 235 | { |
236 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | 236 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); |
237 | 237 | ||
238 | 238 | ||
239 | // create backup file | 239 | // create backup file |
240 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | 240 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); |
241 | 241 | ||
242 | /*US we use a simpler method to create a backupfile | 242 | /*US we use a simpler method to create a backupfile |
243 | 243 | ||
244 | (void) KSaveFile::backupFile( mFileName, QString::null | 244 | (void) KSaveFile::backupFile( mFileName, QString::null |
245 | ,extension ); | 245 | ,extension ); |
246 | 246 | ||
247 | KSaveFile saveFile( mFileName ); | 247 | KSaveFile saveFile( mFileName ); |
248 | bool ok = false; | 248 | bool ok = false; |
249 | if ( saveFile.status() == 0 && saveFile.file() ) | 249 | if ( saveFile.status() == 0 && saveFile.file() ) |
250 | { | 250 | { |
251 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | 251 | mFormat->saveAll( addressBook(), this, saveFile.file() ); |
252 | ok = saveFile.close(); | 252 | ok = saveFile.close(); |
253 | } | 253 | } |
254 | */ | 254 | */ |
255 | 255 | ||
256 | //US ToDo: write backupfile | 256 | //US ToDo: write backupfile |
257 | #ifndef NO_DIRWATCH | 257 | #ifndef NO_DIRWATCH |
258 | mDirWatch.stopScan(); | 258 | mDirWatch.stopScan(); |
259 | #endif | 259 | #endif |
260 | QFile info; | 260 | QFile info; |
261 | info.setName( fileName() ); | 261 | info.setName( fileName() ); |
262 | bool ok = info.open( IO_WriteOnly ); | 262 | bool ok = info.open( IO_WriteOnly ); |
263 | if ( ok ) { | 263 | if ( ok ) { |
264 | mFormat->saveAll( addressBook(), this, &info ); | 264 | mFormat->saveAll( addressBook(), this, &info ); |
265 | 265 | ||
266 | info.close(); | 266 | info.close(); |
267 | ok = true; | 267 | ok = true; |
268 | } | 268 | } |
269 | else { | 269 | else { |
270 | 270 | ||
271 | } | 271 | } |
272 | 272 | ||
273 | if ( !ok ) | 273 | if ( !ok ) |
274 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); | 274 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( fileName() ) ); |
275 | #ifndef NO_DIRWATCH | 275 | #ifndef NO_DIRWATCH |
276 | mDirWatch.startScan(); | 276 | mDirWatch.startScan(); |
277 | #endif | 277 | #endif |
278 | delete ticket; | 278 | delete ticket; |
279 | #ifdef ALLOW_LOCKING | 279 | #ifdef ALLOW_LOCKING |
280 | unlock( mFileName ); | 280 | unlock( mFileName ); |
281 | #endif | 281 | #endif |
282 | 282 | ||
283 | return ok; | 283 | return ok; |
284 | } | 284 | } |
285 | 285 | ||
286 | bool ResourceFile::lock( const QString &fileName ) | 286 | bool ResourceFile::lock( const QString &fileName ) |
287 | { | 287 | { |
288 | #ifdef ALLOW_LOCKING | 288 | #ifdef ALLOW_LOCKING |
289 | 289 | ||
290 | 290 | ||
291 | QString fn = fileName; | 291 | QString fn = fileName; |
292 | 292 | ||
293 | //US change the implementation how the lockfilename is getting created | 293 | //US change the implementation how the lockfilename is getting created |
294 | //US fn.replace( QRegExp("/"), "_" ); | 294 | //US fn.replace( QRegExp("/"), "_" ); |
295 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | 295 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); |
296 | 296 | ||
297 | KURL url(fn); | 297 | KURL url(fn); |
298 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 298 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
299 | 299 | ||
300 | 300 | ||
301 | 301 | ||
302 | if (QFile::exists( lockName )) return false; | 302 | if (QFile::exists( lockName )) return false; |
303 | 303 | ||
304 | QString lockUniqueName; | 304 | QString lockUniqueName; |
305 | lockUniqueName = fn + KApplication::randomString( 8 ); | 305 | lockUniqueName = fn + KApplication::randomString( 8 ); |
306 | 306 | ||
307 | url = lockUniqueName; | 307 | url = lockUniqueName; |
308 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 308 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
309 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 309 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
310 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 310 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
311 | 311 | ||
312 | // Create unique file | 312 | // Create unique file |
313 | QFile file( mLockUniqueName ); | 313 | QFile file( mLockUniqueName ); |
314 | file.open( IO_WriteOnly ); | 314 | file.open( IO_WriteOnly ); |
315 | file.close(); | 315 | file.close(); |
316 | 316 | ||
317 | // Create lock file | 317 | // Create lock file |
318 | int result = 0; | 318 | int result = 0; |
319 | #ifndef _WIN32_ | 319 | #ifndef _WIN32_ |
320 | result = ::link( QFile::encodeName( mLockUniqueName ), | 320 | result = ::link( QFile::encodeName( mLockUniqueName ), |
321 | QFile::encodeName( lockName ) ); | 321 | QFile::encodeName( lockName ) ); |
322 | #endif | 322 | #endif |
323 | if ( result == 0 ) { | 323 | if ( result == 0 ) { |
324 | addressBook()->emitAddressBookLocked(); | 324 | addressBook()->emitAddressBookLocked(); |
325 | return true; | 325 | return true; |
326 | } | 326 | } |
327 | 327 | ||
328 | // TODO: check stat | 328 | // TODO: check stat |
329 | 329 | ||
330 | return false; | 330 | return false; |
331 | #else | 331 | #else |
332 | return true; | 332 | return true; |