3 // Conversion routines for vjournals (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"
35 using namespace Barry::Sync
;
37 //////////////////////////////////////////////////////////////////////////////
40 VJournalConverter::VJournalConverter()
45 VJournalConverter::VJournalConverter(uint32_t newRecordId
)
47 m_RecordId(newRecordId
)
51 VJournalConverter::~VJournalConverter()
57 // Transfers ownership of m_Data to the caller
58 char* VJournalConverter::ExtractData()
60 Trace
trace("VMemoConverter::ExtractData");
66 bool VJournalConverter::ParseData(const char *data
)
68 Trace
trace("VJournalConverter::ParseData");
73 vJournal
vjournal(vtc
);
74 m_Memo
= vjournal
.ToBarry(data
, m_RecordId
);
77 catch( Barry::ConvertError
&ce
) {
78 trace
.logf(_("ERROR: vjournal:Barry::ConvertError exception: %s"), ce
.what());
79 m_last_errmsg
= ce
.what();
86 // Barry storage operator
87 void VJournalConverter::operator()(const Barry::Memo
&rec
)
89 Trace
trace("VJournalConverter::operator()");
91 // Delete data if some already exists
100 vJournal
vjournal(vtc
);
101 vjournal
.ToMemo(rec
);
102 m_Data
= vjournal
.ExtractVJournal();
105 catch( Barry::ConvertError
&ce
) {
106 trace
.logf(_("ERROR: vjournal:Barry::ConvertError exception: %s"), ce
.what());
107 m_last_errmsg
= ce
.what();
111 // Barry builder operator
112 bool VJournalConverter::operator()(Barry::Memo
&rec
, Barry::Builder
&)
114 Trace
trace("VMemoConverter::builder operator()");
120 // Handles calling of the Barry::Controller to fetch a specific
121 // record, indicated by index (into the RecordStateTable).
122 // Returns a g_malloc'd string of data containing the vevent20
123 // data. It is the responsibility of the caller to free it.
124 // This is intended to be passed into the GetChanges() function.
125 char* VJournalConverter::GetRecordData(BarryEnvironment
*env
, unsigned int dbId
,
126 Barry::RecordStateTable::IndexType index
)
128 Trace
trace("VMemoConverter::GetRecordData()");
130 using namespace Barry
;
132 VJournalConverter memo2journal
;
133 RecordParser
<Memo
, VJournalConverter
> parser(memo2journal
);
134 env
->GetDesktop()->GetRecord(dbId
, index
, parser
);
135 return memo2journal
.ExtractData();
138 bool VJournalConverter::CommitRecordData(BarryEnvironment
*env
, unsigned int dbId
,
139 Barry::RecordStateTable::IndexType StateIndex
, uint32_t recordId
,
140 const char *data
, bool add
, std::string
&errmsg
)
142 Trace
trace("VJournalConverter::CommitRecordData()");
144 uint32_t newRecordId
;
146 // use given id if possible
147 if( recordId
&& !env
->m_JournalSync
.m_Table
.GetIndex(recordId
) ) {
148 // recordId is unique and non-zero
149 newRecordId
= recordId
;
152 trace
.log(_("Can't use recommended recordId, generating new one."));
153 newRecordId
= env
->m_JournalSync
.m_Table
.MakeNewRecordId();
157 newRecordId
= env
->m_JournalSync
.m_Table
.StateMap
[StateIndex
].RecordId
;
159 trace
.logf("newRecordId: %u", newRecordId
);
161 VJournalConverter
convert(newRecordId
);
162 if( !convert
.ParseData(data
) ) {
163 std::ostringstream oss
;
164 oss
<< _("unable to parse change data for new RecordId: ")
166 << " (" << convert
.GetLastError() << ") "
167 << _("data: ") << data
;
169 trace
.log(errmsg
.c_str());
173 Barry::RecordBuilder
<Barry::Memo
, VJournalConverter
> builder(convert
);
176 trace
.log(_("adding record"));
177 env
->GetDesktop()->AddRecord(dbId
, builder
);
180 trace
.log(_("setting record"));
181 env
->GetDesktop()->SetRecord(dbId
, StateIndex
, builder
);
182 trace
.log(_("clearing dirty flag"));
183 env
->GetDesktop()->ClearDirty(dbId
, StateIndex
);