diff --git a/kabc/formatfactory.cpp b/kabc/formatfactory.cpp index 84de9b19c..3e966e375 100644 --- a/kabc/formatfactory.cpp +++ b/kabc/formatfactory.cpp @@ -1,195 +1,195 @@ /* This file is part of libkabc. Copyright (c) 2002 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "formatfactory.h" #include "vcardformat.h" #include #include #include #include #include #include #include using namespace KABC; class FormatFactory::Private { public: ~Private() { mFormatList.clear(); qRemovePostRoutine( cleanupFormatFactory ); } KLibrary *openLibrary( const QString &libName ); QHash mFormatList; static FormatFactory *sSelf; static void cleanupFormatFactory() { delete sSelf; sSelf = 0; } }; FormatFactory *FormatFactory::Private::sSelf = 0; KLibrary *FormatFactory::Private::openLibrary( const QString &libName ) { KLibrary *library = 0; QString path = KLibLoader::findLibrary( libName ); if ( path.isEmpty() ) { kDebug( 5700 ) << "No format plugin library was found!"; return 0; } library = KLibLoader::self()->library( path ); if ( !library ) { kDebug( 5700 ) << "Could not load library '" << libName << "'"; return 0; } return library; } FormatFactory *FormatFactory::self() { kDebug(5700) << "FormatFactory::self()"; static Private p; if ( !p.sSelf ) { p.sSelf = new FormatFactory; qAddPostRoutine( Private::cleanupFormatFactory ); } return p.sSelf; } FormatFactory::FormatFactory() : d( new Private ) { // dummy entry for default format FormatInfo info; info.library = ""; info.nameLabel = i18n( "vCard" ); info.descriptionLabel = i18n( "vCard Format" ); d->mFormatList.insert( "vcard", info ); const QStringList list = KGlobal::dirs()->findAllResources( "data","kabc/formats/*.desktop", KStandardDirs::Recursive | KStandardDirs::NoDuplicates ); for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it ) { - KConfig config( *it, KConfig::OnlyLocal ); + KConfig config( *it, KConfig::SimpleConfig ); if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) ) { continue; } KConfigGroup group = config.group( "Plugin" ); QString type = group.readEntry( "Type" ); info.library = group.readEntry( "X-KDE-Library" ); group = config.group( "Misc" ); info.nameLabel = group.readEntry( "Name" ); info.descriptionLabel = group.readEntry( "Comment", i18n( "No description available." ) ); d->mFormatList.insert( type, info ); } } FormatFactory::~FormatFactory() { delete d; } QStringList FormatFactory::formats() { QStringList retval; // make sure 'vcard' is the first entry retval << "vcard"; QHashIterator it( d->mFormatList ); while ( it.hasNext() ) { it.next(); if ( it.key() != "vcard" ) { retval << it.key(); } } return retval; } FormatInfo FormatFactory::info( const QString &type ) const { if ( type.isEmpty() || !d->mFormatList.contains( type ) ) { return FormatInfo(); } else { return d->mFormatList[ type ]; } } Format *FormatFactory::format( const QString &type ) { Format *format = 0; if ( type.isEmpty() ) { return 0; } if ( type == "vcard" ) { format = new VCardFormat; format->setType( type ); format->setNameLabel( i18n( "vCard" ) ); format->setDescriptionLabel( i18n( "vCard Format" ) ); return format; } if ( !d->mFormatList.contains( type ) ) { return 0; } FormatInfo fi = d->mFormatList[ type ]; QString libName = fi.library; KLibrary *library = d->openLibrary( libName ); if ( !library ) { return 0; } KLibrary::void_function_ptr format_func = library->resolveFunction( "format" ); if ( format_func ) { format = ( (Format *(*)())format_func )(); format->setType( type ); format->setNameLabel( fi.nameLabel ); format->setDescriptionLabel( fi.descriptionLabel ); } else { kDebug( 5700 ) << "'" << libName << "' is not a format plugin."; return 0; } return format; } diff --git a/kabc/plugins/net/resourcenet.cpp b/kabc/plugins/net/resourcenet.cpp index fbf189d5f..8b08d143e 100644 --- a/kabc/plugins/net/resourcenet.cpp +++ b/kabc/plugins/net/resourcenet.cpp @@ -1,391 +1,391 @@ /* This file is part of libkabc. Copyright (c) 2003 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "resourcenet.h" #include "resourcenetconfig.h" #include "kabc/addressbook.h" #include "kabc/formatfactory.h" #include "kabc/stdaddressbook.h" #include #include #include #include #include #include #include #include #include using namespace KABC; class ResourceNet::ResourceNetPrivate { public: KIO::Job *mLoadJob; bool mIsLoading; KIO::Job *mSaveJob; bool mIsSaving; }; ResourceNet::ResourceNet() : Resource(), mFormat( 0 ), mTempFile( 0 ), d( new ResourceNetPrivate ) { init( KUrl(), "vcard" ); } ResourceNet::ResourceNet( const KConfigGroup &group ) : Resource( group ), mFormat( 0 ), mTempFile( 0 ), d( new ResourceNetPrivate ) { - init( KUrl( group.readPathEntry( "NetUrl" ) ), group.readEntry( "NetFormat" ) ); + init( KUrl( group.readPathEntry( "NetUrl", QString() ) ), group.readEntry( "NetFormat" ) ); } ResourceNet::ResourceNet( const KUrl &url, const QString &format ) : Resource(), mFormat( 0 ), mTempFile( 0 ), d( new ResourceNetPrivate ) { init( url, format ); } void ResourceNet::init( const KUrl &url, const QString &format ) { d->mLoadJob = 0; d->mIsLoading = false; d->mSaveJob = 0; d->mIsSaving = false; mFormatName = format; FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( mFormatName ); if ( !mFormat ) { mFormatName = "vcard"; mFormat = factory->format( mFormatName ); } setUrl( url ); } ResourceNet::~ResourceNet() { if ( d->mIsLoading ) { d->mLoadJob->kill(); } if ( d->mIsSaving ) { d->mSaveJob->kill(); } delete d; d = 0; delete mFormat; mFormat = 0; deleteLocalTempFile(); } void ResourceNet::writeConfig( KConfigGroup &group ) { Resource::writeConfig( group ); group.writePathEntry( "NetUrl", mUrl.url() ); group.writeEntry( "NetFormat", mFormatName ); } Ticket *ResourceNet::requestSaveTicket() { kDebug(5700) << "ResourceNet::requestSaveTicket()"; return createTicket( this ); } void ResourceNet::releaseSaveTicket( Ticket *ticket ) { delete ticket; } bool ResourceNet::doOpen() { return true; } void ResourceNet::doClose() { } bool ResourceNet::load() { QString tempFile; if ( !KIO::NetAccess::download( mUrl, tempFile, 0 ) ) { addressBook()->error( i18n( "Unable to download file '%1'.", mUrl.prettyUrl() ) ); return false; } QFile file( tempFile ); if ( !file.open( QIODevice::ReadOnly ) ) { addressBook()->error( i18n( "Unable to open file '%1'.", tempFile ) ); KIO::NetAccess::removeTempFile( tempFile ); return false; } bool result = clearAndLoad( &file ); if ( !result ) { addressBook()->error( i18n( "Problems during parsing file '%1'.", tempFile ) ); } KIO::NetAccess::removeTempFile( tempFile ); return result; } bool ResourceNet::clearAndLoad( QFile *file ) { clear(); return mFormat->loadAll( addressBook(), this, file ); } bool ResourceNet::asyncLoad() { if ( d->mIsLoading ) { abortAsyncLoading(); } if ( d->mIsSaving ) { kWarning(5700) << "Aborted asyncLoad() because we're still saving!"; return false; } bool ok = createLocalTempFile(); if ( !ok ) { emit loadingError( this, i18n( "Unable to open file '%1'.", mTempFile->fileName() ) ); deleteLocalTempFile(); return false; } KUrl dest; dest.setPath( mTempFile->fileName() ); KIO::Scheduler::checkSlaveOnHold( true ); d->mLoadJob = KIO::file_copy( mUrl, dest, -1, KIO::Overwrite | KIO::HideProgressInfo ); d->mIsLoading = true; connect( d->mLoadJob, SIGNAL( result( KJob* ) ), this, SLOT( downloadFinished( KJob* ) ) ); return true; } void ResourceNet::abortAsyncLoading() { kDebug(5700) << "ResourceNet::abortAsyncLoading()"; if ( d->mLoadJob ) { d->mLoadJob->kill(); // result not emitted d->mLoadJob = 0; } deleteLocalTempFile(); d->mIsLoading = false; } void ResourceNet::abortAsyncSaving() { kDebug(5700) << "ResourceNet::abortAsyncSaving()"; if ( d->mSaveJob ) { d->mSaveJob->kill(); // result not emitted d->mSaveJob = 0; } deleteLocalTempFile(); d->mIsSaving = false; } bool ResourceNet::save( Ticket *ticket ) { Q_UNUSED( ticket ); kDebug(5700) << "ResourceNet::save()"; if ( d->mIsSaving ) { abortAsyncSaving(); } KTemporaryFile tempFile; bool ok = tempFile.open(); if ( ok ) { saveToFile( &tempFile ); } if ( !ok ) { addressBook()->error( i18n( "Unable to save file '%1'.", tempFile.fileName() ) ); return false; } ok = KIO::NetAccess::upload( tempFile.fileName(), mUrl, 0 ); if ( !ok ) { addressBook()->error( i18n( "Unable to upload to '%1'.", mUrl.prettyUrl() ) ); } return ok; } bool ResourceNet::asyncSave( Ticket *ticket ) { Q_UNUSED( ticket ); kDebug(5700) << "ResourceNet::asyncSave()"; if ( d->mIsSaving ) { abortAsyncSaving(); } if ( d->mIsLoading ) { kWarning(5700) << "Aborted asyncSave() because we're still loading!"; return false; } bool ok = createLocalTempFile(); if ( ok ) { saveToFile( mTempFile ); } if ( !ok ) { emit savingError( this, i18n( "Unable to save file '%1'.", mTempFile->fileName() ) ); deleteLocalTempFile(); return false; } KUrl src; src.setPath( mTempFile->fileName() ); KIO::Scheduler::checkSlaveOnHold( true ); d->mIsSaving = true; d->mSaveJob = KIO::file_copy( src, mUrl, -1, KIO::Overwrite | KIO::HideProgressInfo ); connect( d->mSaveJob, SIGNAL( result( KJob* ) ), this, SLOT( uploadFinished( KJob* ) ) ); return true; } bool ResourceNet::createLocalTempFile() { deleteStaleTempFile(); mTempFile = new KTemporaryFile(); return mTempFile->open(); } void ResourceNet::deleteStaleTempFile() { if ( hasTempFile() ) { kDebug(5700) << "stale temp file detected" << mTempFile->fileName(); deleteLocalTempFile(); } } void ResourceNet::deleteLocalTempFile() { delete mTempFile; mTempFile = 0; } void ResourceNet::saveToFile( QFile *file ) { mFormat->saveAll( addressBook(), this, file ); } void ResourceNet::setUrl( const KUrl &url ) { mUrl = url; } KUrl ResourceNet::url() const { return mUrl; } void ResourceNet::setFormat( const QString &name ) { mFormatName = name; if ( mFormat ) { delete mFormat; } FormatFactory *factory = FormatFactory::self(); mFormat = factory->format( mFormatName ); } QString ResourceNet::format() const { return mFormatName; } void ResourceNet::downloadFinished( KJob *job ) { Q_UNUSED( job ); kDebug(5700) << "ResourceNet::downloadFinished()"; d->mIsLoading = false; if ( !hasTempFile() ) { emit loadingError( this, i18n( "Download failed, could not create temporary file" ) ); return; } QFile file( mTempFile->fileName() ); if ( file.open( QIODevice::ReadOnly ) ) { if ( clearAndLoad( &file ) ) { emit loadingFinished( this ); } else { emit loadingError( this, i18n( "Problems during parsing file '%1'.", mTempFile->fileName() ) ); } } else { emit loadingError( this, i18n( "Unable to open file '%1'.", mTempFile->fileName() ) ); } deleteLocalTempFile(); } void ResourceNet::uploadFinished( KJob *job ) { kDebug(5700) << "ResourceFile::uploadFinished()"; d->mIsSaving = false; if ( job->error() ) { emit savingError( this, job->errorString() ); } else { emit savingFinished( this ); } deleteLocalTempFile(); } #include "resourcenet.moc" diff --git a/kcal/resourcelocal.cpp b/kcal/resourcelocal.cpp index 99c081711..7514beb80 100644 --- a/kcal/resourcelocal.cpp +++ b/kcal/resourcelocal.cpp @@ -1,249 +1,249 @@ /* This file is part of the kcal library. Copyright (c) 1998 Preston Brown Copyright (c) 2001,2003 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /** @file This file is part of the API for handling calendar data and defines the ResourceLocal class. @author Preston Brown @author Cornelius Schumacher */ #include "resourcelocal.moc" #include "resourcelocalconfig.h" #include "vcalformat.h" #include "icalformat.h" #include "exceptions.h" #include "incidence.h" #include "event.h" #include "todo.h" #include "journal.h" #include "kresources/configwidget.h" #include #include #include #include #include #include #include #include using namespace KCal; ResourceLocal::ResourceLocal() : ResourceCached(), d( new ResourceLocal::Private() ) { d->mLock = 0; d->mURL = KUrl(); d->mFormat = new ICalFormat(); init(); } ResourceLocal::ResourceLocal( const KConfigGroup &group ) : ResourceCached( group ), d( new ResourceLocal::Private() ) { d->mLock = 0; - QString url = group.readPathEntry( "CalendarURL" ); + QString url = group.readPathEntry( "CalendarURL", QString() ); d->mURL = KUrl( url ); QString format = group.readEntry( "Format" ); if ( format == "ical" ) { d->mFormat = new ICalFormat(); } else if ( format == "vcal" ) { d->mFormat = new VCalFormat(); } else { d->mFormat = new ICalFormat(); } init(); } ResourceLocal::ResourceLocal( const QString &fileName ) : ResourceCached(), d( new ResourceLocal::Private ) { d->mURL = KUrl::fromPath( fileName ); d->mFormat = new ICalFormat(); init(); } void ResourceLocal::writeConfig( KConfigGroup &group ) { kDebug(5800) << "ResourceLocal::writeConfig()"; ResourceCalendar::writeConfig( group ); group.writePathEntry( "CalendarURL", d->mURL.prettyUrl() ); if ( typeid( *d->mFormat ) == typeid( ICalFormat ) ) { group.writeEntry( "Format", "ical" ); } else if ( typeid( *d->mFormat ) == typeid( VCalFormat ) ) { group.writeEntry( "Format", "vcal" ); } else { kDebug(5800) << "ERROR: Unknown format type"; } } void ResourceLocal::init() { setType( "file" ); setSavePolicy( SaveDelayed ); connect( &d->mDirWatch, SIGNAL( dirty( const QString & ) ), SLOT( reload() ) ); connect( &d->mDirWatch, SIGNAL( created( const QString & ) ), SLOT( reload() ) ); connect( &d->mDirWatch, SIGNAL( deleted( const QString & ) ), SLOT( reload() ) ); d->mLock = new KABC::Lock( d->mURL.path() ); d->mDirWatch.addFile( d->mURL.path() ); d->mDirWatch.startScan(); } ResourceLocal::~ResourceLocal() { d->mDirWatch.stopScan(); close(); delete d->mLock; delete d; } KDateTime ResourceLocal::readLastModified() { QFileInfo fi( d->mURL.path() ); return KDateTime( fi.lastModified() ); // use local time zone } bool ResourceLocal::doLoad( bool ) { bool success; if ( !KStandardDirs::exists( d->mURL.path() ) ) { kDebug(5800) << "ResourceLocal::load(): File doesn't exist yet."; // Save the empty calendar, so the calendar file will be created. success = doSave( true ); } else { success = calendar()->load( d->mURL.path() ); if ( success ) { d->mLastModified = readLastModified(); } } return success; } bool ResourceLocal::doSave( bool ) { bool success = calendar()->save( d->mURL.path() ); d->mLastModified = readLastModified(); return success; } KABC::Lock *ResourceLocal::lock() { return d->mLock; } bool ResourceLocal::doReload() { kDebug(5800) << "ResourceLocal::doReload()"; if ( !isOpen() ) { return false; } if ( d->mLastModified == readLastModified() ) { kDebug(5800) << "ResourceLocal::reload(): file not modified since last read."; return false; } calendar()->close(); calendar()->load( d->mURL.path() ); return true; } void ResourceLocal::reload() { if ( doReload() ) { emit resourceChanged( this ); } } void ResourceLocal::dump() const { ResourceCalendar::dump(); kDebug(5800) << " Url:" << d->mURL.url(); } QString ResourceLocal::fileName() const { return d->mURL.path(); } bool ResourceLocal::setFileName( const QString &fileName ) { bool open = isOpen(); if ( open ) { close(); } delete d->mLock; d->mDirWatch.stopScan(); d->mDirWatch.removeFile( d->mURL.path() ); d->mURL = KUrl::fromPath( fileName ); d->mLock = new KABC::Lock( d->mURL.path() ); d->mDirWatch.addFile( d->mURL.path() ); d->mDirWatch.startScan(); return true; } bool ResourceLocal::setValue( const QString &key, const QString &value ) { if ( key == "File" ) { return setFileName( value ); } else { return false; } } bool ResourceLocal::operator==( const ResourceLocal &other ) { return d->mURL == other.d->mURL && d->mLastModified == other.d->mLastModified; } ResourceLocal &ResourceLocal::operator=( const ResourceLocal &other ) { d->mURL = other.d->mURL; d->mLastModified = other.d->mLastModified; return *this; } diff --git a/kcal/resourcelocaldir.cpp b/kcal/resourcelocaldir.cpp index 6c25649f5..8bf71118c 100644 --- a/kcal/resourcelocaldir.cpp +++ b/kcal/resourcelocaldir.cpp @@ -1,276 +1,276 @@ /* This file is part of the kcal library. Copyright (c) 2003 Cornelius Schumacher This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "resourcelocaldir.h" #include "calendarlocal.h" #include "incidence.h" #include "event.h" #include "todo.h" #include "journal.h" #include "kresources/configwidget.h" #include #include #include #include #include #include #include #include #include #include #include "resourcelocaldir.moc" using namespace KCal; ResourceLocalDir::ResourceLocalDir() : ResourceCached(), d( new KCal::ResourceLocalDir::Private ) { d->init( this ); } ResourceLocalDir::ResourceLocalDir( const KConfigGroup &group ) : ResourceCached( group ), d( new KCal::ResourceLocalDir::Private ) { readConfig( group ); d->init( this ); } ResourceLocalDir::ResourceLocalDir( const QString &dirName ) : ResourceCached(), d( new KCal::ResourceLocalDir::Private( dirName ) ) { d->init( this ); } void ResourceLocalDir::readConfig( const KConfigGroup &group ) { - QString url = group.readPathEntry( "CalendarURL" ); + QString url = group.readPathEntry( "CalendarURL", QString() ); d->mURL = KUrl( url ); } void ResourceLocalDir::writeConfig( KConfigGroup &group ) { kDebug(5800) << "ResourceLocalDir::writeConfig()"; ResourceCalendar::writeConfig( group ); group.writePathEntry( "CalendarURL", d->mURL.prettyUrl() ); } void ResourceLocalDir::Private::init( ResourceLocalDir *rdir ) { rdir->setType( "dir" ); rdir->setSavePolicy( SaveDelayed ); rdir->connect( &mDirWatch, SIGNAL( dirty( const QString & ) ), SLOT( reload( const QString & ) ) ); rdir->connect( &mDirWatch, SIGNAL( created( const QString & ) ), SLOT( reload( const QString & ) ) ); rdir->connect( &mDirWatch, SIGNAL( deleted( const QString & ) ), SLOT( reload( const QString & ) ) ); mLock = new KABC::Lock( mURL.path() ); mDirWatch.addDir( mURL.path(), KDirWatch::WatchFiles ); mDirWatch.startScan(); } ResourceLocalDir::~ResourceLocalDir() { close(); delete d->mLock; delete d; } bool ResourceLocalDir::doLoad( bool ) { kDebug(5800) << "ResourceLocalDir::load()"; calendar()->close(); QString dirName = d->mURL.path(); bool success = true; if ( !( KStandardDirs::exists( dirName ) || KStandardDirs::exists( dirName + '/' ) ) ) { kDebug(5800) << "ResourceLocalDir::load(): Directory '" << dirName << "' doesn't exist yet. Creating it..."; // Create the directory. Use 0775 to allow group-writable if the umask // allows it (permissions will be 0775 & ~umask). This is desired e.g. for // group-shared directories! success = KStandardDirs::makeDir( dirName, 0775 ); } else { kDebug(5800) << "ResourceLocalDir::load(): '" << dirName << "'"; QDir dir( dirName ); QStringList entries = dir.entryList( QDir::Files | QDir::Readable ); QStringList::ConstIterator it; for ( it = entries.begin(); it != entries.end(); ++it ) { if ( (*it).endsWith( '~' ) ) { // is backup file, ignore it continue; } QString fileName = dirName + '/' + *it; kDebug(5800) << " read '" << fileName << "'"; CalendarLocal cal( calendar()->timeSpec() ); if ( !doFileLoad( cal, fileName ) ) { success = false; } } } return success; } bool ResourceLocalDir::doFileLoad( CalendarLocal &cal, const QString &fileName ) { if ( !cal.load( fileName ) ) { return false; } Incidence::List incidences = cal.rawIncidences(); Incidence::List::ConstIterator it; for ( it = incidences.constBegin(); it != incidences.constEnd(); ++it ) { Incidence *i = *it; if ( i ) { calendar()->addIncidence( i->clone() ); } } return true; } bool ResourceLocalDir::doSave( bool ) { Incidence::List list; list = addedIncidences(); list += changedIncidences(); for ( Incidence::List::iterator it = list.begin(); it != list.end(); ++it ) { doSave( true, *it ); } return true; } bool ResourceLocalDir::doSave( bool, Incidence *incidence ) { d->mDirWatch.stopScan(); // do prohibit the dirty() signal and a following reload() QString fileName = d->mURL.path() + '/' + incidence->uid(); kDebug(5800) << "writing '" << fileName << "'"; CalendarLocal cal( calendar()->timeSpec() ); cal.addIncidence( incidence->clone() ); cal.save( fileName ); d->mDirWatch.startScan(); return true; } KABC::Lock *ResourceLocalDir::lock() { return d->mLock; } void ResourceLocalDir::reload( const QString &file ) { kDebug(5800) << "ResourceLocalDir::reload()"; if ( !isOpen() ) { return; } kDebug(5800) << " File: '" << file << "'"; calendar()->close(); load(); emit resourceChanged( this ); } bool ResourceLocalDir::deleteEvent( Event *event ) { kDebug(5800) << "ResourceLocalDir::deleteEvent"; if ( d->deleteIncidenceFile( event ) ) { return calendar()->deleteEvent( event ); } else { return false; } } void ResourceLocalDir::deleteAllEvents() { calendar()->deleteAllEvents(); } bool ResourceLocalDir::deleteTodo( Todo *todo ) { if ( d->deleteIncidenceFile( todo ) ) { return calendar()->deleteTodo( todo ); } else { return false; } } void ResourceLocalDir::deleteAllTodos() { calendar()->deleteAllTodos(); } bool ResourceLocalDir::deleteJournal( Journal *journal ) { if ( d->deleteIncidenceFile( journal ) ) { return calendar()->deleteJournal( journal ); } else { return false; } } void ResourceLocalDir::deleteAllJournals() { calendar()->deleteAllJournals(); } void ResourceLocalDir::dump() const { ResourceCalendar::dump(); kDebug(5800) << " Url:" << d->mURL.url(); } bool ResourceLocalDir::Private::deleteIncidenceFile( Incidence *incidence ) { QFile file( mURL.path() + '/' + incidence->uid() ); if ( !file.exists() ) { return true; } mDirWatch.stopScan(); bool removed = file.remove(); mDirWatch.startScan(); return removed; } diff --git a/kpimidentities/signature.cpp b/kpimidentities/signature.cpp index 656d6b6c9..3b9662562 100644 --- a/kpimidentities/signature.cpp +++ b/kpimidentities/signature.cpp @@ -1,286 +1,286 @@ /* Copyright (c) 2002-2004 Marc Mutz Copyright (c) 2007 Tom Albers This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "signature.h" #include #include #include #include #include #include #include #include #include using namespace KPIMIdentities; Signature::Signature() : mType( Disabled ) {} Signature::Signature( const QString &text ) : mText( text ), mType( Inlined ) {} Signature::Signature( const QString &url, bool isExecutable ) : mUrl( url ), mType( isExecutable ? FromCommand : FromFile ) {} QString Signature::rawText( bool *ok ) const { switch ( mType ) { case Disabled: if ( ok ) { *ok = true; } return QString(); case Inlined: if ( ok ) { *ok = true; } return mText; case FromFile: return textFromFile( ok ); case FromCommand: return textFromCommand( ok ); }; kFatal(5325) << "Signature::type() returned unknown value!"; return QString(); // make compiler happy } QString Signature::textFromCommand( bool *ok ) const { assert( mType == FromCommand ); // handle pathological cases: if ( mUrl.isEmpty() ) { if ( ok ) { *ok = true; } return QString(); } // create a shell process: KProcess proc; proc.setOutputChannelMode( KProcess::SeparateChannels ); proc.setShellCommand( mUrl ); int rc = proc.execute(); // handle errors, if any: if ( rc != 0 ) { if ( ok ) { *ok = false; } QString wmsg = i18n( "Failed to execute signature script
%1:" "

