kitchensync

localkonnector.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "localkonnector.h"
00023 
00024 #include "localkonnectorconfig.h"
00025 
00026 #include <calendarsyncee.h>
00027 #include <addressbooksyncee.h>
00028 #include <bookmarksyncee.h>
00029 #include <synchistory.h>
00030 
00031 #include <libkdepim/kpimprefs.h>
00032 #include <libkdepim/progressmanager.h>
00033 
00034 #include <kabc/resourcefile.h>
00035 
00036 #include <konnectorinfo.h>
00037 
00038 #include <kconfig.h>
00039 #include <kgenericfactory.h>
00040 
00041 using namespace KSync;
00042 
00043 extern "C"
00044 {
00045   void *init_liblocalkonnector()
00046   {
00047     KGlobal::locale()->insertCatalogue( "konnector_local" );
00048     return new KRES::PluginFactory<LocalKonnector,LocalKonnectorConfig>();
00049   }
00050 }
00051 
00052 
00053 LocalKonnector::LocalKonnector( const KConfig *config )
00054     : Konnector( config ), mConfigWidget( 0 ),
00055       mCalendar( KPimPrefs::timezone() ), mProgressItem( 0 )
00056 {
00057   if ( config ) {
00058     mCalendarFile = config->readPathEntry( "CalendarFile" );
00059     mAddressBookFile = config->readPathEntry( "AddressBookFile" );
00060     mBookmarkFile = config->readPathEntry( "BookmarkFile" );
00061   }
00062 
00063   mMd5sumCal = generateMD5Sum( mCalendarFile ) + "_localkonnector_cal.log";
00064   mMd5sumAbk = generateMD5Sum( mAddressBookFile ) + "_localkonnector_abk.log";
00065   mMd5sumBkm = generateMD5Sum( mBookmarkFile ) + "_localkonnector_bkm.log";
00066 
00067   mAddressBookSyncee = new AddressBookSyncee( &mAddressBook );
00068   mAddressBookSyncee->setTitle( i18n( "Local" ) );
00069 
00070   mCalendarSyncee = new CalendarSyncee( &mCalendar );
00071   mCalendarSyncee->setTitle( i18n( "Local" ) );
00072 
00073   mSyncees.append( mCalendarSyncee );
00074   mSyncees.append( mAddressBookSyncee );
00075   mSyncees.append( new BookmarkSyncee( &mBookmarkManager ) );
00076 
00077   mAddressBookResourceFile = new KABC::ResourceFile( mAddressBookFile );
00078   mAddressBook.addResource( mAddressBookResourceFile );
00079 }
00080 
00081 LocalKonnector::~LocalKonnector()
00082 {
00083 }
00084 
00085 void LocalKonnector::writeConfig( KConfig *config )
00086 {
00087   Konnector::writeConfig( config );
00088 
00089   config->writePathEntry( "CalendarFile", mCalendarFile );
00090   config->writePathEntry( "AddressBookFile", mAddressBookFile );
00091   config->writePathEntry( "BookmarkFile", mAddressBookFile );
00092 }
00093 
00094 bool LocalKonnector::readSyncees()
00095 {
00096   kdDebug() << "LocalKonnector::readSyncee()" << endl;
00097 
00098   mProgressItem = progressItem( i18n( "Start loading local data..." ) );
00099 
00100   if ( !mCalendarFile.isEmpty() ) {
00101     kdDebug() << "LocalKonnector::readSyncee(): calendar: " << mCalendarFile
00102               << endl;
00103     mCalendar.close();
00104     mProgressItem->setStatus( i18n( "Load Calendar..." ) );
00105     if ( mCalendar.load( mCalendarFile ) ) {
00106       kdDebug() << "Read succeeded." << endl;
00107       mCalendarSyncee->reset();
00108       mCalendarSyncee->setIdentifier( mCalendarFile );
00109       kdDebug() << "IDENTIFIER: " << mCalendarSyncee->identifier() << endl;
00110 
00111       /* apply SyncInformation here this will also create the SyncEntries */
00112       CalendarSyncHistory cHelper(  mCalendarSyncee, storagePath() + "/"+mMd5sumCal );
00113       cHelper.load();
00114       mProgressItem->setStatus( i18n( "Calendar loaded." ) );
00115     } else {
00116       mProgressItem->setStatus( i18n( "Loading calendar failed." ) );
00117       emit synceeReadError( this );
00118       kdDebug() << "Read failed." << endl;
00119       return false;
00120     }
00121   }
00122 
00123   mProgressItem->setProgress( 50 );
00124 
00125     if ( !mAddressBookFile.isEmpty() ) {
00126       kdDebug() << "LocalKonnector::readSyncee(): addressbook: "
00127                 << mAddressBookFile << endl;
00128 
00129       mProgressItem->setStatus( i18n( "Load AddressBook..." ) );
00130       mAddressBookResourceFile->setFileName( mAddressBookFile );
00131       if ( !mAddressBook.load() ) {
00132         mProgressItem->setStatus( i18n( "Loading AddressBook failed." ) );
00133         emit synceeReadError( this );
00134         kdDebug() << "Read failed." << endl;
00135         return false;
00136       }
00137 
00138       kdDebug() << "Read succeeded." << endl;
00139 
00140       mAddressBookSyncee->reset();
00141       mAddressBookSyncee->setIdentifier( mAddressBook.identifier() );
00142       kdDebug() << "IDENTIFIER: " << mAddressBookSyncee->identifier() << endl;
00143 
00144       KABC::AddressBook::Iterator it;
00145       for ( it = mAddressBook.begin(); it != mAddressBook.end(); ++it ) {
00146         KSync::AddressBookSyncEntry entry( *it, mAddressBookSyncee );
00147         mAddressBookSyncee->addEntry( entry.clone() );
00148       }
00149 
00150       /* let us apply Sync Information */
00151       AddressBookSyncHistory aHelper( mAddressBookSyncee, storagePath() + "/"+mMd5sumAbk );
00152       aHelper.load();
00153       mProgressItem->setStatus( i18n( "AddressBook loaded." ) );
00154     }
00155 
00156   // TODO: Read Bookmarks
00157   mProgressItem->setProgress( 100 );
00158   mProgressItem->setComplete();
00159   mProgressItem = 0;
00160 
00161   emit synceesRead( this );
00162 
00163   return true;
00164 }
00165 
00166 bool LocalKonnector::connectDevice()
00167 {
00168   return true;
00169 }
00170 
00171 bool LocalKonnector::disconnectDevice()
00172 {
00173   return true;
00174 }
00175 
00176 KSync::KonnectorInfo LocalKonnector::info() const
00177 {
00178   return KonnectorInfo( i18n("Dummy Konnector"),
00179                         QIconSet(),
00180                         "agenda", // icon name
00181                         false );
00182 }
00183 
00184 QStringList LocalKonnector::supportedFilterTypes() const
00185 {
00186   QStringList types;
00187   types << "addressbook" << "calendar" << "bookmarks";
00188 
00189   return types;
00190 }
00191 
00192 bool LocalKonnector::writeSyncees()
00193 {
00194   if ( !mCalendarFile.isEmpty() ) {
00195     purgeRemovedEntries( mCalendarSyncee );
00196 
00197     if ( !mCalendar.save( mCalendarFile ) ) return false;
00198     CalendarSyncHistory cHelper(  mCalendarSyncee, storagePath() + "/"+mMd5sumCal );
00199     cHelper.save();
00200   }
00201 
00202   if ( !mAddressBookFile.isEmpty() ) {
00203     purgeRemovedEntries( mAddressBookSyncee );
00204     KABC::Ticket *ticket;
00205     ticket = mAddressBook.requestSaveTicket( mAddressBookResourceFile );
00206     if ( !ticket ) {
00207       kdWarning() << "LocalKonnector::writeSyncees(). Couldn't get ticket for "
00208                   << "addressbook." << endl;
00209       emit synceeWriteError( this );
00210       return false;
00211     }
00212     if ( !mAddressBook.save( ticket ) ) return false;
00213     AddressBookSyncHistory aHelper( mAddressBookSyncee, storagePath() + "/"+mMd5sumAbk );
00214     aHelper.save();
00215   }
00216 
00217   // TODO: Write Bookmarks
00218 
00219   emit synceesWritten( this );
00220 
00221   return true;
00222 }
00223 
00224 
00225 #include "localkonnector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys