summaryrefslogtreecommitdiffstats
path: root/kmail/kmailicalifaceimpl.h
blob: 5a92ffe923abf37b3ace85fa3cdf36015eee1ac1 (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
/*
    This file is part of KMail.

    Copyright (c) 2003 Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
    Copyright (c) 2003 - 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 Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), 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
    Qt.  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.
*/

/** @file This file contains the class KMailICalIfaceImpl which actually
* implements the ICal (DCOP) interface for KMail.
*/

#ifndef KMAILICALIFACEIMPL_H
#define KMAILICALIFACEIMPL_H

#include "kmailicalIface.h"
#include "kmfoldertype.h"
#include <kfoldertree.h>

#include <tqdict.h>
#include <tqguardedptr.h>
#include <tqmap.h>

class KMFolder;
class KMMessage;
class KMFolderDir;
class KMFolderTreeItem;

namespace KMail {

  // Local helper class
class ExtraFolder {
public:
  ExtraFolder( KMFolder* f );
  ~ExtraFolder();
  TQGuardedPtr<KMFolder> folder;
};

class Accumulator {
public:
  Accumulator( const TQString& t, const TQString& f, int c )
  :type( t ), folder( f ), count( c ) {}

  void add( const TQString& incidence ) {
    incidences << incidence;
    count--;
  }
  bool isFull() { return count == 0; }

  const TQString type;
  const TQString folder;
  TQStringList incidences;
  int count;
};

}

/** The implementation of the interface. */
class KMailICalIfaceImpl : public TQObject, virtual public KMailICalIface {
  Q_OBJECT
public:
  KMailICalIfaceImpl();

  bool isWritableFolder( const TQString& type, const TQString& resource );

  StorageFormat storageFormat( const TQString &resource );

  /// Update a kolab storage entry.
  /// If message is not there, it is added and
  /// given the subject as Subject: header.
  /// Returns the new mail serial number,
  /// or 0 if something went wrong,
  Q_UINT32 update( const TQString& resource,
                   Q_UINT32 sernum,
                   const TQString& subject,
                   const TQString& plainTextBody,
                   const TQMap<TQCString, TQString>& customHeaders,
                   const TQStringList& attachmentURLs,
                   const TQStringList& attachmentMimetypes,
                   const TQStringList& attachmentNames,
                   const TQStringList& deletedAttachments );

  bool deleteIncidenceKolab( const TQString& resource,
                             Q_UINT32 sernum );
  int incidencesKolabCount( const TQString& mimetype,
                            const TQString& resource );
  TQMap<Q_UINT32, TQString> incidencesKolab( const TQString& mimetype,
                                           const TQString& resource,
                                           int startIndex,
                                           int nbMessages );

  TQValueList<SubResource> subresourcesKolab( const TQString& contentsType );

  bool triggerSync( const TQString& contentsType );

  // "Get" an attachment. This actually saves the attachment in a file
  // and returns a URL to it
  KURL getAttachment( const TQString& resource,
                      Q_UINT32 sernum,
                      const TQString& filename );

  TQString attachmentMimetype( const TQString &resource,
                              Q_UINT32 sernum,
                              const TQString &filename );

  TQStringList listAttachments( const TQString &resource, Q_UINT32 sernum );


  bool removeSubresource( const TQString& );

  bool addSubresource( const TQString& resource,
                       const TQString& parent,
                       const TQString& contentsType );

  // tell KOrganizer about messages to be deleted
  void msgRemoved( KMFolder*, KMMessage* );

  /** Initialize all folders. */
  void initFolders();

  /** Disconnect all slots and close the dirs. */
  void cleanup();

  /**
   * Returns true if resource mode is enabled and folder is one of the
   * resource folders.
   */
  bool isResourceFolder( KMFolder* folder ) const;

  /* Returns true if the folder is one of the standard resource folders, as
   * opposed to an extra folder. */
  bool isStandardResourceFolder( KMFolder* folder ) const;

  /**
   * Returns true if isResourceFolder( folder ) returns true, and
   * imap folders should be hidden.
   */
  bool hideResourceFolder( KMFolder* folder ) const;

  /**
   * Returns true if the given folder is the root of the groupware account,
   * groupware folders are hidden, and only groupware folders shown in this
   * account.
   */
  bool hideResourceAccountRoot( KMFolder* folder ) const;

  /**
   * Returns the resource folder type. Other is returned if resource
   * isn't enabled or it isn't a resource folder.
   */
  KFolderTreeItem::Type folderType( KMFolder* folder ) const;

  /**
   * Returns the name of the standard icon for a folder of given type or
   * TQString::null if the type is no groupware type.
   */
  TQString folderPixmap( KFolderTreeItem::Type type ) const;

  /** Returns the localized name of a folder of given type.
   */
  TQString folderName( KFolderTreeItem::Type type, int language = -1 ) const;

  /** Get the folder that holds *type* entries */
  KMFolder* folderFromType( const TQString& type, const TQString& folder );

  /** Return the ical type of a folder */
  TQString icalFolderType( KMFolder* folder ) const;

  /** Find message matching a given UID. */
  KMMessage* findMessageByUID( const TQString& uid, KMFolder* folder );
  /** Find message matching a given serial number. */
  static KMMessage* findMessageBySerNum( Q_UINT32 serNum, KMFolder* folder );

  /** Convenience function to delete a message. */
  void deleteMsg( KMMessage* msg );

  bool isEnabled() const { return mUseResourceIMAP; }

  /** Called when a folders contents have changed */
  void folderContentsTypeChanged( KMFolder*, KMail::FolderContentsType );

  /// @return the storage format of a given folder
  StorageFormat storageFormat( KMFolder* folder ) const;
  /// Set the storage format of a given folder. Called when seeing the kolab annotation.
  void setStorageFormat( KMFolder* folder, StorageFormat format );


  static const char* annotationForContentsType( KMail::FolderContentsType type );

  // Called after a folder was synced with the server
  void folderSynced( KMFolder* folder, const KURL& folderURL );
  // Called when deletion of a folder from the server suceeded,
  // triggers fb re-generation
  void folderDeletedOnServer( const KURL& folderURL );
  void addFolderChange( KMFolder* folder, FolderChanges changes );

  // See CachedImapJob::slotPutMessageResult
  bool isResourceQuiet() const;
  void setResourceQuiet(bool q);

public slots:
  /* (Re-)Read configuration file */
  void readConfig();
  void slotFolderRemoved( KMFolder* folder );

  void slotIncidenceAdded( KMFolder* folder, Q_UINT32 sernum );
  void slotIncidenceDeleted( KMFolder* folder, Q_UINT32 sernum );
  void slotRefresh( const TQString& type);

  // Called when a folder is made readonly or readwrite, or renamed,
  // or any other similar change that affects the resources
  void slotFolderPropertiesChanged( KMFolder* folder );

private slots:
  void slotRefreshFolder( KMFolder* );
  void slotCheckDone();
  void slotFolderLocationChanged( const TQString&, const TQString& );
  void slotFolderRenamed();
  void slotMessageRetrieved( KMMessage* );

private:
  /** Helper function for initFolders. Initializes a single folder. */
  KMFolder* initFolder( KMail::FolderContentsType contentsType );
  KMFolder* initScalixFolder( KMail::FolderContentsType contentsType );

  void connectFolder( KMFolder* folder );

  KMFolder* extraFolder( const TQString& type, const TQString& folder );

  void syncFolder( KMFolder* folder ) const;

  struct StandardFolderSearchResult
  {
    enum FoundEnum { FoundAndStandard, NotFound, FoundByType, FoundByName };
    StandardFolderSearchResult() : folder( 0 ) {}
    StandardFolderSearchResult( KMFolder* f, FoundEnum e ) : folder( f ), found( e ) {}
    KMFolder* folder; // NotFound implies folder==0 of course.
    FoundEnum found;
  };

  StandardFolderSearchResult findStandardResourceFolder( KMFolderDir* folderParentDir, KMail::FolderContentsType contentsType );
  KMFolder* findResourceFolder( const TQString& resource );


  bool updateAttachment( KMMessage& msg,
                         const TQString& attachmentURL,
                         const TQString& attachmentName,
                         const TQString& attachmentMimetype,
                         bool lookupByName );
  bool deleteAttachment( KMMessage& msg,
                         const TQString& attachmentURL );
  Q_UINT32 addIncidenceKolab( KMFolder& folder,
                              const TQString& subject,
                              const TQString& plainTextBody,
                              const TQMap<TQCString, TQString>& customHeaders,
                              const TQStringList& attachmentURLs,
                              const TQStringList& attachmentNames,
                              const TQStringList& attachmentMimetypes );
  static bool kolabXMLFoundAndDecoded( const KMMessage& msg, const TQString& mimetype, TQString& s );

  void handleFolderSynced( KMFolder* folder,
                           const KURL& folderURL,
                           int _changes );
  void triggerKolabFreeBusy( const KURL& folderURL );

  StorageFormat globalStorageFormat() const;

  static bool folderIsAlarmRelevant( const KMFolder * );

private:
  TQGuardedPtr<KMFolder> mContacts;
  TQGuardedPtr<KMFolder> mCalendar;
  TQGuardedPtr<KMFolder> mNotes;
  TQGuardedPtr<KMFolder> mTasks;
  TQGuardedPtr<KMFolder> mJournals;

  // The extra IMAP resource folders
  // Key: folder location. Data: folder.
  TQDict<KMail::ExtraFolder> mExtraFolders;
  // used for collecting incidences during async loading
  TQDict<KMail::Accumulator> mAccumulators;
  // More info for each folder we care about (mContacts etc. as well as the extra folders)
  // The reason for storing it here is that it can be shared between
  // kmfoldercachedimap and kmfolderimap, and that it's groupware data anyway.
  struct FolderInfo {
    FolderInfo() {} // for QMap
    FolderInfo( StorageFormat f, FolderChanges c ) :
      mStorageFormat( f ), mChanges( c ) {}
    StorageFormat mStorageFormat;
    FolderChanges mChanges;
  };
  // The storage format used for each folder that we care about
  typedef TQMap<KMFolder*, FolderInfo> FolderInfoMap;
  // helper for reading the FolderInfo from the config file
  FolderInfo readFolderInfo( const KMFolder * const folder ) const;

  FolderInfoMap mFolderInfoMap;

  unsigned int mFolderLanguage;

  KMFolderDir* mFolderParentDir;
  KMFolder*    mFolderParent;
  KMFolderType mFolderType;

  bool mUseResourceIMAP;
  bool mResourceQuiet;
  bool mHideFolders;

  /*
   * Bunch of maps to keep track of incidents currently in transfer, ones
   * which need to be ungotten, once we are done, once with updates pending.
   * Since these are transient attributes of only a small but changing number
   * of incidences they are not encapsulated in a struct or somesuch.
   */
  TQMap<TQString, Q_UINT32> mUIDToSerNum;
  TQMap<Q_UINT32, bool> mTheUnGetMes;
  TQMap<TQString, TQString> mPendingUpdates;
  TQMap<TQString, bool> mInTransit;

};

#endif // KMAILICALIFACEIMPL_H