summaryrefslogtreecommitdiffstats
path: root/kpilot/conduits/malconduit/mal-conduit.cc
blob: 95c4d64b5ff383f9b8ffccc88a45cd9119da1203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
** MAL conduit for KPilot
**
** Copyright (C) 2002 by Reinhold Kainhofer
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
**
**
** Specific permission is granted for this code to be linked to libmal
** (this is necessary because the libmal license is not GPL-compatible).
*/
 
/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/
 



#include "options.h"

#include <tqregexp.h>
#include <kconfig.h>
#include <kdebug.h>

#include "mal-factory.h"
#include "mal-conduit.moc"
#include <libmal.h>
#include "malconduitSettings.h"


static MALConduit *conduitInstance=0L;

int malconduit_logf(const char *, ...) __attribute__ ((format (printf, 1, 2)));

int malconduit_logf(const char *format, ...)
{
	FUNCTIONSETUP;
	va_list val;
	int rval;
	va_start(val, format);
#define WRITE_MAX_BUF	4096
	char msg[WRITE_MAX_BUF];
	msg[0]='\0';
	rval=vsnprintf(&msg[0], sizeof(msg), format, val);
	va_end(val);
	if (rval == -1) {
		msg[WRITE_MAX_BUF-1] = '\0';
		rval=WRITE_MAX_BUF-1;
	}
	if (conduitInstance)
	{
		conduitInstance->printLogMessage(msg);
	}
	else
	{
		// write out to stderr
		WARNINGKPILOT<< msg << endl;
	}
	return rval;
}
 
#ifndef LIBMAL20
int32 cbTask (void * /*out*/,
        int32 * /*returnErrorCode*/,
        char *currentTask,
        AGBool /*bufferable*/)
{
    if (currentTask) {
        malconduit_logf ("%s\n", currentTask);
    }

    return AGCLIENT_CONTINUE;
}

static int32 cbItem (void */*out*/,
        int32 * /*returnErrorCode*/,
        int32   /*currentItemNumber*/,
        int32   /*totalItemCount*/,
        char *  /*currentItem*/)
{
//	The log widget only supports writing out whole lines. You just can't add a single character 
//	to the last line. Thus I completely remove the pseudo-percentbar.
/*    malconduit_logf (".");

    if (currentItemNumber == totalItemCount) {
        malconduit_logf ("\n");
    }
*/
    return AGCLIENT_CONTINUE;
}
#endif

 
MALConduit::MALConduit(KPilotLink * o,
	const char *n, 
	const TQStringList & a) :
	ConduitAction(o, n, a)
{
	FUNCTIONSETUP;
#ifdef LIBMAL20
	register_printStatusHook(malconduit_logf);
	register_printErrorHook(malconduit_logf);
#endif
	conduitInstance=this;
	fConduitName=i18n("MAL");
}



MALConduit::~MALConduit()
{
	FUNCTIONSETUP;
}



void MALConduit::readConfig()
{
	FUNCTIONSETUP;
	MALConduitSettings::self()->readConfig();
#ifdef DEBUG
	DEBUGKPILOT<<"Last sync was "<<MALConduitSettings::lastMALSync().toString()<<endl;
#endif
}



void MALConduit::saveConfig()
{
	FUNCTIONSETUP;
	MALConduitSettings::setLastMALSync( TQDateTime::currentDateTime() );
	MALConduitSettings::self()->writeConfig();
}



bool MALConduit::skip() 
{
	TQDateTime now=TQDateTime::currentDateTime();
	TQDateTime lastSync=MALConduitSettings::lastMALSync();
	
	if (!lastSync.isValid() || !now.isValid()) return false;
	
	switch ( MALConduitSettings::syncFrequency() ) 
	{
		case MALConduitSettings::eEveryHour:
			if ( (lastSync.secsTo(now)<=3600) && (lastSync.time().hour()==now.time().hour()) ) return true;
			else return false;
		case MALConduitSettings::eEveryDay:
			if ( lastSync.date() == now.date() ) return true;
			else return false;
		case MALConduitSettings::eEveryWeek:
			if ( (lastSync.daysTo(now)<=7)  && ( lastSync.date().dayOfWeek()<=now.date().dayOfWeek()) ) return true;
			else return false;
		case MALConduitSettings::eEveryMonth:
			if ( (lastSync.daysTo(now)<=31) && (lastSync.date().month()==now.date().month()) ) return true;
			else return false;
		case MALConduitSettings::eEverySync:
		default:
			return false;
	}
	return false;
}



