desktop: fixed ExecHelper usage, to use child exit code
[barry.git] / opensync-plugin-0.4x / src / vcard.cc
blobfbe81102f7a7bc3f0f6c7c13bed2dec49c0d085e
1 ///
2 /// \file vcard.cc
3 /// Conversion routines for vcards
4 ///
6 /*
7 Copyright (C) 2006-2012, 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 "vcard.h"
23 #include "environment.h"
24 #include "trace.h"
25 #include <stdint.h>
26 #include <glib.h>
27 #include <string.h>
28 #include <sstream>
29 #include <ctype.h>
31 using namespace Barry::Sync;
33 //////////////////////////////////////////////////////////////////////////////
36 VCardConverter::VCardConverter()
37 : m_Data(0)
41 VCardConverter::VCardConverter(uint32_t newRecordId)
42 : m_Data(0),
43 m_RecordId(newRecordId)
47 VCardConverter::~VCardConverter()
49 if( m_Data )
50 g_free(m_Data);
53 // Transfers ownership of m_Data to the caller
54 char* VCardConverter::ExtractData()
56 Trace trace("VCardConverter::ExtractData");
57 char *ret = m_Data;
58 m_Data = 0;
59 return ret;
62 bool VCardConverter::ParseData(const char *data)
64 Trace trace("VCardConverter::ParseData");
66 try {
68 vCard vcard;
69 m_Contact = vcard.ToBarry(data, m_RecordId);
72 catch( Barry::ConvertError &ce ) {
73 trace.logf("ERROR: vcard:Barry::ConvertError exception: %s", ce.what());
74 m_last_errmsg = ce.what();
75 return false;
78 return true;
81 // Barry storage operator
82 void VCardConverter::operator()(const Barry::Contact &rec)
84 Trace trace("VCardConverter::operator()");
86 // Delete data if some already exists
87 if( m_Data ) {
88 g_free(m_Data);
89 m_Data = 0;
92 try {
94 vCard vcard;
95 vcard.ToVCard(rec);
96 m_Data = vcard.ExtractVCard();
99 catch( Barry::ConvertError &ce ) {
100 trace.logf("ERROR: vcard:Barry::ConvertError exception: %s", ce.what());
101 m_last_errmsg = ce.what();
105 // Barry builder operator
106 bool VCardConverter::operator()(Barry::Contact &rec, Barry::Builder &)
108 Trace trace("VCardConverter::builder operator()");
110 rec = m_Contact;
111 return true;
114 // Handles calling of the Barry::Controller to fetch a specific
115 // record, indicated by index (into the RecordStateTable).
116 // Returns a g_malloc'd string of data containing the vcard30
117 // data. It is the responsibility of the caller to free it.
118 // This is intended to be passed into the GetChanges() function.
119 char* VCardConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
120 Barry::RecordStateTable::IndexType index)
122 Trace trace("VCardConverter::GetRecordData()");
124 using namespace Barry;
126 VCardConverter contact2vcard;
127 RecordParser<Contact, VCardConverter> parser(contact2vcard);
128 env->GetDesktop()->GetRecord(dbId, index, parser);
129 return contact2vcard.ExtractData();
132 bool VCardConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
133 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
134 const char *data, bool add, std::string &errmsg)
136 Trace trace("VCardConverter::CommitRecordData()");
138 uint32_t newRecordId;
139 if( add ) {
140 // use given id if possible
141 if( recordId && !env->m_ContactsSync.m_Table.GetIndex(recordId) ) {
142 // recordId is unique and non-zero
143 newRecordId = recordId;
145 else {
146 trace.log("Can't use recommended recordId, generating new one.");
147 newRecordId = env->m_ContactsSync.m_Table.MakeNewRecordId();
150 else {
151 newRecordId = env->m_ContactsSync.m_Table.StateMap[StateIndex].RecordId;
153 trace.logf("newRecordId: %u", newRecordId);
155 VCardConverter convert(newRecordId);
156 if( !convert.ParseData(data) ) {
157 std::ostringstream oss;
158 oss << "unable to parse change data for new RecordId: "
159 << newRecordId
160 << " (" << convert.GetLastError() << ") data: " << data;
161 errmsg = oss.str();
162 trace.log(errmsg.c_str());
163 return false;
166 Barry::RecordBuilder<Barry::Contact, VCardConverter> builder(convert);
168 if( add ) {
169 trace.log("adding record");
170 env->GetDesktop()->AddRecord(dbId, builder);
172 else {
173 trace.log("setting record");
174 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
175 trace.log("clearing dirty flag");
176 env->GetDesktop()->ClearDirty(dbId, StateIndex);
179 return true;