vcard.cc 6.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
vcard.cc
Copyright (C) 2015  Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

20 21
#include "vcard.h"
#include "belcard/belcard.hpp"
22
#include "belcard/belcard_parser.hpp"
23
#include "sal/sal.h"
24 25

struct _LinphoneVCard {
26
	shared_ptr<belcard::BelCard> belCard;
27 28
	const char *etag;
	const char *url;
29 30
};

31 32 33 34 35
#ifdef __cplusplus
extern "C" {
#endif

LinphoneVCard* linphone_vcard_new(void) {
36 37 38
	LinphoneVCard* vCard = (LinphoneVCard*) ms_new0(LinphoneVCard, 1);
	vCard->belCard = belcard::BelCardGeneric::create<belcard::BelCard>();
	return vCard;
39 40
}

41
void linphone_vcard_free(LinphoneVCard *vCard) {
42 43
	if (!vCard) return;
	
44 45
	vCard->belCard.reset();
	ms_free(vCard);
46 47
}

48
MSList* linphone_vcard_list_from_vcard4_file(const char *filename) {
49
	MSList *result = NULL;
50
	if (filename && ortp_file_exist(filename) == 0) {
51 52
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCardList> belCards = parser.parseFile(filename);
53
		if (belCards) {
54 55
			for (auto it = belCards->getCards().begin(); it != belCards->getCards().end(); ++it) {
				shared_ptr<belcard::BelCard> belcard = (*it);
56 57 58
				LinphoneVCard *vCard = linphone_vcard_new();
				vCard->belCard = belcard;
				result = ms_list_append(result, vCard);
59 60 61 62 63 64
			}
		}
	}
	return result;
}

65
MSList* linphone_vcard_list_from_vcard4_buffer(const char *buffer) {
66 67
	MSList *result = NULL;
	if (buffer) {
68 69
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCardList> belCards = parser.parse(buffer);
70 71 72 73 74 75 76 77 78 79 80 81
		if (belCards) {
			for (auto it = belCards->getCards().begin(); it != belCards->getCards().end(); ++it) {
				shared_ptr<belcard::BelCard> belCard = (*it);
				LinphoneVCard *vCard = linphone_vcard_new();
				vCard->belCard = belCard;
				result = ms_list_append(result, vCard);
			}
		}
	}
	return result;
}

82
LinphoneVCard* linphone_vcard_new_from_vcard4_buffer(const char *buffer) {
83 84
	LinphoneVCard *vCard = NULL;
	if (buffer) {
85 86
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCard> belCard = parser.parseOne(buffer);
87 88 89
		if (belCard) {
			vCard = linphone_vcard_new();
			vCard->belCard = belCard;
Sylvain Berfini's avatar
Sylvain Berfini committed
90 91
		} else {
			ms_error("Couldn't parse buffer %s", buffer);
92 93 94 95 96
		}
	}
	return vCard;
}

97
const char * linphone_vcard_as_vcard4_string(LinphoneVCard *vCard) {
98 99
	if (!vCard) return NULL;
	
100
	return vCard->belCard->toFoldedString().c_str();
101 102
}

103
void linphone_vcard_set_full_name(LinphoneVCard *vCard, const char *name) {
104 105
	if (!vCard || !name) return;
	
106 107
	shared_ptr<belcard::BelCardFullName> fn = belcard::BelCardGeneric::create<belcard::BelCardFullName>();
	fn->setValue(name);
108
	vCard->belCard->setFullName(fn);
109 110
}

111
const char* linphone_vcard_get_full_name(const LinphoneVCard *vCard) {
112 113
	if (!vCard) return NULL;
	
114
	const char *result = vCard->belCard->getFullName() ? vCard->belCard->getFullName()->getValue().c_str() : NULL;
115
	return result;
116 117
}

118
void linphone_vcard_add_sip_address(LinphoneVCard *vCard, const char *sip_address) {
119 120
	if (!vCard || !sip_address) return;
	
121 122 123 124 125
	shared_ptr<belcard::BelCardImpp> impp = belcard::BelCardGeneric::create<belcard::BelCardImpp>();
	impp->setValue(sip_address);
	vCard->belCard->addImpp(impp);
}

126
void linphone_vcard_remove_sip_address(LinphoneVCard *vCard, const char *sip_address) {
127 128 129 130 131 132 133 134 135 136 137
	if (!vCard) return;
	
	for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
		const char *value = (*it)->getValue().c_str();
		if (strcmp(value, sip_address) == 0) {
			vCard->belCard->removeImpp(*it);
			break;
		}
	}
}

138
void linphone_vcard_edit_main_sip_address(LinphoneVCard *vCard, const char *sip_address) {
139 140 141 142 143 144 145 146 147 148 149 150
	if (!vCard || !sip_address) return;
	
	if (vCard->belCard->getImpp().size() > 0) {
		const shared_ptr<belcard::BelCardImpp> impp = vCard->belCard->getImpp().front();
		impp->setValue(sip_address);
	} else {
		shared_ptr<belcard::BelCardImpp> impp = belcard::BelCardGeneric::create<belcard::BelCardImpp>();
		impp->setValue(sip_address);
		vCard->belCard->addImpp(impp);
	}
}

151
MSList* linphone_vcard_get_sip_addresses(const LinphoneVCard *vCard) {
152
	MSList *result = NULL;
153 154
	if (!vCard) return NULL;
	
155
	for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
156 157 158 159 160 161
		const char *value = (*it)->getValue().c_str();
		if (strncmp(value, "sip:", 4) == 0) {
			result = ms_list_append(result, (char *)value);
		}
	}
	return result;
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
bool_t linphone_vcard_generate_unique_id(LinphoneVCard *vCard) {
	char uuid[64];
	
	if (vCard) {
		if (linphone_vcard_get_uid(vCard)) {
			return FALSE;
		}
		if (sal_generate_uuid(uuid, sizeof(uuid)) == 0) {
			char vcard_uuid[sizeof(uuid)+4];
			snprintf(vcard_uuid, sizeof(vcard_uuid), "urn:%s", uuid);
			linphone_vcard_set_uid(vCard, ms_strdup(vcard_uuid));
			return TRUE;
		}
	}
	return FALSE;
}

void linphone_vcard_set_uid(LinphoneVCard *vCard, const char *uid) {
	if (!vCard || !uid) return;
	
	shared_ptr<belcard::BelCardUniqueId> uniqueId = belcard::BelCardGeneric::create<belcard::BelCardUniqueId>();
	uniqueId->setValue(uid);
	vCard->belCard->setUniqueId(uniqueId);
}

const char* linphone_vcard_get_uid(const LinphoneVCard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
190
	if (vCard && vCard->belCard->getUniqueId()) {
191 192 193 194 195
		return vCard->belCard->getUniqueId()->getValue().c_str();
	}
	return NULL;
}

196
void linphone_vcard_set_etag(LinphoneVCard *vCard, const char * etag) {
197 198 199
	vCard->etag = ms_strdup(etag);
}

200
const char* linphone_vcard_get_etag(const LinphoneVCard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
201
	if (!vCard) return NULL;
202 203 204
	return vCard->etag;
}

205
void linphone_vcard_set_url(LinphoneVCard *vCard, const char * url) {
206 207 208
	vCard->url = ms_strdup(url);
}

209
const char* linphone_vcard_get_url(const LinphoneVCard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
210
	if (!vCard) return NULL;
211
	return vCard->url;
212 213 214 215 216
}

#ifdef __cplusplus
}
#endif