3 // Conversion routines for vevents (VCALENDAR, etc)
7 Copyright (C) 2006-2013, 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"
36 using namespace Barry::Sync
;
38 //////////////////////////////////////////////////////////////////////////////
41 VEventConverter::VEventConverter()
46 VEventConverter::VEventConverter(uint32_t newRecordId
)
48 m_RecordId(newRecordId
)
52 VEventConverter::~VEventConverter()
58 // Transfers ownership of m_Data to the caller
59 char* VEventConverter::ExtractData()
61 Trace
trace("VEventConverter::ExtractData");
67 bool VEventConverter::ParseData(const char *data
)
69 Trace
trace("VEventConverter::ParseData");
75 m_Cal
= vcal
.ToBarry(data
, m_RecordId
);
78 catch( Barry::ConvertError
&ce
) {
79 trace
.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce
.what());
80 m_last_errmsg
= ce
.what();
87 bool VEventConverter::MergeData(const Barry::Calendar
&origin
)
89 // Save CalendarID value
90 // CalendarID field is used to link an entry event to an account mail
91 if (origin
.CalendarID
!= m_Cal
.CalendarID
)
92 m_Cal
.CalendarID
= origin
.CalendarID
;
97 // Barry storage operator
98 void VEventConverter::operator()(const Barry::Calendar
&rec
)
100 Trace
trace("VEventConverter::operator()");
102 // Delete data if some already exists
108 // Keep a trace of Calendar object (need to merge with the new event)
115 m_Data
= vcal
.ExtractVCal();
118 catch( Barry::ConvertError
&ce
) {
119 trace
.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce
.what());
120 m_last_errmsg
= ce
.what();
124 // Barry builder operator
125 bool VEventConverter::operator()(Barry::Calendar
&rec
, Barry::Builder
&)
127 Trace
trace("VEventConverter::builder operator()");
133 // Handles calling of the Barry::Controller to fetch a specific
134 // record, indicated by index (into the RecordStateTable).
135 // Returns a g_malloc'd string of data containing the vevent20
136 // data. It is the responsibility of the caller to free it.
137 // This is intended to be passed into the GetChanges() function.
138 char* VEventConverter::GetRecordData(BarryEnvironment
*env
, unsigned int dbId
,
139 Barry::RecordStateTable::IndexType index
)
141 Trace
trace("VEventConverter::GetRecordData()");
143 using namespace Barry
;
145 VEventConverter cal2event
;
146 RecordParser
<Calendar
, VEventConverter
> parser(cal2event
);
147 env
->GetDesktop()->GetRecord(dbId
, index
, parser
);
148 return cal2event
.ExtractData();
151 bool VEventConverter::CommitRecordData(BarryEnvironment
*env
, unsigned int dbId
,
152 Barry::RecordStateTable::IndexType StateIndex
, uint32_t recordId
,
153 const char *data
, bool add
, std::string
&errmsg
)
155 Trace
trace("VEventConverter::CommitRecordData()");
157 uint32_t newRecordId
;
159 // use given id if possible
160 if( recordId
&& !env
->m_CalendarSync
.m_Table
.GetIndex(recordId
) ) {
161 // recordId is unique and non-zero
162 newRecordId
= recordId
;
165 trace
.log(_("Can't use recommended recordId, generating new one."));
166 newRecordId
= env
->m_CalendarSync
.m_Table
.MakeNewRecordId();
170 newRecordId
= env
->m_CalendarSync
.m_Table
.StateMap
[StateIndex
].RecordId
;
172 trace
.logf("newRecordId: %u", newRecordId
);
174 VEventConverter
convert(newRecordId
);
175 if( !convert
.ParseData(data
) ) {
176 std::ostringstream oss
;
177 oss
<< _("unable to parse change data for new RecordId: ")
179 << " (" << convert
.GetLastError() << ") "
180 << _("data: ") << data
;
182 trace
.log(errmsg
.c_str());
186 // If we modify a data, we read at first its current value
187 // then we merge with the parsed value from the other opensync member
188 // Merge function is important because, we have to save some BlackBerry fields.
189 // Fix an issue with the new OS release who supports several calendar.
191 using namespace Barry
;
193 VEventConverter cal2event
;
194 RecordParser
<Calendar
, VEventConverter
> parser(cal2event
);
195 env
->GetDesktop()->GetRecord(dbId
, StateIndex
, parser
);
197 convert
.MergeData(cal2event
.GetCalendar());
200 Barry::RecordBuilder
<Barry::Calendar
, VEventConverter
> builder(convert
);
203 trace
.log(_("adding record"));
204 env
->GetDesktop()->AddRecord(dbId
, builder
);
207 trace
.log(_("setting record"));
208 env
->GetDesktop()->SetRecord(dbId
, StateIndex
, builder
);
209 trace
.log(_("clearing dirty flag"));
210 env
->GetDesktop()->ClearDirty(dbId
, StateIndex
);