presence.c 72.8 KB
Newer Older
aymeric's avatar
aymeric committed
1 2
/*
linphone
3
Copyright (C) 2010-2013  Belledonne Communications SARL
aymeric's avatar
aymeric committed
4 5 6 7 8 9 10 11 12 13 14 15 16

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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
aymeric's avatar
aymeric committed
18 19
*/

20 21 22
#include "linphone/core.h"
#include "linphone/lpconfig.h"
#include "linphone/presence.h"
23

24
#include "c-wrapper/c-wrapper.h"
aymeric's avatar
aymeric committed
25

26 27 28
// TODO: From coreapi. Remove me later.
#include "private.h"

29
using namespace LinphonePrivate;
30

aymeric's avatar
aymeric committed
31 32
extern const char *__policy_enum_to_str(LinphoneSubscribePolicy pol);

33
struct _LinphonePresenceNote {
34
	belle_sip_object_t base;
Ghislain MARY's avatar
Ghislain MARY committed
35
	void *user_data;
36 37 38 39
	char *lang;
	char *content;
};

40
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePresenceNote);
Ghislain MARY's avatar
Ghislain MARY committed
41
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphonePresenceNote);
42

43
struct _LinphonePresenceService {
44
	belle_sip_object_t base;
45
	void *user_data;
46 47
	char *id;
	LinphonePresenceBasicStatus status;
48
	char *contact;
49
	bctbx_list_t *notes;				/**< A list of _LinphonePresenceNote structures. */
50
	time_t timestamp;
51
	bctbx_list_t *service_descriptions;
52 53
};

54
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePresenceService);
Ghislain MARY's avatar
Ghislain MARY committed
55
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphonePresenceService);
56

57
struct _LinphonePresenceActivity {
58
	belle_sip_object_t base;
Ghislain MARY's avatar
Ghislain MARY committed
59
	void *user_data;
60
	LinphonePresenceActivityType type;
61 62 63
	char *description;
};

64
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePresenceActivity);
Ghislain MARY's avatar
Ghislain MARY committed
65
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphonePresenceActivity);
66

67
struct _LinphonePresencePerson {
68
	belle_sip_object_t base;
69
	void *user_data;
70
	char *id;
71 72 73
	bctbx_list_t *activities;		/**< A list of _LinphonePresenceActivity structures. */
	bctbx_list_t *activities_notes;	/**< A list of _LinphonePresenceNote structures. */
	bctbx_list_t *notes;			/**< A list of _LinphonePresenceNote structures. */
74 75 76
	time_t timestamp;
};

77
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePresencePerson);
Ghislain MARY's avatar
Ghislain MARY committed
78
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphonePresencePerson);
79

80 81 82 83 84
/**
 * Represents the presence model as defined in RFC 4479 and RFC 4480.
 * This model is not complete. For example, it does not handle devices.
 */
struct _LinphonePresenceModel {
85
	belle_sip_object_t base;
86
	LinphoneAddress *presentity; /* "The model seeks to describe the presentity, identified by a presentity URI.*/
Ghislain MARY's avatar
Ghislain MARY committed
87
	void *user_data;
88 89 90
	bctbx_list_t *services;	/**< A list of _LinphonePresenceService structures. Also named tuples in the RFC. */
	bctbx_list_t *persons;	/**< A list of _LinphonePresencePerson structures. */
	bctbx_list_t *notes;		/**< A list of _LinphonePresenceNote structures. */
91
	bool_t is_online;
92 93
};

94
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePresenceModel);
Ghislain MARY's avatar
Ghislain MARY committed
95
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphonePresenceModel);
96

97

98 99 100 101 102 103
static const char *person_prefix = "/pidf:presence/dm:person";


/*****************************************************************************
 * PRIVATE FUNCTIONS                                                         *
 ****************************************************************************/
104 105

/* Defined in http://www.w3.org/TR/REC-xml/ */
jehan's avatar
jehan committed
106
static char presence_id_valid_characters[] = "0123456789abcdefghijklmnopqrstuvwxyz-.";
107 108

/* NameStartChar (NameChar)* */
jehan's avatar
jehan committed
109
static char presence_id_valid_start_characters[] = ":_abcdefghijklmnopqrstuvwxyz";
110 111 112 113

static char * generate_presence_id(void) {
	char id[7];
	int i;
114
	id[0] = presence_id_valid_start_characters[ortp_random() % (sizeof(presence_id_valid_start_characters)-1)];
jehan's avatar
jehan committed
115
	for (i = 1; i < 6; i++) {
116
		id[i] = presence_id_valid_characters[ortp_random() % (sizeof(presence_id_valid_characters)-1)];
117 118 119 120 121 122
	}
	id[6] = '\0';

	return ms_strdup(id);
}

123
static void presence_note_uninit(LinphonePresenceNote *note) {
124 125 126 127 128 129
	ms_free(note->content);
	if (note->lang != NULL) {
		ms_free(note->lang);
	}
}

130 131 132 133
static void presence_note_unref(void *note) {
	linphone_presence_note_unref((LinphonePresenceNote *)note);
}

134
static LinphonePresenceService * presence_service_new(const char *id, LinphonePresenceBasicStatus status) {
135
	LinphonePresenceService *service = belle_sip_object_new(LinphonePresenceService);
136 137 138 139
	if (id != NULL) {
		service->id = ms_strdup(id);
	}
	service->status = status;
140
	service->timestamp = time(NULL);
141 142 143
	return service;
}

144
static void presence_service_uninit(LinphonePresenceService *service) {
145 146 147
	if (service->id != NULL) {
		ms_free(service->id);
	}
148 149 150
	if (service->contact != NULL) {
		ms_free(service->contact);
	}
151
	bctbx_list_for_each(service->notes, presence_note_unref);
152
	bctbx_list_free(service->notes);
153 154
};

155 156 157 158
static void presence_service_unref(void *service) {
	linphone_presence_service_unref((LinphonePresenceService *)service);
}

159
static void presence_service_set_timestamp(LinphonePresenceService *service, time_t timestamp) {
160 161 162
	service->timestamp = timestamp;
}

163
static void presence_service_add_note(LinphonePresenceService *service, LinphonePresenceNote *note) {
164
	service->notes = bctbx_list_append(service->notes, note);
165 166
}

