presence.c 66.1 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 {
Ghislain MARY's avatar
Ghislain MARY committed
70 71
	void *user_data;
	int refcnt;
72 73 74 75 76 77
	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. */
};


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


/*****************************************************************************
 * PRIVATE FUNCTIONS                                                         *
 ****************************************************************************/
jehan's avatar
jehan committed
84 85 86 87
/*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";
88 89 90 91

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

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

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

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

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

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

150
static void presence_activity_delete(LinphonePresenceActivity *activity) {
151 152 153 154 155 156 157 158 159
	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;
160
#if defined(LINPHONE_WINDOWS_UNIVERSAL) || defined(LINPHONE_MSC_VER_GREATER_19)
161 162 163 164
	long adjust_timezone;
#else
	time_t adjust_timezone;
#endif
165 166 167 168 169 170 171 172 173 174 175 176

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

185
char * linphone_timestamp_to_rfc3339_string(time_t timestamp) {
186 187
	char timestamp_str[22];
	struct tm *ret;
188
#ifndef _WIN32
189 190 191
	struct tm gmt;
	ret = gmtime_r(&timestamp,&gmt);
#else
192
	ret = gmtime(&timestamp);
193 194 195 196 197 198
#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);
}

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

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

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

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

233
static void presence_model_add_person(LinphonePresenceModel *model, LinphonePresencePerson *person) {
234 235 236
	model->persons = ms_list_append(model->persons, person);
}

237
static void presence_model_add_note(LinphonePresenceModel *model, LinphonePresenceNote *note) {
238 239 240
	model->notes = ms_list_append(model->notes, note);
}

241
static void presence_model_find_open_basic_status(LinphonePresenceService *service, LinphonePresenceBasicStatus *status) {
242 243 244 245 246
	if (service->status == LinphonePresenceBasicStatusOpen) {
		*status = LinphonePresenceBasicStatusOpen;
	}
}

247
static void presence_model_delete(LinphonePresenceModel *model) {
248 249
	if (model == NULL) return;

250
	ms_list_for_each(model->services, (MSIterateFunc)linphone_presence_service_unref);
251
	ms_list_free(model->services);
252
	ms_list_for_each(model->persons, (MSIterateFunc)linphone_presence_person_unref);
253
	ms_list_free(model->persons);
254
	ms_list_for_each(model->notes, (MSIterateFunc)linphone_presence_note_unref);
255 256 257 258
	ms_list_free(model->notes);
	ms_free(model);
}

Ghislain MARY's avatar
Ghislain MARY committed
259

260 261 262 263 264 265 266 267 268

/*****************************************************************************
 * 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
269 270 271 272
	}
	return model;
}

273 274 275 276 277 278 279
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
280 281
}

282 283 284
/* 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;
285 286 287
	if (model != NULL) {
		ms_list_for_each2(model->services, (MSIterate2Func)presence_model_find_open_basic_status, &status);
	}
288 289 290
	return status;
}

291
int linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status) {
292
	LinphonePresenceService *service;
293
	int err = 0;
294 295 296

	if (model == NULL) return -1;

297
	linphone_presence_model_clear_services(model);
298
	service = linphone_presence_service_new(NULL, basic_status, NULL);
299 300
	if (service == NULL) return -1;

301 302 303
	err = linphone_presence_model_add_service(model, service);
	linphone_presence_service_unref(service);
	return err;
304 305
}

306
static void presence_service_find_newer_timestamp(LinphonePresenceService *service, time_t *timestamp) {
307 308 309 310
	if (service->timestamp > *timestamp)
		*timestamp = service->timestamp;
}

311
static void presence_person_find_newer_timestamp(LinphonePresencePerson *person, time_t *timestamp) {
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	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;
}

328
static void presence_model_find_contact(LinphonePresenceService *service, char **contact) {
329 330 331 332 333 334 335 336 337 338 339
	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);
}

340 341 342 343 344 345 346
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) {
347
		service = linphone_presence_service_new(NULL, LinphonePresenceBasicStatusClosed, NULL);
348 349
		if (service == NULL) return -1;
		linphone_presence_model_add_service(model, service);
350
	}
351 352 353 354
	return linphone_presence_service_set_contact(service, contact);
}

static void presence_model_count_activities(const LinphonePresencePerson *person, unsigned int *nb) {
355 356 357 358 359 360
	*nb += ms_list_size(person->activities);
}

struct _get_activity_st {
	unsigned int requested_idx;
	unsigned int current_idx;
361
	LinphonePresenceActivity *activity;
362 363
};

364
static void presence_model_get_activity(const LinphonePresencePerson *person, struct _get_activity_st *st) {
365 366
	unsigned int size = ms_list_size(person->activities);
	if (st->requested_idx < (st->current_idx + size)) {
367
		st->activity = (LinphonePresenceActivity *)ms_list_nth_data(person->activities, st->requested_idx - st->current_idx);
368 369 370 371 372
	} else {
		st->current_idx += size;
	}
}

373 374
LinphonePresenceActivity * linphone_presence_model_get_activity(const LinphonePresenceModel *model) {
	return linphone_presence_model_get_nth_activity(model, 0);
375 376
}

377
int linphone_presence_model_set_activity(LinphonePresenceModel *model, LinphonePresenceActivityType acttype, const char *description) {
378
	LinphonePresenceBasicStatus basic_status = LinphonePresenceBasicStatusOpen;
379
	LinphonePresenceActivity *activity;
380
	int err = 0;
381 382 383

	if (model == NULL) return -1;

384
	switch (acttype) {
385 386 387 388 389 390 391 392 393 394 395 396
		case LinphonePresenceActivityAppointment:
		case LinphonePresenceActivityBusy:
		case LinphonePresenceActivityMeeting:
		case LinphonePresenceActivityPermanentAbsence:
		case LinphonePresenceActivityOffline:
		case LinphonePresenceActivityWorship:
			basic_status = LinphonePresenceBasicStatusClosed;
			break;
		default:
			basic_status = LinphonePresenceBasicStatusOpen;
			break;
	}
397
	if (linphone_presence_model_set_basic_status(model, basic_status) < 0) return -1;
398
	linphone_presence_model_clear_activities(model);
399 400
	activity = linphone_presence_activity_new(acttype, description);
	if (activity == NULL) return -1;
401 402 403
	err = linphone_presence_model_add_activity(model, activity);
	linphone_presence_activity_unref(activity);
	return err;
404 405
}

406
unsigned int linphone_presence_model_get_nb_activities(const LinphonePresenceModel *model) {
407 408 409 410 411 412 413 414
	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;

415
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_activities(model)))
416 417 418 419 420 421 422 423 424
		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;
}

425
int linphone_presence_model_add_activity(LinphonePresenceModel *model, LinphonePresenceActivity *activity) {
426
	char *id = NULL;
427
	LinphonePresencePerson *person = NULL;
428

429
	if ((model == NULL) || (activity == NULL)) return -1;
430 431 432 433 434 435 436 437 438 439 440 441

	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. */
442
		person = (LinphonePresencePerson *)ms_list_nth_data(model->persons, 0);
443 444
	}

445
	linphone_presence_person_add_activity(person, activity);
446 447 448 449 450 451
	return 0;
}

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

452
	ms_list_for_each(model->persons, (MSIterateFunc)linphone_presence_person_clear_activities);
453 454 455
	return 0;
}

456 457
struct _find_note_st {
	const char *lang;
458
	LinphonePresenceNote *note;
459 460
};

461
static LinphonePresenceNote * find_presence_note_in_list(MSList *list, const char *lang) {
462 463 464 465 466
	int nb;
	int i;

	nb = ms_list_size(list);
	for (i = 0; i < nb; i++) {
467
		LinphonePresenceNote *note = (LinphonePresenceNote *)ms_list_nth_data(list, i);
468 469 470 471 472 473 474 475 476 477 478
		if (lang == NULL) {
			if (note->lang == NULL) {
				return note;
			}
		} else {
			if ((note->lang != NULL) && (strcmp(lang, note->lang) == 0)) {
				return note;
			}
		}
	}

479 480 481
	return NULL;
}

482
static void find_presence_person_note(LinphonePresencePerson *person, struct _find_note_st *st) {
483 484 485 486 487 488 489 490
	/* 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);
}

491
static void find_presence_service_note(LinphonePresenceService *service, struct _find_note_st *st) {
492 493 494
	st->note = find_presence_note_in_list(service->notes, st->lang);
}

495 496
static LinphonePresenceNote * get_first_presence_note_in_list(MSList *list) {
	return (LinphonePresenceNote *)ms_list_nth_data(list, 0);
497 498
}

499
static void get_first_presence_person_note(LinphonePresencePerson *person, struct _find_note_st *st) {
500 501 502 503 504
	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);
}

505
static void get_first_presence_service_note(LinphonePresenceService *service, struct _find_note_st *st) {
506 507 508
	st->note = get_first_presence_note_in_list(service->notes);
}

509
LinphonePresenceNote * linphone_presence_model_get_note(const LinphonePresenceModel *model, const char *lang) {
510 511 512 513 514 515 516 517 518 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
	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);
		}
	}

550
	return st.note;
551 552 553
}

int linphone_presence_model_add_note(LinphonePresenceModel *model, const char *note_content, const char *lang) {
554 555
	LinphonePresenceService *service;
	LinphonePresenceNote *note;
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

	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) {
572
		note = linphone_presence_note_new(note_content, lang);
573
	} else {
574
		linphone_presence_note_set_content(note, note_content);
575 576 577 578 579 580 581 582 583
	}
	if (note == NULL)
		return -1;

	presence_service_add_note(service, note);

	return 0;
}

584
static void clear_presence_person_notes(LinphonePresencePerson *person) {
585
	ms_list_for_each(person->activities_notes, (MSIterateFunc)linphone_presence_note_unref);
586 587
	ms_list_free(person->activities_notes);
	person->activities_notes = NULL;
588
	ms_list_for_each(person->notes, (MSIterateFunc)linphone_presence_note_unref);
589 590 591 592
	ms_list_free(person->notes);
	person->notes = NULL;
}

593
static void clear_presence_service_notes(LinphonePresenceService *service) {
594
	ms_list_for_each(service->notes, (MSIterateFunc)linphone_presence_note_unref);
595 596 597 598 599 600 601 602 603 604
	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);
605
	ms_list_for_each(model->notes, (MSIterateFunc)linphone_presence_note_unref);
606 607 608 609
	ms_list_free(model->notes);
	model->notes = NULL;

	return 0;
610 611
}

612 613 614 615 616 617 618 619
/*****************************************************************************
 * 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;
620 621
}

622
unsigned int linphone_presence_model_get_nb_services(const LinphonePresenceModel *model) {
623 624
	return ms_list_size(model->services);
}
625

626
LinphonePresenceService * linphone_presence_model_get_nth_service(const LinphonePresenceModel *model, unsigned int idx) {
627
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_services(model)))
628
		return NULL;
629

630
	return (LinphonePresenceService *)ms_list_nth_data(model->services, idx);
631 632
}

633 634
int linphone_presence_model_add_service(LinphonePresenceModel *model, LinphonePresenceService *service) {
	if ((model == NULL) || (service == NULL)) return -1;
635
	model->services = ms_list_append(model->services, linphone_presence_service_ref(service));
636
	return 0;
637 638
}

639 640 641 642 643 644 645
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;
646 647
}

648
unsigned int linphone_presence_model_get_nb_persons(const LinphonePresenceModel *model) {
649 650 651 652
	return ms_list_size(model->persons);
}

LinphonePresencePerson * linphone_presence_model_get_nth_person(const LinphonePresenceModel *model, unsigned int idx) {
653
	if ((model == NULL) || (idx >= linphone_presence_model_get_nb_persons(model)))
654 655 656 657 658 659 660
		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;
661
	model->persons = ms_list_append(model->persons, linphone_presence_person_ref(person));
662 663 664 665 666 667 668 669 670 671 672 673
	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;
}

674

675

676 677 678
/*****************************************************************************
 * PRESENCE SERVICE FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES           *
 ****************************************************************************/
679

680
LinphonePresenceService * linphone_presence_service_new(const char *id, LinphonePresenceBasicStatus basic_status, const char *contact) {
681
	LinphonePresenceService *service;
682 683 684 685 686
	char *service_id;
	if (id == NULL)
		service_id = generate_presence_id();
	else
		service_id = ms_strdup(id);
687 688
	service = presence_service_new(service_id, basic_status);
	linphone_presence_service_set_contact(service, contact);
689 690
	if (service_id != NULL)
		ms_free(service_id);
691 692
	return service;
}
693

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
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;
}

710 711 712
LinphonePresenceBasicStatus linphone_presence_service_get_basic_status(const LinphonePresenceService *service) {
	if (service == NULL) return LinphonePresenceBasicStatusClosed;
	return service->status;
713 714
}

715 716 717 718 719
int linphone_presence_service_set_basic_status(LinphonePresenceService *service, LinphonePresenceBasicStatus basic_status) {
	if (service == NULL) return -1;
	service->status = basic_status;
	return 0;
}
720

721 722 723 724
char * linphone_presence_service_get_contact(const LinphonePresenceService *service) {
	if (service->contact == NULL) return NULL;
	return ms_strdup(service->contact);
}
725

726
int linphone_presence_service_set_contact(LinphonePresenceService *service, const char *contact) {
727
	if (service == NULL) return -1;
728 729 730 731 732 733
	if (service->contact != NULL)
		ms_free(service->contact);
	if (contact != NULL)
		service->contact = ms_strdup(contact);
	else
		service->contact = NULL;
734 735 736
	return 0;
}

737
unsigned int linphone_presence_service_get_nb_notes(const LinphonePresenceService *service) {
738 739 740 741
	return ms_list_size(service->notes);
}

LinphonePresenceNote * linphone_presence_service_get_nth_note(const LinphonePresenceService *service, unsigned int idx) {
742
	if ((service == NULL) || (idx >= linphone_presence_service_get_nb_notes(service)))
743 744 745 746 747
		return NULL;

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

Ghislain MARY's avatar
Ghislain MARY committed
748
int linphone_presence_service_add_note(LinphonePresenceService *service, LinphonePresenceNote *note) {
749
	if ((service == NULL) || (note == NULL)) return -1;
750
	service->notes = ms_list_append(service->notes, linphone_presence_note_ref(note));
751 752 753 754 755 756 757 758 759 760 761 762
	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;
}

763 764


765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
/*****************************************************************************
 * 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;
}

789
unsigned int linphone_presence_person_get_nb_activities(const LinphonePresencePerson *person) {
790 791 792 793 794
	if (person == NULL) return 0;
	return ms_list_size(person->activities);
}

LinphonePresenceActivity * linphone_presence_person_get_nth_activity(const LinphonePresencePerson *person, unsigned int idx) {
795
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_activities(person)))
796 797 798 799 800 801
		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;
802
	person->activities = ms_list_append(person->activities, linphone_presence_activity_ref(activity));
803 804 805 806 807 808 809 810 811 812 813
	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;
}

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

LinphonePresenceNote * linphone_presence_person_get_nth_note(const LinphonePresencePerson *person, unsigned int idx) {
820
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_notes(person)))
821 822 823 824 825 826
		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;
827
	person->notes = ms_list_append(person->notes, linphone_presence_note_ref(note));
828 829 830 831 832 833 834 835 836 837 838
	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;
}

839
unsigned int linphone_presence_person_get_nb_activities_notes(const LinphonePresencePerson *person) {
840 841 842 843 844
	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) {
845
	if ((person == NULL) || (idx >= linphone_presence_person_get_nb_activities_notes(person)))
846 847 848 849 850 851
		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;
852
	person->notes = ms_list_append(person->activities_notes, linphone_presence_note_ref(note));
853 854 855 856 857 858 859 860 861 862
	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;
}
863 864


865 866 867
/*****************************************************************************
 * PRESENCE ACTIVITY FUNCTIONS TO GET ACCESS TO ALL FUNCTIONALITIES          *
 ****************************************************************************/
868 869 870

struct _presence_activity_name_map {
	const char *name;
871
	LinphonePresenceActivityType type;
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
};

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

904
static int activity_name_to_presence_activity_type(const char *name, LinphonePresenceActivityType *acttype) {
905 906 907
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
		if (strcmp(name, activity_map[i].name) == 0) {
908
			*acttype = activity_map[i].type;
909 910 911 912 913 914
			return 0;
		}
	}
	return -1;
}

915
static const char * presence_activity_type_to_string(LinphonePresenceActivityType acttype) {
916 917
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
918
		if (acttype == activity_map[i].type) {
919 920 921 922 923 924
			return activity_map[i].name;
		}
	}
	return NULL;
}

925 926 927 928 929 930
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
931
	}
932
	return act;
Ghislain MARY's avatar
Ghislain MARY committed
933 934
}

935 936 937 938 939 940 941 942 943 944 945 946
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
947 948 949
	return ms_strdup_printf("%s%s%s", acttype_str,
				(description == NULL) ? "" : ": ",
				(description == NULL) ? "" : description);
950 951 952 953 954 955 956 957
}

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

958 959 960 961 962 963
int linphone_presence_activity_set_type(LinphonePresenceActivity *activity, LinphonePresenceActivityType acttype) {
	if (activity == NULL) return -1;
	activity->type = acttype;
	return 0;
}

964 965 966 967
const char * linphone_presence_activity_get_description(const LinphonePresenceActivity *activity) {
	if (activity == NULL)
		return NULL;
	return activity->description;
968 969
}

970 971 972 973 974 975 976 977 978 979 980
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;
}

981 982 983 984 985 986


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

987 988 989 990 991 992 993 994 995 996 997 998 999
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;
}

1000 1001 1002 1003 1004 1005
const char * linphone_presence_note_get_content(const LinphonePresenceNote *note) {
	if (note == NULL)
		return NULL;
	return note->content;
}

