summaryrefslogtreecommitdiffstats
path: root/kpilot/kpilot/kroupware.cc
blob: fc57b4b5cc5bb2b61e86ec2f4fc8b42fe0b7203b (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
/* KPilot
**
** Copyright still to be determined.
**
** This file defines the actions taken when KPilot
** is Kroupware-enabled. Basically it just does a
** little communication with the local Kroupware agent (KMail).
*/

/*
** 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.
*/

/*
** Bug reports and questions can be sent to kde-pim@kde.org
*/

#include "options.h"

#include <qfile.h>

#include <dcopclient.h>
#include <ktempfile.h>


#include <kapplication.h>
#include "kroupware.h"
#include "kpilotConfig.h"

KroupwareSync::KroupwareSync(bool pre,int parts,KPilotDeviceLink *p) :
	SyncAction(p,pre ? "KroupwarePreSync" : "KroupwarePostSync"),
	fPre(pre),
	fParts(parts)
{

}

/* virtual */ bool KroupwareSync::exec()
{
	FUNCTIONSETUP;
	if (fPre)
	{
		preSync();
	}
	else
	{
		postSync();
	}
	// delayDone();
	emit syncDone(this);
	return true;
}

void KroupwareSync::cleanupConfig()
{
  // tempfile check in case app has terminated during sync
  // TODO!!! Use sensitive groups/keys for the kroupware branch...
  KConfig* c = KPilotSettings::self()->config();
  c->setGroup("todoOptions");
  if ( c->hasKey( "CalFileBackup") ) {
    QString fn = c->readPathEntry( "CalFileBackup" );
    if ( fn != CSL1("empty") ) {
      c->writePathEntry( "CalFile" ,fn );
      c->writeEntry( "CalFileBackup" , "empty" );
    }
  }
  c->setGroup("vcalOptions");
  if ( c->hasKey( "CalFileBackup") ) {
    QString fn = c->readPathEntry( "CalFileBackup" );
    if ( fn != CSL1("empty") ) {
      c->writePathEntry( "CalFile" ,fn );
      c->writeEntry( "CalFileBackup" , "empty" );
    }
  }
  c->setGroup("Abbrowser-conduit");
  c->writeEntry( "KMailTempFile" , "empty" );
  KPilotSettings::writeConfig();
}

// For the log messages, I've added i18n to the
// ones I consider relevant for the user. The rest is
// really debug info, and shouldn't go to the normal
// sync log for the user.
//
// TODO!!! better way to read the config options!
void KroupwareSync::start_syncCal_TodosWithKMail( bool cal, bool todos )
{
  if ( !cal && ! todos )
    return;
  KConfig*c = KPilotSettings::self()->config();
  DCOPClient *client = kapp->dcopClient();
  KTempFile  tempfile;
  QString filename = tempfile.name();
  QByteArray  data, reply_data;
  QCString reply_type;
  QDataStream arg(data, IO_WriteOnly);
  arg << filename;
  if (!client->call( "kmail" ,
		     "KOrganizerSyncIface",
		     "pullSyncData(QString)",
		     data,
		     reply_type,
		     reply_data)) {
    logMessage( CSL1("Calling KMail over DCOP failed!" ));
    logMessage(CSL1("Not syncing calendars with KMail"));
    logMessage(CSL1("Not syncing to-dos with KMail"));
  }
  else {
    logMessage(CSL1("Calling Cal/Todo over DCOP succeeded"));
    // now prepare for syncing
    _syncWithKMail = true;
    if ( todos ) {
      logMessage( i18n("Syncing to-dos with KMail" ));
      c->setGroup("todoOptions");
      QString fn = c->readPathEntry( "CalFile" );
      c->writePathEntry( "CalFileBackup" ,fn );
      c->writePathEntry( "CalFile" ,filename );
    }
    else
      logMessage( CSL1("Not syncing todos with KMail" ));
    if ( cal ) {
      logMessage( i18n("Syncing calendar with KMail" ));
      c->setGroup("vcalOptions");
      QString fn = c->readPathEntry( "CalFile" );
      c->writePathEntry( "CalFileBackup" ,fn );
      c->writePathEntry( "CalFile" ,filename );
    }
    else
      logMessage( CSL1("Not syncing calendar with KMail" ));
  }
  KPilotSettings::self()->writeConfig();
}

void KroupwareSync::start_syncAddWithKMail()
{
  logMessage( CSL1("Syncing Addresses with KMail" ));
  DCOPClient *client = kapp->dcopClient();
  KTempFile  tempfile;
  QString filename = tempfile.name();
  QByteArray  data, reply_data;
  QCString reply_type;
  QDataStream arg(data, IO_WriteOnly);
  arg << filename;
  if (!client->call( "kmail" ,
		     "KMailIface",
		     "requestAddresses(QString)",
		     data,
		     reply_type,
		     reply_data)) {
    logMessage(CSL1("Calling KMail over DCOP failed!" ));
    logMessage(CSL1("Not syncing Addresses with KMail"));
  }
  else {
    // TODO!!! better config handling!
    KConfig*c = KPilotSettings::self()->config();
    logMessage(CSL1("Calling addresses over DCOP succeeded"));
    c->setGroup("Abbrowser-conduit");
    c->writePathEntry( "KMailTempFile" , filename );
    KPilotSettings::self()->writeConfig();
  }
}
void KroupwareSync::start_syncNotesWithKMail()
{
  logMessage( i18n("Syncing Notes with Mail" ));
  logMessage( CSL1("Syncing Notes-sorry not implemented" ));
}

void KroupwareSync::end_syncCal_TodosWithKMail( bool cal, bool todos)
{
 if ( !cal && ! todos )
    return;
 QString filename;
 KConfig*c=KPilotSettings::self()->config();
 if ( todos ) {
   logMessage( i18n("Rewriting to-dos to KMail..." ));
   c->setGroup("todoOptions");
   filename = c->readPathEntry( "CalFile" );
   c->writePathEntry( "CalFile", c->readPathEntry( "CalFileBackup" ) );
   c->writeEntry( "CalFileBackup", "empty");
 }
 if ( cal ) {
   logMessage( i18n("Rewriting Calendar to KMail" ));
   c->setGroup("vcalOptions");
   filename = c->readPathEntry( "CalFile" );
   QString tf = c->readPathEntry( "CalFileBackup" ) ;
   c->writePathEntry( "CalFile" , tf  );
   c->writeEntry( "CalFileBackup" ,"empty");
 }
 KPilotSettings::writeConfig();
 if ( !filename.isEmpty() ) {
   logMessage(CSL1("Try to call KMail via DCOP to finish sync..."));
   // try DCOP connection to KMail
   DCOPClient *client = kapp->dcopClient();
   QByteArray  data, reply_data;
   QCString reply_type;
   QDataStream arg(data, IO_WriteOnly);
   arg << filename;
   if (!client->call( "kmail" /*"korganizer" kmdcop */,
		      "KOrganizerSyncIface",
		      "pushSyncData(QString)",
		      data,
		      reply_type,
		      reply_data)) {
     logMessage( CSL1("Calling KMail over DCOP failed!" ));
     logMessage( CSL1("Sync is not complete"));
     logMessage( CSL1("Data from Palm stored in file:"));
     logMessage(filename);
   } else {
     logMessage(CSL1("Calling over DCOP succeeded"));
     logMessage(CSL1("Sync to KMail has finished successfully"));
   }
   QFile::remove( filename );
 }
}
void KroupwareSync::end_syncAddWithKMail()
{
  logMessage( i18n("Syncing KMail with Addresses " ));
  DCOPClient *client = kapp->dcopClient();
  // TODO!! better config handling (KConfig XT)
  KConfig*c = KPilotSettings::self()->config();
  c->setGroup("Abbrowser-conduit");
  QString filename = c->readPathEntry( "KMailTempFile" );
  c->writeEntry( "KMailTempFile" , "empty" );
  KPilotSettings::writeConfig();
  QByteArray  data, reply_data;
  QCString reply_type;
  QDataStream arg(data, IO_WriteOnly);
  arg << filename;
  arg << QStringList();
  if (!client->call( "kmail" ,
		     "KMailIface",
		     "storeAddresses(QString, QStringList)",
		     data,
		     reply_type,
		     reply_data)) {
    logMessage(CSL1("Calling KMail over DCOP failed!" ));
    logMessage(CSL1("Not syncing Addresses with KMail"));
  }
  else {
    logMessage(CSL1("Calling  store addresses over DCOP succeeded"));
  }
  //QFile::remove( filename );
}
void KroupwareSync::end_syncNotesWithKMail()
{
  logMessage( i18n("Syncing KMail with Notes" ));
  logMessage( CSL1("Syncing Notes-sorry not implemented" ));
}



/* static */ bool KroupwareSync::startKMail(QString *error)
{
	FUNCTIONSETUP;

	QCString kmdcop;
	QString mess;
	int pid;

	return KApplication::startServiceByDesktopName(CSL1("kmail"),
						      QString::null,
						      error,
						      &kmdcop,
						      &pid
						      )==0;
}


void KroupwareSync::preSync()
{
	cleanupConfig();
	start_syncCal_TodosWithKMail( fParts & Cal, fParts & Todo );
	if (fParts & Notes)
	{
		start_syncNotesWithKMail();
	}
	if (fParts & Address)
	{
		start_syncAddWithKMail();
	}
}

void KroupwareSync::postSync()
{
	cleanupConfig();
	end_syncCal_TodosWithKMail( fParts & Cal, fParts & Todo );
	if (fParts & Notes)
	{
		end_syncNotesWithKMail();
	}
	if (fParts & Address)
	{
		end_syncAddWithKMail();
	}
}