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 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
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
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 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
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
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
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
00241 pInfo->proxyUsername = const_cast<char *>(fProxyUser.latin1());
00242
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
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
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 }
This file is part of the documentation for kdelibs Version 3.1.5.