presence.c 67.4 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 17 18 19 20 21

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.
*/

#include "linphonecore.h"
#include "private.h"
22
#include "lpconfig.h"
23
#include "linphonepresence.h"
24

aymeric's avatar
aymeric committed
25 26 27 28 29


extern const char *__policy_enum_to_str(LinphoneSubscribePolicy pol);


30 31

struct _LinphonePresenceNote {
Ghislain MARY's avatar
Ghislain MARY committed
32 33
	void *user_data;
	int refcnt;
34 35 36 37 38
	char *lang;
	char *content;
};

struct _LinphonePresenceService {
39 40
	void *user_data;
	int refcnt;
41 42
	char *id;
	LinphonePresenceBasicStatus status;
43 44 45
	char *contact;
	MSList *notes;				/**< A list of _LinphonePresenceNote structures. */
	time_t timestamp;
46 47 48
};

struct _LinphonePresenceActivity {
Ghislain MARY's avatar
Ghislain MARY committed
49 50
	void *user_data;
	int refcnt;
51
	LinphonePresenceActivityType type;
52 53 54 55
	char *description;
};

struct _LinphonePresencePerson {
56 57
	void *user_data;
	int refcnt;
58 59 60 61 62 63 64 65 66 67 68 69
	char *id;
	MSList *activities;		/**< A list of _LinphonePresenceActivity structures. */
	MSList *activities_notes;	/**< A list of _LinphonePresenceNote structures. */
	MSList *notes;			/**< A list of _LinphonePresenceNote structures. */
	time_t timestamp;
};

/**
 * 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 {
70
	LinphoneAddress *presentity; /* "The model seeks to describe the presentity, identified by a presentity URI.*/
Ghislain MARY's avatar
Ghislain MARY committed
71 72
	void *user_data;
	int refcnt;
73 74 75 76 77 78
	MSList *services;	/**< A list of _LinphonePresenceService structures. Also named tuples in the RFC. */
	MSList *persons;	/**< A list of _LinphonePresencePerson structures. */
	MSList *notes;		/**< A list of _LinphonePresenceNote structures. */
};


79 80 81 82 83 84
static const char *person_prefix = "/pidf:presence/dm:person";


/*****************************************************************************
 * PRIVATE FUNCTIONS                                                         *
 ****************************************************************************/
jehan's avatar
jehan committed
85 86 87 88
/*defined in http://www.w3.org/TR/REC-xml/*/
static char presence_id_valid_characters[] = "0123456789abcdefghijklmnopqrstuvwxyz-.";
/*NameStartChar (NameChar)**/
static char presence_id_valid_start_characters[] = ":_abcdefghijklmnopqrstuvwxyz";
89 90 91 92

static char * generate_presence_id(void) {
	char id[7];
	int i;
93
	id[0] = presence_id_valid_start_characters[ortp_random() % (sizeof(presence_id_valid_start_characters)-1)];
jehan's avatar
jehan committed
94
	for (i = 1; i < 6; i++) {
95
		id[i] = presence_id_valid_characters[ortp_random() % (sizeof(presence_id_valid_characters)-1)];
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	}
	id[6] = '\0';

	return ms_strdup(id);
}

static const char * presence_basic_status_to_string(LinphonePresenceBasicStatus basic_status) {
	switch (basic_status) {
		case LinphonePresenceBasicStatusOpen:
			return "open";
		case LinphonePresenceBasicStatusClosed:
		default:
			return "closed";
	}
}

112
static void presence_note_delete(LinphonePresenceNote *note) {
113 114 115 116 117 118 119
	ms_free(note->content);
	if (note->lang != NULL) {
		ms_free(note->lang);
	}
	ms_free(note);
}

120 121 122
static LinphonePresenceService * presence_service_new(const char *id, LinphonePresenceBasicStatus status) {
	LinphonePresenceService *service = ms_new0(LinphonePresenceService, 1);
	service->refcnt = 1;
123 124 125 126
	if (id != NULL) {
		service->id = ms_strdup(id);
	}
	service->status = status;
127
	service->timestamp = time(NULL);
128 129 130
	return service;
}

131
static void presence_service_delete(LinphonePresenceService *service) {
132 133 134
	if (service->id != NULL) {
		ms_free(service->id);
	}
135 136 137
	if (service->contact != NULL) {
		ms_free(service->contact);
	}
138
	ms_list_for_each(service->notes, (MSIterateFunc)linphone_presence_note_unref);
139
	ms_list_free(service->notes);
140 141 142
	ms_free(service);
};

143
static void presence_service_set_timestamp(LinphonePresenceService *service, time_t timestamp) {
144 145 146
	service->timestamp = timestamp;
}

147
static void presence_service_add_note(LinphonePresenceService *service, LinphonePresenceNote *note) {
148 149 150
	service->notes = ms_list_append(service->notes, note);
}

151
static void presence_activity_delete(LinphonePresenceActivity *activity) {
152 153 154 155 156 157 158 159 160
	if (activity->description != NULL) {
		ms_free(activity->description);
	}
	ms_free(activity);
}

static time_t parse_timestamp(const char *timestamp) {
	struct tm ret;
	time_t seconds;
161
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
162 163 164 165
	long adjust_timezone;
#else
	time_t adjust_timezone;
#endif
166 167 168 169 170 171 172 173 174 175 176 177

	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;
	}
178
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
179 180 181 182 183
	_get_timezone(&adjust_timezone);
#else
	adjust_timezone = timezone;
#endif
	return seconds - (time_t)adjust_timezone;
184 185
}

186
char * linphone_timestamp_to_rfc3339_string(time_t timestamp) {
187 188
	char timestamp_str[22];
	struct tm *ret;
189
#ifndef _WIN32
190 191 192
	struct tm gmt;
	ret = gmtime_r(&timestamp,&gmt);
#else
193
	ret = gmtime(&timestamp);
194 195 196 197 198 199
#endif
	snprintf(timestamp_str, sizeof(timestamp_str), "%4d-%02d-%02dT%02d:%02d:%02dZ",
		 ret->tm_year + 1900, ret->tm_mon + 1, ret->tm_mday, ret->tm_hour, ret->tm_min, ret->tm_sec);
	return ms_strdup(timestamp_str);
}

200 201
static LinphonePresencePerson * presence_person_new(const char *id,  time_t timestamp) {
	LinphonePresencePerson *person = ms_new0(LinphonePresencePerson, 1);
202
	person->refcnt = 1;
203 204 205
	if (id != NULL) {
		person->id = ms_strdup(id);
	}
206
	if (person->timestamp == ((time_t)-1))
207
		person->timestamp = time(NULL);
208 209
	else
		person->timestamp = timestamp;
210 211 212
	return person;
}

213
static void presence_person_delete(LinphonePresencePerson *person) {
214 215 216
	if (person->id != NULL) {
		ms_free(person->id);
	}
217
	ms_list_for_each(person->activities, (MSIterateFunc)linphone_presence_activity_unref);
218
	ms_list_free(person->activities);
219
	ms_list_for_each(person->activities_notes, (MSIterateFunc)linphone_presence_note_unref);
220
	ms_list_free(person->activities_notes);
221
	ms_list_for_each(person->notes, (MSIterateFunc)linphone_presence_note_unref);
222 223 224 225
	ms_list_free(person->notes);
	ms_free(person);
}

226
static void presence_person_add_activities_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
227 228 229
	person->activities_notes = ms_list_append(person->activities_notes, note);
}

230
static void presence_person_add_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
231 232 233
	person->notes = ms_list_append(person->notes, note);
}

234
static int presence_model_insert_person_by_timestamp(LinphonePresencePerson *current, LinphonePresencePerson *to_insert) {
235
	return current->timestamp < to_insert->timestamp;
236 237
}

238
static void presence_model_add_person(LinphonePresenceModel *model, LinphonePresencePerson *person) {
239
	model->persons = ms_list_insert_sorted(model->persons, linphone_presence_person_ref(person), (MSCompareFunc)presence_model_insert_person_by_timestamp);
240 241
}

242
static void presence_model_add_note(LinphonePresenceModel *model, LinphonePresenceNote *note) {
243 244 245
	model->notes = ms_list_append(model->notes, note);
}

246
static void presence_model_find_open_basic_status(LinphonePresenceService *service, LinphonePresenceBasicStatus *status) {
247 248 249 250 251
	if (service->status == LinphonePresenceBasicStatusOpen) {
		*status = LinphonePresenceBasicStatusOpen;
	}
}

