kitchensync

remotekonnector.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2004 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 "remotekonnector.h"
00023 
00024 #include "remotekonnectorconfig.h"
00025 
00026 #include <calendarsyncee.h>
00027 #include <addressbooksyncee.h>
00028 #include <bookmarksyncee.h>
00029 #include <synchistory.h>
00030 
00031 #include <kabc/vcardconverter.h>
00032 #include <libkcal/icalformat.h>
00033 #include <libkdepim/kabcresourcenull.h>
00034 #include <libkdepim/kpimprefs.h>
00035 
00036 #include <konnectorinfo.h>
00037 
00038 #include <kconfig.h>
00039 #include <kgenericfactory.h>
00040 
00041 using namespace KSync;
00042 using namespace KABC;
00043 using namespace KCal;
00044 
00045 extern "C"
00046 {
00047   void *init_libremotekonnector()
00048   {
00049     KGlobal::locale()->insertCatalogue( "konnector_remote" );
00050     return new KRES::PluginFactory<RemoteKonnector,RemoteKonnectorConfig>();
00051   }
00052 }
00053 
00054 
00055 RemoteKonnector::RemoteKonnector( const KConfig *config )
00056     : Konnector( config ), mConfigWidget( 0 ),
00057     mCalendar( KPimPrefs::timezone() )
00058 {
00059   mAddressBook.addResource( new KABC::ResourceNull() );
00060   if ( config ) {
00061     mCalendarUrl = config->readPathEntry( "CalendarUrl" );
00062     mAddressBookUrl = config->readPathEntry( "AddressBookUrl" );
00063     mBookmarkUrl = config->readPathEntry( "BookmarkUrl" );
00064   }
00065 
00066   mMd5sumCal = generateMD5Sum( mCalendarUrl ) +    "_remotekonnector_cal.log";
00067   mMd5sumBkm = generateMD5Sum( mBookmarkUrl ) +    "_remotekonnector_bkm.log";
00068   mMd5sumAbk = generateMD5Sum( mAddressBookUrl ) + "_remotekonnector_abk.log";
00069 
00070   mAddressBookSyncee =  new AddressBookSyncee( &mAddressBook );
00071   mAddressBookSyncee->setTitle( i18n( "Remote" ) );
00072   mCalendarSyncee = new CalendarSyncee( &mCalendar );
00073   mCalendarSyncee->setTitle( i18n( "Remote" ) );
00074 
00075   mSyncees.append( mCalendarSyncee );
00076   mSyncees.append( mAddressBookSyncee );
00077   mSyncees.append( new BookmarkSyncee( &mBookmarkManager ) );
00078 }
00079 
00080 RemoteKonnector::~RemoteKonnector()
00081 {
00082 }
00083 
00084 void RemoteKonnector::writeConfig( KConfig *config )
00085 {
00086   Konnector::writeConfig( config );
00087 
00088   config->writePathEntry( "CalendarUrl", mCalendarUrl );
00089   config->writeEntry( "AddressBookUrl", mAddressBookUrl );
00090   config->writeEntry( "BookmarkFile", mAddressBookUrl );
00091 }
00092 
00093 bool RemoteKonnector::readSyncees()
00094 {
00095   mSynceeReadCount = 0;
00096 
00097   if ( !mCalendarUrl.isEmpty() ) {
00098     mCalendarData = "";
00099 
00100     KIO::TransferJob *job = KIO::get( KURL( mCalendarUrl ) );
00101     connect( job, SIGNAL( result( KIO::Job * ) ),
00102              SLOT( slotCalendarReadResult( KIO::Job * ) ) );
00103     connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
00104              SLOT( slotCalendarData( KIO::Job *, const QByteArray & ) ) );
00105 
00106     ++mSynceeReadCount;
00107   }
00108 
00109   if ( !mAddressBookUrl.isEmpty() ) {
00110     mAddressBookData = "";
00111 
00112     KIO::TransferJob *job = KIO::get( KURL( mAddressBookUrl ) );
00113     connect( job, SIGNAL( result( KIO::Job * ) ),
00114              SLOT( slotAddressBookReadResult( KIO::Job * ) ) );
00115     connect( job, SIGNAL( data( KIO::Job *, const QByteArray & ) ),
00116              SLOT( slotAddressBookData( KIO::Job *, const QByteArray & ) ) );
00117 
00118     ++mSynceeReadCount;
00119   }
00120 
00121   // TODO: Read Bookmarks
00122 
00123   return true;
00124 }
00125 
00126 void RemoteKonnector::slotCalendarData( KIO::Job *, const QByteArray &d )
00127 {
00128   mCalendarData += QString::fromUtf8( d );
00129 }
00130 
00131 void RemoteKonnector::slotCalendarReadResult( KIO::Job *job )
00132 {
00133   --mSynceeReadCount;
00134 
00135   if ( job->error() ) {
00136     job->showErrorDialog( 0 );
00137     emit synceeReadError( this );
00138   } else {
00139     mCalendar.close();
00140     ICalFormat ical;
00141     if ( ical.fromString( &mCalendar, mCalendarData ) ) {
00142       mCalendarSyncee->reset();
00143       mCalendarSyncee->setIdentifier( mCalendarUrl );
00144     } else {
00145       emit synceeReadError( this );
00146     }
00147   }
00148 
00149   finishRead();
00150 }
00151 
00152 void RemoteKonnector::slotAddressBookData( KIO::Job *, const QByteArray &d )
00153 {
00154   mAddressBookData += QString::fromUtf8( d );
00155 }
00156 
00157 void RemoteKonnector::slotAddressBookReadResult( KIO::Job *job )
00158 {
00159   --mSynceeReadCount;
00160 
00161   if ( job->error() ) {
00162     job->showErrorDialog( 0 );
00163     emit synceeReadError( this );
00164   } else {
00165     mAddressBook.clear();
00166     VCardConverter v;
00167     Addressee::List a = v.parseVCards( mAddressBookData );
00168     Addressee::List::ConstIterator it;
00169     for( it = a.begin(); it != a.end(); ++it ) {
00170       mAddressBook.insertAddressee( *it );
00171       KSync::AddressBookSyncEntry entry( *it, mAddressBookSyncee );
00172       mAddressBookSyncee->addEntry( entry.clone() );
00173     }
00174   }
00175 
00176   finishRead();
00177 }
00178 
00179 void RemoteKonnector::finishRead()
00180 {
00181   if ( mSynceeReadCount > 0 ) return;
00182 
00183 
00184   CalendarSyncHistory cHelper( mCalendarSyncee, storagePath()+"/"+mMd5sumCal );
00185   cHelper.load();
00186 
00187   AddressBookSyncHistory aHelper( mAddressBookSyncee, storagePath()+"/"+mMd5sumAbk);
00188   aHelper.load();
00189 
00190   emit synceesRead( this );
00191 }
00192 
00193 bool RemoteKonnector::connectDevice()
00194 {
00195   return true;
00196 }
00197 
00198 bool RemoteKonnector::disconnectDevice()
00199 {
00200   return true;
00201 }
00202 
00203 KSync::KonnectorInfo RemoteKonnector::info() const
00204 {
00205   return KonnectorInfo( i18n("Remote Konnector"),
00206                         QIconSet(),
00207                         "agenda", // icon name
00208                         false );
00209 }
00210 
00211 QStringList RemoteKonnector::supportedFilterTypes() const
00212 {
00213   QStringList types;
00214   types << "addressbook" << "calendar" << "bookmarks";
00215 
00216   return types;
00217 }
00218 
00219 bool RemoteKonnector::writeSyncees()
00220 {
00221   mSynceeWriteCount = 0;
00222 
00223   if ( !mCalendarUrl.isEmpty() ) {
00224     purgeRemovedEntries( mCalendarSyncee );
00225 
00226     ICalFormat ical;
00227     mCalendarData = ical.toString( &mCalendar );
00228     if ( !mCalendarData.isEmpty() ) {
00229       KIO::TransferJob *job = KIO::put( KURL( mCalendarUrl ), -1, true, false );
00230       connect( job, SIGNAL( result( KIO::Job * ) ),
00231                SLOT( slotCalendarWriteResult( KIO::Job * ) ) );
00232       connect( job, SIGNAL( dataReq( KIO::Job *, QByteArray & ) ),
00233                SLOT( slotCalendarDataReq( KIO::Job *, QByteArray & ) ) );
00234 
00235       ++mSynceeWriteCount;
00236     }
00237   }
00238 
00239   if ( !mAddressBookUrl.isEmpty() ) {
00240     purgeRemovedEntries( mAddressBookSyncee );
00241 
00242     mAddressBookData = "";
00243 
00244     VCardConverter v;
00245     AddressBook::ConstIterator it;
00246     for ( it = mAddressBook.begin(); it != mAddressBook.end(); ++it ) {
00247       mAddressBookData.append( v.createVCard( *it ) );
00248     }
00249 
00250     if ( !mAddressBookData.isEmpty() ) {
00251       KIO::TransferJob *job = KIO::put( KURL( mAddressBookUrl ), -1, true,
00252                                         false );
00253       connect( job, SIGNAL( result( KIO::Job * ) ),
00254                SLOT( slotAddressBookWriteResult( KIO::Job * ) ) );
00255       connect( job, SIGNAL( dataReq( KIO::Job *, QByteArray & ) ),
00256                SLOT( slotAddressBookDataReq( KIO::Job *, QByteArray & ) ) );
00257 
00258       ++mSynceeWriteCount;
00259     }
00260   }
00261 
00262   // TODO: Write Bookmarks
00263 
00264   return true;
00265 }
00266 
00267 void RemoteKonnector::slotCalendarDataReq( KIO::Job *, QByteArray &d )
00268 {
00269   if ( !mCalendarData.isEmpty() ) {
00270     d = mCalendarData.utf8();
00271     mCalendarData = QString::null;
00272   }
00273 }
00274 
00275 void RemoteKonnector::slotCalendarWriteResult( KIO::Job *job )
00276 {
00277   --mSynceeWriteCount;
00278 
00279   if ( job->error() ) {
00280     job->showErrorDialog( 0 );
00281     emit synceeWriteError( this );
00282   }
00283 
00284   finishWrite();
00285 }
00286 
00287 void RemoteKonnector::slotAddressBookDataReq( KIO::Job *, QByteArray &d )
00288 {
00289   if ( !mAddressBookData.isEmpty() ) {
00290     d = mAddressBookData.utf8();
00291     mAddressBookData = QString::null;
00292   }
00293 }
00294 
00295 void RemoteKonnector::slotAddressBookWriteResult( KIO::Job *job )
00296 {
00297   --mSynceeWriteCount;
00298 
00299   if ( job->error() ) {
00300     job->showErrorDialog( 0 );
00301     emit synceeWriteError( this );
00302   }
00303 
00304   finishWrite();
00305 }
00306 
00307 void RemoteKonnector::finishWrite()
00308 {
00309   if ( mSynceeWriteCount > 0 ) return;
00310 
00311 
00312   CalendarSyncHistory cHelper( mCalendarSyncee, storagePath()+"/"+mMd5sumCal );
00313   cHelper.save();
00314 
00315   AddressBookSyncHistory aHelper( mAddressBookSyncee, storagePath()+"/"+mMd5sumAbk);
00316   aHelper.save();
00317 
00318   emit synceesWritten( this );
00319 }
00320 
00321 
00322 #include "remotekonnector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys