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 <barry/vevent.h>
24 #include "environment.h"
32 using namespace Barry::Sync
;
34 //////////////////////////////////////////////////////////////////////////////
37 VEventConverter::VEventConverter()
42 VEventConverter::VEventConverter(uint32_t newRecordId
)
44 m_RecordId(newRecordId
)
48 VEventConverter::~VEventConverter()
54 // Transfers ownership of m_Data to the caller
55 char* VEventConverter::ExtractData()
57 Trace
trace("VEventConverter::ExtractData");
63 bool VEventConverter::ParseData(const char *data
)
65 Trace
trace("VEventConverter::ParseData");
71 m_Cal
= vcal
.ToBarry(data
, m_RecordId
);
74 catch( Barry::ConvertError
&ce
) {
75 trace
.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce
.what());
76 m_last_errmsg
= ce
.what();
83 bool VEventConverter::MergeData(const Barry::Calendar
&origin
)
85 // Save CalendarID value
86 // CalendarID field is used to link an entry event to an account mail
87 if (origin
.CalendarID
!= m_Cal
.CalendarID
)
88 m_Cal
.CalendarID
= origin
.CalendarID
;
93 // Barry storage operator
94 void VEventConverter::operator()(const Barry::Calendar
&rec
)
96 Trace
trace("VEventConverter::operator()");
98 // Delete data if some already exists
104 // Keep a trace of Calendar object (need to merge with the new event)
112 m_Data
= vcal
.ExtractVCal();
115 catch( Barry::ConvertError
&ce
) {
116 trace
.logf(_("ERROR: vevent:Barry::ConvertError exception: %s"), ce
.what());
117 m_last_errmsg
= 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
->GetDesktop()->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: %lu", newRecordId
);
171 VEventConverter
convert(newRecordId
);
172 if( !convert
.ParseData(data
) ) {
173 std::ostringstream oss
;
174 oss
<< _("unable to parse change data for new RecordId: ")
176 << " (" << convert
.GetLastError() << ") "
177 << _("data: ") << data
;
179 trace
.log(errmsg
.c_str());
183 // If we modify a data, we read at first its current value
184 // then we merge with the parsed value from the other opensync member
185 // Merge function is important because, we have to save some BlackBerry fields.
186 // Fix an issue with the new OS release who supports several calendar.
188 using namespace Barry
;
190 VEventConverter cal2event
;
191 RecordParser
<Calendar
, VEventConverter
> parser(cal2event
);
192 env
->GetDesktop()->GetRecord(dbId
, StateIndex
, parser
);
193 Calendar cal
= cal2event
.GetCalendar();
195 convert
.MergeData(cal
);
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
);