167
static void presence_activity_uninit(LinphonePresenceActivity *activity) {
168 169 170 171 172
	if (activity->description != NULL) {
		ms_free(activity->description);
	}
}

173 174 175 176
static void presence_activity_unref(void *activity) {
	linphone_presence_activity_unref((LinphonePresenceActivity *)activity);
}

177 178 179
static time_t parse_timestamp(const char *timestamp) {
	struct tm ret;
	time_t seconds;
180
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
181 182 183 184
	long adjust_timezone;
#else
	time_t adjust_timezone;
#endif
185 186 187 188 189 190 191 192 193 194 195 196

	memset(&ret, 0, sizeof(ret));
	sscanf(timestamp, "%d-%d-%dT%d:%d:%d",
	       &ret.tm_year, &ret.tm_mon, &ret.tm_mday, &ret.tm_hour, &ret.tm_min, &ret.tm_sec);
	ret.tm_mon--;
	ret.tm_year -= 1900;
	ret.tm_isdst = 0;
	seconds = mktime(&ret);
	if (seconds == (time_t)-1) {
		ms_error("mktime() failed: %s", strerror(errno));
		return (time_t)-1;
	}
197
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
198 199 200 201 202
	_get_timezone(&adjust_timezone);
#else
	adjust_timezone = timezone;
#endif
	return seconds - (time_t)adjust_timezone;
203 204
}

205
char * linphone_timestamp_to_rfc3339_string(time_t timestamp) {
206
	struct tm *ret;
207
#ifndef _WIN32
208 209 210
	struct tm gmt;
	ret = gmtime_r(&timestamp,&gmt);
#else
211
	ret = gmtime(&timestamp);
212
#endif
213
	int n = snprintf(0, 0, "%4d-%02d-%02dT%02d:%02d:%02dZ",
214
		 ret->tm_year + 1900, ret->tm_mon + 1, ret->tm_mday, ret->tm_hour, ret->tm_min, ret->tm_sec);
François Grisez's avatar
François Grisez committed
215
	char *timestamp_str = (char *) ms_malloc(size_t(n + 1));
Sylvain Berfini's avatar
Sylvain Berfini committed
216
	snprintf(timestamp_str, (size_t)n + 1, "%4d-%02d-%02dT%02d:%02d:%02dZ",
217 218
		 ret->tm_year + 1900, ret->tm_mon + 1, ret->tm_mday, ret->tm_hour, ret->tm_min, ret->tm_sec);
	return timestamp_str;
219 220
}

221
static LinphonePresencePerson * presence_person_new(const char *id,  time_t timestamp) {
222
	LinphonePresencePerson *person = belle_sip_object_new(LinphonePresencePerson);
223 224 225
	if (id != NULL) {
		person->id = ms_strdup(id);
	}
226
	if (person->timestamp == ((time_t)-1))
227
		person->timestamp = time(NULL);
228 229
	else
		person->timestamp = timestamp;
230 231 232
	return person;
}

233
static void presence_person_uninit(LinphonePresencePerson *person) {
234 235 236
	if (person->id != NULL) {
		ms_free(person->id);
	}
237
	bctbx_list_for_each(person->activities, presence_activity_unref);
238
	bctbx_list_free(person->activities);
239
	bctbx_list_for_each(person->activities_notes, presence_note_unref);
240
	bctbx_list_free(person->activities_notes);
241
	bctbx_list_for_each(person->notes, presence_note_unref);
242
	bctbx_list_free(person->notes);
243 244
}

245 246 247 248
static void presence_person_unref(void *person) {
	linphone_presence_person_unref((LinphonePresencePerson *)person);
}

249
static void presence_person_add_activities_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
250
	person->activities_notes = bctbx_list_append(person->activities_notes, note);
251 252
}

253
static void presence_person_add_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
254
	person->notes = bctbx_list_append(person->notes, note);
255 256
}

257
static int presence_model_insert_person_by_timestamp(LinphonePresencePerson *current, LinphonePresencePerson *to_insert) {
258
	return current->timestamp < to_insert->timestamp;
259 260
}

261
static void presence_model_add_person(LinphonePresenceModel *model, LinphonePresencePerson *person) {
262
	model->persons = bctbx_list_insert_sorted(model->persons, linphone_presence_person_ref(person), (bctbx_compare_func)presence_model_insert_person_by_timestamp);
263 264
}

265
static void presence_model_add_note(LinphonePresenceModel *model, LinphonePresenceNote *note) {
266
	model->notes = bctbx_list_append(model->notes, note);
267 268
}

269
static void presence_model_find_open_basic_status(LinphonePresenceService *service, LinphonePresenceBasicStatus *status) {
270 271 272 273 274
	if (service->status == LinphonePresenceBasicStatusOpen) {
		*status = LinphonePresenceBasicStatusOpen;
	}
}

275
static void presence_model_uninit(LinphonePresenceModel *model) {
276 277
	if (model->presentity)
		linphone_address_unref(model->presentity);
278
	bctbx_list_for_each(model->services, presence_service_unref);
279
	bctbx_list_free(model->services);
280
	bctbx_list_for_each(model->persons, presence_person_unref);
281
	bctbx_list_free(model->persons);
282
	bctbx_list_for_each(model->notes, presence_note_unref);
283
	bctbx_list_free(model->notes);
284 285
}

Ghislain MARY's avatar
Ghislain MARY committed
286

287 288 289 290 291 292 293 294

/*****************************************************************************
 * HELPER FUNCTIONS TO EASE ACCESS IN MOST SIMPLER CASES                     *
 ****************************************************************************/

LinphonePresenceModel * linphone_presence_model_new_with_activity(LinphonePresenceActivityType acttype, const char *description) {
	LinphonePresenceModel *model = linphone_presence_model_new();
	if (model != NULL) {
295
		linphone_presence_model_set_basic_status(model, LinphonePresenceBasicStatusOpen);
296
		linphone_presence_model_set_activity(model, acttype, description);
Ghislain MARY's avatar
Ghislain MARY committed
297 298 299 300
	}
	return model;
}

301 302 303
LinphonePresenceModel * linphone_presence_model_new_with_activity_and_note(LinphonePresenceActivityType acttype, const char *description, const char *note, const char *lang) {
	LinphonePresenceModel *model = linphone_presence_model_new();
	if (model != NULL) {
304
		linphone_presence_model_set_basic_status(model, LinphonePresenceBasicStatusOpen);
305 306 307 308
		linphone_presence_model_set_activity(model, acttype, description);
		linphone_presence_model_add_note(model, note, lang);
	}
	return model;
Ghislain MARY's avatar
Ghislain MARY committed
309 310
}

311 312 313
/* Suppose that if at least one service is open, then the model is open. */
LinphonePresenceBasicStatus linphone_presence_model_get_basic_status(const LinphonePresenceModel *model) {
	LinphonePresenceBasicStatus status = LinphonePresenceBasicStatusClosed;
314
	if (model != NULL) {
315
		bctbx_list_for_each2(model->services, (MSIterate2Func)presence_model_find_open_basic_status, &status);
316
	}
317 318 319
	return status;
}

320
LinphoneStatus linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status) {
321
	LinphonePresenceService *service;
322
	int err = 0;
323 324 325

	if (model == NULL) return -1;

326
	linphone_presence_model_clear_services(model);
327
	service = linphone_presence_service_new(NULL, basic_status, NULL);
328 329
	if (service == NULL) return -1;

330 331 332
	err = linphone_presence_model_add_service(model, service);
	linphone_presence_service_unref(service);
	return err;
333 334
}

335
static void presence_service_find_newer_timestamp(LinphonePresenceService *service, time_t *timestamp) {
336 337 338 339
	if (service->timestamp > *timestamp)
		*timestamp = service->timestamp;
}

340
static void presence_person_find_newer_timestamp(LinphonePresencePerson *person, time_t *timestamp) {
341 342 343 344 345 346 347 348 349 350
	if (person->timestamp > *timestamp)
		*timestamp = person->timestamp;
}

time_t linphone_presence_model_get_timestamp(const LinphonePresenceModel *model) {
	time_t timestamp = (time_t)-1;

	if (model == NULL)
		return timestamp;

351 352
	bctbx_list_for_each2(model->services, (MSIterate2Func)presence_service_find_newer_timestamp, &timestamp);
	bctbx_list_for_each2(model->persons, (MSIterate2Func)presence_person_find_newer_timestamp, &timestamp);
353 354 355 356

	return timestamp;
}

357
static void presence_model_find_contact(LinphonePresenceService *service, char **contact) {
358 359 360 361 362 363
	if ((service->contact != NULL) && (*contact == NULL))
		*contact = service->contact;
}

char * linphone_presence_model_get_contact(const LinphonePresenceModel *model) {
	char *contact = NULL;
364
	bctbx_list_for_each2(model->services, (MSIterate2Func)presence_model_find_contact, &contact);
365 366 367 368
	if (contact == NULL) return NULL;
	return ms_strdup(contact);
}

369
LinphoneStatus linphone_presence_model_set_contact(LinphonePresenceModel *model, const char *contact) {
370 371 372 373 374 375
	LinphonePresenceService *service;

	if (model == NULL) return -1;

	service = linphone_presence_model_get_nth_service(model, 0);
	if (service == NULL) {
376
		service = linphone_presence_service_new(NULL, LinphonePresenceBasicStatusClosed, NULL);
377 378
		if (service == NULL) return -1;
		linphone_presence_model_add_service(model, service);
379
		linphone_presence_service_unref(service);
380
	}
381 382 383 384
	return linphone_presence_service_set_contact(service, contact);
}

static void presence_model_count_activities(const LinphonePresencePerson *person, unsigned int *nb) {
385
	*nb += (unsigned int)bctbx_list_size(person->activities);
386 387 388 389 390
}

struct _get_activity_st {
	unsigned int requested_idx;
	unsigned int current_idx;
391
	LinphonePresenceActivity *activity;
392 393
};

394
static void presence_model_get_activity(const LinphonePresencePerson *person, struct _get_activity_st *st) {
395
	if (st->current_idx != (unsigned)-1) {
396
		unsigned int size = (unsigned int)bctbx_list_size(person->activities);
397
		if (st->requested_idx < (st->current_idx + size)) {
Benjamin REIS's avatar
Benjamin REIS committed
398
			st->activity = (LinphonePresenceActivity *)bctbx_list_nth_data(person->activities, (int)(st->requested_idx - st->current_idx));
399 400 401 402
			st->current_idx = (unsigned)-1;
		} else {
			st->current_idx += size;
		}
403 404 405
	}
}

406 407
LinphonePresenceActivity * linphone_presence_model_get_activity(const LinphonePresenceModel *model) {
	return linphone_presence_model_get_nth_activity(model, 0);
408 409
}

410
LinphoneStatus linphone_presence_model_set_activity(LinphonePresenceModel *model, LinphonePresenceActivityType acttype, const char *description) {
411
	LinphonePresenceActivity *activity;
412
	int err = 0;
413 414 415

	if (model == NULL) return -1;

416
	linphone_presence_model_clear_activities(model);
417 418
	activity = linphone_presence_activity_new(acttype, description);
	if (activity == NULL) return -1;
419 420 421
	err = linphone_presence_model_add_activity(model, activity);
	linphone_presence_activity_unref(activity);
	return err;
422 423
}

424
unsigned int linphone_presence_model_get_nb_activities(const LinphonePresenceModel *model) {
425
	unsigned int nb = 0;
426
	bctbx_list_for_each2(model->persons, (MSIterate2Func)presence_model_count_activities, &nb);
427 428 429 430 431 432
	return nb;
}

LinphonePresenceActivity * linphone_presence_model_get_nth_activity(const LinphonePresenceModel *model, unsigned int idx) {
	struct _get_activity_st st;

433
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_activities(model)))
434 435 436 437
		return NULL;

	memset(&st, 0, sizeof(st));
	st.requested_idx = idx;
438
	bctbx_list_for_each2(model->persons, (MSIterate2Func)presence_model_get_activity, &st);
439 440 441 442

	return st.activity;
}

443
LinphoneStatus linphone_presence_model_add_activity(LinphonePresenceModel *model, LinphonePresenceActivity *activity) {
444
	char *id = NULL;
445
	LinphonePresencePerson *person = NULL;
446

447
	if ((model == NULL) || (activity == NULL)) return -1;
448

449
	if (bctbx_list_size(model->persons) == 0) {
450 451 452 453 454 455 456 457
		/* There is no person in the presence model, add one. */
		id = generate_presence_id();
		person = presence_person_new(id, time(NULL));
		if (id != NULL) ms_free(id);
		if (person == NULL)
			return -1;

		presence_model_add_person(model, person);
Ghislain MARY's avatar
Ghislain MARY committed
458
		linphone_presence_person_unref(person);
459 460
	} else {
		/* Add the activity to the first person in the model. */
461
		person = (LinphonePresencePerson *)bctbx_list_nth_data(model->persons, 0);
462 463
	}

464
	linphone_presence_person_add_activity(person, activity);
465 466 467
	return 0;
}

468 469 470 471
static void presence_person_clear_activities(void *person) {
	linphone_presence_person_clear_activities((LinphonePresencePerson *)person);
}

472
LinphoneStatus linphone_presence_model_clear_activities(LinphonePresenceModel *model) {
473 474
	if (model == NULL) return -1;

475
	bctbx_list_for_each(model->persons, presence_person_clear_activities);
476 477 478
	return 0;
}

479 480
struct _find_note_st {
	const char *lang;
481
	LinphonePresenceNote *note;
482 483
};

484
static LinphonePresenceNote * find_presence_note_in_list(bctbx_list_t *list, const char *lang) {
485
	int i;
486
	int nb = (int)bctbx_list_size(list);
487
	for (i = 0; i < nb; i++) {
488
		LinphonePresenceNote *note = (LinphonePresenceNote *)bctbx_list_nth_data(list, i);
489 490 491 492 493 494 495 496 497 498 499
		if (lang == NULL) {
			if (note->lang == NULL) {
				return note;
			}
		} else {
			if ((note->lang != NULL) && (strcmp(lang, note->lang) == 0)) {
				return note;
			}
		}
	}

500 501 502
	return NULL;
}

503
static void find_presence_person_note(LinphonePresencePerson *person, struct _find_note_st *st) {
504 505 506 507 508 509 510 511
	/* First look for the note in the activities notes... */
	st->note = find_presence_note_in_list(person->activities_notes, st->lang);
	if (st->note != NULL) return;

	/* ... then look in the person notes. */
	st->note = find_presence_note_in_list(person->notes, st->lang);
}

512
static void find_presence_service_note(LinphonePresenceService *service, struct _find_note_st *st) {
513 514 515
	st->note = find_presence_note_in_list(service->notes, st->lang);
}

516 517
static LinphonePresenceNote * get_first_presence_note_in_list(bctbx_list_t *list) {
	return (LinphonePresenceNote *)bctbx_list_nth_data(list, 0);
518 519
}

520
static void get_first_presence_person_note(LinphonePresencePerson *person, struct _find_note_st *st) {
521 522 523 524 525
	st->note = get_first_presence_note_in_list(person->activities_notes);
	if (st->note != NULL) return;
	st->note = get_first_presence_note_in_list(person->notes);
}

526
static void get_first_presence_service_note(LinphonePresenceService *service, struct _find_note_st *st) {
527 528 529
	st->note = get_first_presence_note_in_list(service->notes);
}

530
LinphonePresenceNote * linphone_presence_model_get_note(const LinphonePresenceModel *model, const char *lang) {
531 532 533 534 535 536 537 538
	struct _find_note_st st;

	if (model == NULL) return NULL;

	st.note = NULL;
	if (lang != NULL) {
		/* First try to find a note in the specified language exactly. */
		st.lang = lang;
539
		bctbx_list_for_each2(model->persons, (MSIterate2Func)find_presence_person_note, &st);
540
		if (st.note == NULL) {
541
			bctbx_list_for_each2(model->services, (MSIterate2Func)find_presence_service_note, &st);
542 543 544 545 546 547 548 549 550
		}
		if (st.note == NULL) {
			st.note = find_presence_note_in_list(model->notes, lang);
		}
	}

	if (st.note == NULL) {
		/* No notes in the specified language has been found, try to find one without language. */
		st.lang = NULL;
551
		bctbx_list_for_each2(model->persons, (MSIterate2Func)find_presence_person_note, &st);
552
		if (st.note == NULL) {
553
			bctbx_list_for_each2(model->services, (MSIterate2Func)find_presence_service_note, &st);
554 555 556 557 558 559 560 561
		}
		if (st.note == NULL) {
			st.note = find_presence_note_in_list(model->notes, NULL);
		}
	}

	if (st.note == NULL) {
		/* Still no result, so get the first note even if it is not in the specified language. */
562
		bctbx_list_for_each2(model->persons, (MSIterate2Func)get_first_presence_person_note, &st);
563
		if (st.note == NULL) {
564
			bctbx_list_for_each2(model->services, (MSIterate2Func)get_first_presence_service_note, &st);
565 566 567 568 569 570
		}
		if (st.note == NULL) {
			st.note = get_first_presence_note_in_list(model->notes);
		}
	}

571
	return st.note;
572 573
}

574
LinphoneStatus linphone_presence_model_add_note(LinphonePresenceModel *model, const char *note_content, const char *lang) {
575 576
	LinphonePresenceService *service;
	LinphonePresenceNote *note;
577 578 579 580 581

	if ((model == NULL) || (note_content == NULL))
		return -1;

	/* Will put the note in the first service. */
582
	service = reinterpret_cast<LinphonePresenceService *>(bctbx_list_nth_data(model->services, 0));
583 584 585 586 587 588 589 590 591 592
	if (service == NULL) {
		/* If no service exists, create one. */
		service = presence_service_new(generate_presence_id(), LinphonePresenceBasicStatusClosed);
	}
	if (service == NULL)
		return -1;

	/* Search for an existing note in the specified language. */
	note = find_presence_note_in_list(service->notes, lang);
	if (note == NULL) {
593
		note = linphone_presence_note_new(note_content, lang);
594
	} else {
595
		linphone_presence_note_set_content(note, note_content);
596 597 598 599 600 601 602 603 604
	}
	if (note == NULL)
		return -1;

	presence_service_add_note(service, note);

	return 0;
}

