kpilot

kroupware.cc

Go to the documentation of this file.
00001 /* KPilot
00002 **
00003 ** Copyright still to be determined.
00004 **
00005 ** This file defines the actions taken when KPilot
00006 ** is Kroupware-enabled. Basically it just does a
00007 ** little communication with the local Kroupware agent (KMail).
00008 */
00009 
00010 /*
00011 ** This program is free software; you can redistribute it and/or modify
00012 ** it under the terms of the GNU General Public License as published by
00013 ** the Free Software Foundation; either version 2 of the License, or
00014 ** (at your option) any later version.
00015 **
00016 ** This program is distributed in the hope that it will be useful,
00017 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 ** GNU General Public License for more details.
00020 **
00021 ** You should have received a copy of the GNU General Public License
00022 ** along with this program in a file called COPYING; if not, write to
00023 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 ** MA 02110-1301, USA.
00025 */
00026 
00027 /*
00028 ** Bug reports and questions can be sent to kde-pim@kde.org
00029 */
00030 
00031 #include "options.h"
00032 
00033 #include <qfile.h>
00034 
00035 #include <dcopclient.h>
00036 #include <ktempfile.h>
00037 
00038 
00039 #include <kapplication.h>
00040 #include "kroupware.h"
00041 #include "kpilotConfig.h"
00042 
00043 KroupwareSync::KroupwareSync(bool pre,int parts,KPilotDeviceLink *p) :
00044     SyncAction(p,pre ? "KroupwarePreSync" : "KroupwarePostSync"),
00045     fPre(pre),
00046     fParts(parts)
00047 {
00048 
00049 }
00050 
00051 /* virtual */ bool KroupwareSync::exec()
00052 {
00053     FUNCTIONSETUP;
00054     if (fPre)
00055     {
00056         preSync();
00057     }
00058     else
00059     {
00060         postSync();
00061     }
00062     // delayDone();
00063     emit syncDone(this);
00064     return true;
00065 }
00066 
00067 void KroupwareSync::cleanupConfig()
00068 {
00069   // tempfile check in case app has terminated during sync
00070   // TODO!!! Use sensitive groups/keys for the kroupware branch...
00071   KConfig* c = KPilotSettings::self()->config();
00072   c->setGroup("todoOptions");
00073   if ( c->hasKey( "CalFileBackup") ) {
00074     QString fn = c->readPathEntry( "CalFileBackup" );
00075     if ( fn != CSL1("empty") ) {
00076       c->writePathEntry( "CalFile" ,fn );
00077       c->writeEntry( "CalFileBackup" , "empty" );
00078     }
00079   }
00080   c->setGroup("vcalOptions");
00081   if ( c->hasKey( "CalFileBackup") ) {
00082     QString fn = c->readPathEntry( "CalFileBackup" );
00083     if ( fn != CSL1("empty") ) {
00084       c->writePathEntry( "CalFile" ,fn );
00085       c->writeEntry( "CalFileBackup" , "empty" );
00086     }
00087   }
00088   c->setGroup("Abbrowser-conduit");
00089   c->writeEntry( "KMailTempFile" , "empty" );
00090   KPilotSettings::writeConfig();
00091 }
00092 
00093 // For the log messages, I've added i18n to the
00094 // ones I consider relevant for the user. The rest is
00095 // really debug info, and shouldn't go to the normal
00096 // sync log for the user.
00097 //
00098 // TODO!!! better way to read the config options!
00099 void KroupwareSync::start_syncCal_TodosWithKMail( bool cal, bool todos )
00100 {
00101   if ( !cal && ! todos )
00102     return;
00103   KConfig*c = KPilotSettings::self()->config();
00104   DCOPClient *client = kapp->dcopClient();
00105   KTempFile  tempfile;
00106   QString filename = tempfile.name();
00107   QByteArray  data, reply_data;
00108   QCString reply_type;
00109   QDataStream arg(data, IO_WriteOnly);
00110   arg << filename;
00111   if (!client->call( "kmail" ,
00112              "KOrganizerSyncIface",
00113              "pullSyncData(QString)",
00114              data,
00115              reply_type,
00116              reply_data)) {
00117     logMessage( CSL1("Calling KMail over DCOP failed!" ));
00118     logMessage(CSL1("Not syncing calendars with KMail"));
00119     logMessage(CSL1("Not syncing to-dos with KMail"));
00120   }
00121   else {
00122     logMessage(CSL1("Calling Cal/Todo over DCOP succeeded"));
00123     // now prepare for syncing
00124     _syncWithKMail = true;
00125     if ( todos ) {
00126       logMessage( i18n("Syncing to-dos with KMail" ));
00127       c->setGroup("todoOptions");
00128       QString fn = c->readPathEntry( "CalFile" );
00129       c->writePathEntry( "CalFileBackup" ,fn );
00130       c->writePathEntry( "CalFile" ,filename );
00131     }
00132     else
00133       logMessage( CSL1("Not syncing todos with KMail" ));
00134     if ( cal ) {
00135       logMessage( i18n("Syncing calendar with KMail" ));
00136       c->setGroup("vcalOptions");
00137       QString fn = c->readPathEntry( "CalFile" );
00138       c->writePathEntry( "CalFileBackup" ,fn );
00139       c->writePathEntry( "CalFile" ,filename );
00140     }
00141     else
00142       logMessage( CSL1("Not syncing calendar with KMail" ));
00143   }
00144   KPilotSettings::self()->writeConfig();
00145 }
00146 
00147 void KroupwareSync::start_syncAddWithKMail()
00148 {
00149   logMessage( CSL1("Syncing Addresses with KMail" ));
00150   DCOPClient *client = kapp->dcopClient();
00151   KTempFile  tempfile;
00152   QString filename = tempfile.name();
00153   QByteArray  data, reply_data;
00154   QCString reply_type;
00155   QDataStream arg(data, IO_WriteOnly);
00156   arg << filename;
00157   if (!client->call( "kmail" ,
00158              "KMailIface",
00159              "requestAddresses(QString)",
00160              data,
00161              reply_type,
00162              reply_data)) {
00163     logMessage(CSL1("Calling KMail over DCOP failed!" ));
00164     logMessage(CSL1("Not syncing Addresses with KMail"));
00165   }
00166   else {
00167     // TODO!!! better config handling!
00168     KConfig*c = KPilotSettings::self()->config();
00169     logMessage(CSL1("Calling addresses over DCOP succeeded"));
00170     c->setGroup("Abbrowser-conduit");
00171     c->writePathEntry( "KMailTempFile" , filename );
00172     KPilotSettings::self()->writeConfig();
00173   }
00174 }
00175 void KroupwareSync::start_syncNotesWithKMail()
00176 {
00177   logMessage( i18n("Syncing Notes with Mail" ));
00178   logMessage( CSL1("Syncing Notes-sorry not implemented" ));
00179 }
00180 
00181 void KroupwareSync::end_syncCal_TodosWithKMail( bool cal, bool todos)
00182 {
00183  if ( !cal && ! todos )
00184     return;
00185  QString filename;
00186  KConfig*c=KPilotSettings::self()->config();
00187  if ( todos ) {
00188    logMessage( i18n("Rewriting to-dos to KMail..." ));
00189    c->setGroup("todoOptions");
00190    filename = c->readPathEntry( "CalFile" );
00191    c->writePathEntry( "CalFile", c->readPathEntry( "CalFileBackup" ) );
00192    c->writeEntry( "CalFileBackup", "empty");
00193  }
00194  if ( cal ) {
00195    logMessage( i18n("Rewriting Calendar to KMail" ));
00196    c->setGroup("vcalOptions");
00197    filename = c->readPathEntry( "CalFile" );
00198    QString tf = c->readPathEntry( "CalFileBackup" ) ;
00199    c->writePathEntry( "CalFile" , tf  );
00200    c->writeEntry( "CalFileBackup" ,"empty");
00201  }
00202  KPilotSettings::writeConfig();
00203  if ( !filename.isEmpty() ) {
00204    logMessage(CSL1("Try to call KMail via DCOP to finish sync..."));
00205    // try DCOP connection to KMail
00206    DCOPClient *client = kapp->dcopClient();
00207    QByteArray  data, reply_data;
00208    QCString reply_type;
00209    QDataStream arg(data, IO_WriteOnly);
00210    arg << filename;
00211    if (!client->call( "kmail" /*"korganizer" kmdcop */,
00212               "KOrganizerSyncIface",
00213               "pushSyncData(QString)",
00214               data,
00215               reply_type,
00216               reply_data)) {
00217      logMessage( CSL1("Calling KMail over DCOP failed!" ));
00218      logMessage( CSL1("Sync is not complete"));
00219      logMessage( CSL1("Data from Palm stored in file:"));
00220      logMessage(filename);
00221    } else {
00222      logMessage(CSL1("Calling over DCOP succeeded"));
00223      logMessage(CSL1("Sync to KMail has finished successfully"));
00224    }
00225    QFile::remove( filename );
00226  }
00227 }
00228 void KroupwareSync::end_syncAddWithKMail()
00229 {
00230   logMessage( i18n("Syncing KMail with Addresses " ));
00231   DCOPClient *client = kapp->dcopClient();
00232   // TODO!! better config handling (KConfig XT)
00233   KConfig*c = KPilotSettings::self()->config();
00234   c->setGroup("Abbrowser-conduit");
00235   QString filename = c->readPathEntry( "KMailTempFile" );
00236   c->writeEntry( "KMailTempFile" , "empty" );
00237   KPilotSettings::writeConfig();
00238   QByteArray  data, reply_data;
00239   QCString reply_type;
00240   QDataStream arg(data, IO_WriteOnly);
00241   arg << filename;
00242   arg << QStringList();
00243   if (!client->call( "kmail" ,
00244              "KMailIface",
00245              "storeAddresses(QString, QStringList)",
00246              data,
00247              reply_type,
00248              reply_data)) {
00249     logMessage(CSL1("Calling KMail over DCOP failed!" ));
00250     logMessage(CSL1("Not syncing Addresses with KMail"));
00251   }
00252   else {
00253     logMessage(CSL1("Calling  store addresses over DCOP succeeded"));
00254   }
00255   //QFile::remove( filename );
00256 }
00257 void KroupwareSync::end_syncNotesWithKMail()
00258 {
00259   logMessage( i18n("Syncing KMail with Notes" ));
00260   logMessage( CSL1("Syncing Notes-sorry not implemented" ));
00261 }
00262 
00263 
00264 
00265 /* static */ bool KroupwareSync::startKMail(QString *error)
00266 {
00267     FUNCTIONSETUP;
00268 
00269     QCString kmdcop;
00270     QString mess;
00271     int pid;
00272 
00273     return KApplication::startServiceByDesktopName(CSL1("kmail"),
00274                               QString::null,
00275                               error,
00276                               &kmdcop,
00277                               &pid
00278                               )==0;
00279 }
00280 
00281 
00282 void KroupwareSync::preSync()
00283 {
00284     cleanupConfig();
00285     start_syncCal_TodosWithKMail( fParts & Cal, fParts & Todo );
00286     if (fParts & Notes)
00287     {
00288         start_syncNotesWithKMail();
00289     }
00290     if (fParts & Address)
00291     {
00292         start_syncAddWithKMail();
00293     }
00294 }
00295 
00296 void KroupwareSync::postSync()
00297 {
00298     cleanupConfig();
00299     end_syncCal_TodosWithKMail( fParts & Cal, fParts & Todo );
00300     if (fParts & Notes)
00301     {
00302         end_syncNotesWithKMail();
00303     }
00304     if (fParts & Address)
00305     {
00306         end_syncAddWithKMail();
00307     }
00308 }
00309 
00310 
00311