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 <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());
79 m_last_errmsg
= ce
.what();
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
;
96 // Barry storage operator
97 void VEventConverter::operator()(const Barry::Calendar
&rec
)
99 Trace
trace("VEventConverter::operator()");
101 // Delete data if some already exists
107 // Keep a trace of Calendar object (need to merge with the new event)
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()");
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
;
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
;
164 trace
.log("Can't use recommended recordId, generating new one.");
165 newRecordId
= env
->m_CalendarSync
.m_Table
.MakeNewRecordId();
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: "
178 << " (" << convert
.GetLastError() << ") data: " << data
;
180 trace
.log(errmsg
.c_str());
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.
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
);
201 trace
.log("adding record");
202 env
->GetDesktop()->AddRecord(dbId
, builder
);
205 trace
.log("setting record");
206 env
->GetDesktop()->SetRecord(dbId
, StateIndex
, builder
);
207 trace
.log("clearing dirty flag");
208 env
->GetDesktop()->ClearDirty(dbId
, StateIndex
);