Bumped copyright dates for 2013
[barry.git] / opensync-plugin / src / vevent.cc
blob4ac476ddd83909f775a05295842b568d614d981d
1 //
2 // \file vevent.cc
3 // Conversion routines for vevents (VCALENDAR, etc)
4 //
6 /*
7 Copyright (C) 2006-2013, 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 #include <barry/vevent.h>
23 #include "vevent.h"
24 #include "environment.h"
25 #include "trace.h"
26 #include <stdint.h>
27 #include <glib.h>
28 #include <strings.h>
29 #include <sstream>
30 #include "i18n.h"
32 using namespace Barry::Sync;
34 //////////////////////////////////////////////////////////////////////////////
37 VEventConverter::VEventConverter()
38 : m_Data(0)
42 VEventConverter::VEventConverter(uint32_t newRecordId)
43 : m_Data(0),
44 m_RecordId(newRecordId)
48 VEventConverter::~VEventConverter()
50 if( m_Data )
51 g_free(m_Data);
54 // Transfers ownership of m_Data to the caller
55 char* VEventConverter::ExtractData()
57 Trace trace("VEventConverter::ExtractData");
58 char *ret = m_Data;
59 m_Data = 0;
60 return ret;
63 bool VEventConverter::ParseData(const char *data)
65 Trace trace("VEventConverter::ParseData");
67 try {
69 vTimeConverter vtc;
70 vCalendar vcal(vtc);
71 m_Cal = vcal.ToBarry(data, m_RecordId);
74 catch( Barry::ConvertError &ce ) {
75 trace.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce.what());
76 m_last_errmsg = ce.what();
77 return false;
80 return true;
83 bool VEventConverter::MergeData(const Barry::Calendar &origin)
85 // Save CalendarID value
86 // CalendarID field is used to link an entry event to an account mail
87 if (origin.CalendarID != m_Cal.CalendarID)
88 m_Cal.CalendarID = origin.CalendarID;
90 return true;
93 // Barry storage operator
94 void VEventConverter::operator()(const Barry::Calendar &rec)
96 Trace trace("VEventConverter::operator()");
98 // Delete data if some already exists
99 if( m_Data ) {
100 g_free(m_Data);
101 m_Data = 0;
104 // Keep a trace of Calendar object (need to merge with the new event)
105 m_Cal = rec;
107 try {
109 vTimeConverter vtc;
110 vCalendar vcal(vtc);
111 vcal.ToVCal(rec);
112 m_Data = vcal.ExtractVCal();
115 catch( Barry::ConvertError &ce ) {
116 trace.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce.what());
117 m_last_errmsg = ce.what();
121 // Barry builder operator
122 bool VEventConverter::operator()(Barry::Calendar &rec, Barry::Builder &)
124 Trace trace("VEventConverter::builder operator()");
126 rec = m_Cal;
127 return true;
130 // Handles calling of the Barry::Controller to fetch a specific
131 // record, indicated by index (into the RecordStateTable).
132 // Returns a g_malloc'd string of data containing the vevent20
133 // data. It is the responsibility of the caller to free it.
134 // This is intended to be passed into the GetChanges() function.
135 char* VEventConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
136 Barry::RecordStateTable::IndexType index)
138 Trace trace("VEventConverter::GetRecordData()");
140 using namespace Barry;
142 VEventConverter cal2event;
143 RecordParser<Calendar, VEventConverter> parser(cal2event);
144 env->GetDesktop()->GetRecord(dbId, index, parser);
145 return cal2event.ExtractData();
148 bool VEventConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
149 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
150 const char *data, bool add, std::string &errmsg)
152 Trace trace("VEventConverter::CommitRecordData()");
154 uint32_t newRecordId;
155 if( add ) {
156 // use given id if possible
157 if( recordId && !env->m_CalendarSync.m_Table.GetIndex(recordId) ) {
158 // recordId is unique and non-zero
159 newRecordId = recordId;
161 else {
162 trace.log(_("Can't use recommended recordId, generating new one."));
163 newRecordId = env->m_CalendarSync.m_Table.MakeNewRecordId();
166 else {
167 newRecordId = env->m_CalendarSync.m_Table.StateMap[StateIndex].RecordId;
169 trace.logf("newRecordId: %lu", newRecordId);
171 VEventConverter convert(newRecordId);
172 if( !convert.ParseData(data) ) {
173 std::ostringstream oss;
174 oss << _("unable to parse change data for new RecordId: ")
175 << newRecordId
176 << " (" << convert.GetLastError() << ") "
177 << _("data: ") << data;
178 errmsg = oss.str();
179 trace.log(errmsg.c_str());
180 return false;
183 // If we modify a data, we read at first its current value
184 // then we merge with the parsed value from the other opensync member
185 // Merge function is important because, we have to save some BlackBerry fields.
186 // Fix an issue with the new OS release who supports several calendar.
187 if( !add ) {
188 using namespace Barry;
190 VEventConverter cal2event;
191 RecordParser<Calendar, VEventConverter> parser(cal2event);
192 env->GetDesktop()->GetRecord(dbId, StateIndex, parser);
193 Calendar cal = cal2event.GetCalendar();
195 convert.MergeData(cal);
198 Barry::RecordBuilder<Barry::Calendar, VEventConverter> builder(convert);
200 if( add ) {
201 trace.log(_("adding record"));
202 env->GetDesktop()->AddRecord(dbId, builder);
204 else {
205 trace.log(_("setting record"));
206 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
207 trace.log(_("clearing dirty flag"));
208 env->GetDesktop()->ClearDirty(dbId, StateIndex);
211 return true;