vcard.cc 5.08 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 24

struct _LinphoneVCard {
25
	shared_ptr<belcard::BelCard> belCard;
26 27 28
};

extern "C" LinphoneVCard* linphone_vcard_new(void) {
29 30 31
	LinphoneVCard* vCard = (LinphoneVCard*) ms_new0(LinphoneVCard, 1);
	vCard->belCard = belcard::BelCardGeneric::create<belcard::BelCard>();
	return vCard;
32 33
}

34
extern "C" void linphone_vcard_free(LinphoneVCard *vCard) {
35 36
	if (!vCard) return;
	
37 38
	vCard->belCard.reset();
	ms_free(vCard);
39 40
}

41
extern "C" MSList* linphone_vcard_list_from_vcard4_file(const char *filename) {
42
	MSList *result = NULL;
43 44 45 46
	if (filename && ortp_file_exist(filename) == 0) {
		belcard::BelCardParser *parser = new belcard::BelCardParser();
		shared_ptr<belcard::BelCardList> belCards = parser->parseFile(filename);
		if (belCards) {
47 48
			for (auto it = belCards->getCards().begin(); it != belCards->getCards().end(); ++it) {
				shared_ptr<belcard::BelCard> belcard = (*it);
49 50 51
				LinphoneVCard *vCard = linphone_vcard_new();
				vCard->belCard = belcard;
				result = ms_list_append(result, vCard);
52 53
			}
		}
54
		delete parser;
55 56 57 58
	}
	return result;
}

59 60 61 62 63 64 65 66 67 68 69 70 71
extern "C" MSList* linphone_vcard_list_from_vcard4_buffer(const char *buffer) {
	MSList *result = NULL;
	if (buffer) {
		belcard::BelCardParser *parser = new belcard::BelCardParser();
		shared_ptr<belcard::BelCardList> belCards = parser->parse(buffer);
		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);
			}
		}
72
		delete parser;
73 74 75 76 77 78 79 80 81 82 83 84 85
	}
	return result;
}

extern "C" LinphoneVCard* linphone_vcard_new_from_vcard4_buffer(const char *buffer) {
	LinphoneVCard *vCard = NULL;
	if (buffer) {
		belcard::BelCardParser *parser = new belcard::BelCardParser();
		shared_ptr<belcard::BelCard> belCard = parser->parseOne(buffer);
		if (belCard) {
			vCard = linphone_vcard_new();
			vCard->belCard = belCard;
		}
86
		delete parser;
87 88 89 90 91
	}
	return vCard;
}

extern "C" const char * linphone_vcard_as_vcard4_string(LinphoneVCard *vCard) {
92 93
	if (!vCard) return NULL;
	
94
	return vCard->belCard->toFoldedString().c_str();
95 96
}

97
extern "C" void linphone_vcard_set_full_name(LinphoneVCard *vCard, const char *name) {
98 99
	if (!vCard || !name) return;
	
100 101
	shared_ptr<belcard::BelCardFullName> fn = belcard::BelCardGeneric::create<belcard::BelCardFullName>();
	fn->setValue(name);
102
	vCard->belCard->setFullName(fn);
103 104
}

105
extern "C" const char* linphone_vcard_get_full_name(const LinphoneVCard *vCard) {
106 107
	if (!vCard) return NULL;
	
108
	const char *result = vCard->belCard->getFullName() ? vCard->belCard->getFullName()->getValue().c_str() : NULL;
109
	return result;
110 111
}

112
extern "C" void linphone_vcard_add_sip_address(LinphoneVCard *vCard, const char *sip_address) {
113 114
	if (!vCard || !sip_address) return;
	
115 116 117 118 119
	shared_ptr<belcard::BelCardImpp> impp = belcard::BelCardGeneric::create<belcard::BelCardImpp>();
	impp->setValue(sip_address);
	vCard->belCard->addImpp(impp);
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
extern "C" void linphone_vcard_remove_sip_address(LinphoneVCard *vCard, const char *sip_address) {
	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;
		}
	}
}

extern "C" void linphone_vcard_edit_main_sip_address(LinphoneVCard *vCard, const char *sip_address) {
	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);
	}
}

145
extern "C" MSList* linphone_vcard_get_sip_addresses(const LinphoneVCard *vCard) {
146
	MSList *result = NULL;
147 148
	if (!vCard) return NULL;
	
149
	for (auto it = vCard->belCard->getImpp().begin(); it != vCard->belCard->getImpp().end(); ++it) {
150 151 152 153 154 155
		const char *value = (*it)->getValue().c_str();
		if (strncmp(value, "sip:", 4) == 0) {
			result = ms_list_append(result, (char *)value);
		}
	}
	return result;
156
}