252
static void presence_model_delete(LinphonePresenceModel *model) {
253
	if (model == NULL) return;
254 255
	if (model->presentity)
		linphone_address_unref(model->presentity);
256
	ms_list_for_each(model->services, (MSIterateFunc)linphone_presence_service_unref);
257
	ms_list_free(model->services);
258
	ms_list_for_each(model->persons, (MSIterateFunc)linphone_presence_person_unref);
259
	ms_list_free(model->persons);
260
	ms_list_for_each(model->notes, (MSIterateFunc)linphone_presence_note_unref);
261 262 263 264
	ms_list_free(model->notes);
	ms_free(model);
}

Ghislain MARY's avatar
Ghislain MARY committed
265

266 267 268 269 270 271 272 273 274

/*****************************************************************************
 * 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) {
		linphone_presence_model_set_activity(model, acttype, description);
Ghislain MARY's avatar
Ghislain MARY committed
275 276 277 278
	}
	return model;
}

279 280 281 282 283 284 285
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) {
		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
286 287
}

288 289 290
/* 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;
291 292 293
	if (model != NULL) {
		ms_list_for_each2(model->services, (MSIterate2Func)presence_model_find_open_basic_status, &status);
	}
294 295 296
	return status;
}

297
int linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status) {
298
	LinphonePresenceService *service;
299
	int err = 0;
300 301 302

	if (model == NULL) return -1;

303
	linphone_presence_model_clear_services(model);
304
	service = linphone_presence_service_new(NULL, basic_status, NULL);
305 306
	if (service == NULL) return -1;

307 308 309
	err = linphone_presence_model_add_service(model, service);
	linphone_presence_service_unref(service);
	return err;
310 311
}

312
static void presence_service_find_newer_timestamp(LinphonePresenceService *service, time_t *timestamp) {
313 314 315 316
	if (service->timestamp > *timestamp)
		*timestamp = service->timestamp;
}

317
static void presence_person_find_newer_timestamp(LinphonePresencePerson *person, time_t *timestamp) {
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	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;

	ms_list_for_each2(model->services, (MSIterate2Func)presence_service_find_newer_timestamp, &timestamp);
	ms_list_for_each2(model->persons, (MSIterate2Func)presence_person_find_newer_timestamp, &timestamp);

	return timestamp;
}

334
static void presence_model_find_contact(LinphonePresenceService *service, char **contact) {
335 336 337 338 339 340 341 342 343 344 345
	if ((service->contact != NULL) && (*contact == NULL))
		*contact = service->contact;
}

char * linphone_presence_model_get_contact(const LinphonePresenceModel *model) {
	char *contact = NULL;
	ms_list_for_each2(model->services, (MSIterate2Func)presence_model_find_contact, &contact);
	if (contact == NULL) return NULL;
	return ms_strdup(contact);
}

346 347 348 349 350 351 352
int linphone_presence_model_set_contact(LinphonePresenceModel *model, const char *contact) {
	LinphonePresenceService *service;

	if (model == NULL) return -1;

	service = linphone_presence_model_get_nth_service(model, 0);
	if (service == NULL) {
353
		service = linphone_presence_service_new(NULL, LinphonePresenceBasicStatusClosed, NULL);
354 355
		if (service == NULL) return -1;
		linphone_presence_model_add_service(model, service);
356
	}
357 358 359 360
	return linphone_presence_service_set_contact(service, contact);
}

static void presence_model_count_activities(const LinphonePresencePerson *person, unsigned int *nb) {
361 362 363 364 365 366
	*nb += ms_list_size(person->activities);
}

struct _get_activity_st {
	unsigned int requested_idx;
	unsigned int current_idx;
367
	LinphonePresenceActivity *activity;
368 369
};

370
static void presence_model_get_activity(const LinphonePresencePerson *person, struct _get_activity_st *st) {
371 372 373 374 375 376 377 378
	if (st->current_idx != (unsigned)-1) {
		unsigned int size = ms_list_size(person->activities);
		if (st->requested_idx < (st->current_idx + size)) {
			st->activity = (LinphonePresenceActivity *)ms_list_nth_data(person->activities, st->requested_idx - st->current_idx);
			st->current_idx = (unsigned)-1;
		} else {
			st->current_idx += size;
		}
379 380 381
	}
}

382 383
LinphonePresenceActivity * linphone_presence_model_get_activity(const LinphonePresenceModel *model) {
	return linphone_presence_model_get_nth_activity(model, 0);
384 385
}

386
int linphone_presence_model_set_activity(LinphonePresenceModel *model, LinphonePresenceActivityType acttype, const char *description) {
387
	LinphonePresenceBasicStatus basic_status = LinphonePresenceBasicStatusOpen;
388
	LinphonePresenceActivity *activity;
389
	int err = 0;
390 391 392

	if (model == NULL) return -1;

393
	switch (acttype) {
394 395 396 397 398 399 400 401 402 403 404 405
		case LinphonePresenceActivityAppointment:
		case LinphonePresenceActivityBusy:
		case LinphonePresenceActivityMeeting:
		case LinphonePresenceActivityPermanentAbsence:
		case LinphonePresenceActivityOffline:
		case LinphonePresenceActivityWorship:
			basic_status = LinphonePresenceBasicStatusClosed;
			break;
		default:
			basic_status = LinphonePresenceBasicStatusOpen;
			break;
	}
406
	if (linphone_presence_model_set_basic_status(model, basic_status) < 0) return -1;
407
	linphone_presence_model_clear_activities(model);
408 409
	activity = linphone_presence_activity_new(acttype, description);
	if (activity == NULL) return -1;
410 411 412
	err = linphone_presence_model_add_activity(model, activity);
	linphone_presence_activity_unref(activity);
	return err;
413 414
}

415
unsigned int linphone_presence_model_get_nb_activities(const LinphonePresenceModel *model) {
416 417 418 419 420 421 422 423
	unsigned int nb = 0;
	ms_list_for_each2(model->persons, (MSIterate2Func)presence_model_count_activities, &nb);
	return nb;
}

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

424
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_activities(model)))
425 426 427 428 429 430 431 432 433
		return NULL;

	memset(&st, 0, sizeof(st));
	st.requested_idx = idx;
	ms_list_for_each2(model->persons, (MSIterate2Func)presence_model_get_activity, &st);

	return st.activity;
}

434
int linphone_presence_model_add_activity(LinphonePresenceModel *model, LinphonePresenceActivity *activity) {
435
	char *id = NULL;
436
	LinphonePresencePerson *person = NULL;
437

438
	if ((model == NULL) || (activity == NULL)) return -1;
439 440 441 442 443 444 445 446 447 448 449 450

	if (ms_list_size(model->persons) == 0) {
		/* 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);
	} else {
		/* Add the activity to the first person in the model. */
