kpilot Library API Documentation

mal-conduit.cc

00001 /*
00002 ** MAL-conduit.cc
00003 **
00004 ** Copyright (C) 2002 by Reinhold Kainhofer
00005 */
00006 
00007 /*
00008 ** This program is free software; you can redistribute it and/or modify
00009 ** it under the terms of the GNU General Public License as published by
00010 ** the Free Software Foundation; either version 2 of the License, or
00011 ** (at your option) any later version.
00012 **
00013 ** This program is distributed in the hope that it will be useful,
00014 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016 ** GNU General Public License for more details.
00017 **
00018 ** You should have received a copy of the GNU General Public License
00019 ** along with this program in a file called COPYING; if not, write to
00020 ** the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00021 ** MA 02111-1307, USA.
00022 **
00023 **
00024 ** Specific permission is granted for this code to be linked to libmal
00025 ** (this is necessary because the libmal license is not GPL-compatible).
00026 */
00027  
00028 /*
00029 ** Bug reports and questions can be sent to kde-pim@kde.org
00030 */
00031  
00032 
00033 
00034 
00035 #include "options.h"
00036 
00037 #include <kconfig.h>
00038 #include <kdebug.h>
00039 
00040 #include "mal-factory.h"
00041 #include "mal-conduit.moc"
00042 #include <libmal.h>
00043 
00044 
00045 // Something to allow us to check what revision
00046 // the modules are that make up a binary distribution.
00047 const char *MAL_conduit_id =
00048         "$Id: mal-conduit.cc,v 1.6.2.6 2003/03/26 18:14:41 adridg Exp $";
00049 
00050 
00051 static MALConduit *conduitInstance=0L;
00052 
00053 int malconduit_logf(const char *format, ...)
00054 {
00055         FUNCTIONSETUP;
00056         va_list val;
00057         int rval;
00058         va_start(val, format);
00059 #define WRITE_MAX_BUF   4096
00060         char msg[WRITE_MAX_BUF];
00061         msg[0]='\0';
00062         rval=vsnprintf(&msg[0], sizeof(msg), format, val);
00063         va_end(val);
00064         if (rval == -1) {
00065                 msg[WRITE_MAX_BUF-1] = '\0';
00066                 rval=WRITE_MAX_BUF-1;
00067         }
00068         if (conduitInstance)
00069         {
00070                 conduitInstance->printLogMessage(msg);
00071         }
00072         else
00073         {
00074                 // write out to stderr
00075                 kdWarning()<<msg<<endl;
00076         }
00077         return rval;
00078 }
00079  
00080 int32 cbTask (void *out,
00081         int32 *returnErrorCode,
00082         char *currentTask,
00083         AGBool bufferable)
00084 {
00085     if (currentTask) {
00086         malconduit_logf ("%s\n", currentTask);
00087     }
00088 
00089     return AGCLIENT_CONTINUE;
00090 }
00091 
00092 static int32 cbItem (void *out,
00093         int32 *returnErrorCode,
00094         int32 currentItemNumber,
00095         int32 totalItemCount,
00096         char *currentItem)
00097 {
00098     malconduit_logf (".");
00099 
00100     if (currentItemNumber == totalItemCount) {
00101         malconduit_logf ("\n");
00102     }
00103 
00104     return AGCLIENT_CONTINUE;
00105 }
00106 
00107  
00108 MALConduit::MALConduit(KPilotDeviceLink * o,
00109         const char *n, 
00110         const QStringList & a) :
00111         ConduitAction(o, n, a)
00112 {
00113         FUNCTIONSETUP;
00114         conduitInstance=this;
00115 #ifdef DEBUG
00116         DEBUGCONDUIT<<MAL_conduit_id<<endl;
00117 #endif
00118 }
00119 
00120 
00121 
00122 MALConduit::~MALConduit()
00123 {
00124         FUNCTIONSETUP;
00125 }
00126 
00127 
00128 
00129 void MALConduit::readConfig()
00130 {
00131         FUNCTIONSETUP;
00132         QDateTime dt;
00133         KConfigGroupSaver g(fConfig, MALConduitFactory::group());
00134         fLastSync = fConfig->readDateTimeEntry(MALConduitFactory::lastSync(), &dt);
00135 #ifdef DEBUG
00136         DEBUGCONDUIT<<"Last sync was "<<fLastSync.toString()<<endl;
00137 #endif
00138 
00139         eSyncTime=(eSyncTimeEnum) fConfig->readNumEntry(MALConduitFactory::syncTime(), (int) eEverySync );
00140 
00141         // Proxy settings
00142         eProxyType=(eProxyTypeEnum) fConfig->readNumEntry(MALConduitFactory::proxyType(), (int) eProxyNone );
00143         fProxyServer=fConfig->readEntry(MALConduitFactory::proxyServer(), QString::null);
00144         
00145         fProxyPort=fConfig->readNumEntry(MALConduitFactory::proxyPort(), 0);
00146         fProxyUser=fConfig->readEntry(MALConduitFactory::proxyUser(), QString::null);
00147         fProxyPassword=fConfig->readEntry(MALConduitFactory::proxyPassword(), QString::null);
00148 
00149         // MAL Server settings (not yet possible!!!)
00150         fMALServer=fConfig->readEntry(MALConduitFactory::malServer(), "sync.avantgo.com");
00151         fMALPort=fConfig->readNumEntry(MALConduitFactory::malPort(), 0);
00152         
00153         fMALUser=fConfig->readEntry(MALConduitFactory::malUser(), QString::null);
00154         fMALPassword=fConfig->readEntry(MALConduitFactory::malPassword(), QString::null);
00155 }
00156 
00157 
00158 
00159 void MALConduit::saveConfig()
00160 {
00161         FUNCTIONSETUP;
00162         KConfigGroupSaver g(fConfig, MALConduitFactory::group());
00163         
00164         fConfig->writeEntry(MALConduitFactory::lastSync(), QDateTime::currentDateTime());
00165 }
00166 
00167 
00168 
00169 bool MALConduit::skip() 
00170 {
00171         QDateTime now=QDateTime::currentDateTime();
00172         if (!fLastSync.isValid() || !now.isValid()) return false;
00173         
00174         switch (eSyncTime) 
00175         {
00176                 case eEveryHour:
00177                         if ( (fLastSync.secsTo(now)<=3600) && (fLastSync.time().hour()==now.time().hour()) ) return true;
00178                         else return false;
00179                 case eEveryDay:
00180                         if ( fLastSync.date() == now.date() ) return true;
00181                         else return false;
00182                 case eEveryWeek:
00183                         if ( (fLastSync.daysTo(now)<=7)  && ( fLastSync.date().dayOfWeek()<=now.date().dayOfWeek()) ) return true;
00184                         else return false;
00185                 case eEveryMonth:
00186                         if ( (fLastSync.daysTo(now)<=31) && (fLastSync.date().month()==now.date().month()) ) return true;
00187                         else return false;
00188                 case eEverySync:
00189                 default:
00190                         return false;
00191         }
00192         return false;
00193 }
00194 
00195 
00196 
00197 /* virtual */ bool MALConduit::exec()
00198 {
00199         FUNCTIONSETUP;
00200 
00201         if (!fConfig)
00202         {
00203                 kdWarning() << k_funcinfo << ": No config file was set!" << endl;
00204                 return false;
00205         }
00206 
00207         readConfig();
00208         
00209         // TODO: set the log/error message hooks of libmal here!!!
00210 
00211         if (skip()) 
00212         {
00213                 emit logMessage(i18n("Skipping MAL sync, because last synchronization was not long enough ago."));
00214                 emit syncDone(this);
00215                 return true;
00216         }
00217         
00218         PalmSyncInfo* pInfo=syncInfoNew();
00219         if (!pInfo) {
00220                 kdWarning() << k_funcinfo << ": Could not allocate SyncInfo!" << endl;
00221                 emit logError(i18n("MAL synchronization failed (no SyncInfo)."));
00222                 return false;
00223         }
00224         
00225         // Set all proxy settings
00226         switch (eProxyType) 
00227         {
00228                 case eProxyHTTP:
00229                         if (fProxyServer.isEmpty()) break;
00230 #ifdef DEBUG
00231                         DEBUGCONDUIT<<" Using HTTP proxy server \""<<fProxyServer<<"\", Port "<<fProxyPort<<", User "<<fProxyUser<<", Password "<<( (fProxyPassword.isEmpty())?QString("not "):QString())<<"set"<<endl;
00232 #endif
00233                                 // XXX not sure if this is right
00234                                 pInfo->httpProxy = const_cast<char *>(fProxyServer.latin1());
00235                         if (fProxyPort>0 && fProxyPort<65536) pInfo->httpProxyPort = fProxyPort;
00236                         else pInfo->httpProxyPort = 80;
00237                         
00238                         if (!fProxyUser.isEmpty()) 
00239                         {
00240                                         // XXX not sure if this is right
00241                                         pInfo->proxyUsername = const_cast<char *>(fProxyUser.latin1());
00242                                         // XXX not sure if this is right
00243                                         if (!fProxyPassword.isEmpty()) pInfo->proxyPassword = const_cast<char *>(fProxyPassword.latin1());
00244                         }
00245                         break;
00246                 case eProxySOCKS:
00247   #ifdef DEBUG
00248                         DEBUGCONDUIT<<" Using SOCKS proxy server \""<<fProxyServer<<"\",  Port "<<fProxyPort<<", User "<<fProxyUser<<", Password "<<( (fProxyPassword.isEmpty())?QString("not "):QString() )<<"set"<<endl;
00249   #endif
00250                         // XXX not sure if this is right
00251                         pInfo->socksProxy = const_cast<char *>(fProxyServer.latin1());
00252                         if (fProxyPort>0 && fProxyPort<65536) pInfo->socksProxyPort = fProxyPort;
00253                         else pInfo->socksProxyPort = 1080;
00254                         break; 
00255                 default:
00256                         break;
00257         }
00258 
00259 
00260         // Now initiate the sync.
00261         pInfo->sd = pilotSocket();
00262         pInfo->taskFunc = cbTask;
00263         pInfo->itemFunc = cbItem;
00264         malsync(pInfo);
00265         syncInfoFree(pInfo);
00266 
00267         saveConfig();
00268         emit syncDone(this);
00269         return true;
00270 }
00271 
00272 void MALConduit::printLogMessage(QString msg)
00273 {
00274         FUNCTIONSETUP;
00275         emit logMessage(msg);
00276 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:44 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001