605
static void clear_presence_person_notes(LinphonePresencePerson *person) {
606
	bctbx_list_for_each(person->activities_notes, presence_note_unref);
607
	bctbx_list_free(person->activities_notes);
608
	person->activities_notes = NULL;
609
	bctbx_list_for_each(person->notes, presence_note_unref);
610
	bctbx_list_free(person->notes);
611 612 613
	person->notes = NULL;
}

614
static void clear_presence_service_notes(LinphonePresenceService *service) {
615
	bctbx_list_for_each(service->notes, presence_note_unref);
616
	bctbx_list_free(service->notes);
617 618 619
	service->notes = NULL;
}

620
LinphoneStatus linphone_presence_model_clear_notes(LinphonePresenceModel *model) {
621 622 623
	if (model == NULL)
		return -1;

624 625
	bctbx_list_for_each(model->persons, (MSIterateFunc)clear_presence_person_notes);
	bctbx_list_for_each(model->services, (MSIterateFunc)clear_presence_service_notes);
626
	bctbx_list_for_each(model->notes, presence_note_unref);
627
	bctbx_list_free(model->notes);
628 629 630
	model->notes = NULL;

	return 0;
631 632
}

633 634 635 636 637
/*****************************************************************************
 * PRESENCE MODEL FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES             *
 ****************************************************************************/

LinphonePresenceModel * linphone_presence_model_new(void) {
638
	LinphonePresenceModel *model = belle_sip_object_new(LinphonePresenceModel);
639
	return model;
640 641
}

642
unsigned int linphone_presence_model_get_nb_services(const LinphonePresenceModel *model) {
643
	return (unsigned int)bctbx_list_size(model->services);
644
}
645

646
LinphonePresenceService * linphone_presence_model_get_nth_service(const LinphonePresenceModel *model, unsigned int idx) {
647
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_services(model)))
648
		return NULL;
649

Benjamin REIS's avatar
Benjamin REIS committed
650
	return (LinphonePresenceService *)bctbx_list_nth_data(model->services, (int)idx);
651 652
}

653
LinphoneStatus linphone_presence_model_add_service(LinphonePresenceModel *model, LinphonePresenceService *service) {
654
	if ((model == NULL) || (service == NULL)) return -1;
655
	model->services = bctbx_list_append(model->services, linphone_presence_service_ref(service));
656
	return 0;
657 658
}

659
LinphoneStatus linphone_presence_model_clear_services(LinphonePresenceModel *model) {
660 661
	if (model == NULL) return -1;

662
	bctbx_list_for_each(model->services, presence_service_unref);
663
	bctbx_list_free(model->services);
664 665
	model->services = NULL;
	return 0;
666 667
}

668
unsigned int linphone_presence_model_get_nb_persons(const LinphonePresenceModel *model) {
669
	return (unsigned int)bctbx_list_size(model->persons);
670 671 672
}

LinphonePresencePerson * linphone_presence_model_get_nth_person(const LinphonePresenceModel *model, unsigned int idx) {
673
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_persons(model)))
674 675
		return NULL;

Benjamin REIS's avatar
Benjamin REIS committed
676
	return (LinphonePresencePerson *)bctbx_list_nth_data(model->persons, (int)idx);
677 678
}

679
LinphoneStatus linphone_presence_model_add_person(LinphonePresenceModel *model, LinphonePresencePerson *person) {
680
	if ((model == NULL) || (person == NULL)) return -1;
681
	presence_model_add_person(model, person);
682 683 684
	return 0;
}

685
LinphoneStatus linphone_presence_model_clear_persons(LinphonePresenceModel *model) {
686 687
	if (model == NULL) return -1;

688
	bctbx_list_for_each(model->persons, presence_person_unref);
689
	bctbx_list_free(model->persons);
690 691 692 693
	model->persons = NULL;
	return 0;
}

694
LinphoneStatus linphone_presence_model_set_presentity(LinphonePresenceModel *model, const LinphoneAddress *presentity) {
695

696 697 698 699 700 701 702 703 704 705
	if (model->presentity) {
		linphone_address_unref(model->presentity);
		model->presentity = NULL;
	}
	if (presentity) {
		model->presentity=linphone_address_clone(presentity);
		linphone_address_clean(model->presentity);
	}
	return 0;
}
706

707 708 709
const LinphoneAddress * linphone_presence_model_get_presentity(const LinphonePresenceModel *model) {
	return model->presentity;
}
710

711 712 713 714 715 716 717 718 719 720 721 722 723
LinphoneConsolidatedPresence linphone_presence_model_get_consolidated_presence(const LinphonePresenceModel *model) {
	LinphonePresenceBasicStatus basic_status;

	if (linphone_presence_model_is_online(model)) return LinphoneConsolidatedPresenceOnline;
	basic_status = linphone_presence_model_get_basic_status(model);
	if (basic_status == LinphonePresenceBasicStatusClosed) {
		unsigned int nb_activities = linphone_presence_model_get_nb_activities(model);
		if (nb_activities == 0) return LinphoneConsolidatedPresenceOffline;
		else return LinphoneConsolidatedPresenceDoNotDisturb;
	}
	return LinphoneConsolidatedPresenceBusy;
}

724 725 726 727 728 729 730 731 732
BELLE_SIP_INSTANCIATE_VPTR(
	LinphonePresenceModel,
	belle_sip_object_t,
	presence_model_uninit, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE // unown
);

733 734 735
/*****************************************************************************
 * PRESENCE SERVICE FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES           *
 ****************************************************************************/
736

737 738 739 740 741 742 743 744 745 746
char * linphone_presence_basic_status_to_string(LinphonePresenceBasicStatus basic_status) {
	switch (basic_status) {
		case LinphonePresenceBasicStatusOpen:
			return ms_strdup("open");
		case LinphonePresenceBasicStatusClosed:
		default:
			return ms_strdup("closed");
	}
}