1006 1007 1008 1009 1010 1011 1012 1013 1014
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;
}

1015 1016 1017 1018 1019 1020
const char * linphone_presence_note_get_lang(const LinphonePresenceNote *note) {
	if (note == NULL)
		return NULL;
	return note->lang;
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
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;
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055


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

LinphonePresenceModel * linphone_presence_model_ref(LinphonePresenceModel *model) {
	model->refcnt++;
	return model;
}

LinphonePresenceModel * linphone_presence_model_unref(LinphonePresenceModel *model) {
	model->refcnt--;
	if (model->refcnt == 0) {
		presence_model_delete(model);
		return NULL;
	}
	return model;
}

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

1056
void * linphone_presence_model_get_user_data(const LinphonePresenceModel *model) {
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
	return model->user_data;
}

LinphonePresenceService * linphone_presence_service_ref(LinphonePresenceService *service) {
	service->refcnt++;
	return service;
}

LinphonePresenceService * linphone_presence_service_unref(LinphonePresenceService *service) {
	service->refcnt--;
	if (service->refcnt == 0) {
		presence_service_delete(service);
		return NULL;
	}
	return service;
}

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

1078
void * linphone_presence_service_get_user_data(const LinphonePresenceService *service) {
1079 1080 1081
	return service->user_data;
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
LinphonePresencePerson * linphone_presence_person_ref(LinphonePresencePerson *person) {
	person->refcnt++;
	return person;
}

LinphonePresencePerson * linphone_presence_person_unref(LinphonePresencePerson *person) {
	person->refcnt--;
	if (person->refcnt == 0) {
		presence_person_delete(person);
		return NULL;
	}
	return person;
}

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

1100
void * linphone_presence_person_get_user_data(const LinphonePresencePerson *person) {
1101 1102 1103
	return person->user_data;
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
LinphonePresenceActivity * linphone_presence_activity_ref(LinphonePresenceActivity *activity) {
	activity->refcnt++;
	return activity;
}

LinphonePresenceActivity * linphone_presence_activity_unref(LinphonePresenceActivity *activity) {
	activity->refcnt--;
	if (activity->refcnt == 0) {
		presence_activity_delete(activity);
		return NULL;
	}
	return activity;
}

void linphone_presence_activity_set_user_data(LinphonePresenceActivity *activity, void *user_data) {
	activity->user_data = user_data;
}

1122
void * linphone_presence_activity_get_user_data(const LinphonePresenceActivity *activity) {
1123 1124 1125
	return activity->user_data;
}

Ghislain MARY's avatar
Ghislain MARY committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
LinphonePresenceNote * linphone_presence_note_ref(LinphonePresenceNote *note) {
	note->refcnt++;
	return note;
}

LinphonePresenceNote * linphone_presence_note_unref(LinphonePresenceNote *note) {
	note->refcnt--;
	if (note->refcnt == 0) {
		presence_note_delete(note);
		return NULL;
	}
	return note;
}

void linphone_presence_note_set_user_data(LinphonePresenceNote *note, void *user_data) {
	note->user_data = user_data;
}

1144
void * linphone_presence_note_get_user_data(const LinphonePresenceNote *note) {
Ghislain MARY's avatar
Ghislain MARY committed
1145 1146 1147
	return note->user_data;
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164


/*****************************************************************************
 * XML PRESENCE INTERNAL HANDLING                                            *
 ****************************************************************************/

static const char *service_prefix = "/pidf:presence/pidf:tuple";

static int process_pidf_xml_presence_service_notes(xmlparsing_context_t *xml_ctx, LinphonePresenceService *service, unsigned int service_idx) {
	char xpath_str[MAX_XPATH_LENGTH];
	xmlXPathObjectPtr note_object;
	LinphonePresenceNote *note;
	const char *note_str;
	const char *lang;
	int i;

	snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:note", service_prefix, service_idx);
1165
	note_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, xpath_str);
1166 1167 1168
	if ((note_object != NULL) && (note_object->nodesetval != NULL)) {
		for (i = 1; i <= note_object->nodesetval->nodeNr; i++) {
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:note[%i]", service_prefix, service_idx, i);
1169
			note_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1170 1171
			if (note_str == NULL) continue;
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:note[%i]/@xml:lang", service_prefix, service_idx, i);
1172
			lang = linphone_get_xml_text_content(xml_ctx, xpath_str);
1173

1174
			note = linphone_presence_note_new(note_str, lang);
1175
			presence_service_add_note(service, note);
1176 1177
			if (lang != NULL) linphone_free_xml_text_content(lang);
			linphone_free_xml_text_content(note_str);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
		}
	}
	if (note_object != NULL) xmlXPathFreeObject(note_object);

	return 0;
}

static int process_pidf_xml_presence_services(xmlparsing_context_t *xml_ctx, LinphonePresenceModel *model) {
	char xpath_str[MAX_XPATH_LENGTH];
	xmlXPathObjectPtr service_object;
	LinphonePresenceService *service;
	const char *basic_status_str;
	const char *service_id_str;
	const char *timestamp_str;
	const char *contact_str;
	LinphonePresenceBasicStatus basic_status;
	int i;

1196
	service_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, service_prefix);
1197 1198 1199
	if ((service_object != NULL) && (service_object->nodesetval != NULL)) {
		for (i = 1; i <= service_object->nodesetval->nodeNr; i++) {
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:status/pidf:basic", service_prefix, i);
1200
			basic_status_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1201 1202 1203 1204 1205 1206 1207 1208 1209
			if (basic_status_str == NULL)
				continue;

			if (strcmp(basic_status_str, "open") == 0) {
				basic_status = LinphonePresenceBasicStatusOpen;
			} else if (strcmp(basic_status_str, "closed") == 0) {
				basic_status = LinphonePresenceBasicStatusClosed;
			} else {
				/* Invalid value for basic status. */
1210
				linphone_free_xml_text_content(basic_status_str);
1211 1212 1213 1214
				return -1;
			}

			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:timestamp", service_prefix, i);
1215
			timestamp_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1216 1217

			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:contact", service_prefix, i);
1218
			contact_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1219 1220

			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/@id", service_prefix, i);
1221
			service_id_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1222 1223
			service = presence_service_new(service_id_str, basic_status);
			if (service != NULL) {
1224 1225
				if (timestamp_str != NULL) presence_service_set_timestamp(service, parse_timestamp(timestamp_str));
				if (contact_str != NULL) linphone_presence_service_set_contact(service, contact_str);
1226 1227 1228
				process_pidf_xml_presence_service_notes(xml_ctx, service, i);
				linphone_presence_model_add_service(model, service);
			}
1229 1230
			if (timestamp_str != NULL) linphone_free_xml_text_content(timestamp_str);
			if (contact_str != NULL) linphone_free_xml_text_content(contact_str);
1231
			if (service_id_str != NULL) linphone_free_xml_text_content(service_id_str);
1232
			linphone_free_xml_text_content(basic_status_str);
1233 1234 1235 1236 1237
		}
	}
	if (service_object != NULL) xmlXPathFreeObject(service_object);

	return 0;
1238 1239
}

1240 1241 1242 1243 1244 1245 1246 1247 1248
static bool_t is_valid_activity_name(const char *name) {
	unsigned int i;
	for (i = 0; i < (sizeof(activity_map) / sizeof(activity_map[0])); i++) {
		if (strcmp(name, activity_map[i].name) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}
1249

1250
static int process_pidf_xml_presence_person_activities(xmlparsing_context_t *xml_ctx, LinphonePresencePerson *person, unsigned int person_idx) {
1251 1252 1253 1254
	char xpath_str[MAX_XPATH_LENGTH];
	xmlXPathObjectPtr activities_nodes_object;
	xmlXPathObjectPtr activities_object;
	xmlNodePtr activity_node;
1255
	LinphonePresenceActivity *activity;
1256 1257 1258 1259 1260
	const char *description;
	int i, j;
	int err = 0;

	snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/rpid:activities", person_prefix, person_idx);
1261
	activities_nodes_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, xpath_str);
1262 1263
	if ((activities_nodes_object != NULL) && (activities_nodes_object->nodesetval != NULL)) {
		for (i = 1; i <= activities_nodes_object->nodesetval->nodeNr; i++) {
1264
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/rpid:activities[%i]/rpid:*", person_prefix, person_idx, i);
1265
			activities_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, xpath_str);
1266 1267 1268
			if ((activities_object != NULL) && (activities_object->nodesetval != NULL)) {
				for (j = 0; j < activities_object->nodesetval->nodeNr; j++) {
					activity_node = activities_object->nodesetval->nodeTab[j];
1269
					if ((activity_node->name != NULL) && (is_valid_activity_name((const char *)activity_node->name) == TRUE)) {
1270
						LinphonePresenceActivityType acttype;
1271 1272
						description = (const char *)xmlNodeGetContent(activity_node);
						if ((description != NULL) && (description[0] == '\0')) {
1273
							linphone_free_xml_text_content(description);
1274 1275
							description = NULL;
						}
1276
						err = activity_name_to_presence_activity_type((const char *)activity_node->name, &acttype);
1277
						if (err < 0) break;
1278
						activity = linphone_presence_activity_new(acttype, description);
1279
						linphone_presence_person_add_activity(person, activity);
1280
						if (description != NULL) linphone_free_xml_text_content(description);
1281 1282 1283
					}
				}
			}
Ghislain MARY's avatar
Ghislain MARY committed
1284
			if (activities_object != NULL) xmlXPathFreeObject(activities_object);
1285 1286 1287 1288 1289 1290 1291 1292
			if (err < 0) break;
		}
	}
	if (activities_nodes_object != NULL) xmlXPathFreeObject(activities_nodes_object);

	return err;
}

1293
static int process_pidf_xml_presence_person_notes(xmlparsing_context_t *xml_ctx, LinphonePresencePerson *person, unsigned int person_idx) {
1294 1295
	char xpath_str[MAX_XPATH_LENGTH];
	xmlXPathObjectPtr note_object;
1296
	LinphonePresenceNote *note;
1297 1298 1299 1300 1301
	const char *note_str;
	const char *lang;
	int i;

	snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/rpid:activities/rpid:note", person_prefix, person_idx);
1302
	note_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, xpath_str);
1303 1304 1305
	if ((note_object != NULL) && (note_object->nodesetval != NULL)) {
		for (i = 1; i <= note_object->nodesetval->nodeNr; i++) {
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/rpid:activities/rpid:note[%i]", person_prefix, person_idx, i);
1306
			note_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1307 1308
			if (note_str == NULL) continue;
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/rpid:activities/rpid:note[%i]/@xml:lang", person_prefix, person_idx, i);
1309
			lang = linphone_get_xml_text_content(xml_ctx, xpath_str);
1310

1311
			note = linphone_presence_note_new(note_str, lang);
1312
			presence_person_add_activities_note(person, note);
1313 1314
			if (lang != NULL) linphone_free_xml_text_content(lang);
			linphone_free_xml_text_content(note_str);
1315 1316 1317 1318
		}
	}
	if (note_object != NULL) xmlXPathFreeObject(note_object);

1319
	snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/dm:note", person_prefix, person_idx);
1320
	note_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, xpath_str);
1321 1322
	if ((note_object != NULL) && (note_object->nodesetval != NULL)) {
		for (i = 1; i <= note_object->nodesetval->nodeNr; i++) {
1323
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/dm:note[%i]", person_prefix, person_idx, i);
1324
			note_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1325
			if (note_str == NULL) continue;
1326
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/dm:note[%i]/@xml:lang", person_prefix, person_idx, i);
1327
			lang = linphone_get_xml_text_content(xml_ctx, xpath_str);
1328

1329
			note = linphone_presence_note_new(note_str, lang);
1330
			presence_person_add_note(person, note);
1331 1332
			if (lang != NULL) linphone_free_xml_text_content(lang);
			linphone_free_xml_text_content(note_str);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
		}
	}
	if (note_object != NULL) xmlXPathFreeObject(note_object);

	return 0;
}

