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 <qregexp.h>
00038
#include <kconfig.h>
00039
#include <kdebug.h>
00040
00041
#include "mal-factory.h"
00042
#include "mal-conduit.moc"
00043
#include <libmal.h>
00044
00045
00046
00047
00048
const char *MAL_conduit_id =
00049
"$Id: mal-conduit.cc,v 1.17 2003/10/13 13:43:38 kainhofe Exp $";
00050
00051
00052
static MALConduit *conduitInstance=0L;
00053
00054
int malconduit_logf(
const char *, ...) __attribute__ ((format (printf, 1, 2)));
00055
00056
int malconduit_logf(const
char *format, ...)
00057 {
00058 FUNCTIONSETUP;
00059 va_list val;
00060
int rval;
00061 va_start(val, format);
00062
#define WRITE_MAX_BUF 4096
00063
char msg[WRITE_MAX_BUF];
00064 msg[0]=
'\0';
00065 rval=vsnprintf(&msg[0],
sizeof(msg), format, val);
00066 va_end(val);
00067
if (rval == -1) {
00068 msg[WRITE_MAX_BUF-1] =
'\0';
00069 rval=WRITE_MAX_BUF-1;
00070 }
00071
if (conduitInstance)
00072 {
00073 conduitInstance->printLogMessage(msg);
00074 }
00075
else
00076 {
00077
00078 kdWarning()<<msg<<endl;
00079 }
00080
return rval;
00081 }
00082
00083
00084 MALConduit::MALConduit(KPilotDeviceLink * o,
00085
const char *n,
00086
const QStringList & a) :
00087
ConduitAction(o, n, a)
00088 {
00089 FUNCTIONSETUP;
00090
#ifdef LIBMAL20
00091
register_printStatusHook(malconduit_logf);
00092 register_printErrorHook(malconduit_logf);
00093
#endif
00094
conduitInstance=
this;
00095
#ifdef DEBUG
00096
DEBUGCONDUIT<<MAL_conduit_id<<endl;
00097
#endif
00098
fConduitName=i18n(
"MAL");
00099 }
00100
00101
00102
00103 MALConduit::~MALConduit()
00104 {
00105 FUNCTIONSETUP;
00106 }
00107
00108
00109
00110
void MALConduit::readConfig()
00111 {
00112 FUNCTIONSETUP;
00113 QDateTime dt;
00114 KConfigGroupSaver g(fConfig, MALConduitFactory::group());
00115 fLastSync = fConfig->readDateTimeEntry(MALConduitFactory::lastSync(), &dt);
00116
#ifdef DEBUG
00117
DEBUGCONDUIT<<
"Last sync was "<<fLastSync.toString()<<endl;
00118
#endif
00119
00120 eSyncTime=(eSyncTimeEnum) fConfig->readNumEntry(MALConduitFactory::syncTime(), (
int) eEverySync );
00121
00122
00123 eProxyType=(eProxyTypeEnum) fConfig->readNumEntry(MALConduitFactory::proxyType(), (
int) eProxyNone );
00124 fProxyServer=fConfig->readEntry(MALConduitFactory::proxyServer());
00125
00126 fProxyPort=fConfig->readNumEntry(MALConduitFactory::proxyPort(), 0);
00127 fProxyUser=fConfig->readEntry(MALConduitFactory::proxyUser());
00128 fProxyPassword=fConfig->readEntry(MALConduitFactory::proxyPassword());
00129
00130
00131 fMALServer=fConfig->readEntry(MALConduitFactory::malServer(),
"sync.avantgo.com");
00132 fMALPort=fConfig->readNumEntry(MALConduitFactory::malPort(), 0);
00133
00134 fMALUser=fConfig->readEntry(MALConduitFactory::malUser());
00135 fMALPassword=fConfig->readEntry(MALConduitFactory::malPassword());
00136 }
00137
00138
00139
00140
void MALConduit::saveConfig()
00141 {
00142 FUNCTIONSETUP;
00143 KConfigGroupSaver g(fConfig, MALConduitFactory::group());
00144
00145 fConfig->writeEntry(MALConduitFactory::lastSync(), QDateTime::currentDateTime());
00146 }
00147
00148
00149
00150
bool MALConduit::skip()
00151 {
00152 QDateTime now=QDateTime::currentDateTime();
00153
if (!fLastSync.isValid() || !now.isValid())
return false;
00154
00155
switch (eSyncTime)
00156 {
00157
case eEveryHour:
00158
if ( (fLastSync.secsTo(now)<=3600) && (fLastSync.time().hour()==now.time().hour()) )
return true;
00159
else return false;
00160
case eEveryDay:
00161
if ( fLastSync.date() == now.date() )
return true;
00162
else return false;
00163
case eEveryWeek:
00164
if ( (fLastSync.daysTo(now)<=7) && ( fLastSync.date().dayOfWeek()<=now.date().dayOfWeek()) )
return true;
00165
else return false;
00166
case eEveryMonth:
00167
if ( (fLastSync.daysTo(now)<=31) && (fLastSync.date().month()==now.date().month()) )
return true;
00168
else return false;
00169
case eEverySync:
00170
default:
00171
return false;
00172 }
00173
return false;
00174 }
00175
00176
00177
00178
bool MALConduit::exec()
00179 {
00180 FUNCTIONSETUP;
00181 DEBUGCONDUIT<<MAL_conduit_id<<endl;
00182
00183
if (!fConfig)
00184 {
00185 kdWarning() << k_funcinfo <<
": No config file was set!" << endl;
00186
return false;
00187 }
00188
00189 readConfig();
00190
00191
00192
00193
if (skip())
00194 {
00195 emit logMessage(i18n(
"Skipping MAL sync, because last synchronization was not long enough ago."));
00196 emit syncDone(
this);
00197
return true;
00198 }
00199
00200
00201
00202 PalmSyncInfo* pInfo=syncInfoNew();
00203
if (!pInfo) {
00204 kdWarning() << k_funcinfo <<
": Could not allocate SyncInfo!" << endl;
00205 emit logError(i18n(
"MAL synchronization failed (no SyncInfo)."));
00206
return false;
00207 }
00208
00209
00210
switch (eProxyType)
00211 {
00212
case eProxyHTTP:
00213
if (fProxyServer.isEmpty())
break;
00214
#ifdef DEBUG
00215
DEBUGCONDUIT<<
" Using HTTP proxy server \""<<fProxyServer<<
"\", Port "<<fProxyPort<<
", User "<<fProxyUser<<
", Password "<<( (fProxyPassword.isEmpty())?QString(
"not "):QString())<<"set"<<endl;
00216
#endif
00217
#ifdef LIBMAL20
00218
setHttpProxy(const_cast<char *>(fProxyServer.latin1()));
00219
if (fProxyPort>0 && fProxyPort<65536) setHttpProxyPort( fProxyPort );
00220
else setHttpProxyPort(80);
00221
#else
00222
pInfo->httpProxy =
new char[ fProxyServer.length() + 1 ];
00223 strncpy( pInfo->httpProxy, fProxyServer.latin1(), fProxyServer.length() );
00224
if (fProxyPort>0 && fProxyPort<65536) pInfo->httpProxyPort = fProxyPort;
00225
else pInfo->httpProxyPort = 80;
00226
#endif
00227
00228
if (!fProxyUser.isEmpty())
00229 {
00230
#ifdef LIBMAL20
00231
setProxyUsername( const_cast<char *>(fProxyUser.latin1()) );
00232
if (!fProxyPassword.isEmpty()) setProxyPassword( const_cast<char *>(fProxyPassword.latin1()) );
00233
#else
00234
pInfo->proxyUsername =
new char[ fProxyUser.length() + 1 ];
00235 strncpy( pInfo->proxyUsername, fProxyUser.latin1(), fProxyUser.length() );
00236
00237
if (!fProxyPassword.isEmpty()) {
00238
00239 pInfo->proxyPassword =
new char[ fProxyPassword.length() + 1 ];
00240 strncpy( pInfo->proxyPassword, fProxyPassword.latin1(), fProxyPassword.length() );
00241 }
00242
#endif
00243
}
00244
break;
00245
case eProxySOCKS:
00246
#ifdef DEBUG
00247
DEBUGCONDUIT<<
" Using SOCKS proxy server \""<<fProxyServer<<
"\", Port "<<fProxyPort<<
", User "<<fProxyUser<<
", Password "<<( (fProxyPassword.isEmpty())?QString(
"not "):QString() )<<"set"<<endl;
00248
#endif
00249
#ifdef LIBMAL20
00250
setSocksProxy( const_cast<char *>(fProxyServer.latin1()) );
00251
if (fProxyPort>0 && fProxyPort<65536) setSocksProxyPort( fProxyPort );
00252
else setSocksProxyPort(1080);
00253
#else
00254
00255 pInfo->socksProxy =
new char[ fProxyServer.length() + 1 ];
00256 strncpy( pInfo->socksProxy, fProxyServer.latin1(), fProxyServer.length() );
00257
if (fProxyPort>0 && fProxyPort<65536) pInfo->socksProxyPort = fProxyPort;
00258
else pInfo->socksProxyPort = 1080;
00259
#endif
00260
break;
00261
default:
00262
break;
00263 }
00264
00265
#ifdef LIBMAL20
00266
malsync( pilotSocket(), pInfo);
00267
#else
00268
00269
00270
00271
00272 pInfo->pilot_rHandle = pilotSocket();
00273
delete[] pInfo->httpProxy;
00274
delete[] pInfo->proxyUsername;
00275
delete[] pInfo->proxyPassword;
00276
delete[] pInfo->socksProxy;
00277 syncInfoFree(pInfo);
00278
#endif
00279
00280 saveConfig();
00281 emit syncDone(
this);
00282
return true;
00283 }
00284
00285
void MALConduit::printLogMessage(QString msg)
00286 {
00287 FUNCTIONSETUP;
00288
00289 QString newmsg(msg);
00290 newmsg.replace( QRegExp(
"^\\s*\\.*\\s*"),
"");
00291 newmsg.replace( QRegExp(
"\\s*\\.*\\s*$"),
"");
00292
if (newmsg.length()>0)
00293 {
00294 emit logMessage(newmsg);
00295 }
00296 }
00297