kitchensync

kabckonnector.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2004 Tobias Koenig <tokoe@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 <qtimer.h>
00023 
00024 #include <addressbooksyncee.h>
00025 #include <synchistory.h>
00026 
00027 
00028 #include <kabc/resource.h>
00029 #include <kabc/resourcefile.h>
00030 #include <kconfig.h>
00031 #include <kgenericfactory.h>
00032 #include <konnectorinfo.h>
00033 #include <libkdepim/kabcresourcenull.h>
00034 
00035 #include "kabckonnector.h"
00036 #include "kabckonnectorconfig.h"
00037 
00038 using namespace KSync;
00039 
00040 extern "C"
00041 {
00042   void *init_libkabckonnector()
00043   {
00044     KGlobal::locale()->insertCatalogue( "konnector_kabc" );
00045     return new KRES::PluginFactory<KABCKonnector,KABCKonnectorConfig>();
00046   }
00047 }
00048 
00049 class AddressBookWrapper : public KABC::AddressBook
00050 {
00051   public:
00052     AddressBookWrapper( KABC::AddressBook* );
00053 
00054     KRES::Manager<KABC::Resource>* getResourceManager()
00055     {
00056       return resourceManager();
00057     }
00058 };
00059 
00060 KABCKonnector::KABCKonnector( const KConfig *config )
00061     : Konnector( config ), mConfigWidget( 0 ), mResource( 0 )
00062 {
00063   if ( config ) {
00064     mResourceIdentifier = config->readEntry( "CurrentResource" );
00065   }
00066 
00067   mMd5sum = generateMD5Sum( mResourceIdentifier ) + "_kabckonnector.log";
00068 
00069   mResource = createResource( mResourceIdentifier );
00070   if ( mResource ) {
00071     mAddressBook.addResource( mResource );
00072 
00073     mAddressBookSyncee = new AddressBookSyncee( &mAddressBook );
00074     mAddressBookSyncee->setTitle( i18n( "Address Book" ) );
00075 
00076     mSyncees.append( mAddressBookSyncee );
00077 
00078     connect( mResource, SIGNAL( loadingFinished( Resource* ) ),
00079              SLOT( loadingFinished() ) );
00080   }
00081 }
00082 
00083 KABCKonnector::~KABCKonnector()
00084 {
00085 }
00086 
00087 void KABCKonnector::writeConfig( KConfig *config )
00088 {
00089   Konnector::writeConfig( config );
00090 
00091   config->writeEntry( "CurrentResource", mResourceIdentifier );
00092 }
00093 
00094 bool KABCKonnector::readSyncees()
00095 {
00096   if ( !mResource )
00097     return false;
00098 
00099   if ( !mResource->open() )
00100     return false;
00101 
00102   mResource->asyncLoad();
00103 
00104   return true;
00105 }
00106 
00107 bool KABCKonnector::connectDevice()
00108 {
00109   return true;
00110 }
00111 
00112 bool KABCKonnector::disconnectDevice()
00113 {
00114   return true;
00115 }
00116 
00117 KSync::KonnectorInfo KABCKonnector::info() const
00118 {
00119   return KonnectorInfo( i18n( "Address Book Konnector" ),
00120                         QIconSet(),
00121                         "kaddressbook",
00122                         false );
00123 }
00124 
00125 QStringList KABCKonnector::supportedFilterTypes() const
00126 {
00127   return QStringList( "addressbook" );
00128 }
00129 
00130 bool KABCKonnector::writeSyncees()
00131 {
00132   if ( !mResource )
00133     return false;
00134 
00135   purgeRemovedEntries( mAddressBookSyncee );
00136 
00137   KABC::Ticket *ticket = mAddressBook.requestSaveTicket( mResource );
00138   if ( !ticket ) {
00139     kdWarning() << "KABCKonnector::writeSyncees(). Couldn't get ticket for resource." << endl;
00140     return false;
00141   }
00142 
00143   if ( !mAddressBook.save( ticket ) ) {
00144     kdWarning() << "KABCKonnector::writeSyncees(). Couldn't save resource." << endl;
00145     return false;
00146   }
00147 
00148   AddressBookSyncHistory syncInfo( mAddressBookSyncee, storagePath() + "/" + mMd5sum );
00149   syncInfo.save();
00150 
00151   emit synceesWritten( this );
00152 
00153   return true;
00154 }
00155 
00156 void KABCKonnector::loadingFinished()
00157 {
00158   mAddressBookSyncee->reset();
00159 
00160   KABC::AddressBook::Iterator it;
00161   for ( it = mAddressBook.begin(); it != mAddressBook.end(); ++it ) {
00162     KSync::AddressBookSyncEntry entry( *it, mAddressBookSyncee );
00163     mAddressBookSyncee->addEntry( entry.clone() );
00164   }
00165 
00166   AddressBookSyncHistory syncInfo( mAddressBookSyncee, storagePath() + "/" + mMd5sum );
00167   syncInfo.load();
00168 
00169   emit synceesRead( this );
00170 }
00171 
00172 KABC::Resource* KABCKonnector::createResource( const QString &identifier )
00173 {
00174   KConfig config( "kresources/contact/stdrc" );
00175 
00176   config.setGroup( "General" );
00177   QStringList activeKeys = config.readListEntry( "ResourceKeys" );
00178   if ( !activeKeys.contains( identifier ) )
00179     return 0;
00180 
00181   KRES::Factory *factory = KRES::Factory::self( "contact" );
00182   config.setGroup( "Resource_" + identifier );
00183 
00184   QString type = config.readEntry( "ResourceType" );
00185   QString name = config.readEntry( "ResourceName" );
00186   KABC::Resource *resource = dynamic_cast<KABC::Resource*>( factory->resource( type, &config ) );
00187   if ( !resource ) {
00188     kdError() << "Failed to create resource with id " << identifier << endl;
00189     return 0;
00190   }
00191 
00192   return resource;
00193 }
00194 
00195 #include "kabckonnector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys