os22: fix exception messages, to indicate where they came from
[barry.git] / opensync-plugin / src / vevent.cc
blob4233befda23338b49a306e59d044e4c9348394af
1 //
2 // \file vevent.cc
3 // Conversion routines for vevents (VCALENDAR, etc)
4 //
6 /*
7 Copyright (C) 2006-2010, 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 return false;
78 return true;
81 bool VEventConverter::MergeData(const Barry::Calendar &origin)
83 // Save CalendarID value
84 // CalendarID field is used to link an entry event to an account mail
85 if (origin.CalendarID != m_Cal.CalendarID)
86 m_Cal.CalendarID = origin.CalendarID;
88 return true;
91 // Barry storage operator
92 void VEventConverter::operator()(const Barry::Calendar &rec)
94 Trace trace("VEventConverter::operator()");
96 // Delete data if some already exists
97 if( m_Data ) {
98 g_free(m_Data);
99 m_Data = 0;
102 // Keep a trace of Calendar object (need to merge with the new event)
103 m_Cal = rec;
105 try {
107 vTimeConverter vtc;
108 vCalendar vcal(vtc);
109 vcal.ToVCal(rec);
110 m_Data = vcal.ExtractVCal();
113 catch( Barry::ConvertError &ce ) {
114 trace.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce.what());
118 // Barry builder operator
119 bool VEventConverter::operator()(Barry::Calendar &rec, unsigned int dbId)
121 Trace trace("VEventConverter::builder operator()");
123 rec = m_Cal;
124 return true;
127 // Handles calling of the Barry::Controller to fetch a specific
128 // record, indicated by index (into the RecordStateTable).
129 // Returns a g_malloc'd string of data containing the vevent20
130 // data. It is the responsibility of the caller to free it.
131 // This is intended to be passed into the GetChanges() function.
132 char* VEventConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
133 Barry::RecordStateTable::IndexType index)
135 Trace trace("VEventConverter::GetRecordData()");
137 using namespace Barry;
139 VEventConverter cal2event;
140 RecordParser<Calendar, VEventConverter> parser(cal2event);
141 env->m_pDesktop->GetRecord(dbId, index, parser);
142 return cal2event.ExtractData();
145 bool VEventConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
146 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
147 const char *data, bool add, std::string &errmsg)
149 Trace trace("VEventConverter::CommitRecordData()");
151 uint32_t newRecordId;
152 if( add ) {
153 // use given id if possible
154 if( recordId && !env->m_CalendarSync.m_Table.GetIndex(recordId) ) {
155 // recordId is unique and non-zero
156 newRecordId = recordId;
158 else {
159 trace.log("Can't use recommended recordId, generating new one.");
160 newRecordId = env->m_CalendarSync.m_Table.MakeNewRecordId();
163 else {
164 newRecordId = env->m_CalendarSync.m_Table.StateMap[StateIndex].RecordId;
166 trace.logf("newRecordId: %lu", newRecordId);
168 VEventConverter convert(newRecordId);
169 if( !convert.ParseData(data) ) {
170 std::ostringstream oss;
171 oss << "unable to parse change data for new RecordId: "
172 << newRecordId << " data: " << data;
173 errmsg = oss.str();
174 trace.log(errmsg.c_str());
175 return false;
178 // If we modify a data, we read at first its current value
179 // then we merge with the parsed value from the other opensync member
180 // Merge function is important because, we have to save some BlackBerry fields.
181 // Fix an issue with the new OS release who supports several calendar.
182 if( !add ) {
183 using namespace Barry;
185 VEventConverter cal2event;
186 RecordParser<Calendar, VEventConverter> parser(cal2event);
187 env->m_pDesktop->GetRecord(dbId, StateIndex, parser);
188 Calendar cal = cal2event.GetCalendar();
190 convert.MergeData(cal);
193 Barry::RecordBuilder<Barry::Calendar, VEventConverter> builder(convert);
195 if( add ) {
196 trace.log("adding record");
197 env->m_pDesktop->AddRecord(dbId, builder);
199 else {
200 trace.log("setting record");
201 env->m_pDesktop->SetRecord(dbId, StateIndex, builder);
202 trace.log("clearing dirty flag");
203 env->m_pDesktop->ClearDirty(dbId, StateIndex);
206 return true;