summaryrefslogtreecommitdiffstats
path: root/kresources/caldav/resource.h
blob: ba1f3e9af04fc44227f4f24a08250494656350a1 (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
/*=========================================================================
| KCalDAV
|--------------------------------------------------------------------------
| (c) 2010  Timothy Pearson
| (c) 2009  Kumaran Santhanam (initial KDE4 version)
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| Main interface to the KResource system.
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/

#ifndef KCAL_RESOURCECALDAV_H
#define KCAL_RESOURCECALDAV_H

#include "preferences.h"
#include <tqthread.h>
#include <tqptrqueue.h>

#include <libkcal/resourcecached.h>
#include <libkdepim/progressmanager.h>

#include <kabc/locknull.h>

#include <kdepimmacros.h>
#include <kconfig.h>

namespace KCal {

class CalDavReader;
class CalDavWriter;

/*=========================================================================
| CLASS
 ========================================================================*/

/**
 * This class provides a resource for accessing calendars via CalDAV protocol.
 */
class KDE_EXPORT ResourceCalDav : public ResourceCached
{
    Q_OBJECT

public:

    explicit ResourceCalDav( const KConfig *config );
    virtual ~ResourceCalDav();

    void readConfig( const KConfig *config );
    void writeConfig( KConfig *config );

    /**
     * @return This resource preferences.
     */
    CalDavPrefs* prefs() {
        return mPrefs;
    }

    /**
     * @return This resource preferences.
     */
    const CalDavPrefs* prefs() const {
        return mPrefs;
    }

    virtual void setReadOnly(bool v);

    bool isSaving();

protected slots:

    void loadFinished();

    virtual bool doSave();

    void writingFinished();

    void releaseReadLockout();

protected:

    struct LoadingTask {
        TQString url;
    };

    struct WritingTask {
        TQString url;
        TQString added;
        TQString changed;
        TQString deleted;
    };


    virtual bool doLoad();
//    virtual bool doSave();

    virtual KABC::Lock* lock();

    /**
     * Creates prefs and configures them.
     * @return a newly created preferences object. It should be removed by the caller.
     */
    CalDavPrefs* createPrefs() const;

    /**
     * Initializes internal state.
     * Particulary, sets save and reload policies to default values,
     * creates writing and reading jobs and preferences objects.
     */
    void init();

    /**
     * Updates the progress bar
     */
    void updateProgressBar(int direction);

    /**
     * Initiates calendar loading process.
     * @param url URL to load calendar data from.
     */
    void startLoading(const TQString& url);

    /**
     * Checks if the data is correct and can be parsed.
     * @param data ical string to check.
     * @return true if the data is correct, false otherwise.
     */
    bool checkData(const TQString& data);

    /**
     * Parses the data and adds events to the calendar.
     * @param data calendar data.
     * @return true on success, false on fail.
     */
    bool parseData(const TQString& data);

    /**
     * Initiates calendar writing process.
     * @param url URL to save calendar data to.
     * @return true if write was queued successfully, false if not
     */
    bool startWriting(const TQString& url);

    /**
     * Returns a list of incidences as a valid iCalendar string.
     * @param inc list of incidences.
     * @return a string in iCalendar format which describes the given incidences.
     */
    TQString getICalString(const Incidence::List& inc);

    /**
     * Changes read-only status of incidences from a given list.
     * @param inc list of incidences.
     * @param readOnly read-only status that all the incidences should have after the method finishes.
     */
    static void setIncidencesReadOnly(Incidence::List& inc, bool readOnly);

    /**
     * Ensures incidences' read-only states are the same as the calendar's read-only state.
     */
    void ensureReadOnlyFlagHonored();

    /**
     * If the loading queue is empty or the loader is not ready, does nothing.
     * Otherwise, pops a head element and starts a loading process for it.
     */
    void loadingQueuePop();

    /**
     * Pushes the given loading task to the loading queue.
     * Then calls loadingQueuePop.
     */
    void loadingQueuePush(const LoadingTask *task);

    /**
     * If the writing queue is empty or the writer is not ready, does nothing.
     * Otherwise, pops a head element and starts a writing process for it.
     */
    void writingQueuePop();

    /**
     * Pushes the given writing task to the writing queue.
     * Then calls writingQueuePop.
     */
    void writingQueuePush(const WritingTask *task);

    virtual bool event ( TQEvent * e );

private:

    // constants: =============================================================

    /// Termination waiting time in milliseconds. Used to terminate job threads.
    static const unsigned long TERMINATION_WAITING_TIME;

    /**
     * Resource caches only events which are from the interval [-CACHE_DAYS, CACHE_DAYS].
     */
    static const int CACHE_DAYS;

    static const int DEFAULT_RELOAD_INTERVAL;
    static const int DEFAULT_SAVE_INTERVAL;
    static const int DEFAULT_RELOAD_POLICY;
    static const int DEFAULT_SAVE_POLICY;

    bool readLockout;
    bool mAllWritesComplete;

    // members: ===============================================================

    KABC::LockNull mLock;
    CalDavPrefs* mPrefs;
    CalDavReader* mLoader;
    CalDavWriter* mWriter;
    KPIM::ProgressItem *mProgress;

    bool mLoadingQueueReady;
    TQPtrQueue<LoadingTask> mLoadingQueue;

    bool mWritingQueueReady;
    TQPtrQueue<WritingTask> mWritingQueue;

    TQTimer *mWriteRetryTimer;

};



} // namespace KCal

#endif // KCAL_RESOURCECALDAV_H