%2

", mUrl, QString( proc.readAllStandardError() ) ); KMessageBox::error( 0, wmsg ); return QString(); } // no errors: if ( ok ) { *ok = true; } // get output: QByteArray output = proc.readAllStandardOutput(); // TODO: hmm, should we allow other encodings, too? return QString::fromLocal8Bit( output.data(), output.size() ); } QString Signature::textFromFile( bool *ok ) const { assert( mType == FromFile ); // TODO: Use KIO::NetAccess to download non-local files! if ( !KUrl( mUrl ).isLocalFile() && !( QFileInfo( mUrl ).isRelative() && QFileInfo( mUrl ).exists() ) ) { kDebug(5325) << "Signature::textFromFile:" << "non-local URLs are unsupported"; if ( ok ) { *ok = false; } return QString(); } if ( ok ) { *ok = true; } // TODO: hmm, should we allow other encodings, too? const QByteArray ba = KPIMUtils::kFileToByteArray( mUrl, false ); return QString::fromLocal8Bit( ba.data(), ba.size() ); } QString Signature::withSeparator( bool *ok ) const { bool internalOK = false; QString signature = rawText( &internalOK ); if ( !internalOK ) { if ( ok ) { *ok = false; } return QString(); } if ( ok ) { *ok = true; } if ( signature.isEmpty() ) { return signature; // don't add a separator in this case } if ( signature.startsWith( QString::fromLatin1( "-- \n" ) ) ) { // already have signature separator at start of sig: return QString::fromLatin1( "\n" ) += signature; } else if ( signature.indexOf( QString::fromLatin1( "\n-- \n" ) ) != -1 ) { // already have signature separator inside sig; don't prepend '\n' // to improve abusing signatures as templates: return signature; } else { // need to prepend one: return QString::fromLatin1( "\n-- \n" ) + signature; } } void Signature::setUrl( const QString &url, bool isExecutable ) { mUrl = url; mType = isExecutable ? FromCommand : FromFile; } // config keys and values: static const char sigTypeKey[] = "Signature Type"; static const char sigTypeInlineValue[] = "inline"; static const char sigTypeFileValue[] = "file"; static const char sigTypeCommandValue[] = "command"; static const char sigTypeDisabledValue[] = "disabled"; static const char sigTextKey[] = "Inline Signature"; static const char sigFileKey[] = "Signature File"; static const char sigCommandKey[] = "Signature Command"; void Signature::readConfig( const KConfigGroup &config ) { QString sigType = config.readEntry( sigTypeKey ); if ( sigType == sigTypeInlineValue ) { mType = Inlined; } else if ( sigType == sigTypeFileValue ) { mType = FromFile; - mUrl = config.readPathEntry( sigFileKey ); + mUrl = config.readPathEntry( sigFileKey, QString() ); } else if ( sigType == sigTypeCommandValue ) { mType = FromCommand; - mUrl = config.readPathEntry( sigCommandKey ); + mUrl = config.readPathEntry( sigCommandKey, QString() ); } else { mType = Disabled; } mText = config.readEntry( sigTextKey ); } void Signature::writeConfig( KConfigGroup &config ) const { switch ( mType ) { case Inlined: config.writeEntry( sigTypeKey, sigTypeInlineValue ); break; case FromFile: config.writeEntry( sigTypeKey, sigTypeFileValue ); config.writePathEntry( sigFileKey, mUrl ); break; case FromCommand: config.writeEntry( sigTypeKey, sigTypeCommandValue ); config.writePathEntry( sigCommandKey, mUrl ); break; case Disabled: config.writeEntry( sigTypeKey, sigTypeDisabledValue ); default: break; } config.writeEntry( sigTextKey, mText ); } // --------------------- Operators -------------------// QDataStream &KPIMIdentities::operator<< ( QDataStream &stream, const KPIMIdentities::Signature &sig ) { return stream << static_cast( sig.mType ) << sig.mUrl << sig.mText; } QDataStream &KPIMIdentities::operator>> ( QDataStream &stream, KPIMIdentities::Signature &sig ) { quint8 s; stream >> s >> sig.mUrl >> sig.mText; sig.mType = static_cast( s ); return stream; } bool Signature::operator== ( const Signature &other ) const { if ( mType != other.mType ) { return false; } switch ( mType ) { case Inlined: return mText == other.mText; case FromFile: case FromCommand: return mUrl == other.mUrl; default: case Disabled: return true; } } // --------------- Getters -----------------------// QString Signature::text() const { return mText; } QString Signature::url() const { return mUrl; } Signature::Type Signature::type() const { return mType; } // --------------- Setters -----------------------// void Signature::setText( const QString &text ) { mText = text; } void Signature::setType( Type type ) { mType = type; } diff --git a/kresources/Mainpage.dox b/kresources/Mainpage.dox index 07a28b4e4..c3cd583f7 100644 --- a/kresources/Mainpage.dox +++ b/kresources/Mainpage.dox @@ -1,183 +1,183 @@ /** \mainpage The KDE Resource library The KDE Resource framework can be used to manage resources of different types, organized in families. The Resource framework is for example used for addressbook resources in libkabc and for calendar resources in libkcal. When you want to use the framework for a new family, you need to
  • Define a name for your resource family
  • subclass Resource and add the fields and method that are needed in your application
  • If needed, override the doOpen() and doClose() methods.
  • In your application, you can use ResourceManager to keep track of the resources in your family, and you can use ResourceSelectDialog to let the user select a single resource.
When you want to add a new resource type to an existing resource family, you need to
  • Further subclass the family-specific Resource to implement resource type-specific operation
  • Subclass ResourceConfigWidget to provide a configuration widget for your new resource type
  • Provide a .desktop file so that the new resource type can be found automatically by the ResourceManager
Example: resourceexample.h: \code #include #include class ResourceExample : public KRES::Resource { public: ResourceExample( const KConfig * ); ~ResourceExample(); void writeConfig( KConfig *config ); private: QString mLocation; QString mPassword; } \endcode resourceexample.cpp: \code #include #include "resourceexample.h" ResourceExample::ResourceExample( const KConfig *config ) : Resource( config ) { if ( config ) { - mLocation = config->readPathEntry( "Location" ); + mLocation = config->readPathEntry( "Location", QString() ); mPassword = KStringHandler::obscure( config->readEntry( "Password" ) ); } else { mLocation = ""; // Or some sensible default mPassword = ""; } } void ResourceExample::writeConfig( KConfig *config ) { KRES::Resource::writeConfig( config ); config->writePathEntry( "Location", mLocation ); config->writeEntry( "Password", KStringHandler::obscure( mPassword ) ); } extern "C" { KRES::ResourceExample *config_widget( QWidget *parent ) { return new ResourceExampleConfig( parent, "Configure Example Resource" ); } KRES::Resource *resource( const KConfig *config ) { return new ResourceExample( config ); } } \endcode resourceexampleconfig.h: \code #include #include #include "resourceexample.h" class ResourceExampleConfig : public KRES::ResourceConfigWidget { Q_OBJECT public: ResourceExampleConfig( QWidget *parent = 0, const char *name = 0 ); public Q_SLOTS: virtual void loadSettings( KRES::Resource *resource); virtual void saveSettings( KRES::Resource *resource ); private: KLineEdit *mLocationEdit; KLineEdit *mPasswordEdit; }; \endcode resourceexampleconfig.cpp: \code #include #include #include #include "resourceexample.h" #include "resourceexampleconfig.h" ResourceExampleConfig::ResourceExampleConfig( QWidget *parent, const char *name ) : KRES::ResourceConfigWidget( parent, name ) { QGridLayout *mainLayout = new QGridLayout( this, 2, 2 ); QLabel *label = new QLabel( i18n( "Location:" ), this ); mHostEdit = new KLineEdit( this ); mainLayout->addWidget( label, 1, 0 ); mainLayout->addWidget( mHostEdit, 1, 1 ); label = new QLabel( i18n( "Password:" ), this ); mPasswordEdit = new KLineEdit( this ); mPasswordEdit->setEchoMode( QLineEdit::Password ); mainLayout->addWidget( label, 2, 0 ); mainLayout->addWidget( mPasswordEdit, 2, 1 ); } void ResourceExampleConfig::loadSettings( KRES::Resource *resource ) { ResourceExample *res = dynamic_cast( resource ); if ( res ) { mHostEdit->setText( res->host() ); mPasswordEdit->setText( res->password() ); } else kDebug() << "ERROR: ResourceExampleConfig::loadSettings(): no ResourceExample, cast failed"; } void ResourceExampleConfig::saveSettings( KRES::Resource *resource ) { ResourceExample *res = dynamic_cast( resource ); if ( res ) { res->setHost( mHostEdit->text() ); res->setPassword( mPasswordEdit->text() ); } else kDebug() << "ERROR: ResourceExampleConfig::saveSettings(): no ResourceExample, cast failed"; } \endcode resourceexample.desktop: \code [Desktop Entry] Type=Service [Misc] Encoding=UTF-8 Name=Example Resource [Plugin] Type=exchange X-KDE-Library=resourceexample \endcode Makefile.am \code kde_module_LTLIBRARIES = resourceexample.la resourceexample_la_SOURCES = resourceexample.cpp resourceexampleconfig.cpp resourceexample_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN) resourceexample_la_LIBADD = -lkresources servicedir = $(kde_datadir)/resources/example service_DATA = resourceexample.desktop \endcode */ // DOXYGEN_REFERENCES=kdecore kdeui