747
LinphonePresenceService * linphone_presence_service_new(const char *id, LinphonePresenceBasicStatus basic_status, const char *contact) {
748
	LinphonePresenceService *service;
749 750 751 752 753
	char *service_id;
	if (id == NULL)
		service_id = generate_presence_id();
	else
		service_id = ms_strdup(id);
754 755
	service = presence_service_new(service_id, basic_status);
	linphone_presence_service_set_contact(service, contact);
756 757
	if (service_id != NULL)
		ms_free(service_id);
758 759
	return service;
}
760

761 762 763 764 765
char * linphone_presence_service_get_id(const LinphonePresenceService *service) {
	if (service == NULL) return NULL;
	return ms_strdup(service->id);
}

766
LinphoneStatus linphone_presence_service_set_id(LinphonePresenceService *service, const char *id) {
767 768 769 770 771 772 773 774 775 776
	if (service == NULL) return -1;
	if (service->id != NULL)
		ms_free(service->id);
	if (id == NULL)
		service->id = generate_presence_id();
	else
		service->id = ms_strdup(id);
	return 0;
}

777 778 779
LinphonePresenceBasicStatus linphone_presence_service_get_basic_status(const LinphonePresenceService *service) {
	if (service == NULL) return LinphonePresenceBasicStatusClosed;
	return service->status;
780 781
}

782
LinphoneStatus linphone_presence_service_set_basic_status(LinphonePresenceService *service, LinphonePresenceBasicStatus basic_status) {
783 784 785 786
	if (service == NULL) return -1;
	service->status = basic_status;
	return 0;
}
787

788 789 790 791
char * linphone_presence_service_get_contact(const LinphonePresenceService *service) {
	if (service->contact == NULL) return NULL;
	return ms_strdup(service->contact);
}
792

793
LinphoneStatus linphone_presence_service_set_contact(LinphonePresenceService *service, const char *contact) {
794
	if (service == NULL) return -1;
795 796 797 798 799 800
	if (service->contact != NULL)
		ms_free(service->contact);
	if (contact != NULL)
		service->contact = ms_strdup(contact);
	else
		service->contact = NULL;
801 802 803
	return 0;
}

804 805 806 807 808 809 810 811 812 813 814 815 816
bctbx_list_t * linphone_presence_service_get_service_descriptions(const LinphonePresenceService *service) {
	return service->service_descriptions;
}

LinphoneStatus linphone_presence_service_set_service_descriptions(LinphonePresenceService *service, bctbx_list_t *descriptions) {
	if (!service) return -1;
	if (service->service_descriptions)
		bctbx_list_free_with_data(service->service_descriptions, bctbx_free);

	service->service_descriptions = descriptions;
	return 0;
}

817
unsigned int linphone_presence_service_get_nb_notes(const LinphonePresenceService *service) {
818
	return (unsigned int)bctbx_list_size(service->notes);
819 820 821
}

LinphonePresenceNote * linphone_presence_service_get_nth_note(const LinphonePresenceService *service, unsigned int idx) {
822
	if ((service == NULL) || (idx >= linphone_presence_service_get_nb_notes(service)))
823 824
		return NULL;

Benjamin REIS's avatar
Benjamin REIS committed
825
	return (LinphonePresenceNote *)bctbx_list_nth_data(service->notes, (int)idx);
826 827
}

828
LinphoneStatus linphone_presence_service_add_note(LinphonePresenceService *service, LinphonePresenceNote *note) {
829
	if ((service == NULL) || (note == NULL)) return -1;
830
	service->notes = bctbx_list_append(service->notes, linphone_presence_note_ref(note));
831 832 833
	return 0;
}

834
LinphoneStatus linphone_presence_service_clear_notes(LinphonePresenceService *service) {
835 836
	if (service == NULL) return -1;

837
	bctbx_list_for_each(service->notes, presence_note_unref);
838
	bctbx_list_free(service->notes);
839 840 841 842
	service->notes = NULL;
	return 0;
}

843 844 845 846 847 848 849 850 851
BELLE_SIP_INSTANCIATE_VPTR(
	LinphonePresenceService,
	belle_sip_object_t,
	presence_service_uninit, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE // unown
);

852 853


854 855 856 857 858 859 860 861 862 863 864 865 866
/*****************************************************************************
 * PRESENCE PERSON FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES            *
 ****************************************************************************/

LinphonePresencePerson * linphone_presence_person_new(const char *id) {
	return presence_person_new(id, time(NULL));
}

char * linphone_presence_person_get_id(const LinphonePresencePerson *person) {
	if (person == NULL) return NULL;
	return ms_strdup(person->id);
}

867
LinphoneStatus linphone_presence_person_set_id(LinphonePresencePerson *person, const char *id) {
868 869 870 871 872 873 874 875 876 877
	if (person == NULL) return -1;
	if (person->id != NULL)
		ms_free(person->id);
	if (id == NULL)
		person->id = generate_presence_id();
	else
		person->id = ms_strdup(id);
	return 0;
}

878
unsigned int linphone_presence_person_get_nb_activities(const LinphonePresencePerson *person) {
879
	if (person == NULL) return 0;
880
	return (unsigned int)bctbx_list_size(person->activities);
881 882 883
}

LinphonePresenceActivity * linphone_presence_person_get_nth_activity(const LinphonePresencePerson *person, unsigned int idx) {
884
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_activities(person)))
885
		return NULL;
Benjamin REIS's avatar
Benjamin REIS committed
886
	return (LinphonePresenceActivity *)bctbx_list_nth_data(person->activities, (int)idx);
887 888
}

889
LinphoneStatus linphone_presence_person_add_activity(LinphonePresencePerson *person, LinphonePresenceActivity *activity) {
890
	if ((person == NULL) || (activity == NULL)) return -1;
891
	// insert in first position since its the most recent activity!
892
	person->activities = bctbx_list_prepend(person->activities, linphone_presence_activity_ref(activity));
893 894 895
	return 0;
}

896
LinphoneStatus linphone_presence_person_clear_activities(LinphonePresencePerson *person) {
897
	if (person == NULL) return -1;
898
	bctbx_list_for_each(person->activities, presence_activity_unref);
899
	bctbx_list_free(person->activities);
900 901 902 903
	person->activities = NULL;
	return 0;
}

904
unsigned int linphone_presence_person_get_nb_notes(const LinphonePresencePerson *person) {
905
	if (person == NULL) return 0;
906
	return (unsigned int)bctbx_list_size(person->notes);
907 908 909
}

