lib: add prefixes to enums to avoid name clashes on Windows (like ERROR)
[barry.git] / opensync-plugin-0.4x / src / vevent.cc
blob785c06f6deaf61b603726e6cca25c2981977d188
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 <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>
35 using namespace Barry::Sync;
37 //////////////////////////////////////////////////////////////////////////////
40 VEventConverter::VEventConverter()
41 : m_Data(0)
45 VEventConverter::VEventConverter(uint32_t newRecordId)
46 : m_Data(0),
47 m_RecordId(newRecordId)
51 VEventConverter::~VEventConverter()
53 if( m_Data )
54 g_free(m_Data);
57 // Transfers ownership of m_Data to the caller
58 char* VEventConverter::ExtractData()
60 Trace trace("VEventConverter::ExtractData");
61 char *ret = m_Data;
62 m_Data = 0;
63 return ret;
66 bool VEventConverter::ParseData(const char *data)
68 Trace trace("VEventConverter::ParseData");
70 try {
72 vTimeConverter vtc;
73 vCalendar vcal(vtc);
74 m_Cal = vcal.ToBarry(data, m_RecordId);
77 catch( Barry::ConvertError &ce ) {
78 trace.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce.what());
79 return false;
82 return true;
85 bool VEventConverter::MergeData(const Barry::Calendar &origin)
87 // Save CalendarID value
88 // CalendarID field is used to link an entry event to an account mail
89 if (origin.CalendarID != m_Cal.CalendarID)
90 m_Cal.CalendarID = origin.CalendarID;
92 return true;
95 // Barry storage operator
96 void VEventConverter::operator()(const Barry::Calendar &rec)
98 Trace trace("VEventConverter::operator()");
100 // Delete data if some already exists
101 if( m_Data ) {
102 g_free(m_Data);
103 m_Data = 0;
106 // Keep a trace of Calendar object (need to merge with the new event)
107 m_Cal = rec;
109 try {
110 vTimeConverter vtc;
111 vCalendar vcal(vtc);
112 vcal.ToVCal(rec);
113 m_Data = vcal.ExtractVCal();
116 catch( Barry::ConvertError &ce ) {
117 trace.logf("ERROR: vevent:Barry::ConvertError exception: %s", 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->m_pDesktop->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: %u", 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 << " 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->m_pDesktop->GetRecord(dbId, StateIndex, parser);
192 convert.MergeData(cal2event.GetCalendar());
195 Barry::RecordBuilder<Barry::Calendar, VEventConverter> builder(convert);
197 if( add ) {
198 trace.log("adding record");
199 env->m_pDesktop->AddRecord(dbId, builder);
201 else {
202 trace.log("setting record");
203 env->m_pDesktop->SetRecord(dbId, StateIndex, builder);
204 trace.log("clearing dirty flag");
205 env->m_pDesktop->ClearDirty(dbId, StateIndex);
208 return true;