/* diversity-phone-kit.c - DiversityPhoneKit * * Copyright 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 #include #include #define PHONEKIT_DBUS_BUS DBUS_BUS_SESSION #define PHONEKIT_DBUS_SERVICE "org.openmoko.PhoneKit" #define PHONEKIT_NETWORK_DBUS_PATH "/org/openmoko/PhoneKit/Network" #define PHONEKIT_DIALER_DBUS_PATH "/org/openmoko/PhoneKit/Dialer" #define PHONEKIT_SMS_DBUS_PATH "/org/openmoko/PhoneKit/Sms" #define PHONEKIT_NETWORK_DBUS_INTERFACE "org.openmoko.PhoneKit.Dialer" #define PHONEKIT_DIALER_DBUS_INTERFACE "org.openmoko.PhoneKit.Network" #define PHONEKIT_SMS_DBUS_INTERFACE "org.openmoko.PhoneKit.Sms" enum { PHONEKIT_SMS_NOTREADY = 0, PHONEKIT_SMS_READY, }; #define DIVERSITY_PHONE_KIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), DIVERSITY_TYPE_PHONE_KIT, DiversityPhoneKitPrivate)) typedef struct _DiversityPhoneKitPrivate DiversityPhoneKitPrivate; struct _DiversityPhoneKitPrivate { DBusGConnection *connection; DBusGProxy *sms_proxy; DiversitySmsStatus sms_status; }; static void diversity_phone_kit_iface_init(gpointer g_iface, gpointer iface_data); G_DEFINE_TYPE_EXTENDED(DiversityPhoneKit, diversity_phone_kit, DIVERSITY_TYPE_EQUIPMENT, 0, G_IMPLEMENT_INTERFACE(DIVERSITY_TYPE_SMS, diversity_phone_kit_iface_init)); static void diversity_phone_kit_dispose(GObject *obj) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(obj); if (priv->sms_proxy) { g_object_unref(priv->sms_proxy); priv->sms_proxy = NULL; } ((GObjectClass *) diversity_phone_kit_parent_class)->dispose(obj); } static void diversity_phone_kit_finalize(GObject *obj) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(obj); dbus_g_connection_unref(priv->connection); ((GObjectClass *) diversity_phone_kit_parent_class)->finalize(obj); } static void on_sms_status_changed(DBusGProxy *proxy, gint status, gpointer data) { DiversityPhoneKit *pk = data; DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(pk); if (priv->sms_status == status) return; priv->sms_status = status; g_signal_emit_by_name(pk, "status-changed", priv->sms_status); } static gboolean diversity_phone_kit_equip(DiversityEquipment *eqp) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(eqp); dbus_g_proxy_connect_signal(priv->sms_proxy, "StatusChanged", G_CALLBACK(on_sms_status_changed), eqp, NULL); return TRUE; } static void diversity_phone_kit_unequip(DiversityEquipment *eqp) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(eqp); dbus_g_proxy_disconnect_signal(priv->sms_proxy, "StatusChanged", G_CALLBACK(on_sms_status_changed), eqp); } static gboolean diversity_phone_kit_set_config(DiversityEquipment *eqp, const gchar *key, const GValue *val) { return TRUE; } static gboolean diversity_phone_kit_get_config(DiversityEquipment *eqp, const gchar *key, GValue *val) { g_value_init(val, G_TYPE_STRING); g_value_set_static_string(val, "some-value"); return TRUE; } static void diversity_phone_kit_class_init(DiversityPhoneKitClass *klass) { GObjectClass *o_class = (GObjectClass *) klass; DiversityEquipmentClass *eqp_class = (DiversityEquipmentClass *) klass; o_class->dispose = diversity_phone_kit_dispose; o_class->finalize = diversity_phone_kit_finalize; eqp_class->equip = diversity_phone_kit_equip; eqp_class->unequip = diversity_phone_kit_unequip; eqp_class->set_config = diversity_phone_kit_set_config; eqp_class->get_config = diversity_phone_kit_get_config; g_type_class_add_private(klass, sizeof(DiversityPhoneKitPrivate)); } static void diversity_phone_kit_init(DiversityPhoneKit *pk) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(pk); priv->connection = NULL; priv->sms_proxy = NULL; priv->sms_status = PHONEKIT_SMS_NOTREADY; } DiversityPhoneKit *diversity_phone_kit_new(const gchar *name, GError **error) { DiversityPhoneKit *pk; DiversityPhoneKitPrivate *priv; DBusGProxy *prop_proxy; if (!name) return NULL; pk = g_object_new(DIVERSITY_TYPE_PHONE_KIT, "name", name, NULL); priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(pk); priv->connection = dbus_g_bus_get(PHONEKIT_DBUS_BUS, error); if (!priv->connection) { g_object_unref(pk); return NULL; } priv->sms_proxy = dbus_g_proxy_new_for_name(priv->connection, PHONEKIT_DBUS_SERVICE, PHONEKIT_SMS_DBUS_PATH, PHONEKIT_SMS_DBUS_INTERFACE); if (!priv->sms_proxy) { g_set_error(error, 0, 0, "failed to create proxy\n"); g_object_unref(pk); return NULL; } prop_proxy = dbus_g_proxy_new_for_name(priv->connection, PHONEKIT_DBUS_SERVICE, PHONEKIT_SMS_DBUS_PATH, DBUS_INTERFACE_PROPERTIES); if (prop_proxy) { GValue val = { 0 }; if (dbus_g_proxy_call(prop_proxy, "Get", NULL, G_TYPE_STRING, PHONEKIT_SMS_DBUS_INTERFACE, G_TYPE_STRING, "Status", G_TYPE_INVALID, G_TYPE_VALUE, &val, G_TYPE_INVALID)) { priv->sms_status = g_value_get_int(&val); g_value_unset(&val); } g_object_unref(prop_proxy); } dbus_g_object_register_marshaller(g_cclosure_marshal_VOID__INT, G_TYPE_NONE, G_TYPE_INT, G_TYPE_INVALID); dbus_g_proxy_add_signal(priv->sms_proxy, "StatusChanged", G_TYPE_INT, G_TYPE_INVALID); return pk; } /* DiversitySms method implementations */ static DiversitySmsStatus diversity_phone_kit_sms_get_status(DiversitySms *sms) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(sms); return priv->sms_status; } static gboolean diversity_phone_kit_sms_send(DiversitySms *sms, const gchar *number, const gchar *message, gboolean ask_ds, GError **error) { DiversityPhoneKitPrivate *priv = DIVERSITY_PHONE_KIT_GET_PRIVATE(sms); gboolean ret; ret = dbus_g_proxy_call(priv->sms_proxy, "Send", error, G_TYPE_STRING, number, G_TYPE_STRING, message, G_TYPE_BOOLEAN, ask_ds, G_TYPE_INVALID, G_TYPE_STRING, NULL, G_TYPE_INVALID); return ret; } static void diversity_phone_kit_iface_init(gpointer g_iface, gpointer iface_data) { DiversitySmsIface *iface = (DiversitySmsIface *) g_iface; iface->get_status = diversity_phone_kit_sms_get_status; iface->send = diversity_phone_kit_sms_send; }