lib: add prefixes to enums to avoid name clashes on Windows (like ERROR)
[barry.git] / opensync-plugin-0.4x / src / vjournal.cc
blobd13d500af18b32b82cf3951a46253d8de08bd495
1 //
2 // \file vjournal.cc
3 // Conversion routines for vjournals (VCALENDAR, etc)
4 //
6 /*
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>
26 #include "vjournal.h"
27 #include "environment.h"
28 #include "trace.h"
29 #include <stdint.h>
30 #include <glib.h>
31 #include <strings.h>
32 #include <sstream>
34 using namespace Barry::Sync;
36 //////////////////////////////////////////////////////////////////////////////
39 VJournalConverter::VJournalConverter()
40 : m_Data(0)
44 VJournalConverter::VJournalConverter(uint32_t newRecordId)
45 : m_Data(0),
46 m_RecordId(newRecordId)
50 VJournalConverter::~VJournalConverter()
52 if( m_Data )
53 g_free(m_Data);
56 // Transfers ownership of m_Data to the caller
57 char* VJournalConverter::ExtractData()
59 Trace trace("VMemoConverter::ExtractData");
60 char *ret = m_Data;
61 m_Data = 0;
62 return ret;
65 bool VJournalConverter::ParseData(const char *data)
67 Trace trace("VJournalConverter::ParseData");
69 try {
71 vJournal vjournal;
72 m_Memo = vjournal.ToBarry(data, m_RecordId);
75 catch( Barry::ConvertError &ce ) {
76 trace.logf("ERROR: vjournal:Barry::ConvertError exception: %s", ce.what());
77 return false;
80 return true;
83 // Barry storage operator
84 void VJournalConverter::operator()(const Barry::Memo &rec)
86 Trace trace("VJournalConverter::operator()");
88 // Delete data if some already exists
89 if( m_Data ) {
90 g_free(m_Data);
91 m_Data = 0;
94 try {
96 vJournal vjournal;
97 vjournal.ToMemo(rec);
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()");
111 rec = m_Memo;
112 return true;
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;
140 if( add ) {
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;
146 else {
147 trace.log("Can't use recommended recordId, generating new one.");
148 newRecordId = env->m_JournalSync.m_Table.MakeNewRecordId();
151 else {
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;
161 errmsg = oss.str();
162 trace.log(errmsg.c_str());
163 return false;
166 Barry::RecordBuilder<Barry::Memo, VJournalConverter> builder(convert);
168 if( add ) {
169 trace.log("adding record");
170 env->m_pDesktop->AddRecord(dbId, builder);
172 else {
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);
179 return true;