451
		person = (LinphonePresencePerson *)ms_list_nth_data(model->persons, 0);
452 453
	}

454
	linphone_presence_person_add_activity(person, activity);
455 456 457 458 459 460
	return 0;
}

int linphone_presence_model_clear_activities(LinphonePresenceModel *model) {
	if (model == NULL) return -1;

461
	ms_list_for_each(model->persons, (MSIterateFunc)linphone_presence_person_clear_activities);
462 463 464
	return 0;
}

465 466
struct _find_note_st {
	const char *lang;
467
	LinphonePresenceNote *note;
468 469
};

470
static LinphonePresenceNote * find_presence_note_in_list(MSList *list, const char *lang) {
471 472 473 474 475
	int nb;
	int i;

	nb = ms_list_size(list);
	for (i = 0; i < nb; i++) {
476
		LinphonePresenceNote *note = (LinphonePresenceNote *)ms_list_nth_data(list, i);
477 478 479 480 481 482 483 484 485 486 487
		if (lang == NULL) {
			if (note->lang == NULL) {
				return note;
			}
		} else {
			if ((note->lang != NULL) && (strcmp(lang, note->lang) == 0)) {
				return note;
			}
		}
	}

488 489 490
	return NULL;
}

491
static void find_presence_person_note(LinphonePresencePerson *person, struct _find_note_st *st) {
492 493 494 495 496 497 498 499
	/* 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);
}

500
static void find_presence_service_note(LinphonePresenceService *service, struct _find_note_st *st) {
501 502 503
	st->note = find_presence_note_in_list(service->notes, st->lang);
}

504 505
static LinphonePresenceNote * get_first_presence_note_in_list(MSList *list) {
	return (LinphonePresenceNote *)ms_list_nth_data(list, 0);
506 507
}

508
static void get_first_presence_person_note(LinphonePresencePerson *person, struct _find_note_st *st) {
509 510 511 512 513
	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);
}

514
static void get_first_presence_service_note(LinphonePresenceService *service, struct _find_note_st *st) {
515 516 517
	st->note = get_first_presence_note_in_list(service->notes);
}

518
LinphonePresenceNote * linphone_presence_model_get_note(const LinphonePresenceModel *model, const char *lang) {
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
	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;
		ms_list_for_each2(model->persons, (MSIterate2Func)find_presence_person_note, &st);
		if (st.note == NULL) {
			ms_list_for_each2(model->services, (MSIterate2Func)find_presence_service_note, &st);
		}
		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;
		ms_list_for_each2(model->persons, (MSIterate2Func)find_presence_person_note, &st);
		if (st.note == NULL) {
			ms_list_for_each2(model->services, (MSIterate2Func)find_presence_service_note, &st);
		}
		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. */
		ms_list_for_each2(model->persons, (MSIterate2Func)get_first_presence_person_note, &st);
		if (st.note == NULL) {
			ms_list_for_each2(model->services, (MSIterate2Func)get_first_presence_service_note, &st);
		}
		if (st.note == NULL) {
			st.note = get_first_presence_note_in_list(model->notes);
		}
	}

