-
Notifications
You must be signed in to change notification settings - Fork 1
/
CWrapper.h
413 lines (363 loc) · 13.5 KB
/
CWrapper.h
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
#ifndef CWRAPPER_H
#define CWRAPPER_H
#include <glib.h>
#include <gio/gio.h>
#include <algorithm>
#include <QDate>
#include <QMetaType>
#include <CMulticalendar.h>
#include <CCalendar.h>
#include <CEvent.h>
#include <CTodo.h>
#include <CJournal.h>
#include <CBdayEvent.h>
#include "ComponentInstance.h"
Q_DECLARE_METATYPE (CEvent*)
Q_DECLARE_METATYPE (CTodo*)
Q_DECLARE_METATYPE (CJournal*)
enum EntryType
{
E_CALENDAR = 0,
E_EVENT,
E_TODO,
E_JOURNAL,
E_BDAY,
E_SPARE,
E_VTIMEZONE
};
enum RecurrenceType
{
// This is not in the API, but the API doesn't care that much what value
// is used for type, so maybe it's not a crime to sneak this in
E_DISABLED = -1,
E_NONE = 0, // External
E_DAILY,
E_WEEKDAY,
E_WEEKLY,
E_MONTHLY,
E_YEARLY,
E_COMPLEX
};
enum CalendarColourExtra
{
COLOUR_NEW = -1
};
namespace CWrapper
{
inline QString calendarName(const string &name)
{
// Handle translatable names
if (name == "cal_ti_calendar_private")
return QObject::tr("Private");
if (name == "cal_ti_smart_birthdays")
return QObject::tr("Birthdays");
return QString::fromUtf8(name.c_str());
}
inline bool instanceComparator(ComponentInstance *i1, ComponentInstance *i2)
{
if (i1->stamp < i2->stamp)
return true;
if (i1->stamp == i2->stamp
&& i1->duration() > i2->duration())
return true;
return false;
}
inline bool todoComparator(CTodo *t1, CTodo *t2)
{
if (t1->getStatus() < t2->getStatus())
return true;
if (t1->getStatus() == t2->getStatus()
&& t1->getDue() < t2->getDue())
return true;
return false;
}
inline bool journalComparator(CJournal *j1, CJournal *j2)
{
return j1->getDateStart() > j2->getDateStart();
}
inline bool calendarComparator(CCalendar *c1, CCalendar *c2)
{
return QString::localeAwareCompare(calendarName(c1->getCalendarName()),
calendarName(c2->getCalendarName())) < 0;
}
inline bool calendarVisibilityComparator(CCalendar *c1, CCalendar *c2)
{
if (c1->IsShown() && !c2->IsShown())
return true;
if (c2->IsShown() && !c1->IsShown())
return false;
return calendarComparator(c1, c2);
}
inline void sort(vector<ComponentInstance*> &instances)
{
sort(instances.begin(), instances.end(), instanceComparator);
}
inline void sort(vector<CTodo*> &todos)
{
sort(todos.begin(), todos.end(), todoComparator);
}
inline void sort(vector<CJournal*> &journals)
{
sort(journals.begin(), journals.end(), journalComparator);
}
inline void sort(vector<CCalendar*> &calendars, bool visibilityMatters)
{
sort(calendars.begin(), calendars.end(), visibilityMatters ? calendarVisibilityComparator
: calendarComparator);
}
// Expand a component to specific instances
inline void expand(CComponent *component, vector<ComponentInstance*> &instances, const time_t startStamp, const time_t endStamp)
{
const time_t duration = component->getDateEnd() - component->getDateStart();
if (component->getRecurrence()) {
vector<time_t> stamps;
component->getInstanceTimes(startStamp, endStamp, stamps);
for (unsigned int s = 0; s < stamps.size(); s++) {
// NOTE: Looks like there is a bug in the backend. When
// recurrence is present, components are fetched also from
// outside of the requested range (one day before). Below is
// a workaround for that.
if (stamps[s] > endStamp || stamps[s] + duration < startStamp)
continue;
// When calendar-backend is fetching components from a certain
// range, it treats the component bounds inclusively. However,
// this is not what we want, so an additional filtering step
// is required.
if (stamps[s] + duration == startStamp && duration != 0)
continue;
// NOTE: There is yet another bug in the backend. If multiple
// recurrence rules with different frequencies are specified and
// they happen to produce duplicate results, the duplicates will
// not be removed. We have to find them on our own.
unsigned int i = 0;
while (i < instances.size()
&& (instances[i]->stamp != stamps[s] || instances[i]->component != component))
i++;
// Add the considered instance only if there are no duplicates
if (i == instances.size())
instances.push_back(new ComponentInstance(component, stamps[s]));
}
} else {
// Interpret the end date as non-inclusive (see above)
if (component->getDateEnd() == startStamp && duration != 0)
return;
instances.push_back(new ComponentInstance(component, component->getDateStart()));
}
}
inline void expand(const vector<CComponent*> &components, vector<ComponentInstance*> &instances, const time_t startStamp, const time_t endStamp)
{
for (unsigned int i = 0; i < components.size(); i++)
expand(components[i], instances, startStamp, endStamp);
}
// Some functions in the API retrieve incomplete component representations
// and that can lead to unexpected segfaults. This function returns the full
// represenation of the provided component.
inline CEvent* details(CEvent *event)
{
int error;
CCalendar *calendar = CMulticalendar::MCInstance()->getCalendarById(event->getCalendarId(), error);
event = calendar->getEvent(event->getId(), error);
if (event) event->setCalendarId(calendar->getCalendarId()); // It doesn't happen on its own, sigh...
delete calendar;
return event;
}
inline CTodo* details(CTodo *todo)
{
int error;
CCalendar *calendar = CMulticalendar::MCInstance()->getCalendarById(todo->getCalendarId(), error);
todo = calendar->getTodo(todo->getId(), error);
if (todo) todo->setCalendarId(calendar->getCalendarId());
delete calendar;
return todo;
}
inline CJournal* details(CJournal *journal)
{
int error;
CCalendar *calendar = CMulticalendar::MCInstance()->getCalendarById(journal->getCalendarId(), error);
journal = calendar->getJournal(journal->getId(), error);
if (journal) journal->setCalendarId(calendar->getCalendarId());
delete calendar;
return journal;
}
inline CBdayEvent* details(CBdayEvent *event)
{
int error;
CCalendar *calendar = CMulticalendar::MCInstance()->getCalendarById(event->getCalendarId(), error);
event = reinterpret_cast<CBdayEvent*>(calendar->getBirthDayEvent(event->getId(), error));
if (event) event->setCalendarId(calendar->getCalendarId());
delete calendar;
return event;
}
// Adapt the given string to be displayed in one line of a limited length.
// The purpose of the limit is to prevent wasting too much time
// on impractical amounts of data that cannot fit on the screen anyway.
inline QString simplify(const string &std_str, size_t limit)
{
return QString::fromUtf8(std_str.c_str(), qMin(std_str.length(), limit)).replace('\n', ' ');
}
// Some additional processing for summaries
inline QString summary(CComponent *component, const QDate &date, size_t limit = 0)
{
QString summary = limit ? simplify(component->getSummary(), limit)
: QString::fromUtf8(component->getSummary().c_str());
if (component->getType() == E_BDAY && (!limit || summary.size() < limit))
summary += " (" + QString::number(date.year() - component->getStatus()) + ")";
return summary;
}
inline const char* calendarType(const int type)
{
switch (type)
{
case LOCAL_CALENDAR:
return "LOCAL_CALENDAR";
case BIRTHDAY_CALENDAR:
return "BIRTHDAY_CALENDAR";
case SYNC_CALENDAR:
return "SYNC_CALENDAR";
case DEFAULT_PRIVATE:
return "DEFAULT_PRIVATE";
case DEFAULT_SYNC:
return "DEFAULT_SYNC";
default:
return "UNKNOWN";
}
}
inline const char* colorIcon(const int color)
{
switch (color) {
case COLOUR_NEW:
return "general_add";
case COLOUR_DARKBLUE:
return "calendar_colors_darkblue";
case COLOUR_DARKGREEN:
return "calendar_colors_darkgreen";
case COLOUR_DARKRED:
return "calendar_colors_darkred";
case COLOUR_ORANGE:
return "calendar_colors_orange";
case COLOUR_VIOLET:
return "calendar_colors_violet";
case COLOUR_YELLOW:
return "calendar_colors_yellow";
case COLOUR_WHITE:
return "calendar_colors_white";
case COLOUR_BLUE:
return "calendar_colors_blue";
case COLOUR_RED:
return "calendar_colors_red";
case COLOUR_GREEN:
return "calendar_colors_green";
default:
return "";
}
}
inline const char* colorStripe(const int color)
{
switch (color) {
case COLOUR_DARKBLUE:
return "calendar_event_darkblue";
case COLOUR_DARKGREEN:
return "calendar_event_darkgreen";
case COLOUR_DARKRED:
return "calendar_event_darkred";
case COLOUR_ORANGE:
return "calendar_event_orange";
case COLOUR_VIOLET:
return "calendar_event_violet";
case COLOUR_YELLOW:
return "calendar_event_yellow";
case COLOUR_WHITE:
return "calendar_event_white";
case COLOUR_BLUE:
return "calendar_event_blue";
case COLOUR_RED:
return "calendar_event_red";
case COLOUR_GREEN:
return "calendar_event_green";
default:
return "";
}
}
inline const char* colorStripeDim(const int color)
{
switch (color) {
case COLOUR_DARKBLUE:
return "calendar_event_darkblue_dim";
case COLOUR_DARKGREEN:
return "calendar_event_darkgreen_dim";
case COLOUR_DARKRED:
return "calendar_event_darkred_dim";
case COLOUR_ORANGE:
return "calendar_event_orange_dim";
case COLOUR_VIOLET:
return "calendar_event_violet_dim";
case COLOUR_YELLOW:
return "calendar_event_yellow_dim";
case COLOUR_WHITE:
return "calendar_event_white_dim";
case COLOUR_BLUE:
return "calendar_event_blue_dim";
case COLOUR_RED:
return "calendar_event_red_dim";
case COLOUR_GREEN:
return "calendar_event_green_dim";
default:
return "";
}
}
inline const char* colorHour(const int color)
{
switch (color) {
case COLOUR_DARKBLUE:
return "calendar_event_hour_darkblue";
case COLOUR_DARKGREEN:
return "calendar_event_hour_darkgreen";
case COLOUR_DARKRED:
return "calendar_event_hour_darkred";
case COLOUR_ORANGE:
return "calendar_event_hour_orange";
case COLOUR_VIOLET:
return "calendar_event_hour_violet";
case COLOUR_YELLOW:
return "calendar_event_hour_yellow";
case COLOUR_WHITE:
return "calendar_event_hour_white";
case COLOUR_BLUE:
return "calendar_event_hour_blue";
case COLOUR_RED:
return "calendar_event_hour_red";
case COLOUR_GREEN:
return "calendar_event_hour_green";
default:
return "";
}
}
inline const char* colorAllDay(const int color)
{
switch (color) {
case COLOUR_DARKBLUE:
return "calendar_colors_allday_darkblue";
case COLOUR_DARKGREEN:
return "calendar_colors_allday_darkgreen";
case COLOUR_DARKRED:
return "calendar_colors_allday_darkred";
case COLOUR_ORANGE:
return "calendar_colors_allday_orange";
case COLOUR_VIOLET:
return "calendar_colors_allday_violet";
case COLOUR_YELLOW:
return "calendar_colors_allday_yellow";
case COLOUR_WHITE:
return "calendar_colors_allday_white";
case COLOUR_BLUE:
return "calendar_colors_allday_blue";
case COLOUR_RED:
return "calendar_colors_allday_red";
case COLOUR_GREEN:
return "calendar_colors_allday_green";
default:
return "";
}
}
}
#endif // CWRAPPER_H