kitchensync

kcalkonnector.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 <calendarsyncee.h>
00023 
00024 #include <kconfig.h>
00025 #include <kgenericfactory.h>
00026 #include <konnectorinfo.h>
00027 #include <synchistory.h>
00028 
00029 #include <libkcal/resourcecalendar.h>
00030 #include <libkdepim/kpimprefs.h>
00031 
00032 #include "kcalkonnector.h"
00033 #include "kcalkonnectorconfig.h"
00034 
00035 using namespace KSync;
00036 
00037 extern "C"
00038 {
00039   void *init_libkcalkonnector()
00040   {
00041     KGlobal::locale()->insertCatalogue( "konnector_kcal" );
00042     return new KRES::PluginFactory<KCalKonnector,KCalKonnectorConfig>();
00043   }
00044 }
00045 
00046 
00047 KCalKonnector::KCalKonnector( const KConfig *config )
00048     : Konnector( config ), mConfigWidget( 0 ), mResource( 0 )
00049 {
00050   if ( config ) {
00051     mResourceIdentifier = config->readEntry( "CurrentResource" );
00052   }
00053 
00054   mMd5sum = generateMD5Sum( mResourceIdentifier ) + "_kcalkonnector.log";
00055 
00056   mCalendar = new KCal::CalendarResources( KPimPrefs::timezone() );
00057 
00058   mResource = createResource( mResourceIdentifier );
00059 
00060   if ( mResource ) {
00061     mCalendar->resourceManager()->add( mResource );
00062     connect( mResource, SIGNAL( resourceLoaded( ResourceCalendar* ) ),
00063              SLOT( loadingFinished() ) );
00064     connect( mResource, SIGNAL( resourceSaved( ResourceCalendar* ) ),
00065              SLOT( savingFinished() ) );
00066 
00067     mCalendarSyncee = new CalendarSyncee( mCalendar );
00068     mCalendarSyncee->setTitle( i18n( "Calendar" ) );
00069     mCalendarSyncee->setIdentifier( "calendar" );
00070 
00071     mSyncees.append( mCalendarSyncee );
00072   }
00073 }
00074 
00075 KCalKonnector::~KCalKonnector()
00076 {
00077   delete mCalendar;
00078 }
00079 
00080 void KCalKonnector::writeConfig( KConfig *config )
00081 {
00082   Konnector::writeConfig( config );
00083 
00084   config->writeEntry( "CurrentResource", mResourceIdentifier );
00085 }
00086 
00087 bool KCalKonnector::readSyncees()
00088 {
00089   if ( mCalendar->resourceManager()->isEmpty() )
00090     return false;
00091 
00092   mCalendarSyncee->reset();
00093 
00094   mCalendar->close();
00095   mCalendar->load();
00096 
00097   return true;
00098 }
00099 
00100 bool KCalKonnector::connectDevice()
00101 {
00102   return true;
00103 }
00104 
00105 bool KCalKonnector::disconnectDevice()
00106 {
00107   return true;
00108 }
00109 
00110 KSync::KonnectorInfo KCalKonnector::info() const
00111 {
00112   return KonnectorInfo( i18n( "Calendar Konnector" ),
00113                         QIconSet(),
00114                         "korganizer",
00115                         false );
00116 }
00117 
00118 QStringList KCalKonnector::supportedFilterTypes() const
00119 {
00120   return QStringList( "calendar" );
00121 }
00122 
00123 bool KCalKonnector::writeSyncees()
00124 {
00125   if ( mCalendar->resourceManager()->isEmpty() )
00126     return false;
00127 
00128   /* remove the deleted entries before saving */
00129   purgeRemovedEntries( mCalendarSyncee );
00130   KCal::CalendarResources::Ticket *ticket = mCalendar->requestSaveTicket( mResource );
00131   if ( !ticket ) {
00132     kdWarning() << "KCalKonnector::writeSyncees(). Couldn't get ticket for resource." << endl;
00133     return false;
00134   }
00135 
00136   mCalendar->save( ticket );
00137 
00138   return true;
00139 }
00140 
00141 void KCalKonnector::loadingFinished()
00142 {
00143   CalendarSyncHistory helper( mCalendarSyncee, storagePath()+"/"+mMd5sum );
00144   helper.load();
00145   emit synceesRead( this );
00146 }
00147 
00148 void KCalKonnector::savingFinished()
00149 {
00150   CalendarSyncHistory helper( mCalendarSyncee, storagePath()+"/"+mMd5sum );
00151   helper.save();
00152   emit synceesWritten( this );
00153 }
00154 
00155 KCal::ResourceCalendar* KCalKonnector::createResource( const QString &identifier )
00156 {
00157   KConfig config( "kresources/calendar/stdrc" );
00158 
00159   config.setGroup( "General" );
00160   QStringList activeKeys = config.readListEntry( "ResourceKeys" );
00161   if ( !activeKeys.contains( identifier ) )
00162     return 0;
00163 
00164   KRES::Factory *factory = KRES::Factory::self( "calendar" );
00165   config.setGroup( "Resource_" + identifier );
00166 
00167   QString type = config.readEntry( "ResourceType" );
00168   QString name = config.readEntry( "ResourceName" );
00169   KCal::ResourceCalendar *resource = dynamic_cast<KCal::ResourceCalendar*>( factory->resource( type, &config ) );
00170   if ( !resource ) {
00171     kdError() << "Failed to create resource with id " << identifier << endl;
00172     return 0;
00173   }
00174 
00175   return resource;
00176 }
00177 
00178 #include "kcalkonnector.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys