summaryrefslogtreecommitdiffstats
path: root/kresources/scalix/shared/scalixbase.cpp
blob: d40bdb03ff4f6a9cf42888913ad417568bb95dfe (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
    This file is part of the scalix resource - based on the kolab resource.

    Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include "scalixbase.h"

#include <kabc/addressee.h>
#include <libkcal/journal.h>
#include <libtdepim/kpimprefs.h>
#include <kdebug.h>
#include <tqfile.h>

using namespace Scalix;


ScalixBase::ScalixBase( const TQString& tz )
  : mCreationDate( TQDateTime::currentDateTime() ),
    mLastModified( TQDateTime::currentDateTime() ),
    mSensitivity( Public ), mTimeZoneId( tz ),
    mHasPilotSyncId( false ),  mHasPilotSyncStatus( false )
{
}

ScalixBase::~ScalixBase()
{
}

void ScalixBase::setFields( const KCal::Incidence* incidence )
{
  // So far unhandled KCal::IncidenceBase fields:
  // mPilotID, mSyncStatus, mFloats

  setUid( incidence->uid() );
  setBody( incidence->description() );
  setCategories( incidence->categoriesStr() );
  setCreationDate( localToUTC( incidence->created() ) );
  setLastModified( localToUTC( incidence->lastModified() ) );
  setSensitivity( static_cast<Sensitivity>( incidence->secrecy() ) );
  // TODO: Attachments
}

void ScalixBase::saveTo( KCal::Incidence* incidence ) const
{
  incidence->setUid( uid() );
  incidence->setDescription( body() );
  incidence->setCategories( categories() );
  incidence->setCreated( utcToLocal( creationDate() ) );
  incidence->setLastModified( utcToLocal( lastModified() ) );
  incidence->setSecrecy( sensitivity() );
  // TODO: Attachments
}

void ScalixBase::setFields( const KABC::Addressee* addressee )
{
  // An addressee does not have a creation date, so somehow we should
  // make one, if this is a new entry

  setUid( addressee->uid() );
  setBody( addressee->note() );
  setCategories( addressee->categories().join( "," ) );

  // Set creation-time and last-modification-time
  const TQString creationString = addressee->custom( "KOLAB", "CreationDate" );
  kdDebug(5006) << "Creation time string: " << creationString << endl;
  TQDateTime creationDate;
  if ( creationString.isEmpty() ) {
    creationDate = TQDateTime::currentDateTime();
    kdDebug(5006) << "Creation date set to current time\n";
  }
  else {
    creationDate = stringToDateTime( creationString );
    kdDebug(5006) << "Creation date loaded\n";
  }
  TQDateTime modified = addressee->revision();
  if ( !modified.isValid() )
    modified = TQDateTime::currentDateTime();
  setLastModified( modified );
  if ( modified < creationDate ) {
    // It's not possible that the modification date is earlier than creation
    creationDate = modified;
    kdDebug(5006) << "Creation date set to modification date\n";
  }
  setCreationDate( creationDate );
  const TQString newCreationDate = dateTimeToString( creationDate );
  if ( creationString != newCreationDate ) {
    // We modified the creation date, so store it for future reference
    const_cast<KABC::Addressee*>( addressee )
      ->insertCustom( "KOLAB", "CreationDate", newCreationDate );
    kdDebug(5006) << "Creation date modified. New one: " << newCreationDate << endl;
  }

  switch( addressee->secrecy().type() ) {
  case KABC::Secrecy::Private:
    setSensitivity( Private );
    break;
  case KABC::Secrecy::Confidential:
    setSensitivity( Confidential );
    break;
  default:
    setSensitivity( Public );
  }

  // TODO: Attachments
}

void ScalixBase::saveTo( KABC::Addressee* addressee ) const
{
  addressee->setUid( uid() );
  addressee->setNote( body() );
  addressee->setCategories( TQStringList::split( ',', categories() ) );
  addressee->setRevision( lastModified() );
  addressee->insertCustom( "KOLAB", "CreationDate",
                           dateTimeToString( creationDate() ) );

  switch( sensitivity() ) {
  case Private:
    addressee->setSecrecy( KABC::Secrecy( KABC::Secrecy::Private ) );
    break;
  case Confidential:
    addressee->setSecrecy( KABC::Secrecy( KABC::Secrecy::Confidential ) );
    break;
  default:
    addressee->setSecrecy( KABC::Secrecy( KABC::Secrecy::Public ) );
    break;
  }

  // TODO: Attachments
}

void ScalixBase::setUid( const TQString& uid )
{
  mUid = uid;
}

TQString ScalixBase::uid() const
{
  return mUid;
}

void ScalixBase::setBody( const TQString& body )
{
  mBody = body;
}

TQString ScalixBase::body() const
{
  return mBody;
}

void ScalixBase::setCategories( const TQString& categories )
{
  mCategories = categories;
}

TQString ScalixBase::categories() const
{
  return mCategories;
}

void ScalixBase::setCreationDate( const TQDateTime& date )
{
  mCreationDate = date;
}

TQDateTime ScalixBase::creationDate() const
{
  return mCreationDate;
}

void ScalixBase::setLastModified( const TQDateTime& date )
{
  mLastModified = date;
}

TQDateTime ScalixBase::lastModified() const
{
  return mLastModified;
}

void ScalixBase::setSensitivity( Sensitivity sensitivity )
{
  mSensitivity = sensitivity;
}

ScalixBase::Sensitivity ScalixBase::sensitivity() const
{
  return mSensitivity;
}

void ScalixBase::setPilotSyncId( unsigned long id )
{
  mHasPilotSyncId = true;
  mPilotSyncId = id;
}

bool ScalixBase::hasPilotSyncId() const
{
  return mHasPilotSyncId;
}

unsigned long ScalixBase::pilotSyncId() const
{
  return mPilotSyncId;
}

void ScalixBase::setPilotSyncStatus( int status )
{
  mHasPilotSyncStatus = true;
  mPilotSyncStatus = status;
}

bool ScalixBase::hasPilotSyncStatus() const
{
  return mHasPilotSyncStatus;
}

int ScalixBase::pilotSyncStatus() const
{
  return mPilotSyncStatus;
}

bool ScalixBase::loadEmailAttribute( TQDomElement& element, Email& email )
{
  for ( TQDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    if ( n.isComment() )
      continue;
    if ( n.isElement() ) {
      TQDomElement e = n.toElement();
      TQString tagName = e.tagName();

      if ( tagName == "display-name" )
        email.displayName = e.text();
      else if ( tagName == "smtp-address" )
        email.smtpAddress = e.text();
      else
        // TODO: Unhandled tag - save for later storage
        kdDebug() << "Warning: Unhandled tag " << e.tagName() << endl;
    } else
      kdDebug() << "Node is not a comment or an element???" << endl;
  }

  return true;
}

void ScalixBase::saveEmailAttribute( TQDomElement& element, const Email& email,
                                    const TQString& tagName ) const
{
  TQDomElement e = element.ownerDocument().createElement( tagName );
  element.appendChild( e );
  writeString( e, "display-name", email.displayName );
  writeString( e, "smtp-address", email.smtpAddress );
}

bool ScalixBase::loadAttribute( TQDomElement& element )
{
  TQString tagName = element.tagName();

  if ( tagName == "uid" )
    setUid( element.text() );
  else if ( tagName == "body" )
    setBody( element.text() );
  else if ( tagName == "categories" )
    setCategories( element.text() );
  else if ( tagName == "creation-date" )
    setCreationDate( stringToDateTime( element.text() ) );
  else if ( tagName == "last-modification-date" )
    setLastModified( stringToDateTime( element.text() ) );
  else if ( tagName == "sensitivity" )
    setSensitivity( stringToSensitivity( element.text() ) );
  else if ( tagName == "product-id" )
    return true; // ignore this field
  else if ( tagName == "pilot-sync-id" )
    setPilotSyncId( element.text().toULong() );
  else if ( tagName == "pilot-sync-status" )
    setPilotSyncStatus( element.text().toInt() );
  else
    return false;

  // Handled here
  return true;
}

bool ScalixBase::saveAttributes( TQDomElement& element ) const
{
  writeString( element, "product-id", productID() );
  writeString( element, "uid", uid() );
  writeString( element, "body", body() );
  writeString( element, "categories", categories() );
  writeString( element, "creation-date", dateTimeToString( creationDate() ) );
  writeString( element, "last-modification-date",
               dateTimeToString( lastModified() ) );
  writeString( element, "sensitivity", sensitivityToString( sensitivity() ) );
  if ( hasPilotSyncId() )
    writeString( element, "pilot-sync-id", TQString::number( pilotSyncId() ) );
  if ( hasPilotSyncStatus() )
    writeString( element, "pilot-sync-status", TQString::number( pilotSyncStatus() ) );
  return true;
}

bool ScalixBase::load( const TQString& xml )
{
  TQString errorMsg;
  int errorLine, errorColumn;
  TQDomDocument document;
  bool ok = document.setContent( xml, &errorMsg, &errorLine, &errorColumn );

  if ( !ok ) {
    tqWarning( "Error loading document: %s, line %d, column %d",
              errorMsg.latin1(), errorLine, errorColumn );
    return false;
  }

  // XML file loaded into tree. Now parse it
  return loadXML( document );
}

bool ScalixBase::load( TQFile& xml )
{
  TQString errorMsg;
  int errorLine, errorColumn;
  TQDomDocument document;
  bool ok = document.setContent( &xml, &errorMsg, &errorLine, &errorColumn );

  if ( !ok ) {
    tqWarning( "Error loading document: %s, line %d, column %d",
              errorMsg.latin1(), errorLine, errorColumn );
    return false;
  }

  // XML file loaded into tree. Now parse it
  return loadXML( document );
}

TQDomDocument ScalixBase::domTree()
{
  TQDomDocument document;

  TQString p = "version=\"1.0\" encoding=\"UTF-8\"";
  document.appendChild(document.createProcessingInstruction( "xml", p ) );

  return document;
}


TQString ScalixBase::dateTimeToString( const TQDateTime& time )
{
  return time.toString( Qt::ISODate ) + 'Z';
}

TQString ScalixBase::dateToString( const TQDate& date )
{
  return date.toString( Qt::ISODate );
}

TQDateTime ScalixBase::stringToDateTime( const TQString& _date )
{
  TQString date( _date );
  if ( date.endsWith( "Z" ) )
    date.truncate( date.length() - 1 );
  return TQDateTime::fromString( date, Qt::ISODate );
}

TQDate ScalixBase::stringToDate( const TQString& date )
{
  return TQDate::fromString( date, Qt::ISODate );
}

TQString ScalixBase::sensitivityToString( Sensitivity s )
{
  switch( s ) {
  case Private: return "private";
  case Confidential: return "confidential";
  case Public: return "public";
  }

  return "What what what???";
}

ScalixBase::Sensitivity ScalixBase::stringToSensitivity( const TQString& s )
{
  if ( s == "private" )
    return Private;
  if ( s == "confidential" )
    return Confidential;
  return Public;
}

TQString ScalixBase::colorToString( const TQColor& color )
{
  // Color is in the format "#RRGGBB"
  return color.name();
}

TQColor ScalixBase::stringToColor( const TQString& s )
{
  return TQColor( s );
}

void ScalixBase::writeString( TQDomElement& element, const TQString& tag,
                             const TQString& tagString )
{
  if ( !tagString.isEmpty() ) {
    TQDomElement e = element.ownerDocument().createElement( tag );
    TQDomText t = element.ownerDocument().createTextNode( tagString );
    e.appendChild( t );
    element.appendChild( e );
  }
}

TQDateTime ScalixBase::localToUTC( const TQDateTime& time ) const
{
  return KPimPrefs::localTimeToUtc( time, mTimeZoneId );
}

TQDateTime ScalixBase::utcToLocal( const TQDateTime& time ) const
{
  return KPimPrefs::utcToLocalTime( time, mTimeZoneId );
}