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  
00081 MALConduit::MALConduit(KPilotDeviceLink * o,
00082         const char *n, 
00083         const QStringList & a) :
00084         ConduitAction(o, n, a)
00085 {
00086         FUNCTIONSETUP;
00087         register_printStatusHook(malconduit_logf);
00088         register_printErrorHook(malconduit_logf);
00089         conduitInstance=this;
00090 #ifdef DEBUG
00091         DEBUGCONDUIT<<MAL_conduit_id<<endl;
00092 #endif
00093 }
00094 
00095 
00096 
00097 MALConduit::~MALConduit()
00098 {
00099         FUNCTIONSETUP;
00100 }
00101 
00102 
00103 
00104 void MALConduit::readConfig()
00105 {
00106         FUNCTIONSETUP;
00107         QDateTime dt;
00108         KConfigGroupSaver g(fConfig, MALConduitFactory::group());
00109         fLastSync = fConfig->readDateTimeEntry(MALConduitFactory::lastSync(), &dt);
00110 #ifdef DEBUG
00111         DEBUGCONDUIT<<"Last sync was "<<fLastSync.toString()<<endl;
00112 #endif
00113 
00114         eSyncTime=(eSyncTimeEnum) fConfig->readNumEntry(MALConduitFactory::syncTime(), (int) eEverySync );
00115 
00116         // Proxy settings
00117         eProxyType=(eProxyTypeEnum) fConfig->readNumEntry(MALConduitFactory::proxyType(), (int) eProxyNone );
00118         fProxyServer=fConfig->readEntry(MALConduitFactory::proxyServer(), QString::null);
00119         
00120         fProxyPort=fConfig->readNumEntry(MALConduitFactory::proxyPort(), 0);
00121         fProxyUser=fConfig->readEntry(MALConduitFactory::proxyUser(), QString::null);
00122         fProxyPassword=fConfig->readEntry(MALConduitFactory::proxyPassword(), QString::null);
00123 
00124         // MAL Server settings (not yet possible!!!)
00125         fMALServer=fConfig->readEntry(MALConduitFactory::malServer(), "sync.avantgo.com");
00126         fMALPort=fConfig->readNumEntry(MALConduitFactory::malPort(), 0);
00127         
00128         fMALUser=fConfig->readEntry(MALConduitFactory::malUser(), QString::null);
00129         fMALPassword=fConfig->readEntry(MALConduitFactory::malPassword(), QString::null);
00130 }
00131 
00132 
00133 
00134 void MALConduit::saveConfig()
00135 {
00136         FUNCTIONSETUP;
00137         KConfigGroupSaver g(fConfig, MALConduitFactory::group());
00138         
00139         fConfig->writeEntry(MALConduitFactory::lastSync(), QDateTime::currentDateTime());
00140 }
00141 
00142 
00143 
00144 bool MALConduit::skip() 
00145 {
00146         QDateTime now=QDateTime::currentDateTime();
00147         if (!fLastSync.isValid() || !now.isValid()) return false;
00148         
00149         switch (eSyncTime) 
00150         {
00151                 case eEveryHour:
00152                         if ( (fLastSync.secsTo(now)<=3600) && (fLastSync.time().hour()==now.time().hour()) ) return true;
00153                         else return false;
00154                 case eEveryDay:
00155                         if ( fLastSync.date() == now.date() ) return true;
00156                         else return false;
00157                 case eEveryWeek:
00158                         if ( (fLastSync.daysTo(now)<=7)  && ( fLastSync.date().dayOfWeek()<=now.date().dayOfWeek()) ) return true;
00159                         else return false;
00160                 case eEveryMonth:
00161                         if ( (fLastSync.daysTo(now)<=31) && (fLastSync.date().month()==now.date().month()) ) return true;
00162                         else return false;
00163                 case eEverySync:
00164                 default:
00165                         return false;
00166         }
00167         return false;
00168 }
00169 
00170 
00171 
00172 /* virtual */ bool MALConduit::exec()
00173 {
00174         FUNCTIONSETUP;
00175 
00176         if (!fConfig)
00177         {
00178                 kdWarning() << k_funcinfo << ": No config file was set!" << endl;
00179                 return false;
00180         }
00181 
00182         readConfig();
00183         
00184         // TODO: set the log/error message hooks of libmal here!!!
00185 
00186         if (skip()) 
00187         {
00188                 emit logMessage(i18n("Skipping MAL sync, because last synchronization was not long enough ago."));
00189                 emit syncDone(this);
00190                 return true;
00191         }
00192         
00193         
00194         // Set all proxy settings
00195         switch (eProxyType) 
00196         {
00197                 case eProxyHTTP:
00198                         if (fProxyServer.isEmpty()) break;
00199 #ifdef DEBUG
00200                         DEBUGCONDUIT<<" Using HTTP proxy server \""<<fProxyServer<<"\", Port "<<fProxyPort<<", User "<<fProxyUser<<", Password "<<( (fProxyPassword.isEmpty())?QString("not "):QString())<<"set"<<endl;
00201 #endif
00202                                 setHttpProxy(const_cast<char *>(fProxyServer.latin1()));
00203                         if (fProxyPort>0 && fProxyPort<65536) setHttpProxyPort( fProxyPort );
00204                         else setHttpProxyPort(80);
00205                         
00206                         if (!fProxyUser.isEmpty()) 
00207                         {
00208                                         setProxyUsername( const_cast<char *>(fProxyUser.latin1()) );
00209                                         if (!fProxyPassword.isEmpty()) setProxyPassword( const_cast<char *>(fProxyPassword.latin1()) );
00210                         }
00211                         break;
00212                 case eProxySOCKS:
00213   #ifdef DEBUG
00214                         DEBUGCONDUIT<<" Using SOCKS proxy server \""<<fProxyServer<<"\",  Port "<<fProxyPort<<", User "<<fProxyUser<<", Password "<<( (fProxyPassword.isEmpty())?QString("not "):QString() )<<"set"<<endl;
00215   #endif
00216                         setSocksProxy( const_cast<char *>(fProxyServer.latin1()) );
00217                         if (fProxyPort>0 && fProxyPort<65536) setSocksProxyPort( fProxyPort );
00218                         else setSocksProxyPort(1080);
00219                         break; 
00220                 default:
00221                         break;
00222         }
00223 
00224 
00225         // Now initiate the sync.
00226         PalmSyncInfo* pInfo=syncInfoNew();
00227         if (!pInfo) {
00228                 kdWarning() << k_funcinfo << ": Could not allocate SyncInfo!" << endl;
00229                 emit logError(i18n("MAL synchronization failed (no SyncInfo)."));
00230                 return false;
00231         }
00232         malsync( pilotSocket(), pInfo);
00233         syncInfoFree(pInfo);
00234 
00235         saveConfig();
00236         emit syncDone(this);
00237         return true;
00238 }
00239 
00240 void MALConduit::printLogMessage(QString msg)
00241 {
00242         FUNCTIONSETUP;
00243         emit logMessage(msg);
00244 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:14 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001