vcard.cc 6.9 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 29
	char *etag;
	char *url;
	char *md5;
30 31
};

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

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

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

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

66
MSList* linphone_vcard_list_from_vcard4_buffer(const char *buffer) {
67 68
	MSList *result = NULL;
	if (buffer) {
69 70
		belcard::BelCardParser parser = belcard::BelCardParser::getInstance();
		shared_ptr<belcard::BelCardList> belCards = parser.parse(buffer);
71 72 73 74 75 76 77 78 79 80 81 82
		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;
}

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

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

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

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

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

127
void linphone_vcard_remove_sip_address(LinphoneVCard *vCard, const char *sip_address) {
128 129 130 131 132 133 134 135 136 137 138
	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;
		}
	}
}

139
void linphone_vcard_edit_main_sip_address(LinphoneVCard *vCard, const char *sip_address) {
140 141 142 143 144 145 146 147 148 149 150 151
	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);
	}
}

152
MSList* linphone_vcard_get_sip_addresses(const LinphoneVCard *vCard) {
153
	MSList *result = NULL;
154 155
	if (!vCard) return NULL;
	
156
	for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
157 158 159 160 161 162
		const char *value = (*it)->getValue().c_str();
		if (strncmp(value, "sip:", 4) == 0) {
			result = ms_list_append(result, (char *)value);
		}
	}
	return result;
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 190
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
191
	if (vCard && vCard->belCard->getUniqueId()) {
192 193 194 195 196
		return vCard->belCard->getUniqueId()->getValue().c_str();
	}
	return NULL;
}

197
void linphone_vcard_set_etag(LinphoneVCard *vCard, const char * etag) {
198 199 200 201 202 203
	if (!vCard) {
		return;
	}
	if (vCard->etag) {
		ms_free(vCard->etag);
	}
204 205 206
	vCard->etag = ms_strdup(etag);
}

207
const char* linphone_vcard_get_etag(const LinphoneVCard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
208
	if (!vCard) return NULL;
209 210 211
	return vCard->etag;
}

212
void linphone_vcard_set_url(LinphoneVCard *vCard, const char * url) {
213 214 215 216 217 218
	if (!vCard) {
		return;
	}
	if (vCard->url) {
		ms_free(vCard->url);
	}
219 220 221
	vCard->url = ms_strdup(url);
}

222
const char* linphone_vcard_get_url(const LinphoneVCard *vCard) {
Sylvain Berfini's avatar
Sylvain Berfini committed
223
	if (!vCard) return NULL;
224
	return vCard->url;
225 226
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
void linphone_vcard_compute_md5_hash(LinphoneVCard *vCard) {
	if (!vCard) {
		return;
	}
	if (vCard->md5) {
		ms_free(vCard->md5);
	}
	//TODO: compute md5 hash
}

const char *linphone_vcard_get_md5_hash(LinphoneVCard *vCard) {
	if (!vCard) {
		return NULL;
	}
	return vCard->md5;
}

244 245 246
#ifdef __cplusplus
}
#endif