static int process_pidf_xml_presence_persons(xmlparsing_context_t *xml_ctx, LinphonePresenceModel *model) {
	char xpath_str[MAX_XPATH_LENGTH];
	xmlXPathObjectPtr person_object;
1343
	LinphonePresencePerson *person;
1344 1345
	const char *person_id_str;
	const char *person_timestamp_str;
1346
	time_t timestamp;
1347 1348 1349
	int i;
	int err = 0;

1350
	person_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, person_prefix);
1351 1352 1353
	if ((person_object != NULL) && (person_object->nodesetval != NULL)) {
		for (i = 1; i <= person_object->nodesetval->nodeNr; i++) {
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/@id", person_prefix, i);
1354
			person_id_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1355
			snprintf(xpath_str, sizeof(xpath_str), "%s[%i]/pidf:timestamp", person_prefix, i);
1356
			person_timestamp_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1357 1358 1359 1360 1361
			if (person_timestamp_str == NULL)
				timestamp = time(NULL);
			else
				timestamp = parse_timestamp(person_timestamp_str);
			person = presence_person_new(person_id_str, timestamp);
1362 1363 1364 1365 1366 1367 1368 1369
			if (person != NULL) {
				err = process_pidf_xml_presence_person_activities(xml_ctx, person, i);
				if (err == 0) {
					err = process_pidf_xml_presence_person_notes(xml_ctx, person, i);
				}
				if (err == 0) {
					presence_model_add_person(model, person);
				} else {
1370
					linphone_presence_person_unref(person);
1371 1372 1373
					break;
				}
			}
1374 1375
			if (person_id_str != NULL) linphone_free_xml_text_content(person_id_str);
			if (person_timestamp_str != NULL) linphone_free_xml_text_content(person_timestamp_str);
1376 1377 1378 1379 1380 1381
		}
	}
	if (person_object != NULL) xmlXPathFreeObject(person_object);

	if (err < 0) {
		/* Remove all the persons added since there was an error. */
1382
		ms_list_for_each(model->persons, (MSIterateFunc)linphone_presence_person_unref);
1383 1384 1385 1386 1387 1388 1389
	}
	return err;
}

