diff --git a/coreapi/linphonepresence.h b/coreapi/linphonepresence.h index f275d59d4..d4cf6cfd0 100644 --- a/coreapi/linphonepresence.h +++ b/coreapi/linphonepresence.h @@ -243,6 +243,14 @@ void * linphone_presence_model_get_user_data(LinphonePresenceModel *model); */ LINPHONE_PUBLIC LinphonePresenceBasicStatus linphone_presence_model_get_basic_status(const LinphonePresenceModel *model); +/** + * @brief Sets the basic status of a presence model. + * @param[in] model The #LinphonePresenceModel object for which to set the basic status. + * @param[in] basic_status The #LinphonePresenceBasicStatus to set for the #LinphonePresenceModel object. + * @return 0 if successful, a value < 0 in case of error. + */ +LINPHONE_PUBLIC int linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status); + /** * @brief Gets the timestamp of a presence model. * @param[in] model The #LinphonePresenceModel object to get the timestamp from. @@ -294,9 +302,29 @@ LINPHONE_PUBLIC LinphonePresenceActivity * linphone_presence_model_get_activity( * @param[in] activity The #LinphonePresenceActivityType to set for the model. * @param[in] description An additional description of the activity to set for the model. Can be NULL if no additional description is to be added. * @return 0 if successful, a value < 0 in case of error. + * + * WARNING: This function will modify the basic status of the model according to the activity being set. + * If you don't want the basic status to be modified automatically, you can use the combination of linphone_presence_model_set_basic_status(), + * linphone_presence_model_clear_activities() and linphone_presence_model_add_activity(). */ LINPHONE_PUBLIC int linphone_presence_model_set_activity(LinphonePresenceModel *model, LinphonePresenceActivityType activity, const char *description); +/** + * @brief Adds an activity to a presence model. + * @param[in] model The #LinphonePresenceModel object for which to add an activity. + * @param[in] activity The #LinphonePresenceActivityType to add to the model. + * @param[in] description An additional description of the activity to add to the model. Can be NULL if no additional description is to be added. + * @return 0 if successful, a value < 0 in case of error. + */ +LINPHONE_PUBLIC int linphone_presence_model_add_activity(LinphonePresenceModel *model, LinphonePresenceActivityType activity, const char *description); + +/** + * @brief Clears the activities of a presence model. + * @param[in] model The #LinphonePresenceModel object for which to clear the activities. + * @return 0 if successful, a value < 0 in case of error. + */ +LINPHONE_PUBLIC int linphone_presence_model_clear_activities(LinphonePresenceModel *model); + /** * @brief Gets the first note of a presence model (there is usually only one). * @param[in] model The #LinphonePresenceModel object to get the note from. diff --git a/coreapi/presence.c b/coreapi/presence.c index 9f85d8af4..becf3afaf 100644 --- a/coreapi/presence.c +++ b/coreapi/presence.c @@ -306,51 +306,6 @@ static void presence_model_add_note(LinphonePresenceModel *model, struct _Linpho model->notes = ms_list_append(model->notes, note); } -static int presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status) { - struct _LinphonePresenceService *service; - char *id; - if (ms_list_size(model->services) > 0) { - ms_list_for_each(model->services, (MSIterateFunc)presence_service_delete); - ms_list_free(model->services); - model->services = NULL; - } - id = generate_presence_id(); - service = presence_service_new(id, basic_status); - ms_free(id); - if (service == NULL) return -1; - presence_model_add_service(model, service); - return 0; -} - -static void presence_model_clear_activities(LinphonePresenceModel *model) { - ms_list_for_each(model->persons, (MSIterateFunc)presence_person_clear_activities); -} - -static int presence_model_add_activity(LinphonePresenceModel *model, LinphonePresenceActivityType acttype, const char *description) { - char *id = NULL; - struct _LinphonePresencePerson *person = NULL; - struct _LinphonePresenceActivity *act = NULL; - - 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. */ - person = (struct _LinphonePresencePerson *)ms_list_nth_data(model->persons, 0); - } - act = presence_activity_new(acttype, description); - if (act == NULL) - return -1; - presence_person_add_activity(person, act); - - return 0; -} - static void presence_model_find_open_basic_status(struct _LinphonePresenceService *service, LinphonePresenceBasicStatus *status) { if (service->status == LinphonePresenceBasicStatusOpen) { *status = LinphonePresenceBasicStatusOpen; @@ -423,6 +378,26 @@ LinphonePresenceBasicStatus linphone_presence_model_get_basic_status(const Linph return status; } +int linphone_presence_model_set_basic_status(LinphonePresenceModel *model, LinphonePresenceBasicStatus basic_status) { + struct _LinphonePresenceService *service; + char *id; + + if (model == NULL) return -1; + + if (ms_list_size(model->services) > 0) { + ms_list_for_each(model->services, (MSIterateFunc)presence_service_delete); + ms_list_free(model->services); + model->services = NULL; + } + id = generate_presence_id(); + service = presence_service_new(id, basic_status); + ms_free(id); + if (service == NULL) return -1; + + presence_model_add_service(model, service); + return 0; +} + static void presence_service_find_newer_timestamp(struct _LinphonePresenceService *service, time_t *timestamp) { if (service->timestamp > *timestamp) *timestamp = service->timestamp; @@ -532,15 +507,50 @@ int linphone_presence_model_set_activity(LinphonePresenceModel *model, LinphoneP basic_status = LinphonePresenceBasicStatusOpen; break; } - if (presence_model_set_basic_status(model, basic_status) < 0) + if (linphone_presence_model_set_basic_status(model, basic_status) < 0) return -1; - presence_model_clear_activities(model); - if (presence_model_add_activity(model, acttype, description) < 0) + linphone_presence_model_clear_activities(model); + if (linphone_presence_model_add_activity(model, acttype, description) < 0) return -1; return 0; } +int linphone_presence_model_add_activity(LinphonePresenceModel *model, LinphonePresenceActivityType acttype, const char *description) { + char *id = NULL; + struct _LinphonePresencePerson *person = NULL; + struct _LinphonePresenceActivity *act = NULL; + + if (model == NULL) return -1; + + 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. */ + person = (struct _LinphonePresencePerson *)ms_list_nth_data(model->persons, 0); + } + act = presence_activity_new(acttype, description); + if (act == NULL) + return -1; + + presence_person_add_activity(person, act); + return 0; +} + +int linphone_presence_model_clear_activities(LinphonePresenceModel *model) { + if (model == NULL) return -1; + + ms_list_for_each(model->persons, (MSIterateFunc)presence_person_clear_activities); + return 0; +} + struct _find_note_st { const char *lang; struct _LinphonePresenceNote *note; @@ -1292,7 +1302,7 @@ void linphone_notify_parse_presence(SalOp *op, const char *content_type, const c acttype = LinphonePresenceActivityOffline; break; } - presence_model_add_activity(model, acttype, NULL); + linphone_presence_model_add_activity(model, acttype, NULL); } }