3 // Conversion routines for vtodos (VCALENDAR, etc)
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2006-2013, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <opensync/opensync.h>
24 #include <opensync/opensync-time.h>
27 #include "environment.h"
29 #include "tosserror.h"
36 using namespace Barry::Sync
;
38 //////////////////////////////////////////////////////////////////////////////
41 VTodoConverter::VTodoConverter()
46 VTodoConverter::VTodoConverter(uint32_t newRecordId
)
48 m_RecordId(newRecordId
)
52 VTodoConverter::~VTodoConverter()
58 // Transfers ownership of m_Data to the caller
59 char* VTodoConverter::ExtractData()
61 Trace
trace("VTodoConverter::ExtractData");
67 bool VTodoConverter::ParseData(const char *data
)
69 Trace
trace("VTodoConverter::ParseData");
75 m_Task
= vtodo
.ToBarry(data
, m_RecordId
);
78 catch( Barry::ConvertError
&ce
) {
79 trace
.logf(_("ERROR: vtodo:Barry::ConvertError exception: %s"), ce
.what());
80 m_last_errmsg
= ce
.what();
87 // Barry storage operator
88 void VTodoConverter::operator()(const Barry::Task
&rec
)
90 Trace
trace("VTodoConverter::operator()");
92 // Delete data if some already exists
103 m_Data
= vtodo
.ExtractVTodo();
106 catch( Barry::ConvertError
&ce
) {
107 trace
.logf(_("ERROR: vtodo:Barry::ConvertError exception: %s"), ce
.what());
108 m_last_errmsg
= ce
.what();
112 // Barry builder operator
113 bool VTodoConverter::operator()(Barry::Task
&rec
, Barry::Builder
&)
115 Trace
trace("VTodoConverter::builder operator()");
121 // Handles calling of the Barry::Controller to fetch a specific
122 // record, indicated by index (into the RecordStateTable).
123 // Returns a g_malloc'd string of data containing the vevent20
124 // data. It is the responsibility of the caller to free it.
125 // This is intended to be passed into the GetChanges() function.
126 char* VTodoConverter::GetRecordData(BarryEnvironment
*env
, unsigned int dbId
,
127 Barry::RecordStateTable::IndexType index
)
129 Trace
trace("VTodoConverter::GetRecordData()");
131 using namespace Barry
;
133 VTodoConverter task2todo
;
134 RecordParser
<Task
, VTodoConverter
> parser(task2todo
);
135 env
->GetDesktop()->GetRecord(dbId
, index
, parser
);
136 return task2todo
.ExtractData();
139 bool VTodoConverter::CommitRecordData(BarryEnvironment
*env
, unsigned int dbId
,
140 Barry::RecordStateTable::IndexType StateIndex
, uint32_t recordId
,
141 const char *data
, bool add
, std::string
&errmsg
)
143 Trace
trace("VTodoConverter::CommitRecordData()");
145 uint32_t newRecordId
;
147 // use given id if possible
148 if( recordId
&& !env
->m_TodoSync
.m_Table
.GetIndex(recordId
) ) {
149 // recordId is unique and non-zero
150 newRecordId
= recordId
;
153 trace
.log(_("Can't use recommended recordId, generating new one."));
154 newRecordId
= env
->m_TodoSync
.m_Table
.MakeNewRecordId();
158 newRecordId
= env
->m_TodoSync
.m_Table
.StateMap
[StateIndex
].RecordId
;
160 trace
.logf("newRecordId: %u", newRecordId
);
162 VTodoConverter
convert(newRecordId
);
163 if( !convert
.ParseData(data
) ) {
164 std::ostringstream oss
;
165 oss
<< _("unable to parse change data for new RecordId: ")
167 << " (" << convert
.GetLastError() << ") "
168 << _("data: ") << data
;
170 trace
.log(errmsg
.c_str());
174 Barry::RecordBuilder
<Barry::Task
, VTodoConverter
> builder(convert
);
177 trace
.log(_("adding record"));
178 env
->GetDesktop()->AddRecord(dbId
, builder
);
181 // we need to use a workaround for the Tasks database,
182 // since there is a bug in many device firmwares which
183 // causes corruption when using SetRecord().
185 // so instead of the nice simple:
188 trace.log("setting record");
189 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
190 trace.log("clearing dirty flag");
191 env->GetDesktop()->ClearDirty(dbId, StateIndex);
194 // we have to delete, add, refresh the state index table,
195 // and then clear the dirty flag on the new record
197 // but since the upper level code will clear all the
198 // dirty flags for us, we can skip the state index and
199 // dirty flag step, and leave it for FinishSync() in
202 trace
.log(_("deleting task record"));
203 env
->GetDesktop()->DeleteRecord(dbId
, StateIndex
);
204 trace
.log(_("re-adding task record"));
205 env
->GetDesktop()->AddRecord(dbId
, builder
);