3 // Conversion routines for vevents (VCALENDAR, etc)
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>
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());
75 m_last_errmsg
= ce
.what();
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
;
92 // Barry storage operator
93 void VEventConverter::operator()(const Barry::Calendar
&rec
)
95 Trace
trace("VEventConverter::operator()");
97 // Delete data if some already exists
103 // Keep a trace of Calendar object (need to merge with the new event)
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()");
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
;
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
;
161 trace
.log("Can't use recommended recordId, generating new one.");
162 newRecordId
= env
->m_CalendarSync
.m_Table
.MakeNewRecordId();
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: "
175 << " (" << convert
.GetLastError() << ") data: " << data
;
177 trace
.log(errmsg
.c_str());
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.
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
);
199 trace
.log("adding record");
200 env
->GetDesktop()->AddRecord(dbId
, builder
);
203 trace
.log("setting record");
204 env
->GetDesktop()->SetRecord(dbId
, StateIndex
, builder
);
205 trace
.log("clearing dirty flag");
206 env
->GetDesktop()->ClearDirty(dbId
, StateIndex
);