lib: added implicit ctor converter from DatabaseDatabase to DBListType
[barry/progweb.git] / opensync-plugin-0.4x / src / vevent.cc
blobeaa98601c21f90097574e3014d447ff6ff5c6248
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 <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 m_last_errmsg = ce.what();
80 return false;
83 return true;
86 bool VEventConverter::MergeData(const Barry::Calendar &origin)
88 // Save CalendarID value
89 // CalendarID field is used to link an entry event to an account mail
90 if (origin.CalendarID != m_Cal.CalendarID)
91 m_Cal.CalendarID = origin.CalendarID;
93 return true;
96 // Barry storage operator
97 void VEventConverter::operator()(const Barry::Calendar &rec)
99 Trace trace("VEventConverter::operator()");
101 // Delete data if some already exists
102 if( m_Data ) {
103 g_free(m_Data);
104 m_Data = 0;
107 // Keep a trace of Calendar object (need to merge with the new event)
108 m_Cal = rec;
110 try {
111 vTimeConverter vtc;
112 vCalendar vcal(vtc);
113 vcal.ToVCal(rec);
114 m_Data = vcal.ExtractVCal();
117 catch( Barry::ConvertError &ce ) {
118 trace.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce.what());
119 m_last_errmsg = ce.what();
123 // Barry builder operator
124 bool VEventConverter::operator()(Barry::Calendar &rec, Barry::Builder &)
126 Trace trace("VEventConverter::builder operator()");
128 rec = m_Cal;
129 return true;
132 // Handles calling of the Barry::Controller to fetch a specific
133 // record, indicated by index (into the RecordStateTable).
134 // Returns a g_malloc'd string of data containing the vevent20
135 // data. It is the responsibility of the caller to free it.
136 // This is intended to be passed into the GetChanges() function.
137 char* VEventConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
138 Barry::RecordStateTable::IndexType index)
140 Trace trace("VEventConverter::GetRecordData()");
142 using namespace Barry;
144 VEventConverter cal2event;
145 RecordParser<Calendar, VEventConverter> parser(cal2event);
146 env->GetDesktop()->GetRecord(dbId, index, parser);
147 return cal2event.ExtractData();
150 bool VEventConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
151 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
152 const char *data, bool add, std::string &errmsg)
154 Trace trace("VEventConverter::CommitRecordData()");
156 uint32_t newRecordId;
157 if( add ) {
158 // use given id if possible
159 if( recordId && !env->m_CalendarSync.m_Table.GetIndex(recordId) ) {
160 // recordId is unique and non-zero
161 newRecordId = recordId;
163 else {
164 trace.log("Can't use recommended recordId, generating new one.");
165 newRecordId = env->m_CalendarSync.m_Table.MakeNewRecordId();
168 else {
169 newRecordId = env->m_CalendarSync.m_Table.StateMap[StateIndex].RecordId;
171 trace.logf("newRecordId: %u", newRecordId);
173 VEventConverter convert(newRecordId);
174 if( !convert.ParseData(data) ) {
175 std::ostringstream oss;
176 oss << "unable to parse change data for new RecordId: "
177 << newRecordId
178 << " (" << convert.GetLastError() << ") data: " << data;
179 errmsg = oss.str();
180 trace.log(errmsg.c_str());
181 return false;
184 // If we modify a data, we read at first its current value
185 // then we merge with the parsed value from the other opensync member
186 // Merge function is important because, we have to save some BlackBerry fields.
187 // Fix an issue with the new OS release who supports several calendar.
188 if( !add ) {
189 using namespace Barry;
191 VEventConverter cal2event;
192 RecordParser<Calendar, VEventConverter> parser(cal2event);
193 env->GetDesktop()->GetRecord(dbId, StateIndex, parser);
195 convert.MergeData(cal2event.GetCalendar());
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;