mal-conduit.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
00046
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
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
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
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 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
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
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
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 }
This file is part of the documentation for kdelibs Version 3.1.4.