24 #include <sys/types.h>
29 #include <tqfileinfo.h>
32 #include <kapplication.h>
35 #include <kio/scheduler.h>
37 #include <ksavefile.h>
38 #include <kstandarddirs.h>
40 #include "formatfactory.h"
41 #include "resourcefileconfig.h"
42 #include "stdaddressbook.h"
45 #include "resourcefile.h"
50 : Resource( config ), mFormat( 0 ),
51 mAsynchronous( false )
57 formatName = config->
readEntry(
"FileFormat",
"vcard" );
63 init( fileName, formatName );
67 const TQString &formatName )
68 : Resource( 0 ), mFormat( 0 ),
69 mAsynchronous( false )
71 init( fileName, formatName );
74 void ResourceFile::init(
const TQString &fileName,
const TQString &formatName )
76 mFormatName = formatName;
79 mFormat = factory->
format( mFormatName );
82 mFormatName =
"vcard";
83 mFormat = factory->
format( mFormatName );
86 connect( &mDirWatch, TQT_SIGNAL( dirty(
const TQString&) ), TQT_SLOT( fileChanged() ) );
87 connect( &mDirWatch, TQT_SIGNAL( created(
const TQString&) ), TQT_SLOT( fileChanged() ) );
88 connect( &mDirWatch, TQT_SIGNAL( deleted(
const TQString&) ), TQT_SLOT( fileChanged() ) );
103 Resource::writeConfig( config );
110 config->
writeEntry(
"FileFormat", mFormatName );
115 kdDebug(5700) <<
"ResourceFile::requestSaveTicket()" <<
endl;
117 if ( !addressBook() )
return 0;
120 mLock =
new Lock( mFileName );
122 if ( mLock->
lock() ) {
123 addressBook()->emitAddressBookLocked();
125 addressBook()->error( mLock->error() );
126 kdDebug(5700) <<
"ResourceFile::requestSaveTicket(): Unable to lock file '"
127 << mFileName <<
"': " << mLock->error() <<
endl;
131 return createTicket(
this );
134 void ResourceFile::releaseSaveTicket(
Ticket *ticket )
141 addressBook()->emitAddressBookUnlocked();
146 TQFile file( mFileName );
148 if ( !file.exists() ) {
150 bool ok = file.open( IO_WriteOnly );
156 TQFileInfo fileInfo( mFileName );
157 if ( readOnly() || !fileInfo.isWritable() ) {
158 if ( !file.open( IO_ReadOnly ) )
161 if ( !file.open( IO_ReadWrite ) )
165 if ( file.size() == 0 ) {
167 kdDebug() <<
"File size is zero. Evaluating backups" <<
endl;
168 for (
int i=0; i!=20; i++)
170 TQFile backup( mFileName +
"__" + TQString::number(i) );
171 kdDebug() <<
"Evaluating" << backup.name() <<
" size: " << backup.size() <<
endl;
172 if ( backup.size() != 0 )
175 const TQString src = mFileName +
"__" + TQString::number(i);
176 const TQString dest = mFileName;
179 TQFile::remove( dest );
182 if ( backup.open( IO_ReadOnly ) ) {
183 const TQByteArray data = backup.readAll();
186 if ( out.open( IO_WriteOnly ) ) {
187 out.writeBlock( data );
212 kdDebug(5700) <<
"ResourceFile::load(): '" << mFileName <<
"'" <<
endl;
214 mAsynchronous =
false;
216 TQFile file( mFileName );
217 if ( !file.open( IO_ReadOnly ) ) {
218 addressBook()->error( i18n(
"Unable to open file '%1'." ).arg( mFileName ) );
224 return mFormat->
loadAll( addressBook(),
this, &file );
227 bool ResourceFile::asyncLoad()
229 kdDebug(5700) <<
"ResourceFile::asyncLoad()" <<
endl;
231 mAsynchronous =
true;
238 emitLoadingFinished();
248 TQFile file( mFileName +
"__0" );
249 if ( file.size() != 0 ) {
250 const TQString last = mFileName +
"__20";
253 TQFile::remove( last );
255 for (
int i=19; i>=0; i--)
257 const TQString src = mFileName +
"__" + TQString::number(i);
258 const TQString dest = mFileName +
"__" + TQString::number(i+1);
259 kdDebug() <<
"moving " << src <<
" -> " << dest <<
endl;
263 if ( in.open( IO_ReadOnly ) ) {
264 const TQByteArray data = in.readAll();
267 if ( out.open( IO_WriteOnly ) ) {
268 out.writeBlock( data );
276 TQFile::remove( src );
279 kdDebug() <<
"Not starting logrotate __0 is 0 bytes." <<
endl;
281 TQString extension =
"__0";
285 mDirWatch.stopScan();
290 if ( saveFile.
status() == 0 && saveFile.
file() ) {
291 mFormat->
saveAll( addressBook(),
this, saveFile.
file() );
292 ok = saveFile.
close();
297 addressBook()->error( i18n(
"Unable to save file '%1'." ).arg( mFileName ) );
300 mDirWatch.startScan();
305 bool ResourceFile::asyncSave(
Ticket *ticket )
307 kdDebug(5700) <<
"ResourceFile::asyncSave()" <<
endl;
309 bool ok =
save( ticket );
312 TQTimer::singleShot( 0,
this, TQT_SLOT( emitSavingError() ) );
314 TQTimer::singleShot( 0,
this, TQT_SLOT( emitSavingFinished() ) );
321 mDirWatch.stopScan();
322 if ( mDirWatch.contains( mFileName ) )
323 mDirWatch.removeFile( mFileName );
327 mDirWatch.addFile( mFileName );
328 mDirWatch.startScan();
342 mFormat = factory->
format( mFormatName );
350 void ResourceFile::fileChanged()
352 kdDebug(5700) <<
"ResourceFile::fileChanged(): " << mFileName <<
endl;
354 if ( !addressBook() )
362 addressBook()->emitAddressBookChanged();
368 TQFile::remove( TQFile::encodeName(
locateLocal(
"data",
"kabc/photos/" ) + addr.
uid() ) );
369 TQFile::remove( TQFile::encodeName(
locateLocal(
"data",
"kabc/logos/" ) + addr.
uid() ) );
370 TQFile::remove( TQFile::encodeName(
locateLocal(
"data",
"kabc/sounds/" ) + addr.
uid() ) );
372 mAddrMap.erase( addr.
uid() );
375 void ResourceFile::emitSavingFinished()
377 emit savingFinished(
this );
380 void ResourceFile::emitSavingError()
382 emit savingError(
this, i18n(
"Unable to save file '%1'." ).arg( mFileName ) );
385 void ResourceFile::emitLoadingFinished()
387 emit loadingFinished(
this );
390 void ResourceFile::emitLoadingError()
392 emit loadingError(
this, i18n(
"Problems during parsing file '%1'." ).arg( mFileName ) );
395 #include "resourcefile.moc"