static int process_pidf_xml_presence_notes(xmlparsing_context_t *xml_ctx, LinphonePresenceModel *model) {
	char xpath_str[MAX_XPATH_LENGTH];
	xmlXPathObjectPtr note_object;
1390
	LinphonePresenceNote *note;
1391 1392 1393 1394
	const char *note_str;
	const char *lang;
	int i;

1395
	note_object = linphone_get_xml_xpath_object_for_node_list(xml_ctx, "/pidf:presence/pidf:note");
1396 1397
	if ((note_object != NULL) && (note_object->nodesetval != NULL)) {
		for (i = 1; i <= note_object->nodesetval->nodeNr; i++) {
1398
			snprintf(xpath_str, sizeof(xpath_str), "/pidf:presence/pidf:note[%i]", i);
1399
			note_str = linphone_get_xml_text_content(xml_ctx, xpath_str);
1400
			if (note_str == NULL) continue;
1401
			snprintf(xpath_str, sizeof(xpath_str), "/pidf:presence/pidf:note[%i]/@xml:lang", i);
1402
			lang = linphone_get_xml_text_content(xml_ctx, xpath_str);
1403

1404
			note = linphone_presence_note_new(note_str, lang);
1405
			presence_model_add_note(model, note);
1406 1407
			if (lang != NULL) linphone_free_xml_text_content(lang);
			linphone_free_xml_text_content(note_str);
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		}
	}
	if (note_object != NULL) xmlXPathFreeObject(note_object);

	return 0;
}

