os22: fix exception messages, to indicate where they came from
[barry.git] / opensync-plugin / src / vcard.cc
blobe005da87e084f47aaf33e738fe7da406330e357b
1 ///
2 /// \file vcard.cc
3 /// Conversion routines for vcards
4 ///
6 /*
7 Copyright (C) 2006-2010, 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>
32 using namespace Barry::Sync;
34 //////////////////////////////////////////////////////////////////////////////
37 VCardConverter::VCardConverter()
38 : m_Data(0)
42 VCardConverter::VCardConverter(uint32_t newRecordId)
43 : m_Data(0),
44 m_RecordId(newRecordId)
48 VCardConverter::~VCardConverter()
50 if( m_Data )
51 g_free(m_Data);
54 // Transfers ownership of m_Data to the caller
55 char* VCardConverter::ExtractData()
57 Trace trace("VCardConverter::ExtractData");
58 char *ret = m_Data;
59 m_Data = 0;
60 return ret;
63 bool VCardConverter::ParseData(const char *data)
65 Trace trace("VCardConverter::ParseData");
67 try {
69 vCard vcard;
70 m_Contact = vcard.ToBarry(data, m_RecordId);
73 catch( Barry::ConvertError &ce ) {
74 trace.logf("ERROR: vcard:Barry::ConvertError exception: %s", 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());
104 // Barry builder operator
105 bool VCardConverter::operator()(Barry::Contact &rec, unsigned int dbId)
107 Trace trace("VCardConverter::builder operator()");
109 rec = m_Contact;
110 return true;
113 // Handles calling of the Barry::Controller to fetch a specific
114 // record, indicated by index (into the RecordStateTable).
115 // Returns a g_malloc'd string of data containing the vcard30
116 // data. It is the responsibility of the caller to free it.
117 // This is intended to be passed into the GetChanges() function.
118 char* VCardConverter::GetRecordData(BarryEnvironment *env, unsigned int dbId,
119 Barry::RecordStateTable::IndexType index)
121 Trace trace("VCardConverter::GetRecordData()");
123 using namespace Barry;
125 VCardConverter contact2vcard;
126 RecordParser<Contact, VCardConverter> parser(contact2vcard);
127 env->m_pDesktop->GetRecord(dbId, index, parser);
128 return contact2vcard.ExtractData();
131 bool VCardConverter::CommitRecordData(BarryEnvironment *env, unsigned int dbId,
132 Barry::RecordStateTable::IndexType StateIndex, uint32_t recordId,
133 const char *data, bool add, std::string &errmsg)
135 Trace trace("VCardConverter::CommitRecordData()");
137 uint32_t newRecordId;
138 if( add ) {
139 // use given id if possible
140 if( recordId && !env->m_ContactsSync.m_Table.GetIndex(recordId) ) {
141 // recordId is unique and non-zero
142 newRecordId = recordId;
144 else {
145 trace.log("Can't use recommended recordId, generating new one.");
146 newRecordId = env->m_ContactsSync.m_Table.MakeNewRecordId();
149 else {
150 newRecordId = env->m_ContactsSync.m_Table.StateMap[StateIndex].RecordId;
152 trace.logf("newRecordId: %lu", newRecordId);
154 VCardConverter convert(newRecordId);
155 if( !convert.ParseData(data) ) {
156 std::ostringstream oss;
157 oss << "unable to parse change data for new RecordId: "
158 << newRecordId << " data: " << data;
159 errmsg = oss.str();
160 trace.log(errmsg.c_str());
161 return false;
164 Barry::RecordBuilder<Barry::Contact, VCardConverter> builder(convert);
166 if( add ) {
167 trace.log("adding record");
168 env->m_pDesktop->AddRecord(dbId, builder);
170 else {
171 trace.log("setting record");
172 env->m_pDesktop->SetRecord(dbId, StateIndex, builder);
173 trace.log("clearing dirty flag");
174 env->m_pDesktop->ClearDirty(dbId, StateIndex);
177 return true;