3 /// Conversion routines for vcards
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.
23 #include "environment.h"
31 using namespace Barry::Sync
;
33 //////////////////////////////////////////////////////////////////////////////
36 VCardConverter::VCardConverter()
41 VCardConverter::VCardConverter(uint32_t newRecordId
)
43 m_RecordId(newRecordId
)
47 VCardConverter::~VCardConverter()
53 // Transfers ownership of m_Data to the caller
54 char* VCardConverter::ExtractData()
56 Trace
trace("VCardConverter::ExtractData");
62 bool VCardConverter::ParseData(const char *data
)
64 Trace
trace("VCardConverter::ParseData");
69 m_Contact
= vcard
.ToBarry(data
, m_RecordId
);
72 catch( Barry::ConvertError
&ce
) {
73 trace
.logf("ERROR: vcard:Barry::ConvertError exception: %s", ce
.what());
80 // Barry storage operator
81 void VCardConverter::operator()(const Barry::Contact
&rec
)
83 Trace
trace("VCardConverter::operator()");
85 // Delete data if some already exists
95 m_Data
= vcard
.ExtractVCard();
98 catch( Barry::ConvertError
&ce
) {
99 trace
.logf("ERROR: vcard:Barry::ConvertError exception: %s", ce
.what());
103 // Barry builder operator
104 bool VCardConverter::operator()(Barry::Contact
&rec
, Barry::Builder
&)
106 Trace
trace("VCardConverter::builder operator()");
112 // Handles calling of the Barry::Controller to fetch a specific
113 // record, indicated by index (into the RecordStateTable).
114 // Returns a g_malloc'd string of data containing the vcard30
115 // data. It is the responsibility of the caller to free it.
116 // This is intended to be passed into the GetChanges() function.
117 char* VCardConverter::GetRecordData(BarryEnvironment
*env
, unsigned int dbId
,
118 Barry::RecordStateTable::IndexType index
)
120 Trace
trace("VCardConverter::GetRecordData()");
122 using namespace Barry
;
124 VCardConverter contact2vcard
;
125 RecordParser
<Contact
, VCardConverter
> parser(contact2vcard
);
126 env
->m_pDesktop
->GetRecord(dbId
, index
, parser
);
127 return contact2vcard
.ExtractData();
130 bool VCardConverter::CommitRecordData(BarryEnvironment
*env
, unsigned int dbId
,
131 Barry::RecordStateTable::IndexType StateIndex
, uint32_t recordId
,
132 const char *data
, bool add
, std::string
&errmsg
)
134 Trace
trace("VCardConverter::CommitRecordData()");
136 uint32_t newRecordId
;
138 // use given id if possible
139 if( recordId
&& !env
->m_ContactsSync
.m_Table
.GetIndex(recordId
) ) {
140 // recordId is unique and non-zero
141 newRecordId
= recordId
;
144 trace
.log("Can't use recommended recordId, generating new one.");
145 newRecordId
= env
->m_ContactsSync
.m_Table
.MakeNewRecordId();
149 newRecordId
= env
->m_ContactsSync
.m_Table
.StateMap
[StateIndex
].RecordId
;
151 trace
.logf("newRecordId: %u", newRecordId
);
153 VCardConverter
convert(newRecordId
);
154 if( !convert
.ParseData(data
) ) {
155 std::ostringstream oss
;
156 oss
<< "unable to parse change data for new RecordId: "
157 << newRecordId
<< " data: " << data
;
159 trace
.log(errmsg
.c_str());
163 Barry::RecordBuilder
<Barry::Contact
, VCardConverter
> builder(convert
);
166 trace
.log("adding record");
167 env
->m_pDesktop
->AddRecord(dbId
, builder
);
170 trace
.log("setting record");
171 env
->m_pDesktop
->SetRecord(dbId
, StateIndex
, builder
);
172 trace
.log("clearing dirty flag");
173 env
->m_pDesktop
->ClearDirty(dbId
, StateIndex
);