menu: added new Keywords tag to .desktop files
[barry.git] / opensync-plugin-0.4x / src / vcard.cc
blob441ba2e32a7f82d9093aabfa213efff53e87c3f6
1 ///
2 /// \file vcard.cc
3 /// Conversion routines for vcards
4 ///
6 /*
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 "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>
30 #include "i18n.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 m_last_errmsg = ce.what();
76 return false;
79 return true;
82 // Barry storage operator
83 void VCardConverter::operator()(const Barry::Contact &rec)
85 Trace trace("VCardConverter::operator()");
87 // Delete data if some already exists
88 if( m_Data ) {
89 g_free(m_Data);
90 m_Data = 0;
93 try {
95 vCard vcard;
96 vcard.ToVCard(rec);
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()");
111 rec = m_Contact;
112 return true;
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;
140 if( add ) {
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;
146 else {
147 trace.log(_("Can't use recommended recordId, generating new one."));
148 newRecordId = env->m_ContactsSync.m_Table.MakeNewRecordId();
151 else {
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: ")
160 << newRecordId
161 << " (" << convert.GetLastError() << ") "
162 << _("data: ") << data;
163 errmsg = oss.str();
164 trace.log(errmsg.c_str());
165 return false;
168 Barry::RecordBuilder<Barry::Contact, VCardConverter> builder(convert);
170 if( add ) {
171 trace.log(_("adding record"));
172 env->GetDesktop()->AddRecord(dbId, builder);
174 else {
175 trace.log(_("setting record"));
176 env->GetDesktop()->SetRecord(dbId, StateIndex, builder);
177 trace.log(_("clearing dirty flag"));
178 env->GetDesktop()->ClearDirty(dbId, StateIndex);
181 return true;