1 /*
2  * Copyright (C) 2011  Christian Mollekopf <mollekopf@kolabsys.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef KOLAB_CONTAINERS_H
19 #define KOLAB_CONTAINERS_H
20 #include <string>
21 #include <vector>
22 #include <boost/scoped_ptr.hpp>
23 
24 namespace Kolab {
25 
26 /**
27 * - months: 1-12
28 * - days: 1-31
29 * - hour: 0-23
30 * - minute: 0-59
31 * - second: 0-59
32 */
33 class cDateTime {
34 public:
35     cDateTime();
36     cDateTime(int year, int month, int day, int hour, int minute, int second, bool isUtc=false);
37     cDateTime(const std::string &timezone, int year, int month, int day, int hour, int minute, int second);
38     cDateTime(int year, int month, int day);
39     ~cDateTime();
40     cDateTime(const cDateTime &);
41     void operator=(const cDateTime &);
42     bool operator==(const cDateTime &) const;
43 
44 
45     void setDate(int year, int month, int day);
46     int year() const;
47     int month() const;
48     int day() const;
49 
50     bool isDateOnly() const;
51 
52     void setTime(int hour, int minute, int second);
53     int hour() const;
54     int minute() const;
55     int second() const;
56 
57     void setUTC(bool);
58     bool isUTC() const;
59     void setTimezone(const std::string &);
60     std::string timezone() const;
61 
62     bool isValid() const;
63 private:
64     struct Private;
65     boost::scoped_ptr<Private> d;
66 };
67 
68 enum Classification {
69     ClassPublic,
70     ClassPrivate,
71     ClassConfidential
72 };
73 
74 enum Status {
75     StatusUndefined,
76     StatusNeedsAction,
77     StatusCompleted,
78     StatusInProcess,
79     StatusCancelled,
80     StatusTentative,
81     StatusConfirmed,
82     StatusDraft,
83     StatusFinal
84 };
85 
86 enum Weekday {
87     Monday,
88     Tuesday,
89     Wednesday,
90     Thursday,
91     Friday,
92     Saturday,
93     Sunday
94 };
95 
96 struct DayPos {
DayPosDayPos97     DayPos(): mIsValid(false){};
DayPosDayPos98     DayPos(int occurrence, Weekday weekday): mOccurrence(occurrence), mWeekday(weekday), mIsValid(true){};
99     bool operator==(const DayPos &other) const { return mOccurrence == other.mOccurrence && mWeekday == other.mWeekday; };
occurenceDayPos100     int occurence() const { return mOccurrence; };
weekdayDayPos101     Weekday weekday() const { return mWeekday; };
isValidDayPos102     bool isValid() { return mIsValid; };
103 private:
104     int mOccurrence;
105     Weekday mWeekday;
106     bool mIsValid;
107 };
108 
109 class Attachment {
110 public:
111     Attachment();
112     Attachment(const Kolab::Attachment &);
113     ~Attachment();
114 
115     void operator=(const Attachment &);
116     bool operator==(const Attachment &) const;
117 
118     void setUri(const std::string &uri, const std::string &mimetype);
119     std::string uri() const;
120 
121      ///Un-encoded binary content, Implies embedded, will be encoded
122      void setData(const std::string &, const std::string &mimetype);
123      ///Decoded binary content.
124      std::string data() const;
125      //TODO add possibility to set already encoded data and uri to be embedded as performance/convenience improvement?
126 //     ///Base64 encoded binary content, Implies embedded
127 //      void setEncodedData(const std::string &, const std::string &mimetype);
128 
129     std::string mimetype() const;
130 
131     ///User visible label
132     void setLabel(const std::string &);
133     std::string label() const;
134 
135     bool isValid() const;
136 private:
137     struct Private;
138     boost::scoped_ptr<Private> d;
139 };
140 
141 enum Relative {
142     Start,
143     End
144 };
145 
146 struct Duration {
DurationDuration147     Duration():mWeeks(0), mDays(0), mHours(0), mMinutes(0), mSeconds(0), mNegative(false), valid(false){};
mWeeksDuration148     Duration(int weeks, bool negative = false): mWeeks(weeks), mDays(0), mHours(0), mMinutes(0), mSeconds(0), mNegative(negative), valid(true){};
149     Duration(int days, int hours, int minutes, int seconds, bool negative = false): mWeeks(0), mDays(days), mHours(hours), mMinutes(minutes), mSeconds(seconds), mNegative(negative), valid(true){};
150     bool operator==(const Duration &other) const{ return (/*mWeeks == other.mWeeks &&
151                                                             mDays == other.mDays &&
152                                                             mHours == other.mHours &&
153                                                             mMinutes == other.mMinutes &&
154                                                             mSeconds == other.mSeconds &&
155                                                             mNegative == other.mNegative &&*/
156                                                             ( ((((mWeeks * 7 + mDays) * 24 + mHours) * 60 + mMinutes) * 60 + mSeconds) ==
157                                                               ((((other.mWeeks * 7 + other.mDays) * 24 + other.mHours) * 60 + other.mMinutes) * 60 + other.mSeconds) ) &&
158                                                             valid == other.valid );};
weeksDuration159     int weeks() const { return mWeeks; };
daysDuration160     int days() const { return mDays; };
hoursDuration161     int hours() const { return mHours; };
minutesDuration162     int minutes() const { return mMinutes; };
secondsDuration163     int seconds() const { return mSeconds; };
164 
isNegativeDuration165     bool isNegative() const { return mNegative; };
isValidDuration166     bool isValid() const { return valid; };
167 private:
168     int mWeeks;
169     int mDays;
170     int mHours;
171     int mMinutes;
172     int mSeconds;
173     bool mNegative;
174     bool valid;
175 };
176 
177 struct ContactReference {
178     enum ReferenceType {
179         Invalid,
180         EmailReference,
181         UidReference,
182         EmailAndUidReference
183     };
ContactReferenceContactReference184     ContactReference(): mType(Invalid) {};
185     ///For xCal
mTypeContactReference186     ContactReference(const std::string &email, const std::string &name = std::string(), const std::string &uid = std::string()): mType(EmailAndUidReference), mEmail(email), mUid(uid), mName(name) {};
187     ///For xCard
mTypeContactReference188     ContactReference(ReferenceType type, const std::string &emailOrUID, const std::string &name = std::string()): mType(type), mName(name) {
189         if (type == EmailReference) {
190             mEmail = emailOrUID;
191         } else {
192             mUid = emailOrUID;
193         }
194     };
195     bool operator==(const ContactReference &other) const { return mEmail == other.mEmail &&
196                                                         mName == other.mName &&
197                                                         mUid == other.mUid;
198     };
199 
isValidContactReference200     bool isValid() const { return mType != Invalid; };
201 
setNameContactReference202     void setName(const std::string &name) { mName = name; };
203 
emailContactReference204     std::string email() const { return mEmail; };
uidContactReference205     std::string uid() const { return mUid; };
nameContactReference206     std::string name() const { return mName; };
207 
typeContactReference208     ReferenceType type() const { return mType; };
209 
210 private:
211     ReferenceType mType;
212     std::string mEmail;
213     std::string mUid;
214     std::string mName;
215 };
216 
217 class Alarm {
218 public:
219     enum Type {
220         InvalidAlarm,
221         EMailAlarm,
222         DisplayAlarm,
223         AudioAlarm
224     };
225 
226     Alarm();
227     Alarm(Type);
228     Alarm(const Alarm &);
229     ~Alarm();
230 
231     void operator=(const Alarm &);
232     bool operator==(const Alarm &other) const;
233 
234     ///EMail Alarm, @param attendees accepts only email + name and no uid
235     Alarm(const std::string &summary, const std::string &description, const std::vector<ContactReference> attendees);
236     std::string summary() const;
237     std::string description() const;
238     std::vector<ContactReference> attendees() const;
239 
240     ///Display Alarm
241     Alarm(const std::string &text);
242     std::string text() const;
243 
244     ///Audio Alarm
245     Alarm(const Attachment &audio);
246     Attachment audioFile() const;
247 
248     void setRelativeStart(const Duration &, Relative);
249     Duration relativeStart() const;
250     Relative relativeTo() const;
251 
252     void setStart(const cDateTime &);
253     cDateTime start() const;
254 
255     void setDuration(const Duration &, int numrepeat);
256     Duration duration() const;
257     int numrepeat() const;
258 
259     Type type() const;
260 
261 private:
262     struct Private;
263     boost::scoped_ptr<Private> d;
264 };
265 
266 
267 class RecurrenceRule {
268 public:
269 
270     RecurrenceRule();
271     RecurrenceRule(const RecurrenceRule &);
272     ~RecurrenceRule();
273 
274     void operator=(const RecurrenceRule &);
275     bool operator==(const RecurrenceRule &other) const;
276 
277     enum Frequency {
278         FreqNone,
279         Yearly,
280         Monthly,
281         Weekly,
282         Daily,
283         Hourly,
284         Minutely,
285         Secondly
286     };
287 
288     void setFrequency(Frequency);
289     Frequency frequency() const;
290 
291     void setWeekStart(Weekday);
292     Weekday weekStart() const;
293 
294     void setEnd(const cDateTime &);
295     cDateTime end() const;
296 
297     void setCount(int count);
298     int count() const;
299 
300     void setInterval(int);
301     int interval() const;
302 
303     void setBysecond(const std::vector<int> &);
304     std::vector<int> bysecond() const;
305 
306     void setByminute(const std::vector<int> &);
307     std::vector<int> byminute() const;
308 
309     void setByhour(const std::vector<int> &);
310     std::vector<int> byhour() const;
311 
312     void setByday(const std::vector<DayPos> &);
313     std::vector<DayPos> byday() const;
314 
315     void setBymonthday(const std::vector<int> &);
316     std::vector<int> bymonthday() const;
317 
318     void setByyearday(const std::vector<int> &);
319     std::vector<int> byyearday() const;
320 
321     void setByweekno(const std::vector<int> &);
322     std::vector<int> byweekno() const;
323 
324     void setBymonth(const std::vector<int> &);
325     std::vector<int> bymonth() const;
326 
327     bool isValid() const;
328 
329 private:
330     struct Private;
331     boost::scoped_ptr<Private> d;
332 };
333 
334 
335 enum PartStatus {
336     PartNeedsAction,
337     PartAccepted,
338     PartDeclined,
339     PartTentative,
340     PartDelegated,
341     PartInProcess,
342     PartCompleted
343 };
344 
345 enum Role {
346     Required,
347     Chair,
348     Optional,
349     NonParticipant
350 };
351 
352 enum Cutype {
353     CutypeUnknown,
354     CutypeGroup,
355     CutypeIndividual,
356     CutypeResource,
357     CutypeRoom
358 };
359 
360 class Attendee {
361 public:
362     Attendee();
363     Attendee(const ContactReference &contact);
364     Attendee(const Attendee &);
365     ~Attendee();
366 
367     void operator=(const Attendee &);
368     bool operator==(const Attendee &) const;
369 
370     bool isValid() const;
371 
372     void setContact(const ContactReference &);
373     ContactReference contact() const;
374 
375     void setPartStat(PartStatus);
376     PartStatus partStat() const;
377 
378     void setRole(Role);
379     Role role() const;
380 
381     void setRSVP(bool);
382     bool rsvp() const;
383 
384     void setDelegatedTo(const std::vector<ContactReference> &);
385     std::vector<ContactReference> delegatedTo() const;
386 
387     void setDelegatedFrom(const std::vector<ContactReference> &);
388     std::vector<ContactReference> delegatedFrom() const;
389 
390     void setCutype(Cutype);
391     Cutype cutype() const;
392 private:
393     struct Private;
394     boost::scoped_ptr<Private> d;
395 };
396 
397 struct CustomProperty {
CustomPropertyCustomProperty398     CustomProperty(){};
CustomPropertyCustomProperty399     CustomProperty(const std::string &i, const std::string &v)
400     : identifier(i), value(v) {};
401 
402     bool operator==(const CustomProperty &other) const{ return (identifier == other.identifier && value == other.value);};
403     std::string identifier;
404     std::string value;
405 };
406 
407 //WARNING this function copies the vector and does not modify it
408 template <typename T>
409 std::vector<T> operator<< ( std::vector<T> v, const T &s)
410 {
411     v.push_back(s);
412     return v;
413 }
414 
415 
416 }
417 
418 #endif
419