static LinphonePresenceModel * process_pidf_xml_presence_notification(xmlparsing_context_t *xml_ctx) {
	LinphonePresenceModel *model = NULL;
	int err;

1419
	if (linphone_create_xml_xpath_context(xml_ctx) < 0)
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
		return NULL;

	model = linphone_presence_model_new();
	xmlXPathRegisterNs(xml_ctx->xpath_ctx, (const xmlChar *)"pidf", (const xmlChar *)"urn:ietf:params:xml:ns:pidf");
	xmlXPathRegisterNs(xml_ctx->xpath_ctx, (const xmlChar *)"dm", (const xmlChar *)"urn:ietf:params:xml:ns:pidf:data-model");
	xmlXPathRegisterNs(xml_ctx->xpath_ctx, (const xmlChar *)"rpid", (const xmlChar *)"urn:ietf:params:xml:ns:pidf:rpid");
	err = process_pidf_xml_presence_services(xml_ctx, model);
	if (err == 0) {
		err = process_pidf_xml_presence_persons(xml_ctx, model);
	}
	if (err == 0) {
		err = process_pidf_xml_presence_notes(xml_ctx, model);
	}

	if (err < 0) {
1435
		linphone_presence_model_unref(model);
1436 1437 1438 1439 1440 1441 1442 1443 1444
		model = NULL;
	}

	return model;
}




1445
void linphone_core_add_subscriber(LinphoneCore *lc, const char *subscriber, SalOp *op){
1446
	LinphoneFriend *fl=linphone_friend_new_with_address(subscriber);
1447 1448
	char *tmp;
	
aymeric's avatar
aymeric committed
1449
	if (fl==NULL) return ;
1450
	fl->lc = lc;
1451
	linphone_friend_add_incoming_subscription(fl, op);
aymeric's avatar
aymeric committed
1452 1453
	linphone_friend_set_inc_subscribe_policy(fl,LinphoneSPAccept);
	fl->inc_subscribe_pending=TRUE;