24 #include <sys/types.h>
29 #include <tqfileinfo.h>
32 #include <tdeapplication.h>
33 #include <tdeconfig.h>
35 #include <tdeio/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 );
106 config->deleteEntry(
"FileName" );
108 config->writePathEntry(
"FileName", mFileName );
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(5700) <<
"File size is zero. Evaluating backups" <<
endl;
168 for (
int i=0; i!=20; i++)
170 TQFile backup( mFileName +
"__" + TQString::number(i) );
171 kdDebug(5700) <<
"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 if ( ! backup.open( IO_ReadOnly ) ) {
181 kdDebug(5700) <<
"can not open source for reading " << src <<
endl;
186 TQFile::remove( dest );
189 TQTextStream instream( &backup );
190 instream.setEncoding( TQTextStream::UnicodeUTF8 );
191 text = instream.read();
195 if ( ! out.open( IO_WriteOnly ) ) {
197 kdDebug(5700) <<
"can not open target for writing " << dest <<
endl;
200 TQTextStream outstream( &out );
201 outstream.setEncoding( TQTextStream::UnicodeUTF8 );
224 kdDebug(5700) <<
"ResourceFile::load(): '" << mFileName <<
"'" <<
endl;
226 mAsynchronous =
false;
228 TQFile file( mFileName );
229 if ( !file.open( IO_ReadOnly ) ) {
230 addressBook()->error( i18n(
"Unable to open file '%1'." ).arg( mFileName ) );
236 return mFormat->
loadAll( addressBook(),
this, &file );
239 bool ResourceFile::asyncLoad()
241 kdDebug(5700) <<
"ResourceFile::asyncLoad()" <<
endl;
243 mAsynchronous =
true;
250 emitLoadingFinished();
260 TQFile file( mFileName +
"__0" );
261 if ( file.size() != 0 ) {
262 const TQString last = mFileName +
"__20";
265 TQFile::remove( last );
267 for (
int i=19; i>=0; i--)
269 const TQString src = mFileName +
"__" + TQString::number(i);
270 const TQString dest = mFileName +
"__" + TQString::number(i+1);
271 kdDebug() <<
"moving " << src <<
" -> " << dest <<
endl;
277 if ( ! in.open( IO_ReadOnly ) ) {
279 kdDebug(5700) <<
"can not open source for reading " << src <<
endl;
283 TQTextStream instream( &in );
285 instream.setEncoding( TQTextStream::UnicodeUTF8 );
286 text = instream.read();
290 if ( ! out.open( IO_WriteOnly ) ) {
292 kdDebug(5700) <<
"can not open target for writing " << dest <<
endl;
295 TQTextStream outstream( &out );
296 outstream.setEncoding( TQTextStream::UnicodeUTF8 );
301 TQFile::remove( src );
304 kdDebug(5700) <<
"Not starting logrotate __0 is 0 bytes." <<
endl;
306 TQString extension =
"__0";
309 mDirWatch.stopScan();
314 if ( saveFile.
status() == 0 && saveFile.
file() ) {
315 mFormat->
saveAll( addressBook(),
this, saveFile.
file() );
316 ok = saveFile.
close();
321 addressBook()->error( i18n(
"Unable to save file '%1'." ).arg( mFileName ) );
324 mDirWatch.startScan();
329 bool ResourceFile::asyncSave(
Ticket *ticket )
331 kdDebug(5700) <<
"ResourceFile::asyncSave()" <<
endl;
333 bool ok =
save( ticket );
336 TQTimer::singleShot( 0,
this, TQT_SLOT( emitSavingError() ) );
338 TQTimer::singleShot( 0,
this, TQT_SLOT( emitSavingFinished() ) );
345 mDirWatch.stopScan();
346 if ( mDirWatch.contains( mFileName ) )
347 mDirWatch.removeFile( mFileName );
351 mDirWatch.addFile( mFileName );
352 mDirWatch.startScan();
366 mFormat = factory->
format( mFormatName );
374 void ResourceFile::fileChanged()
376 kdDebug(5700) <<
"ResourceFile::fileChanged(): " << mFileName <<
endl;
378 if ( !addressBook() )
386 addressBook()->emitAddressBookChanged();
392 TQFile::remove( TQFile::encodeName(
locateLocal(
"data",
"tdeabc/photos/" ) + addr.
uid() ) );
393 TQFile::remove( TQFile::encodeName(
locateLocal(
"data",
"tdeabc/logos/" ) + addr.
uid() ) );
394 TQFile::remove( TQFile::encodeName(
locateLocal(
"data",
"tdeabc/sounds/" ) + addr.
uid() ) );
396 mAddrMap.erase( addr.
uid() );
399 void ResourceFile::emitSavingFinished()
401 emit savingFinished(
this );
404 void ResourceFile::emitSavingError()
406 emit savingError(
this, i18n(
"Unable to save file '%1'." ).arg( mFileName ) );
409 void ResourceFile::emitLoadingFinished()
411 emit loadingFinished(
this );
414 void ResourceFile::emitLoadingError()
416 emit loadingError(
this, i18n(
"Problems during parsing file '%1'." ).arg( mFileName ) );
419 #include "resourcefile.moc"
void setFormat(const TQString &name)
Sets a new format by name.
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
virtual void writeConfig(TDEConfig *cfg)
Writes the config back.
~ResourceFile()
Destructor.
static TQString fileName()
Returns the default file name for vcard-based addressbook.
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
kdbgstream kdDebug(int area=0)
virtual Ticket * requestSaveTicket()
Requests a save ticket, that is used by save()
virtual void removeAddressee(const Addressee &addr)
Remove a addressee from its source.
static data, shared by ALL addressee objects
Helper class for handling coordinated save of address books.
TQString uid() const
Return unique identifier.
virtual bool load()
Loads all addressees from file to the address book.
TQString format() const
Returns the format name.
virtual bool lock()
Lock resource.
virtual bool doOpen()
Tries to open the file and checks for the proper format.
virtual void doClose()
Closes the file again.
ResourceFile(const TDEConfig *cfg)
Constructor.
TQString fileName() const
Return name of file used for loading and saving the address book.
static bool backupFile(const TQString &filename, const TQString &backupDir=TQString::null, const TQString &backupExtension=TQString::fromLatin1("~"))
kndbgstream & endl(kndbgstream &s)
virtual bool save(Ticket *ticket)
Saves all addresses from address book to file.
void setFileName(const TQString &)
Set name of file to be used for saving.