559
	return st.note;
560 561 562
}

int linphone_presence_model_add_note(LinphonePresenceModel *model, const char *note_content, const char *lang) {
563 564
	LinphonePresenceService *service;
	LinphonePresenceNote *note;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

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

	/* Will put the note in the first service. */
	service = ms_list_nth_data(model->services, 0);
	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) {
Ghislain MARY's avatar
Ghislain MARY committed
581
		note = linphone_presence_note_new(note_content, lang);
582
	} else {
Ghislain MARY's avatar
Ghislain MARY committed
583
		linphone_presence_note_set_content(note, note_content);
584 585 586 587 588 589 590 591 592
	}
	if (note == NULL)
		return -1;

	presence_service_add_note(service, note);

	return 0;
}

593
static void clear_presence_person_notes(LinphonePresencePerson *person) {
594
	ms_list_for_each(person->activities_notes, (MSIterateFunc)linphone_presence_note_unref);
595 596
	ms_list_free(person->activities_notes);
	person->activities_notes = NULL;
597
	ms_list_for_each(person->notes, (MSIterateFunc)linphone_presence_note_unref);
598 599 600 601
	ms_list_free(person->notes);
	person->notes = NULL;
}

602
static void clear_presence_service_notes(LinphonePresenceService *service) {
603
	ms_list_for_each(service->notes, (MSIterateFunc)linphone_presence_note_unref);
604 605 606 607 608 609 610 611 612 613
	ms_list_free(service->notes);
	service->notes = NULL;
}

int linphone_presence_model_clear_notes(LinphonePresenceModel *model) {
	if (model == NULL)
		return -1;

	ms_list_for_each(model->persons, (MSIterateFunc)clear_presence_person_notes);
	ms_list_for_each(model->services, (MSIterateFunc)clear_presence_service_notes);
614
	ms_list_for_each(model->notes, (MSIterateFunc)linphone_presence_note_unref);
615 616 617 618
	ms_list_free(model->notes);
	model->notes = NULL;

	return 0;
619 620
}

621 622 623 624 625 626 627 628
/*****************************************************************************
 * PRESENCE MODEL FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES             *
 ****************************************************************************/

LinphonePresenceModel * linphone_presence_model_new(void) {
	LinphonePresenceModel *model = ms_new0(LinphonePresenceModel, 1);
	model->refcnt = 1;
	return model;
629 630
}

631
unsigned int linphone_presence_model_get_nb_services(const LinphonePresenceModel *model) {
632 633
	return ms_list_size(model->services);
}
634

635
LinphonePresenceService * linphone_presence_model_get_nth_service(const LinphonePresenceModel *model, unsigned int idx) {
636
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_services(model)))
637
		return NULL;
638

639
	return (LinphonePresenceService *)ms_list_nth_data(model->services, idx);
640 641
}

642 643
int linphone_presence_model_add_service(LinphonePresenceModel *model, LinphonePresenceService *service) {
	if ((model == NULL) || (service == NULL)) return -1;
644
	model->services = ms_list_append(model->services, linphone_presence_service_ref(service));
645
	return 0;
646 647
}

