Bumped copyright dates for 2013
[barry.git] / opensync-plugin-0.4x / src / vevent.cc
blob48485d7427ec958ef516e27c1f8f849ebae473d3
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 <opensync/opensync.h>
23 #include <opensync/opensync-time.h>
25 #include "vevent.h"
26 #include "environment.h"
27 #include "trace.h"
28 #include "tosserror.h"
29 #include <stdint.h>
30 #include <glib.h>
31 #include <strings.h>
32 #include <sstream>
33 #include <vector>
34 #include "i18n.h"
36 using namespace Barry::Sync;
38 //////////////////////////////////////////////////////////////////////////////
41 VEventConverter::VEventConverter()
42 : m_Data(0)
46 VEventConverter::VEventConverter(uint32_t newRecordId)
47 : m_Data(0),
48 m_RecordId(newRecordId)
52 VEventConverter::~VEventConverter()
54 if( m_Data )
55 g_free(m_Data);
58 // Transfers ownership of m_Data to the caller
59 char* VEventConverter::ExtractData()
61 Trace trace("VEventConverter::ExtractData");
62 char *ret = m_Data;
63 m_Data = 0;
64 return ret;
67 bool VEventConverter::ParseData(const char *data)
69 Trace trace("VEventConverter::ParseData");
71 try {
73 vTimeConverter vtc;
74 vCalendar vcal(vtc);
75 m_Cal = vcal.ToBarry(data, m_RecordId);
78 catch( Barry::ConvertError &ce ) {
79 trace.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce.what());
80 m_last_errmsg = ce.what();
81 return false;
84 return true;
87 bool VEventConverter::MergeData(const Barry::Calendar &origin)
89 // Save CalendarID value
90 // CalendarID field is used to link an entry event to an account mail
91 if (origin.CalendarID != m_Cal.CalendarID)
92 m_Cal.CalendarID = origin.CalendarID;
94 return true;
97 // Barry storage operator
98 void VEventConverter::operator()(const Barry::Calendar &rec)
100 Trace trace("VEventConverter::operator()");
102 // Delete data if some already exists
103 if( m_Data ) {
104 g_free(m_Data);
105 m_Data = 0;
108 // Keep a trace of Calendar object (need to merge with the new event)
109 m_Cal = rec;
111 try {
112 vTimeConverter vtc;
113 vCalendar vcal(vtc);
114 vcal.ToVCal(rec);
115 m_Data = vcal.ExtractVCal();
118 catch( Barry::ConvertError &ce ) {
119 trace.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce.what());
120 m_last_errmsg = ce.what();
124 // Barry builder operator
125 bool VEventConverter::operator()(Barry::Calendar &rec, Barry::Builder &)
127 Trace trace("VEventConverter::builder operator()");
129 rec = m_Cal;
130 return true;
133 // Handles calling of the Barry::Controller to fetch a specific
134 // record, indicated by index (into the RecordStateTable).
135 // Returns a g_malloc'd string of data containing the vevent20
136 // data. It is the responsibility of the caller to free it.
137 // This is intended to be passed into the GetChanges() function.
138 char* VEventConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
139 Barry::RecordStateTable::IndexType index)
141 Trace trace("VEventConverter::GetRecordData()");
143 using namespace Barry;
145 VEventConverter cal2event;
146 RecordParser<Calendar, VEventConverter> parser(cal2event);
147 env->GetDesktop()->GetRecord(dbId, index, parser);
148 return cal2event.ExtractData();
151 bool VEventConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
152 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
153 const char *data, bool add, std::string &errmsg)
155 Trace trace("VEventConverter::CommitRecordData()");
157 uint32_t newRecordId;
158 if( add ) {
159 // use given id if possible
160 if( recordId && !env->m_CalendarSync.m_Table.GetIndex(recordId) ) {
161 // recordId is unique and non-zero
162 newRecordId = recordId;
164 else {
165 trace.log(_("Can't use recommended recordId, generating new one."));
166 newRecordId = env->m_CalendarSync.m_Table.MakeNewRecordId();
169 else {
170 newRecordId = env->m_CalendarSync.m_Table.StateMap[StateIndex].RecordId;
172 trace.logf("newRecordId: %u", newRecordId);
174 VEventConverter convert(newRecordId);
175 if( !convert.ParseData(data) ) {
176 std::ostringstream oss;
177 oss << _("unable to parse change data for new RecordId: ")
178 << newRecordId
179 << " (" << convert.GetLastError() << ") "
180 << _("data: ") << data;
181 errmsg = oss.str();
182 trace.log(errmsg.c_str());
183 return false;
186 // If we modify a data, we read at first its current value
187 // then we merge with the parsed value from the other opensync member
188 // Merge function is important because, we have to save some BlackBerry fields.
189 // Fix an issue with the new OS release who supports several calendar.
190 if( !add ) {
191 using namespace Barry;
193 VEventConverter cal2event;
194 RecordParser<Calendar, VEventConverter> parser(cal2event);
195 env->GetDesktop()->GetRecord(dbId, StateIndex, parser);
197 convert.MergeData(cal2event.GetCalendar());
200 Barry::RecordBuilder<Barry::Calendar, VEventConverter> builder(convert);
202 if( add ) {
203 trace.log(_("adding record"));
204 env->GetDesktop()->AddRecord(dbId, builder);
206 else {
207 trace.log(_("setting record"));
208 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
209 trace.log(_("clearing dirty flag"));
210 env->GetDesktop()->ClearDirty(dbId, StateIndex);
213 return true;