lib: changed asserts to exceptions, to guarantee these are never used
[barry.git] / opensync-plugin / src / vevent.cc
blob68ea539cf6dca6bffd6096911c89a147bc831bbf
1 //
2 // \file vevent.cc
3 // Conversion routines for vevents (VCALENDAR, etc)
4 //
6 /*
7 Copyright (C) 2006-2012, 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>
31 using namespace Barry::Sync;
33 //////////////////////////////////////////////////////////////////////////////
36 VEventConverter::VEventConverter()
37 : m_Data(0)
41 VEventConverter::VEventConverter(uint32_t newRecordId)
42 : m_Data(0),
43 m_RecordId(newRecordId)
47 VEventConverter::~VEventConverter()
49 if( m_Data )
50 g_free(m_Data);
53 // Transfers ownership of m_Data to the caller
54 char* VEventConverter::ExtractData()
56 Trace trace("VEventConverter::ExtractData");
57 char *ret = m_Data;
58 m_Data = 0;
59 return ret;
62 bool VEventConverter::ParseData(const char *data)
64 Trace trace("VEventConverter::ParseData");
66 try {
68 vTimeConverter vtc;
69 vCalendar vcal(vtc);
70 m_Cal = vcal.ToBarry(data, m_RecordId);
73 catch( Barry::ConvertError &ce ) {
74 trace.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce.what());
75 m_last_errmsg = ce.what();
76 return false;
79 return true;
82 bool VEventConverter::MergeData(const Barry::Calendar &origin)
84 // Save CalendarID value
85 // CalendarID field is used to link an entry event to an account mail
86 if (origin.CalendarID != m_Cal.CalendarID)
87 m_Cal.CalendarID = origin.CalendarID;
89 return true;
92 // Barry storage operator
93 void VEventConverter::operator()(const Barry::Calendar &rec)
95 Trace trace("VEventConverter::operator()");
97 // Delete data if some already exists
98 if( m_Data ) {
99 g_free(m_Data);
100 m_Data = 0;
103 // Keep a trace of Calendar object (need to merge with the new event)
104 m_Cal = rec;
106 try {
108 vTimeConverter vtc;
109 vCalendar vcal(vtc);
110 vcal.ToVCal(rec);
111 m_Data = vcal.ExtractVCal();
114 catch( Barry::ConvertError &ce ) {
115 trace.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce.what());
116 m_last_errmsg = ce.what();
120 // Barry builder operator
121 bool VEventConverter::operator()(Barry::Calendar &rec, Barry::Builder &)
123 Trace trace("VEventConverter::builder operator()");
125 rec = m_Cal;
126 return true;
129 // Handles calling of the Barry::Controller to fetch a specific
130 // record, indicated by index (into the RecordStateTable).
131 // Returns a g_malloc'd string of data containing the vevent20
132 // data. It is the responsibility of the caller to free it.
133 // This is intended to be passed into the GetChanges() function.
134 char* VEventConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
135 Barry::RecordStateTable::IndexType index)
137 Trace trace("VEventConverter::GetRecordData()");
139 using namespace Barry;
141 VEventConverter cal2event;
142 RecordParser<Calendar, VEventConverter> parser(cal2event);
143 env->GetDesktop()->GetRecord(dbId, index, parser);
144 return cal2event.ExtractData();
147 bool VEventConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
148 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
149 const char *data, bool add, std::string &errmsg)
151 Trace trace("VEventConverter::CommitRecordData()");
153 uint32_t newRecordId;
154 if( add ) {
155 // use given id if possible
156 if( recordId && !env->m_CalendarSync.m_Table.GetIndex(recordId) ) {
157 // recordId is unique and non-zero
158 newRecordId = recordId;
160 else {
161 trace.log("Can't use recommended recordId, generating new one.");
162 newRecordId = env->m_CalendarSync.m_Table.MakeNewRecordId();
165 else {
166 newRecordId = env->m_CalendarSync.m_Table.StateMap[StateIndex].RecordId;
168 trace.logf("newRecordId: %lu", newRecordId);
170 VEventConverter convert(newRecordId);
171 if( !convert.ParseData(data) ) {
172 std::ostringstream oss;
173 oss << "unable to parse change data for new RecordId: "
174 << newRecordId
175 << " (" << convert.GetLastError() << ") data: " << data;
176 errmsg = oss.str();
177 trace.log(errmsg.c_str());
178 return false;
181 // If we modify a data, we read at first its current value
182 // then we merge with the parsed value from the other opensync member
183 // Merge function is important because, we have to save some BlackBerry fields.
184 // Fix an issue with the new OS release who supports several calendar.
185 if( !add ) {
186 using namespace Barry;
188 VEventConverter cal2event;
189 RecordParser<Calendar, VEventConverter> parser(cal2event);
190 env->GetDesktop()->GetRecord(dbId, StateIndex, parser);
191 Calendar cal = cal2event.GetCalendar();
193 convert.MergeData(cal);
196 Barry::RecordBuilder<Barry::Calendar, VEventConverter> builder(convert);
198 if( add ) {
199 trace.log("adding record");
200 env->GetDesktop()->AddRecord(dbId, builder);
202 else {
203 trace.log("setting record");
204 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
205 trace.log("clearing dirty flag");
206 env->GetDesktop()->ClearDirty(dbId, StateIndex);
209 return true;