- upper management directive: revert all OpenSync 0.3x changes
[barry.git] / opensync-plugin / src / vevent.h
blob09c89d8da5f2f9db35d8fcda8522a4834d521d0c
1 //
2 // \file vevent.h
3 // Conversion routines for vevents (VCALENDAR, etc)
4 //
6 /*
7 Copyright (C) 2006-2007, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_SYNC_VEVENT_H__
23 #define __BARRY_SYNC_VEVENT_H__
25 #include <barry/barry.h>
26 #include <stdint.h>
27 #include <string>
28 #include "vformat.h"
30 // forward declarations
31 class BarryEnvironment;
33 // FIXME - possibly scrap this in favour of opensync's xml time routines,
34 // but this will require an overhaul of the plugin itself.
35 class vTimeZone
37 public:
38 struct ZoneBlock
40 bool m_daylightSaving;
41 std::string m_name;
42 std::string m_dtstart;
43 std::string m_tzoffsetfrom;
44 std::string m_tzoffsetto;
47 private:
48 bool m_valid;
50 public:
51 std::string m_id;
53 public:
54 vTimeZone();
55 ~vTimeZone();
57 void Clear();
59 bool IsValid();
61 /// used for comparing by TZID
62 bool operator==(const std::string &tzid) const;
66 // A special smart pointer for vformat pointer handling.
67 // Behaves like std::auto_ptr<> in that only one object
68 // at a time owns the pointer, and destruction frees it.
69 template <class T, class FT, void (*FreeFunc)(FT *pt)>
70 class vSmartPtr
72 mutable T *m_pt;
74 public:
75 vSmartPtr() : m_pt(0) {}
76 vSmartPtr(T *pt) : m_pt(pt) {}
77 vSmartPtr(const vSmartPtr &sp) : m_pt(sp.m_pt)
79 sp.m_pt = 0;
81 ~vSmartPtr()
83 if( m_pt )
84 FreeFunc(m_pt);
87 vSmartPtr& operator=(T *pt)
89 Extract();
90 m_pt = pt;
91 return *this;
94 vSmartPtr& operator=(const vSmartPtr &sp)
96 Extract();
97 m_pt = sp.Extract();
98 return *this;
101 T* Extract()
103 T *rp = m_pt;
104 m_pt = 0;
105 return rp;
108 T* Get()
110 return m_pt;
114 typedef vSmartPtr<VFormatAttribute, VFormatAttribute, &vformat_attribute_free> vAttrPtr;
115 typedef vSmartPtr<VFormatParam, VFormatParam, &vformat_attribute_param_free> vParamPtr;
116 typedef vSmartPtr<char, void, &g_free> gStringPtr;
120 // vCalendar
122 /// Class for converting between RFC 2445 iCalendar data format,
123 /// and the Barry::Calendar class.
125 class vCalendar
127 // data to pass to external requests
128 char *m_gCalData; // dynamic memory returned by vformat()... can
129 // be used directly by the plugin, without
130 // overmuch allocation and freeing (see Extract())
131 std::string m_vCalData; // copy of m_gCalData, for C++ use
132 Barry::Calendar m_BarryCal;
134 // internal data for managing the vformat
135 VFormat *m_format;
137 static const char *WeekDays[7];
139 public:
140 // FIXME - if you put this class in the Barry library,
141 // you'll need to change the class hierarchy
142 class ConvertError : public std::runtime_error
144 public:
145 ConvertError(const std::string &msg) : std::runtime_error(msg) {}
148 protected:
149 vAttrPtr NewAttr(const char *name);
150 vAttrPtr NewAttr(const char *name, const char *value);
151 void AddAttr(vAttrPtr attr);
152 void AddParam(vAttrPtr &attr, const char *name, const char *value);
153 std::string GetAttr(const char *attrname);
155 void RecurToVCal();
156 void RecurToBarryCal();
158 static unsigned short GetWeekDayIndex(const char *dayname);
159 bool HasMultipleVEvents() const;
161 public:
162 vCalendar();
163 ~vCalendar();
165 const std::string& ToVCal(const Barry::Calendar &cal);
166 const Barry::Calendar& ToBarry(const char *vcal, uint32_t RecordId);
168 const std::string& GetVCal() const { return m_vCalData; }
169 const Barry::Calendar& GetBarryCal() const { return m_BarryCal; }
171 char* ExtractVCal();
173 void Clear();
177 class VEventConverter
179 char *m_Data;
180 Barry::Calendar m_Cal;
181 uint32_t m_RecordId;
183 public:
184 VEventConverter();
185 explicit VEventConverter(uint32_t newRecordId);
186 ~VEventConverter();
188 // Transfers ownership of m_Data to the caller
189 char* ExtractData();
191 // Parses vevent data
192 bool ParseData(const char *data);
194 // Barry storage operator
195 void operator()(const Barry::Calendar &rec);
197 // Barry builder operator
198 bool operator()(Barry::Calendar &rec, unsigned int dbId);
200 // Handles calling of the Barry::Controller to fetch a specific
201 // record, indicated by index (into the RecordStateTable).
202 // Returns a g_malloc'd string of data containing the vevent20
203 // data. It is the responsibility of the caller to free it.
204 // This is intended to be passed into the GetChanges() function.
205 static char* GetRecordData(BarryEnvironment *env, unsigned int dbId,
206 Barry::RecordStateTable::IndexType index);
208 // Handles either adding or overwriting a calendar record,
209 // given vevent20 data in data, and the proper environmebnt,
210 // dbId, StateIndex. Set add to true if adding.
211 static bool CommitRecordData(BarryEnvironment *env, unsigned int dbId,
212 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
213 const char *data, bool add, std::string &errmsg);
217 #endif