/* virtual */ bool MALConduit::exec()
{
	FUNCTIONSETUP;

	readConfig();
	
	// TODO: set the log/error message hooks of libmal here!!!

	if (skip()) 
	{
		emit logMessage(i18n("Skipping MAL sync, because last synchronization was not long enough ago."));
		emit syncDone(this);
		return true;
	}
	
	// Now initiate the sync.
	PalmSyncInfo* pInfo=syncInfoNew();
	if (!pInfo) {
		WARNINGKPILOT << "Could not allocate SyncInfo!" << endl;
		emit logError(i18n("MAL synchronization failed (no SyncInfo)."));
		return false;
	}

	TQString proxyServer( MALConduitSettings::proxyServer() );
	int proxyPort( MALConduitSettings::proxyPort() );
	TQString syncMessage;
	bool canContinue = true;
	// Set all proxy settings
	switch (MALConduitSettings::proxyType()) 
	{
		case MALConduitSettings::eProxyHTTP:
			if (proxyServer.isEmpty()) 
			{
				canContinue = false;
				syncMessage = i18n("No proxy server is set.");
				break;
			}
			syncMessage = i18n("Using proxy server: %1").arg(proxyServer);

#ifdef DEBUG
			DEBUGKPILOT<<" Using HTTP proxy server \""<<proxyServer<<
				"\", Port "<<proxyPort<<", User "<<MALConduitSettings::proxyUser()<<
				", Password "<<( (MALConduitSettings::proxyPassword().isEmpty())?TQString("not "):TQString())<<"set"
				<<endl;
#endif
#ifdef LIBMAL20
			setHttpProxy(const_cast<char *>(proxyServer.latin1()));
			if (proxyPort>0 && proxyPort<65536) setHttpProxyPort( proxyPort );
			else setHttpProxyPort(80);
#else
			pInfo->httpProxy = new char[ proxyServer.length() + 1 ];
			strlcpy( pInfo->httpProxy, proxyServer.latin1(), proxyServer.length() + 1);
			if (proxyPort>0 && proxyPort<65536) pInfo->httpProxyPort = proxyPort;
			else pInfo->httpProxyPort = 80;
#endif
			
			if (!MALConduitSettings::proxyUser().isEmpty()) 
			{
#ifdef LIBMAL20
				setProxyUsername( const_cast<char *>(MALConduitSettings::proxyUser().latin1()) );
				if (!MALConduitSettings::proxyPassword().isEmpty()) setProxyPassword( const_cast<char *>(MALConduitSettings::proxyPassword().latin1()) );
#else
				pInfo->proxyUsername = new char[ MALConduitSettings::proxyUser().length() + 1 ];
				strlcpy( pInfo->proxyUsername, MALConduitSettings::proxyUser().latin1(), MALConduitSettings::proxyUser().length() + 1);
				if (!MALConduitSettings::proxyPassword().isEmpty()) {
//						pInfo->proxyPassword = MALConduitSettings::proxyPassword().latin1();
					pInfo->proxyPassword = new char[ MALConduitSettings::proxyPassword().length() + 1 ];
					strlcpy( pInfo->proxyPassword, MALConduitSettings::proxyPassword().latin1(), MALConduitSettings::proxyPassword().length() + 1);
				}
#endif
			}
			break;
		case MALConduitSettings::eProxySOCKS:
			if (proxyServer.isEmpty()) 
			{
				canContinue = false;
				syncMessage = i18n("No SOCKS proxy is set.");
				break;
			}
			syncMessage = i18n("Using SOCKS proxy: %1").arg(proxyServer);
#ifdef DEBUG
			DEBUGKPILOT<<" Using SOCKS proxy server \""<<proxyServer<<"\",  Port "<<proxyPort<<", User "<<MALConduitSettings::proxyUser()<<", Password "<<( (MALConduitSettings::proxyPassword().isEmpty())?TQString("not "):TQString() )<<"set"<<endl;
#endif
#ifdef LIBMAL20
			setSocksProxy( const_cast<char *>(proxyServer.latin1()) );
			if (proxyPort>0 && proxyPort<65536) setSocksProxyPort( proxyPort );
			else setSocksProxyPort(1080);
#else
//			pInfo->socksProxy = proxyServer.latin1();
			pInfo->socksProxy = new char[ proxyServer.length() + 1 ];
			strlcpy( pInfo->socksProxy, proxyServer.latin1(), proxyServer.length() + 1);
			if (proxyPort>0 && proxyPort<65536) pInfo->socksProxyPort = proxyPort;
			else pInfo->socksProxyPort = 1080;
#endif
			break; 
		default:
			break;
	}

	logMessage(syncMessage);

	if (!canContinue)
	{
		return false;
	}

#ifdef LIBMAL20
	malsync( pilotSocket(), pInfo);
#else
	pInfo->sd = pilotSocket();
	pInfo->taskFunc = cbTask;
	pInfo->itemFunc = cbItem;
	malsync( pInfo );
	delete[] pInfo->httpProxy;
	delete[] pInfo->proxyUsername;
	delete[] pInfo->proxyPassword;
	delete[] pInfo->socksProxy;
	syncInfoFree(pInfo);
#endif

	saveConfig();
	return delayDone();
}

void MALConduit::printLogMessage(TQString msg)
{
	FUNCTIONSETUP;
	// Remove the pseudo-progressbar:
	TQString newmsg(msg);
	newmsg.replace( TQRegExp("^\\s*\\.*\\s*"), "");
	newmsg.replace( TQRegExp("\\s*\\.*\\s*$"), "");
	if (newmsg.length()>0)
	{
		emit logMessage(newmsg);
	}
}