648 649 650 651 652 653 654
int linphone_presence_model_clear_services(LinphonePresenceModel *model) {
	if (model == NULL) return -1;

	ms_list_for_each(model->services, (MSIterateFunc)linphone_presence_service_unref);
	ms_list_free(model->services);
	model->services = NULL;
	return 0;
655 656
}

657
unsigned int linphone_presence_model_get_nb_persons(const LinphonePresenceModel *model) {
658 659 660 661
	return ms_list_size(model->persons);
}

LinphonePresencePerson * linphone_presence_model_get_nth_person(const LinphonePresenceModel *model, unsigned int idx) {
662
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_persons(model)))
663 664 665 666 667 668 669
		return NULL;

	return (LinphonePresencePerson *)ms_list_nth_data(model->persons, idx);
}

int linphone_presence_model_add_person(LinphonePresenceModel *model, LinphonePresencePerson *person) {
	if ((model == NULL) || (person == NULL)) return -1;
670
	presence_model_add_person(model, person);
671 672 673 674 675 676 677 678 679 680 681 682
	return 0;
}

int linphone_presence_model_clear_persons(LinphonePresenceModel *model) {
	if (model == NULL) return -1;

	ms_list_for_each(model->persons, (MSIterateFunc)linphone_presence_person_unref);
	ms_list_free(model->persons);
	model->persons = NULL;
	return 0;
}

683
int linphone_presence_model_set_presentity(LinphonePresenceModel *model, const LinphoneAddress *presentity) {
684

685 686 687 688 689 690 691 692 693 694
	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;
}
695

696 697 698
const LinphoneAddress * linphone_presence_model_get_presentity(const LinphonePresenceModel *model) {
	return model->presentity;
}
699

700 701 702
/*****************************************************************************
 * PRESENCE SERVICE FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES           *
 ****************************************************************************/
703

704
LinphonePresenceService * linphone_presence_service_new(const char *id, LinphonePresenceBasicStatus basic_status, const char *contact) {
705
	LinphonePresenceService *service;
706 707 708 709 710
	char *service_id;
	if (id == NULL)
		service_id = generate_presence_id();
	else
		service_id = ms_strdup(id);
711 712
	service = presence_service_new(service_id, basic_status);
	linphone_presence_service_set_contact(service, contact);
713 714
	if (service_id != NULL)
		ms_free(service_id);
715 716
	return service;
}
717

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
char * linphone_presence_service_get_id(const LinphonePresenceService *service) {
	if (service == NULL) return NULL;
	return ms_strdup(service->id);
}

int linphone_presence_service_set_id(LinphonePresenceService *service, const char *id) {
	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;
}

734 735 736
LinphonePresenceBasicStatus linphone_presence_service_get_basic_status(const LinphonePresenceService *service) {
	if (service == NULL) return LinphonePresenceBasicStatusClosed;
	return service->status;
737 738
}

739 740 741 742 743
int linphone_presence_service_set_basic_status(LinphonePresenceService *service, LinphonePresenceBasicStatus basic_status) {
	if (service == NULL) return -1;
	service->status = basic_status;
	return 0;
}
744

745 746 747 748
char * linphone_presence_service_get_contact(const LinphonePresenceService *service) {
	if (service->contact == NULL) return NULL;
	return ms_strdup(service->contact);
}
749

750
int linphone_presence_service_set_contact(LinphonePresenceService *service, const char *contact) {
751
	if (service == NULL) return -1;
752 753 754 755 756 757
	if (service->contact != NULL)
		ms_free(service->contact);
	if (contact != NULL)
		service->contact = ms_strdup(contact);
	else
		service->contact = NULL;
758 759 760
	return 0;
}

761
unsigned int linphone_presence_service_get_nb_notes(const LinphonePresenceService *service) {
762 763 764 765
	return ms_list_size(service->notes);
}

LinphonePresenceNote * linphone_presence_service_get_nth_note(const LinphonePresenceService *service, unsigned int idx) {
766
	if ((service == NULL) || (idx >= linphone_presence_service_get_nb_notes(service)))
767 768 769 770 771
		return NULL;

	return (LinphonePresenceNote *)ms_list_nth_data(service->notes, idx);
}

