/* diversity-bard.c - DiversityBard * * Copyright 2007-2008 OpenMoko, Inc. * Authored by Chia-I Wu * * 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 #include #include /* for strcmp */ #include "diversity-marshal.h" enum { EQUIPMENT_EQUIPPED, EQUIPMENT_UNEQUIPPED, LAST_SIGNAL }; enum { PROP_0, PROP_UID, PROP_FULLNAME, PROP_PHONE, PROP_ALIAS, PROP_TWITTER, }; #define DIVERSITY_BARD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), DIVERSITY_TYPE_BARD, DiversityBardPrivate)) typedef struct _DiversityBardPrivate DiversityBardPrivate; struct _DiversityBardPrivate { gchar *uid; gchar *fullname; gchar *phone; gchar *alias; gchar *twitter; GSList *equipments; }; static guint bard_signals[LAST_SIGNAL] = { 0 }; G_DEFINE_TYPE(DiversityBard, diversity_bard, DIVERSITY_TYPE_OBJECT); static void diversity_bard_dispose(GObject *obj) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(obj); if (priv->equipments) { GSList *tmp_list; tmp_list = priv->equipments; while (tmp_list) { diversity_equipment_unequip(tmp_list->data); g_object_unref(tmp_list->data); tmp_list = tmp_list->next; } g_slist_free(priv->equipments); priv->equipments = NULL; } ((GObjectClass *) diversity_bard_parent_class)->dispose(obj); } static void diversity_bard_finalize(GObject *obj) { DiversityBard *bard = DIVERSITY_BARD(obj); DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); g_free(priv->uid); g_free(priv->fullname); g_free(priv->phone); g_free(priv->alias); g_free(priv->twitter); ((GObjectClass *) diversity_bard_parent_class)->finalize(obj); } static void diversity_bard_set_property(GObject *obj, guint prop_id, const GValue *value, GParamSpec *pspec) { DiversityBard *bard = DIVERSITY_BARD(obj); switch (prop_id) { case PROP_UID: diversity_bard_set_uid(bard, g_value_get_string(value)); break; case PROP_FULLNAME: diversity_bard_set_fullname(bard, g_value_get_string(value)); break; case PROP_PHONE: diversity_bard_set_phone(bard, g_value_get_string(value)); break; case PROP_ALIAS: diversity_bard_set_alias(bard, g_value_get_string(value)); break; case PROP_TWITTER: diversity_bard_set_twitter(bard, g_value_get_string(value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec); break; } } static void diversity_bard_get_property(GObject *obj, guint prop_id, GValue *value, GParamSpec *pspec) { DiversityBard *bard = DIVERSITY_BARD(obj); DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); switch (prop_id) { case PROP_UID: g_value_set_string(value, priv->uid); break; case PROP_FULLNAME: g_value_set_string(value, priv->fullname); break; case PROP_PHONE: g_value_set_string(value, priv->phone); break; case PROP_ALIAS: g_value_set_string(value, priv->alias); break; case PROP_TWITTER: g_value_set_string(value, priv->twitter); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec); break; } } static void diversity_bard_class_init(DiversityBardClass *klass) { GObjectClass *o_class = (GObjectClass *) klass; o_class->dispose = diversity_bard_dispose; o_class->finalize = diversity_bard_finalize; o_class->set_property = diversity_bard_set_property; o_class->get_property = diversity_bard_get_property; g_type_class_add_private(klass, sizeof(DiversityBardPrivate)); g_object_class_install_property(o_class, PROP_UID, g_param_spec_string("uid", "UID", "EContact UID", "", G_PARAM_READWRITE)); g_object_class_install_property(o_class, PROP_FULLNAME, g_param_spec_string("fullname", "Full name", "Full name of bard", "", G_PARAM_READWRITE)); g_object_class_install_property(o_class, PROP_PHONE, g_param_spec_string("phone", "Phone", "Phone number of bard", "", G_PARAM_READWRITE)); g_object_class_install_property(o_class, PROP_ALIAS, g_param_spec_string("alias", "Alias", "Alias of bard", "", G_PARAM_READWRITE)); g_object_class_install_property(o_class, PROP_TWITTER, g_param_spec_string("twitter", "Twitter", "Bards sing", "", G_PARAM_READWRITE)); bard_signals[EQUIPMENT_EQUIPPED] = g_signal_new("equipment-equipped", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(DiversityBardClass, equipment_equipped), NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT); bard_signals[EQUIPMENT_UNEQUIPPED] = g_signal_new("equipment-unequipped", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(DiversityBardClass, equipment_unequipped), NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT); } static void diversity_bard_init(DiversityBard *bard) { DiversityObject *obj = DIVERSITY_OBJECT(bard); DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); obj->type = DIVERSITY_OBJECT_TYPE_BARD; priv->uid = NULL; priv->fullname = NULL; priv->phone = NULL; priv->alias = NULL; priv->twitter = NULL; priv->equipments = NULL; } DiversityBard *diversity_bard_new(void) { DiversityBard *bard; bard = g_object_new(DIVERSITY_TYPE_BARD, NULL); return bard; } void diversity_bard_set_uid(DiversityBard *bard, const gchar *uid) { DiversityObject *obj = DIVERSITY_OBJECT(bard); DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); if (priv->uid) g_free(priv->uid); priv->uid = g_strdup(uid); if (obj->id) g_free(obj->id); obj->id = g_strdup(uid); g_object_notify(G_OBJECT(bard), "uid"); } const gchar *diversity_bard_get_uid(DiversityBard *bard) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); return priv->uid; } void diversity_bard_set_fullname(DiversityBard *bard, const gchar *fullname) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); if (priv->fullname) g_free(priv->fullname); priv->fullname = g_strdup(fullname); g_object_notify(G_OBJECT(bard), "fullname"); } const gchar *diversity_bard_get_fullname(DiversityBard *bard) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); return priv->fullname; } void diversity_bard_set_phone(DiversityBard *bard, const gchar *phone) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); if (priv->phone) g_free(priv->phone); priv->phone = g_strdup(phone); g_object_notify(G_OBJECT(bard), "phone"); } const gchar *diversity_bard_get_phone(DiversityBard *bard) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); return priv->phone; } void diversity_bard_set_alias(DiversityBard *bard, const gchar *alias) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); if (priv->alias) g_free(priv->alias); priv->alias = g_strdup(alias); g_object_notify(G_OBJECT(bard), "alias"); } const gchar *diversity_bard_get_alias(DiversityBard *bard) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); return priv->alias; } void diversity_bard_set_twitter(DiversityBard *bard, const gchar *twitter) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); if (priv->twitter) g_free(priv->twitter); priv->twitter = g_strdup(twitter); g_object_notify(G_OBJECT(bard), "twitter"); } const gchar *diversity_bard_get_twitter(DiversityBard *bard) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); return priv->twitter; } void diversity_bard_add_equipment(DiversityBard *bard, DiversityEquipment *eqp) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); const gchar *name; name = diversity_equipment_get_name(eqp); if (name && !diversity_bard_get_equipment_by_name(bard, name)) { priv->equipments = g_slist_prepend(priv->equipments, eqp); g_object_ref(eqp); } } void diversity_bard_remove_equipment(DiversityBard *bard, DiversityEquipment *eqp) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); GSList *tmp; tmp = g_slist_find(priv->equipments, eqp); if (tmp) { priv->equipments = g_slist_delete_link(priv->equipments, tmp); g_object_unref(eqp); } } gboolean diversity_bard_equip_equipment(DiversityBard *bard, DiversityEquipment *eqp) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); GSList *tmp; gboolean success = FALSE; tmp = g_slist_find(priv->equipments, eqp); if (tmp) { success = diversity_equipment_equip(eqp, bard); if (success) g_signal_emit(bard, bard_signals[EQUIPMENT_EQUIPPED], 0, eqp); } return success; } void diversity_bard_unequip_equipment(DiversityBard *bard, DiversityEquipment *eqp) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); GSList *tmp; tmp = g_slist_find(priv->equipments, eqp); if (tmp && (bard == diversity_equipment_get_equipper(eqp))) { diversity_equipment_unequip(eqp); g_signal_emit(bard, bard_signals[EQUIPMENT_UNEQUIPPED], 0, eqp); } } gboolean diversity_bard_has_equipment(DiversityBard *bard, DiversityEquipment *eqp) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); GSList *tmp; tmp = g_slist_find(priv->equipments, eqp); return (tmp != NULL); } DiversityEquipment *diversity_bard_get_equipment_by_name(DiversityBard *bard, const gchar *name) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); DiversityEquipment *eqp = NULL; GSList *tmp_list; if (!name) return NULL; tmp_list = priv->equipments; while (tmp_list) { const gchar *eqp_name; eqp_name = diversity_equipment_get_name(tmp_list->data); if (strcmp(name, eqp_name) == 0) { eqp = tmp_list->data; break; } tmp_list = tmp_list->next; } return eqp; } const GSList *diversity_bard_get_equipments(DiversityBard *bard) { DiversityBardPrivate *priv = DIVERSITY_BARD_GET_PRIVATE(bard); return priv->equipments; }