23 #include <sys/types.h>
31 #include <tdeapplication.h>
32 #include <tdeconfig.h>
34 #include <kgenericfactory.h>
35 #include <tdeglobal.h>
37 #include <kstandarddirs.h>
38 #include <kurlrequester.h>
40 #include "addressbook.h"
41 #include "formatfactory.h"
42 #include "resourcedirconfig.h"
43 #include "stdaddressbook.h"
46 #include "resourcedir.h"
52 void *init_tdeabc_dir()
54 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>();
59 ResourceDir::ResourceDir(
const TDEConfig *config )
60 : Resource( config ), mAsynchronous( false )
63 init( config->
readPathEntry(
"FilePath", StdAddressBook::directoryName() ),
64 config->
readEntry(
"FileFormat",
"vcard" ) );
66 init( StdAddressBook::directoryName(),
"vcard" );
70 ResourceDir::ResourceDir(
const TQString &path,
const TQString &format )
71 : Resource( 0 ), mAsynchronous( false )
76 void ResourceDir::init(
const TQString &path,
const TQString &format )
81 mFormat = factory->
format( mFormatName );
84 mFormatName =
"vcard";
85 mFormat = factory->
format( mFormatName );
90 connect( &mDirWatch, TQ_SIGNAL( dirty(
const TQString&) ), TQ_SLOT( pathChanged() ) );
91 connect( &mDirWatch, TQ_SIGNAL( created(
const TQString&) ), TQ_SLOT( pathChanged() ) );
92 connect( &mDirWatch, TQ_SIGNAL( deleted(
const TQString&) ), TQ_SLOT( pathChanged() ) );
97 ResourceDir::~ResourceDir()
103 void ResourceDir::writeConfig(
TDEConfig *config )
105 Resource::writeConfig( config );
107 if ( mPath == StdAddressBook::directoryName() )
112 config->
writeEntry(
"FileFormat", mFormatName );
115 Ticket *ResourceDir::requestSaveTicket()
117 kdDebug(5700) <<
"ResourceDir::requestSaveTicket()" <<
endl;
119 if ( !addressBook() )
return 0;
122 mLock =
new Lock( mPath );
124 if ( mLock->lock() ) {
125 addressBook()->emitAddressBookLocked();
127 addressBook()->error( mLock->error() );
128 kdDebug(5700) <<
"ResourceFile::requestSaveTicket(): Unable to lock path '"
129 << mPath <<
"': " << mLock->error() <<
endl;
133 return createTicket(
this );
136 void ResourceDir::releaseSaveTicket(
Ticket *ticket )
144 bool ResourceDir::doOpen()
147 if ( !dir.exists() ) {
148 return dir.mkdir( dir.path() );
150 TQString testName = dir.entryList( TQDir::Files )[0];
151 if ( testName.isNull() || testName.isEmpty() )
154 TQFile file( mPath +
"/" + testName );
155 if ( file.open( IO_ReadOnly ) )
158 if ( file.size() == 0 )
161 bool ok = mFormat->checkFormat( &file );
167 void ResourceDir::doClose()
171 bool ResourceDir::load()
173 kdDebug(5700) <<
"ResourceDir::load(): '" << mPath <<
"'" <<
endl;
175 mAsynchronous =
false;
178 TQStringList files = dir.entryList( TQDir::Files );
180 TQStringList::Iterator it;
182 for ( it = files.begin(); it != files.end(); ++it ) {
183 TQFile file( mPath +
"/" + (*it) );
185 if ( !file.open( IO_ReadOnly ) ) {
186 addressBook()->error( i18n(
"Unable to open file '%1' for reading" ).arg( file.name() ) );
191 if ( !mFormat->loadAll( addressBook(),
this, &file ) )
200 bool ResourceDir::asyncLoad()
202 mAsynchronous =
true;
206 emit loadingError(
this, i18n(
"Loading resource '%1' failed!" )
207 .arg( resourceName() ) );
209 emit loadingFinished(
this );
214 bool ResourceDir::save(
Ticket * )
216 kdDebug(5700) <<
"ResourceDir::save(): '" << mPath <<
"'" <<
endl;
218 Addressee::Map::Iterator it;
221 mDirWatch.stopScan();
223 for ( it = mAddrMap.begin(); it != mAddrMap.end(); ++it ) {
224 if ( !it.data().changed() )
227 TQFile file( mPath +
"/" + (*it).uid() );
228 if ( !file.open( IO_WriteOnly ) ) {
229 addressBook()->error( i18n(
"Unable to open file '%1' for writing" ).arg( file.name() ) );
233 mFormat->save( *it, &file );
236 (*it).setChanged(
false );
241 mDirWatch.startScan();
246 bool ResourceDir::asyncSave(
Ticket *ticket )
248 bool ok =
save( ticket );
250 emit savingError(
this, i18n(
"Saving resource '%1' failed!" )
251 .arg( resourceName() ) );
253 emit savingFinished(
this );
258 void ResourceDir::setPath(
const TQString &path )
260 mDirWatch.stopScan();
261 if ( mDirWatch.contains( mPath ) )
262 mDirWatch.removeDir( mPath );
265 mDirWatch.addDir( mPath,
true );
266 mDirWatch.startScan();
269 TQString ResourceDir::path()
const
274 void ResourceDir::setFormat(
const TQString &format )
276 mFormatName = format;
282 mFormat = factory->
format( mFormatName );
285 TQString ResourceDir::format()
const
290 void ResourceDir::pathChanged()
292 if ( !addressBook() )
300 addressBook()->emitAddressBookChanged();
304 void ResourceDir::removeAddressee(
const Addressee& addr )
306 TQFile::remove( mPath +
"/" + addr.
uid() );
307 mAddrMap.erase( addr.
uid() );
310 #include "resourcedir.moc"
TQString uid() const
Return unique identifier.
This class provides locking functionality for a file, directory or an arbitrary string-represented re...
Helper class for handling coordinated save of address books.
TQString readEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
void writePathEntry(const TQString &pKey, const TQString &path, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
void deleteEntry(const TQString &pKey, bool bNLS=false, bool bGlobal=false)
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
TQString readPathEntry(const TQString &pKey, const TQString &aDefault=TQString::null) const
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
TDEAction * clear(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
static data, shared by ALL addressee objects
const TDEShortcut & save()