3 // Conversion routines for vevents (VCALENDAR, etc)
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>
24 #include "environment.h"
31 using namespace Barry::Sync
;
33 //////////////////////////////////////////////////////////////////////////////
36 VEventConverter::VEventConverter()
41 VEventConverter::VEventConverter(uint32_t newRecordId
)
43 m_RecordId(newRecordId
)
47 VEventConverter::~VEventConverter()
53 // Transfers ownership of m_Data to the caller
54 char* VEventConverter::ExtractData()
56 Trace
trace("VEventConverter::ExtractData");
62 bool VEventConverter::ParseData(const char *data
)
64 Trace
trace("VEventConverter::ParseData");
70 m_Cal
= vcal
.ToBarry(data
, m_RecordId
);
73 catch( Barry::ConvertError
&ce
) {
74 trace
.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce
.what());
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
;
91 // Barry storage operator
92 void VEventConverter::operator()(const Barry::Calendar
&rec
)
94 Trace
trace("VEventConverter::operator()");
96 // Delete data if some already exists
102 // Keep a trace of Calendar object (need to merge with the new event)
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()");
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
;
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
;
159 trace
.log("Can't use recommended recordId, generating new one.");
160 newRecordId
= env
->m_CalendarSync
.m_Table
.MakeNewRecordId();
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
;
174 trace
.log(errmsg
.c_str());
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.
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
);
196 trace
.log("adding record");
197 env
->m_pDesktop
->AddRecord(dbId
, builder
);
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
);