Ghislain MARY's avatar
Ghislain MARY committed
772
int linphone_presence_service_add_note(LinphonePresenceService *service, LinphonePresenceNote *note) {
773
	if ((service == NULL) || (note == NULL)) return -1;
774
	service->notes = ms_list_append(service->notes, linphone_presence_note_ref(note));
775 776 777 778 779 780 781 782 783 784 785 786
	return 0;
}

int linphone_presence_service_clear_notes(LinphonePresenceService *service) {
	if (service == NULL) return -1;

	ms_list_for_each(service->notes, (MSIterateFunc)linphone_presence_note_unref);
	ms_list_free(service->notes);
	service->notes = NULL;
	return 0;
}

787 788


789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
/*****************************************************************************
 * 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);
}

int linphone_presence_person_set_id(LinphonePresencePerson *person, const char *id) {
	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;
}

813
unsigned int linphone_presence_person_get_nb_activities(const LinphonePresencePerson *person) {
814 815 816 817 818
	if (person == NULL) return 0;
	return ms_list_size(person->activities);
}

LinphonePresenceActivity * linphone_presence_person_get_nth_activity(const LinphonePresencePerson *person, unsigned int idx) {
819
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_activities(person)))
820 821 822 823 824 825
		return NULL;
	return (LinphonePresenceActivity *)ms_list_nth_data(person->activities, idx);
}

int linphone_presence_person_add_activity(LinphonePresencePerson *person, LinphonePresenceActivity *activity) {
	if ((person == NULL) || (activity == NULL)) return -1;
826 827
	// insert in first position since its the most recent activity!
	person->activities = ms_list_prepend(person->activities, linphone_presence_activity_ref(activity));
828 829 830 831 832 833 834 835 836 837 838
	return 0;
}

int linphone_presence_person_clear_activities(LinphonePresencePerson *person) {
	if (person == NULL) return -1;
	ms_list_for_each(person->activities, (MSIterateFunc)linphone_presence_activity_unref);
	ms_list_free(person->activities);
	person->activities = NULL;
	return 0;
}

839
unsigned int linphone_presence_person_get_nb_notes(const LinphonePresencePerson *person) {
840 841 842 843 844
	if (person == NULL) return 0;
	return ms_list_size(person->notes);
}

LinphonePresenceNote * linphone_presence_person_get_nth_note(const LinphonePresencePerson *person, unsigned int idx) {
845
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_notes(person)))
846 847 848 849 850 851
		return NULL;
	return (LinphonePresenceNote *)ms_list_nth_data(person->notes, idx);
}

int linphone_presence_person_add_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
	if ((person == NULL) || (note == NULL)) return -1;
852
	person->notes = ms_list_append(person->notes, linphone_presence_note_ref(note));
853 854 855 856 857 858 859 860 861 862 863
	return 0;
}

int linphone_presence_person_clear_notes(LinphonePresencePerson *person) {
	if (person == NULL) return -1;
	ms_list_for_each(person->notes, (MSIterateFunc)linphone_presence_note_unref);
	ms_list_free(person->notes);
	person->notes = NULL;
	return 0;
}

864
unsigned int linphone_presence_person_get_nb_activities_notes(const LinphonePresencePerson *person) {
865 866 867 868 869
	if (person == NULL) return 0;
	return ms_list_size(person->activities_notes);
}

LinphonePresenceNote * linphone_presence_person_get_nth_activities_note(const LinphonePresencePerson *person, unsigned int idx) {
870
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_activities_notes(person)))
871 872 873 874 875 876
		return NULL;
	return (LinphonePresenceNote *)ms_list_nth_data(person->activities_notes, idx);
}

int linphone_presence_person_add_activities_note(LinphonePresencePerson *person, LinphonePresenceNote *note) {
	if ((person == NULL) || (note == NULL)) return -1;
877
	person->notes = ms_list_append(person->activities_notes, linphone_presence_note_ref(note));
878 879 880 881 882 883 884 885 886 887
	return 0;
}

int linphone_presence_person_clear_activities_notes(LinphonePresencePerson *person) {
	if (person == NULL) return -1;
	ms_list_for_each(person->activities_notes, (MSIterateFunc)linphone_presence_note_unref);
	ms_list_free(person->activities_notes);
	person->activities_notes = NULL;
	return 0;
}
888 889


890 891 892
/*****************************************************************************
 * PRESENCE ACTIVITY FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES          *
 ****************************************************************************/