LinphonePresenceNote * linphone_presence_person_get_nth_note(const LinphonePresencePerson *person, unsigned int idx) {
910
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_notes(person)))
911
		return NULL;
Benjamin REIS's avatar
Benjamin REIS committed
912
	return (LinphonePresenceNote *)bctbx_list_nth_data(person->notes, (int)idx);
913 914
}

915
LinphoneStatus linphone_presence_person_add_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
916
	if ((person == NULL) || (note == NULL)) return -1;
917
	person->notes = bctbx_list_append(person->notes, linphone_presence_note_ref(note));
918 919 920
	return 0;
}

921
LinphoneStatus linphone_presence_person_clear_notes(LinphonePresencePerson *person) {
922
	if (person == NULL) return -1;
923
	bctbx_list_for_each(person->notes, presence_note_unref);
924
	bctbx_list_free(person->notes);
925 926 927 928
	person->notes = NULL;
	return 0;
}

929
unsigned int linphone_presence_person_get_nb_activities_notes(const LinphonePresencePerson *person) {
930
	if (person == NULL) return 0;
931
	return (unsigned int)bctbx_list_size(person->activities_notes);
932 933 934
}

LinphonePresenceNote * linphone_presence_person_get_nth_activities_note(const LinphonePresencePerson *person, unsigned int idx) {
935
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_activities_notes(person)))
936
		return NULL;
Benjamin REIS's avatar
Benjamin REIS committed
937
	return (LinphonePresenceNote *)bctbx_list_nth_data(person->activities_notes, (int)idx);
938 939
}

940
LinphoneStatus linphone_presence_person_add_activities_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
941
	if ((person == NULL) || (note == NULL)) return -1;
942
	person->notes = bctbx_list_append(person->activities_notes, linphone_presence_note_ref(note));
943 944 945
	return 0;
}

946
LinphoneStatus linphone_presence_person_clear_activities_notes(LinphonePresencePerson *person) {
947
	if (person == NULL) return -1;
948
	bctbx_list_for_each(person->activities_notes, presence_note_unref);
949
	bctbx_list_free(person->activities_notes);
950 951 952
	person->activities_notes = NULL;
	return 0;
}
953

954 955 956 957 958 959 960 961 962
BELLE_SIP_INSTANCIATE_VPTR(
	LinphonePresencePerson,
	belle_sip_object_t,
	presence_person_uninit, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE // unown
)

963

964 965 966
/*****************************************************************************
 * PRESENCE ACTIVITY FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES          *
 ****************************************************************************/
967 968 969

struct _presence_activity_name_map {
	const char *name;
970
	LinphonePresenceActivityType type;
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
};

static struct _presence_activity_name_map activity_map[] = {
	{ "appointment", LinphonePresenceActivityAppointment },
	{ "away", LinphonePresenceActivityAway },
	{ "breakfast", LinphonePresenceActivityBreakfast },
	{ "busy", LinphonePresenceActivityBusy },
	{ "dinner", LinphonePresenceActivityDinner },
	{ "holiday", LinphonePresenceActivityHoliday },
	{ "in-transit", LinphonePresenceActivityInTransit },
	{ "looking-for-work", LinphonePresenceActivityLookingForWork },
	{ "lunch", LinphonePresenceActivityLunch },
	{ "meal", LinphonePresenceActivityMeal },
	{ "meeting", LinphonePresenceActivityMeeting },
	{ "on-the-phone", LinphonePresenceActivityOnThePhone },
	{ "other", LinphonePresenceActivityOther },
	{ "performance", LinphonePresenceActivityPerformance },
	{ "permanent-absence", LinphonePresenceActivityPermanentAbsence },
	{ "playing", LinphonePresenceActivityPlaying },
	{ "presentation", LinphonePresenceActivityPresentation },
	{ "shopping", LinphonePresenceActivityShopping },
	{ "sleeping", LinphonePresenceActivitySleeping },
	{ "spectator", LinphonePresenceActivitySpectator },
	{ "steering", LinphonePresenceActivitySteering },
	{ "travel", LinphonePresenceActivityTravel },
	{ "tv", LinphonePresenceActivityTV },
	{ "unknown", LinphonePresenceActivityUnknown },
	{ "vacation", LinphonePresenceActivityVacation },
	{ "working", LinphonePresenceActivityWorking },
	{ "worship", LinphonePresenceActivityWorship }
};

1003
static int activity_name_to_presence_activity_type(const char *name, LinphonePresenceActivityType *acttype) {
1004 1005 1006
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
		if (strcmp(name, activity_map[i].name) == 0) {
1007
			*acttype = activity_map[i].type;
1008 1009 1010 1011 1012 1013
			return 0;
		}
	}
	return -1;
}

1014
static const char * presence_activity_type_to_string(LinphonePresenceActivityType acttype) {
1015 1016
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
1017
		if (acttype == activity_map[i].type) {
1018 1019 1020 1021 1022 1023
			return activity_map[i].name;
		}
	}
	return NULL;
}

1024
LinphonePresenceActivity * linphone_presence_activity_new(LinphonePresenceActivityType acttype, const char *description) {
1025
	LinphonePresenceActivity *act = belle_sip_object_new(LinphonePresenceActivity);
1026 1027 1028
	act->type = acttype;
	if (description != NULL) {
		act->description = ms_strdup(description);
Ghislain MARY's avatar
Ghislain MARY committed
1029
	}
1030
	return act;
Ghislain MARY's avatar
Ghislain MARY committed
1031 1032
}

1033 1034 1035
char * linphone_presence_activity_to_string(const LinphonePresenceActivity *activity) {
	LinphonePresenceActivityType acttype = linphone_presence_activity_get_type(activity);
	const char *description = linphone_presence_activity_get_description(activity);
Wescoeur's avatar
Wescoeur committed
1036
	const char *acttype_str = presence_activity_type_to_string(acttype);
Ghislain MARY's avatar
Ghislain MARY committed
1037 1038 1039
	return ms_strdup_printf("%s%s%s", acttype_str,
				(description == NULL) ? "" : ": ",
				(description == NULL) ? "" : description);
1040 1041 1042 1043 1044 1045
}

