tzwrapper.cc: fixed use of iterator after erase
[barry.git] / opensync-plugin / src / vcard.cc
blob8cc95df9db2121f9ef7a62509c419764cafe8fe8
1 ///
2 /// \file vcard.cc
3 /// Conversion routines for vcards
4 ///
6 /*
7 Copyright (C) 2006-2013, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "environment.h"
23 #include "trace.h"
24 #include "vcard.h"
25 #include <barry/vcard.h>
26 #include <stdint.h>
27 #include <glib.h>
28 #include <string.h>
29 #include <sstream>
30 #include <ctype.h>
31 #include "i18n.h"
33 using namespace Barry::Sync;
35 //////////////////////////////////////////////////////////////////////////////
38 VCardConverter::VCardConverter()
39 : m_Data(0)
43 VCardConverter::VCardConverter(uint32_t newRecordId)
44 : m_Data(0),
45 m_RecordId(newRecordId)
49 VCardConverter::~VCardConverter()
51 if( m_Data )
52 g_free(m_Data);
55 // Transfers ownership of m_Data to the caller
56 char* VCardConverter::ExtractData()
58 Trace trace("VCardConverter::ExtractData");
59 char *ret = m_Data;
60 m_Data = 0;
61 return ret;
64 bool VCardConverter::ParseData(const char *data)
66 Trace trace("VCardConverter::ParseData");
68 try {
70 vCard vcard;
71 m_Contact = vcard.ToBarry(data, m_RecordId);
74 catch( Barry::ConvertError &ce ) {
75 trace.logf(_("ERROR: vcard:Barry::ConvertError exception: %s"), ce.what());
76 m_last_errmsg = ce.what();
77 return false;
80 return true;
83 // Barry storage operator
84 void VCardConverter::operator()(const Barry::Contact &rec)
86 Trace trace("VCardConverter::operator()");
88 // Delete data if some already exists
89 if( m_Data ) {
90 g_free(m_Data);
91 m_Data = 0;
94 try {
96 vCard vcard;
97 vcard.ToVCard(rec);
98 m_Data = vcard.ExtractVCard();
101 catch( Barry::ConvertError &ce ) {
102 trace.logf(_("ERROR: vcard:Barry::ConvertError exception: %s"), ce.what());
103 m_last_errmsg = ce.what();
107 // Barry builder operator
108 bool VCardConverter::operator()(Barry::Contact &rec, Barry::Builder &)
110 Trace trace("VCardConverter::builder operator()");
112 rec = m_Contact;
113 return true;
116 // Handles calling of the Barry::Controller to fetch a specific
117 // record, indicated by index (into the RecordStateTable).
118 // Returns a g_malloc'd string of data containing the vcard30
119 // data. It is the responsibility of the caller to free it.
120 // This is intended to be passed into the GetChanges() function.
121 char* VCardConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
122 Barry::RecordStateTable::IndexType index)
124 Trace trace("VCardConverter::GetRecordData()");
126 using namespace Barry;
128 VCardConverter contact2vcard;
129 RecordParser<Contact, VCardConverter> parser(contact2vcard);
130 env->GetDesktop()->GetRecord(dbId, index, parser);
131 return contact2vcard.ExtractData();
134 bool VCardConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
135 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
136 const char *data, bool add, std::string &errmsg)
138 Trace trace("VCardConverter::CommitRecordData()");
140 uint32_t newRecordId;
141 if( add ) {
142 // use given id if possible
143 if( recordId && !env->m_ContactsSync.m_Table.GetIndex(recordId) ) {
144 // recordId is unique and non-zero
145 newRecordId = recordId;
147 else {
148 trace.log(_("Can't use recommended recordId, generating new one."));
149 newRecordId = env->m_ContactsSync.m_Table.MakeNewRecordId();
152 else {
153 newRecordId = env->m_ContactsSync.m_Table.StateMap[StateIndex].RecordId;
155 trace.logf("newRecordId: %lu", newRecordId);
157 VCardConverter convert(newRecordId);
158 if( !convert.ParseData(data) ) {
159 std::ostringstream oss;
160 oss << _("unable to parse change data for new RecordId: ")
161 << newRecordId
162 << " (" << convert.GetLastError() << ") "
163 << _("data: ") << data;
164 errmsg = oss.str();
165 trace.log(errmsg.c_str());
166 return false;
169 Barry::RecordBuilder<Barry::Contact, VCardConverter> builder(convert);
171 if( add ) {
172 trace.log(_("adding record"));
173 env->GetDesktop()->AddRecord(dbId, builder);
175 else {
176 trace.log(_("setting record"));
177 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
178 trace.log(_("clearing dirty flag"));
179 env->GetDesktop()->ClearDirty(dbId, StateIndex);
182 return true;