893 894 895

struct _presence_activity_name_map {
	const char *name;
896
	LinphonePresenceActivityType type;
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
};

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 }
};

929
static int activity_name_to_presence_activity_type(const char *name, LinphonePresenceActivityType *acttype) {
930 931 932
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
		if (strcmp(name, activity_map[i].name) == 0) {
933
			*acttype = activity_map[i].type;
934 935 936 937 938 939
			return 0;
		}
	}
	return -1;
}

940
static const char * presence_activity_type_to_string(LinphonePresenceActivityType acttype) {
941 942
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
943
		if (acttype == activity_map[i].type) {
944 945 946 947 948 949
			return activity_map[i].name;
		}
	}
	return NULL;
}

950 951 952 953 954 955
LinphonePresenceActivity * linphone_presence_activity_new(LinphonePresenceActivityType acttype, const char *description) {
	LinphonePresenceActivity *act = ms_new0(LinphonePresenceActivity, 1);
	act->refcnt = 1;
	act->type = acttype;
	if (description != NULL) {
		act->description = ms_strdup(description);
Ghislain MARY's avatar
Ghislain MARY committed
956
	}
957
	return act;
Ghislain MARY's avatar
Ghislain MARY committed
958 959
}

960 961 962 963 964 965 966 967 968 969 970 971
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);
	const char *acttype_str;

	if (acttype == LinphonePresenceActivityOffline)
		acttype_str = "offline";
	else if (acttype == LinphonePresenceActivityOnline)
		acttype_str = "online";
	else
		acttype_str = presence_activity_type_to_string(acttype);

Ghislain MARY's avatar
Ghislain MARY committed
972 973 974
	return ms_strdup_printf("%s%s%s", acttype_str,
				(description == NULL) ? "" : ": ",
				(description == NULL) ? "" : description);
975 976 977 978 979 980 981 982
}

LinphonePresenceActivityType linphone_presence_activity_get_type(const LinphonePresenceActivity *activity) {
	if (activity == NULL)
		return LinphonePresenceActivityOffline;
	return activity->type;
}

983 984 985 986 987 988
int linphone_presence_activity_set_type(LinphonePresenceActivity *activity, LinphonePresenceActivityType acttype) {
	if (activity == NULL) return -1;
	activity->type = acttype;
	return 0;
}

989 990 991 992
const char * linphone_presence_activity_get_description(const LinphonePresenceActivity *activity) {
	if (activity == NULL)
		return NULL;
	return activity->description;
993 994
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005
int linphone_presence_activity_set_description(LinphonePresenceActivity *activity, const char *description) {
	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;
}

1006 1007 1008 1009 1010 1011


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

Ghislain MARY's avatar
Ghislain MARY committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
LinphonePresenceNote * linphone_presence_note_new(const char *content, const char *lang) {
	LinphonePresenceNote *note;

	if (content == NULL) return NULL;
	note = ms_new0(LinphonePresenceNote, 1);
	note->refcnt = 1;
	note->content = ms_strdup(content);
	if (lang != NULL) {
		note->lang = ms_strdup(lang);
	}
	return note;
}

1025 1026 1027 1028 1029 1030
const char * linphone_presence_note_get_content(const LinphonePresenceNote *note) {
	if (note == NULL)
		return NULL;
	return note->content;
}

Ghislain MARY's avatar
Ghislain MARY committed
1031 1032 1033 1034 1035 1036 1037 1038 1039
int linphone_presence_note_set_content(LinphonePresenceNote *note, const char *content) {
	if (content == NULL) return -1;
	if (note->content != NULL) {
		ms_free(note->content);
	}
	note->content = ms_strdup(content);
	return 0;
}

1040 1041 1042 1043 1044 1045
const char * linphone_presence_note_get_lang(const LinphonePresenceNote *note) {
	if (note == NULL)
		return NULL;
	return note->lang;
}

Ghislain MARY's avatar
Ghislain MARY committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
int linphone_presence_note_set_lang(LinphonePresenceNote *note, const char *lang) {
	if (note->lang != NULL) {
		ms_free(note->lang);
		note->lang = NULL;
	}
	if (lang != NULL) {
		note->lang = ms_strdup(lang);
	}
	return 0;
}

1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074