3 // Conversion routines for vjournals (VCALENDAR, etc)
7 Copyright (C) 2008-2009, Nicolas VIVIEN
8 Copyright (C) 2006-2010, 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"
34 using namespace Barry::Sync
;
36 //////////////////////////////////////////////////////////////////////////////
39 VJournalConverter::VJournalConverter()
44 VJournalConverter::VJournalConverter(uint32_t newRecordId
)
46 m_RecordId(newRecordId
)
50 VJournalConverter::~VJournalConverter()
56 // Transfers ownership of m_Data to the caller
57 char* VJournalConverter::ExtractData()
59 Trace
trace("VMemoConverter::ExtractData");
65 bool VJournalConverter::ParseData(const char *data
)
67 Trace
trace("VJournalConverter::ParseData");
72 m_Memo
= vjournal
.ToBarry(data
, m_RecordId
);
75 catch( Barry::ConvertError
&ce
) {
76 trace
.logf("ERROR: vjournal:Barry::ConvertError exception: %s", ce
.what());
83 // Barry storage operator
84 void VJournalConverter::operator()(const Barry::Memo
&rec
)
86 Trace
trace("VJournalConverter::operator()");
88 // Delete data if some already exists
98 m_Data
= vjournal
.ExtractVJournal();
101 catch( Barry::ConvertError
&ce
) {
102 trace
.logf("ERROR: vjournal:Barry::ConvertError exception: %s", ce
.what());
106 // Barry builder operator
107 bool VJournalConverter::operator()(Barry::Memo
&rec
, Barry::Builder
&)
109 Trace
trace("VMemoConverter::builder operator()");
115 // Handles calling of the Barry::Controller to fetch a specific
116 // record, indicated by index (into the RecordStateTable).
117 // Returns a g_malloc'd string of data containing the vevent20
118 // data. It is the responsibility of the caller to free it.
119 // This is intended to be passed into the GetChanges() function.
120 char* VJournalConverter::GetRecordData(BarryEnvironment
*env
, unsigned int dbId
,
121 Barry::RecordStateTable::IndexType index
)
123 Trace
trace("VMemoConverter::GetRecordData()");
125 using namespace Barry
;
127 VJournalConverter memo2journal
;
128 RecordParser
<Memo
, VJournalConverter
> parser(memo2journal
);
129 env
->m_pDesktop
->GetRecord(dbId
, index
, parser
);
130 return memo2journal
.ExtractData();
133 bool VJournalConverter::CommitRecordData(BarryEnvironment
*env
, unsigned int dbId
,
134 Barry::RecordStateTable::IndexType StateIndex
, uint32_t recordId
,
135 const char *data
, bool add
, std::string
&errmsg
)
137 Trace
trace("VJournalConverter::CommitRecordData()");
139 uint32_t newRecordId
;
141 // use given id if possible
142 if( recordId
&& !env
->m_JournalSync
.m_Table
.GetIndex(recordId
) ) {
143 // recordId is unique and non-zero
144 newRecordId
= recordId
;
147 trace
.log("Can't use recommended recordId, generating new one.");
148 newRecordId
= env
->m_JournalSync
.m_Table
.MakeNewRecordId();
152 newRecordId
= env
->m_JournalSync
.m_Table
.StateMap
[StateIndex
].RecordId
;
154 trace
.logf("newRecordId: %u", newRecordId
);
156 VJournalConverter
convert(newRecordId
);
157 if( !convert
.ParseData(data
) ) {
158 std::ostringstream oss
;
159 oss
<< "unable to parse change data for new RecordId: "
160 << newRecordId
<< " data: " << data
;
162 trace
.log(errmsg
.c_str());
166 Barry::RecordBuilder
<Barry::Memo
, VJournalConverter
> builder(convert
);
169 trace
.log("adding record");
170 env
->m_pDesktop
->AddRecord(dbId
, builder
);
173 trace
.log("setting record");
174 env
->m_pDesktop
->SetRecord(dbId
, StateIndex
, builder
);
175 trace
.log("clearing dirty flag");
176 env
->m_pDesktop
->ClearDirty(dbId
, StateIndex
);