Bumped copyright dates for 2013
[barry.git] / opensync-plugin-0.4x / src / vjournal.cc
bloba9d7102644af8b3b692188d5b3bbe91f7ef12e32
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-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>
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>
33 #include "i18n.h"
35 using namespace Barry::Sync;
37 //////////////////////////////////////////////////////////////////////////////
40 VJournalConverter::VJournalConverter()
41 : m_Data(0)
45 VJournalConverter::VJournalConverter(uint32_t newRecordId)
46 : m_Data(0),
47 m_RecordId(newRecordId)
51 VJournalConverter::~VJournalConverter()
53 if( m_Data )
54 g_free(m_Data);
57 // Transfers ownership of m_Data to the caller
58 char* VJournalConverter::ExtractData()
60 Trace trace("VMemoConverter::ExtractData");
61 char *ret = m_Data;
62 m_Data = 0;
63 return ret;
66 bool VJournalConverter::ParseData(const char *data)
68 Trace trace("VJournalConverter::ParseData");
70 try {
72 vTimeConverter vtc;
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();
80 return false;
83 return true;
86 // Barry storage operator
87 void VJournalConverter::operator()(const Barry::Memo &rec)
89 Trace trace("VJournalConverter::operator()");
91 // Delete data if some already exists
92 if( m_Data ) {
93 g_free(m_Data);
94 m_Data = 0;
97 try {
99 vTimeConverter vtc;
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()");
116 rec = m_Memo;
117 return true;
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;
145 if( add ) {
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;
151 else {
152 trace.log(_("Can't use recommended recordId, generating new one."));
153 newRecordId = env->m_JournalSync.m_Table.MakeNewRecordId();
156 else {
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: ")
165 << newRecordId
166 << " (" << convert.GetLastError() << ") "
167 << _("data: ") << data;
168 errmsg = oss.str();
169 trace.log(errmsg.c_str());
170 return false;
173 Barry::RecordBuilder<Barry::Memo, VJournalConverter> builder(convert);
175 if( add ) {
176 trace.log(_("adding record"));
177 env->GetDesktop()->AddRecord(dbId, builder);
179 else {
180 trace.log(_("setting record"));
181 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
182 trace.log(_("clearing dirty flag"));
183 env->GetDesktop()->ClearDirty(dbId, StateIndex);
186 return true;