3 /// Conversion routines for vcards
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"
25 #include <barry/vcard.h>
33 using namespace Barry::Sync
;
35 //////////////////////////////////////////////////////////////////////////////
38 VCardConverter::VCardConverter()
43 VCardConverter::VCardConverter(uint32_t newRecordId
)
45 m_RecordId(newRecordId
)
49 VCardConverter::~VCardConverter()
55 // Transfers ownership of m_Data to the caller
56 char* VCardConverter::ExtractData()
58 Trace
trace("VCardConverter::ExtractData");
64 bool VCardConverter::ParseData(const char *data
)
66 Trace
trace("VCardConverter::ParseData");
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();
83 // Barry storage operator
84 void VCardConverter::operator()(const Barry::Contact
&rec
)
86 Trace
trace("VCardConverter::operator()");
88 // Delete data if some already exists
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()");
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
;
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
;
148 trace
.log(_("Can't use recommended recordId, generating new one."));
149 newRecordId
= env
->m_ContactsSync
.m_Table
.MakeNewRecordId();
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: ")
162 << " (" << convert
.GetLastError() << ") "
163 << _("data: ") << data
;
165 trace
.log(errmsg
.c_str());
169 Barry::RecordBuilder
<Barry::Contact
, VCardConverter
> builder(convert
);
172 trace
.log(_("adding record"));
173 env
->GetDesktop()->AddRecord(dbId
, builder
);
176 trace
.log(_("setting record"));
177 env
->GetDesktop()->SetRecord(dbId
, StateIndex
, builder
);
178 trace
.log(_("clearing dirty flag"));
179 env
->GetDesktop()->ClearDirty(dbId
, StateIndex
);