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 <opensync/opensync.h>
23 #include <opensync/opensync-time.h>
26 #include "environment.h"
28 #include "tosserror.h"
35 using namespace Barry::Sync
;
37 //////////////////////////////////////////////////////////////////////////////
40 VEventConverter::VEventConverter()
45 VEventConverter::VEventConverter(uint32_t newRecordId
)
47 m_RecordId(newRecordId
)
51 VEventConverter::~VEventConverter()
57 // Transfers ownership of m_Data to the caller
58 char* VEventConverter::ExtractData()
60 Trace
trace("VEventConverter::ExtractData");
66 bool VEventConverter::ParseData(const char *data
)
68 Trace
trace("VEventConverter::ParseData");
74 m_Cal
= vcal
.ToBarry(data
, m_RecordId
);
77 catch( Barry::ConvertError
&ce
) {
78 trace
.logf("ERROR: vevent:Barry::ConvertError exception: %s", ce
.what());
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
;
95 // Barry storage operator
96 void VEventConverter::operator()(const Barry::Calendar
&rec
)
98 Trace
trace("VEventConverter::operator()");
100 // Delete data if some already exists
106 // Keep a trace of Calendar object (need to merge with the new event)
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()");
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
;
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
;
162 trace
.log("Can't use recommended recordId, generating new one.");
163 newRecordId
= env
->m_CalendarSync
.m_Table
.MakeNewRecordId();
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
;
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
->m_pDesktop
->GetRecord(dbId
, StateIndex
, parser
);
192 convert
.MergeData(cal2event
.GetCalendar());
195 Barry::RecordBuilder
<Barry::Calendar
, VEventConverter
> builder(convert
);
198 trace
.log("adding record");
199 env
->m_pDesktop
->AddRecord(dbId
, builder
);
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
);