summaryrefslogtreecommitdiffstats
path: root/tderesources/kolab/kcal/resourcekolab.h
blob: fdf952cc353804a797e4e2e80113d9eb086e5cec (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
/*
    This file is part of the kolab resource - the implementation of the
    Kolab storage format. See www.kolab.org for documentation on this.

    Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
                  2004 Till Adam <till@klaralvdalens-datakonsult.se>

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

#ifndef KCAL_RESOURCEKOLAB_H
#define KCAL_RESOURCEKOLAB_H

#include <tqtimer.h>

#include <tdepimmacros.h>
#include <libkcal/calendarlocal.h>
#include <libkcal/icalformat.h>
#include <libkcal/resourcecalendar.h>
#include "../shared/resourcekolabbase.h"

namespace KCal {

struct TemporarySilencer;

class KDE_EXPORT ResourceKolab : public KCal::ResourceCalendar,
                      public KCal::IncidenceBase::Observer,
                      public Kolab::ResourceKolabBase
{
  Q_OBJECT
  
    friend struct TemporarySilencer;

public:
  ResourceKolab( const TDEConfig* );
  virtual ~ResourceKolab();

  /// Load resource data.
  bool doLoad();

  /// Save resource data.
  bool doSave();

  /// Open the notes resource.
  bool doOpen();
  /// Close the notes resource.
  void doClose();

  // The libkcal functions. See the resource for descriptions
  KDE_DEPRECATED bool addEvent( KCal::Event *event );
  bool addEvent( KCal::Event *event, const TQString &subResource );
  bool deleteEvent( KCal::Event * );
  KCal::Event* event( const TQString &UniqueStr );
  KCal::Event::List rawEvents( EventSortField sortField = EventSortUnsorted, SortDirection sortDirection = SortDirectionAscending );
  KCal::Event::List rawEventsForDate(
    const TQDate& date,
    EventSortField sortField=EventSortUnsorted,
    SortDirection sortDirection=SortDirectionAscending );
  KCal::Event::List rawEventsForDate( const TQDateTime& qdt );
  KCal::Event::List rawEvents( const TQDate& start, const TQDate& end,
                               bool inclusive = false );

  KDE_DEPRECATED bool addTodo( KCal::Todo * todo );
  bool addTodo( KCal::Todo *todo, const TQString &subResource );
  bool deleteTodo( KCal::Todo * );
  KCal::Todo* todo( const TQString &uid );
  KCal::Todo::List rawTodos( TodoSortField sortField = TodoSortUnsorted, SortDirection sortDirection = SortDirectionAscending );
  KCal::Todo::List rawTodosForDate( const TQDate& date );

  KDE_DEPRECATED bool addJournal( KCal::Journal * );
  bool addJournal( KCal::Journal *, const TQString &subResource );
  bool deleteJournal( KCal::Journal * );
  KCal::Journal* journal( const TQString &uid );
  KCal::Journal::List rawJournals( JournalSortField sortField = JournalSortUnsorted, SortDirection sortDirection = SortDirectionAscending );
  KCal::Journal::List rawJournalsForDate( const TQDate &date );

  KCal::Alarm::List alarms( const TQDateTime& from, const TQDateTime& to );
  KCal::Alarm::List alarmsTo( const TQDateTime& to );

  void setTimeZoneId( const TQString& tzid );

  bool deleteIncidence( KCal::Incidence* i );

  /// The ResourceKolabBase methods called by KMail
  bool fromKMailAddIncidence( const TQString& type, const TQString& subResource,
                              TQ_UINT32 sernum, int format, const TQString& data );
  void fromKMailDelIncidence( const TQString& type, const TQString& subResource,
                              const TQString& uid );
  void fromKMailRefresh( const TQString& type, const TQString& subResource );

  /// Listen to KMail changes in the amount of sub resources
  void fromKMailAddSubresource( const TQString& type, const TQString& subResource,
                                const TQString& label, bool writable,
                                bool alarmRelevant );
  void fromKMailDelSubresource( const TQString& type, const TQString& subResource );

  void fromKMailAsyncLoadResult( const TQMap<TQ_UINT32, TQString>& map,
                                 const TQString& type,
                                 const TQString& folder );

  /** Return the list of subresources. */
  TQStringList subresources() const;

  bool canHaveSubresources() const { return true; }

  /** Is this subresource active? */
  bool subresourceActive( const TQString& ) const;
  /** (De)activate the subresource */
  virtual void setSubresourceActive( const TQString &, bool );

  /** Is this subresource writable? */
  bool subresourceWritable( const TQString& ) const;

  /** What is the label for this subresource? */
  virtual const TQString labelForSubresource( const TQString& resource ) const;

  virtual TQString subresourceIdentifier( Incidence *incidence );

  virtual bool addSubresource( const TQString& resource, const TQString& parent );
  virtual bool removeSubresource( const TQString& resource );

  virtual TQString subresourceType( const TQString &resource );

  KABC::Lock* lock();

  void beginAddingIncidences();

  void endAddingIncidences();

signals:
  void useGlobalMode();
protected slots:
  void slotEmitResourceChanged();
  void writeConfig();

protected:
  /**
   * Return list of alarms which are relevant for the current user. These
   * are the ones coming from folders which the user has "Administer" rights
   * for, as per ACL */
  KCal::Alarm::List relevantAlarms( const KCal::Alarm::List &alarms );

private:
  void removeIncidences( const TQCString& incidenceType );
  void resolveConflict( KCal::Incidence*, const TQString& subresource, TQ_UINT32 sernum );
  void addIncidence( const char* mimetype, const TQString& xml,
                     const TQString& subResource, TQ_UINT32 sernum );


  /**
     Caller guarantees i is not null.
   */
  bool addIncidence( KCal::Incidence *i, const TQString& subresource,
                     TQ_UINT32 sernum );

  void addEvent( const TQString& xml, const TQString& subresource,
                 TQ_UINT32 sernum );
  void addTodo( const TQString& xml, const TQString& subresource,
                TQ_UINT32 sernum );
  void addJournal( const TQString& xml, const TQString& subresource,
                   TQ_UINT32 sernum );


  bool loadAllEvents();
  bool loadAllTodos();
  bool loadAllJournals();

  bool doLoadAll( Kolab::ResourceMap& map, const char* mimetype );

  /// Reimplemented from IncidenceBase::Observer to know when an incidence was changed
  void incidenceUpdated( KCal::IncidenceBase* );
  void incidenceUpdatedSilent( KCal::IncidenceBase* incidencebase);

  bool openResource( TDEConfig& config, const char* contentType,
                     Kolab::ResourceMap& map );
  void loadSubResourceConfig( TDEConfig& config, const TQString& name,
                              const TQString& label, bool writable,
                              bool alarmRelevant, Kolab::ResourceMap& subResource );
  bool loadSubResource( const TQString& subResource, const char* mimetype );
  bool unloadSubResource( const TQString& subResource );

  TQString configFile() const {
    return ResourceKolabBase::configFile( "kcal" );
  }

  Kolab::ResourceMap* subResourceMap( const TQString& contentsType );

  bool sendKMailUpdate( KCal::IncidenceBase* incidence, const TQString& _subresource,
                        TQ_UINT32 sernum );


  KCal::CalendarLocal mCalendar;

  // The list of subresources
  Kolab::ResourceMap mEventSubResources, mTodoSubResources, mJournalSubResources;

  bool mOpen; // If the resource is open, this is true
  TQDict<KCal::IncidenceBase> mPendingUpdates;
  TQTimer mResourceChangedTimer;
  ICalFormat mFormat;

  /**
    This map contains the association between a new added incidence
    and the subresource it belongs to.
    That's needed to return the correct mapping in subresourceIdentifier().

    We can't trust on mUidMap here, because it contains only non-pending uids.
   */
  TQMap<TQString, TQString> mNewIncidencesMap;
  int mProgressDialogIncidenceLimit;

  /**
   * If a user has a subresource for viewing another user's folder then it can happen
   * that addIncidence(...) adds an incidence with an already existing UID.
   *
   * When this happens, addIncidence(...) sets a new random UID and stores the
   * original UID using incidence->setSchedulingID(uid) because KCal doesn't
   * allow two incidences to have the same UID.
   *
   * This map keeps track of the generated UIDs (which are local) so we can delete the
   * right incidence inside fromKMailDelIncidence(...) whenever we sync.
   *
   * The key is originalUID,subResource and the value is the fake UID.
   */
  TQMap< TQPair<TQString, TQString>, TQString > mOriginalUID2fakeUID;

  bool mBatchAddingInProgress;
  TQMap<Kolab::ResourceType,TQString> mLastUsedResources;

  /**
     Indexed by uid, it holds the last known revision of an incidence.
     If we receive an update where the incidence still has the same
     revision as the last known, we ignore it and don't send it to kmail,
     because shortly after, IncidenceChanger will increment the revision
     and that will trigger another update.

     If we didn't discard the first update, kmail would have been updated twice.
  */
  TQMap<TQString,int> mLastKnownRevisions;

};

struct TemporarySilencer {
 TemporarySilencer( ResourceKolab *_resource )
  {
    resource = _resource;
    oldValue = resource->mSilent;
    resource->mSilent = true;
  }
  ~TemporarySilencer()
  {
    resource->mSilent = oldValue;
  }
  ResourceKolab *resource;
  bool oldValue;
};

}

#endif // KCAL_RESOURCEKOLAB_H