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.
23 #include "environment.h"
32 using namespace Barry::Sync
;
34 //////////////////////////////////////////////////////////////////////////////
37 VCardConverter::VCardConverter()
42 VCardConverter::VCardConverter(uint32_t newRecordId
)
44 m_RecordId(newRecordId
)
48 VCardConverter::~VCardConverter()
54 // Transfers ownership of m_Data to the caller
55 char* VCardConverter::ExtractData()
57 Trace
trace("VCardConverter::ExtractData");
63 bool VCardConverter::ParseData(const char *data
)
65 Trace
trace("VCardConverter::ParseData");
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 m_last_errmsg
= ce
.what();
82 // Barry storage operator
83 void VCardConverter::operator()(const Barry::Contact
&rec
)
85 Trace
trace("VCardConverter::operator()");
87 // Delete data if some already exists
97 m_Data
= vcard
.ExtractVCard();
100 catch( Barry::ConvertError
&ce
) {
101 trace
.logf(_("ERROR: vcard:Barry::ConvertError exception: %s"), ce
.what());
102 m_last_errmsg
= ce
.what();
106 // Barry builder operator
107 bool VCardConverter::operator()(Barry::Contact
&rec
, Barry::Builder
&)
109 Trace
trace("VCardConverter::builder operator()");
115 // Handles calling of the Barry::Controller to fetch a specific
116 // record, indicated by index (into the RecordStateTable).
117 // Returns a g_malloc'd string of data containing the vcard30
118 // data. It is the responsibility of the caller to free it.
119 // This is intended to be passed into the GetChanges() function.
120 char* VCardConverter::GetRecordData(BarryEnvironment
*env
, unsigned int dbId
,
121 Barry::RecordStateTable::IndexType index
)
123 Trace
trace("VCardConverter::GetRecordData()");
125 using namespace Barry
;
127 VCardConverter contact2vcard
;
128 RecordParser
<Contact
, VCardConverter
> parser(contact2vcard
);
129 env
->GetDesktop()->GetRecord(dbId
, index
, parser
);
130 return contact2vcard
.ExtractData();
133 bool VCardConverter::CommitRecordData(BarryEnvironment
*env
, unsigned int dbId
,
134 Barry::RecordStateTable::IndexType StateIndex
, uint32_t recordId
,
135 const char *data
, bool add
, std::string
&errmsg
)
137 Trace
trace("VCardConverter::CommitRecordData()");
139 uint32_t newRecordId
;
141 // use given id if possible
142 if( recordId
&& !env
->m_ContactsSync
.m_Table
.GetIndex(recordId
) ) {
143 // recordId is unique and non-zero
144 newRecordId
= recordId
;
147 trace
.log(_("Can't use recommended recordId, generating new one."));
148 newRecordId
= env
->m_ContactsSync
.m_Table
.MakeNewRecordId();
152 newRecordId
= env
->m_ContactsSync
.m_Table
.StateMap
[StateIndex
].RecordId
;
154 trace
.logf("newRecordId: %u", newRecordId
);
156 VCardConverter
convert(newRecordId
);
157 if( !convert
.ParseData(data
) ) {
158 std::ostringstream oss
;
159 oss
<< _("unable to parse change data for new RecordId: ")
161 << " (" << convert
.GetLastError() << ") "
162 << _("data: ") << data
;
164 trace
.log(errmsg
.c_str());
168 Barry::RecordBuilder
<Barry::Contact
, VCardConverter
> builder(convert
);
171 trace
.log(_("adding record"));
172 env
->GetDesktop()->AddRecord(dbId
, builder
);
175 trace
.log(_("setting record"));
176 env
->GetDesktop()->SetRecord(dbId
, StateIndex
, builder
);
177 trace
.log(_("clearing dirty flag"));
178 env
->GetDesktop()->ClearDirty(dbId
, StateIndex
);