LinphonePresenceActivityType linphone_presence_activity_get_type(const LinphonePresenceActivity *activity) {
	return activity->type;
}

1046
LinphoneStatus linphone_presence_activity_set_type(LinphonePresenceActivity *activity, LinphonePresenceActivityType acttype) {
1047 1048 1049 1050 1051
	if (activity == NULL) return -1;
	activity->type = acttype;
	return 0;
}

1052 1053 1054 1055
const char * linphone_presence_activity_get_description(const LinphonePresenceActivity *activity) {
	if (activity == NULL)
		return NULL;
	return activity->description;
1056 1057
}

1058
LinphoneStatus linphone_presence_activity_set_description(LinphonePresenceActivity *activity, const char *description) {
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	if (activity == NULL) return -1;
	if (activity->description != NULL)
		ms_free(activity->description);
	if (description != NULL)
		activity->description = ms_strdup(description);
	else
		activity->description = NULL;
	return 0;
}

1069 1070 1071 1072 1073 1074 1075 1076
BELLE_SIP_INSTANCIATE_VPTR(
	LinphonePresenceActivity,
	belle_sip_object_t, // parent
	presence_activity_uninit, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE // unown
);
1077 1078 1079 1080 1081 1082


/*****************************************************************************
 * PRESENCE NOTE FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES              *
 ****************************************************************************/

1083 1084 1085 1086
LinphonePresenceNote * linphone_presence_note_new(const char *content, const char *lang) {
	LinphonePresenceNote *note;

	if (content == NULL) return NULL;
1087
	note = belle_sip_object_new(LinphonePresenceNote);
1088 1089 1090 1091 1092 1093 1094
	note->content = ms_strdup(content);
	if (lang != NULL) {
		note->lang = ms_strdup(lang);
	}
	return note;
}

1095 1096 1097 1098 1099 1100
const char * linphone_presence_note_get_content(const LinphonePresenceNote *note) {
	if (note == NULL)
		return NULL;
	return note->content;
}

1101
LinphoneStatus linphone_presence_note_set_content(LinphonePresenceNote *note, const char *content) {
1102 1103 1104 1105 1106 1107 1108 1109
	if (content == NULL) return -1;
	if (note->content != NULL) {
		ms_free(note->content);
	}
	note->content = ms_strdup(content);
	return 0;
}

1110 1111 1112 1113 1114 1115
const char * linphone_presence_note_get_lang(const LinphonePresenceNote *note) {
	if (note == NULL)
		return NULL;
	return note->lang;
}

1116
LinphoneStatus linphone_presence_note_set_lang(LinphonePresenceNote *note, const char *lang) {
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
	if (note->lang != NULL) {
		ms_free(note->lang);
		note->lang = NULL;
	}
	if (lang != NULL) {
		note->lang = ms_strdup(lang);
	}
	return 0;
}

1127 1128 1129 1130 1131 1132 1133 1134
BELLE_SIP_INSTANCIATE_VPTR(
	LinphonePresenceNote,
	belle_sip_object_t, // parent
	presence_note_uninit, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE // unown
)
1135 1136 1137 1138 1139 1140 1141


/*****************************************************************************
 * PRESENCE INTERNAL FUNCTIONS FOR WRAPPERS IN OTHER PROGRAMMING LANGUAGES   *
 ****************************************************************************/

LinphonePresenceModel * linphone_presence_model_ref(LinphonePresenceModel *model) {
1142
	return (LinphonePresenceModel *)belle_sip_object_ref(model);
1143 1144 1145
}

LinphonePresenceModel * linphone_presence_model_unref(LinphonePresenceModel *model) {
1146 1147 1148
	LinphonePresenceModel *returned_model = model->base.ref > 1 ? model : NULL;
	belle_sip_object_unref(model);
	return returned_model;
1149 1150 1151 1152 1153 1154
}

void linphone_presence_model_set_user_data(LinphonePresenceModel *model, void *user_data) {
	model->user_data = user_data;
}

1155
void * linphone_presence_model_get_user_data(const LinphonePresenceModel *model) {
1156 1157 1158 1159
	return model->user_data;
}

LinphonePresenceService * linphone_presence_service_ref(LinphonePresenceService *service) {
1160
	return (LinphonePresenceService *)belle_sip_object_ref(service);
1161 1162 1163
}

LinphonePresenceService * linphone_presence_service_unref(LinphonePresenceService *service) {
1164 1165 1166
	LinphonePresenceService *returned_service = service->base.ref > 1 ? service : NULL;
	belle_sip_object_unref(service);
	return returned_service;
1167 1168 1169 1170 1171 1172
}

void linphone_presence_service_set_user_data(LinphonePresenceService *service, void *user_data) {
	service->user_data = user_data;
}

1173
void * linphone_presence_service_get_user_data(const LinphonePresenceService *service) {
1174 1175 1176
	return service->user_data;
}

1177
LinphonePresencePerson * linphone_presence_person_ref(LinphonePresencePerson *person) {
1178
	return (LinphonePresencePerson *)belle_sip_object_ref(person);
1179 1180 1181
}

LinphonePresencePerson * linphone_presence_person_unref(LinphonePresencePerson *person) {
1182 1183 1184
	LinphonePresencePerson *returned_person = person->base.ref > 1 ? person : NULL;
	belle_sip_object_unref(person);
	return returned_person;
1185 1186 1187 1188 1189 1190
}

void linphone_presence_person_set_user_data(LinphonePresencePerson *person, void *user_data) {
	person->user_data = user_data;
}

1191
void * linphone_presence_person_get_user_data(const LinphonePresencePerson *person) {
1192 1193 1194
	return person->user_data;
}

1195
LinphonePresenceActivity * linphone_presence_activity_ref(LinphonePresenceActivity *activity) {
1196
	return (LinphonePresenceActivity *)belle_sip_object_ref(activity);
1197 1198 1199
}

LinphonePresenceActivity * linphone_presence_activity_unref(LinphonePresenceActivity *activity) {
1200 1201 1202
	LinphonePresenceActivity *returned_activity = activity->base.ref > 1 ? activity : NULL;
	belle_sip_object_unref(activity);
	return returned_activity;