Fixed bug 6003: On/Of" text under word Flashlight should be secondary colour.
[flashlight-appl] / src / flashlight_applet.c
index 285d596..cac13af 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Flashlight applet (widget) for Maemo.
- *  Copyright (C) 2009 Roman Moravcik
+ *  Copyright (C) 2009, 2010 Roman Moravcik
  *
  *  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
@@ -30,6 +30,8 @@
 #include <libhal.h>
 #include <dbus/dbus.h>
 
+#include <libosso.h>
+
 #include "flashlight_applet.h"
 #include "flashlight_lib.h"
 
 #define CAM_COVER_UDI "/org/freedesktop/Hal/devices/platform_cam_shutter"
 #define CAM_COVER_STATE "button.state.value"
 
+#define FLASHLIGHT_APPLET_SERVICE "org.maemo.flashlight_applet"
+#define FLASHLIGHT_APPLET_OBJECT "/org/maemo/flashlight_applet"
+#define FLASHLIGHT_APPLET_IFACE "org.maemo.flashlight_applet"
+
+#define FLASHLIGHT_APPLET_METHOD_ENABLE "enable"
+#define FLASHLIGHT_APPLET_METHOD_DISABLE "disable"
+
+#define _CAMERAUI(str) dgettext("osso-camera-ui",str)
+
 #define FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE (obj,   \
                             TYPE_FLASHLIGHT_STATUS_PLUGIN, FlashlightPluginPrivate))
 
@@ -51,7 +62,9 @@ struct _FlashlightPluginPrivate
        guint status_timer;
 
        FlashlightContext_t *flashlight;
+       DBusConnection *dbus_connection;
        LibHalContext *hal;
+       osso_context_t *osso_context;
 };
 
 HD_DEFINE_PLUGIN_MODULE (FlashlightPlugin, flashlight_status_plugin, HD_TYPE_STATUS_MENU_ITEM)
@@ -67,6 +80,7 @@ flashlight_status_plugin_show_notification (FlashlightPlugin *plugin,
        GtkWidget *banner;
 
        g_return_if_fail (priv);
+       g_return_if_fail (priv->button);
 
        banner = hildon_banner_show_information (GTK_WIDGET (priv->button), NULL, text);
        hildon_banner_set_timeout (HILDON_BANNER (banner), 3000);
@@ -79,6 +93,7 @@ flashlight_status_plugin_enable (FlashlightPlugin *plugin,
        FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
 
        g_return_if_fail (priv);
+       g_return_if_fail (priv->button);
 
        if (enable) {
                if (flashlight_open (priv->flashlight, "/dev/video0") < 0) {
@@ -125,6 +140,53 @@ flashlight_status_plugin_enable (FlashlightPlugin *plugin,
        }
 }
 
+static gint
+flashlight_applet_dbus_request_handler (const gchar *interface,
+                                       const gchar *method,
+                                       GArray *arguments,
+                                       gpointer data,
+                                       osso_rpc_t *retval)
+{
+       FlashlightPlugin *plugin = data;
+       FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
+       gboolean is_open = FALSE;
+
+       g_return_val_if_fail (interface, OSSO_ERROR);
+       g_return_val_if_fail (method, OSSO_ERROR);
+
+       g_return_val_if_fail (priv, OSSO_ERROR);
+       g_return_val_if_fail (priv->hal, OSSO_ERROR);
+       g_return_val_if_fail (priv->button, OSSO_ERROR);
+
+       if (strcmp (interface, FLASHLIGHT_APPLET_IFACE))
+               return OSSO_ERROR;
+
+       is_open = !libhal_device_get_property_bool (priv->hal, CAM_COVER_UDI, CAM_COVER_STATE, NULL);
+
+       if (!strcmp (method, FLASHLIGHT_APPLET_METHOD_ENABLE)) {
+               /* check if cover is open */
+               if (is_open) {
+                       /* try to enable flashlight, only if it's disabled */
+                       if (!strcmp (hildon_button_get_value (HILDON_BUTTON (priv->button)), MSG_FLASHLIGHT_OFF)) {
+                               flashlight_status_plugin_enable (plugin, TRUE);
+                       }
+               } else {
+                       flashlight_status_plugin_show_notification (plugin, _CAMERAUI ("camera_ia_open_lens_cover"));
+               }
+       } else if (!strcmp (method, FLASHLIGHT_APPLET_METHOD_DISABLE)) {
+               if (is_open) {
+                       /* try to disable flashlight, only if it's enabled */
+                       if (!strcmp (hildon_button_get_value (HILDON_BUTTON (priv->button)), MSG_FLASHLIGHT_ON)) {
+                               flashlight_status_plugin_enable (plugin, FALSE);
+                       }
+               }
+       } else {
+               return OSSO_ERROR;
+       }
+
+       return OSSO_OK;
+}
+
 static void
 flashlight_status_plugin_on_hal_property_modified (LibHalContext *ctx,
                                                   const char *udi,
@@ -134,7 +196,7 @@ flashlight_status_plugin_on_hal_property_modified (LibHalContext *ctx,
 {
        FlashlightPlugin *plugin = libhal_ctx_get_user_data (ctx);
        FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
-       gboolean is_open;
+       gboolean is_open = FALSE;
        int intensity = 0;
 
        g_return_if_fail (priv);
@@ -224,6 +286,7 @@ flashlight_status_plugin_ui (FlashlightPlugin *plugin)
 
        button = hildon_button_new (HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
        gtk_button_set_alignment (GTK_BUTTON (button), 0.0, 0.5);
+       hildon_button_set_style (HILDON_BUTTON (button), HILDON_BUTTON_STYLE_PICKER);
        hildon_button_set_title (HILDON_BUTTON (button), _("Flashlight"));
        hildon_button_set_value (HILDON_BUTTON (button), MSG_FLASHLIGHT_OFF);
        hildon_button_set_image (HILDON_BUTTON (button),
@@ -255,12 +318,27 @@ static void
 flashlight_status_plugin_init (FlashlightPlugin *plugin)
 {
        FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
-       DBusConnection *dbus_connection;
        DBusError error;
 
+       /* initialize osso */
+       priv->osso_context = osso_initialize (PACKAGE, VERSION, TRUE, NULL);
+       if (!priv->osso_context) {
+               g_critical ("flashlight_status_plugin_init: Could not initialize OSSO\n");
+               return;
+       }
+
+       if (osso_rpc_set_cb_f (priv->osso_context,
+                              FLASHLIGHT_APPLET_SERVICE,
+                              FLASHLIGHT_APPLET_OBJECT,
+                              FLASHLIGHT_APPLET_IFACE,
+                              flashlight_applet_dbus_request_handler, plugin) != OSSO_OK) {
+               g_critical ("flashlight_status_plugin_init: Unable to register D-BUS request handler\n");
+               return;
+       }
+
        /* initialize dbus */
        dbus_error_init (&error);
-       dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+       priv->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
        if (dbus_error_is_set (&error)) {
                g_critical ("flashlight_status_plugin_init: Could not get the system DBus connection, %s",
                            error.message);
@@ -281,7 +359,7 @@ flashlight_status_plugin_init (FlashlightPlugin *plugin)
                return;
        }
 
-       libhal_ctx_set_dbus_connection (priv->hal, dbus_connection);
+       libhal_ctx_set_dbus_connection (priv->hal, priv->dbus_connection);
        libhal_ctx_set_user_data (priv->hal, plugin);
        libhal_ctx_set_device_property_modified (priv->hal,
                                                 flashlight_status_plugin_on_hal_property_modified);
@@ -297,6 +375,7 @@ flashlight_status_plugin_init (FlashlightPlugin *plugin)
                }
                libhal_ctx_set_user_data (priv->hal, NULL);
                libhal_ctx_free (priv->hal);
+               priv->hal = NULL;
                return;
        }
 
@@ -327,16 +406,31 @@ flashlight_status_plugin_finalize (GObject *object)
                libhal_ctx_shutdown (priv->hal, NULL);
                libhal_ctx_free (priv->hal);
        }
+       priv->hal = NULL;
+
+       /* unreference dbus connection */
+       if (priv->dbus_connection) {
+               dbus_connection_unref (priv->dbus_connection);
+       }
+       priv->dbus_connection = NULL;
 
        /* cancel status timer */
        if (priv->status_timer) {
                g_source_remove (priv->status_timer);
        }
+       priv->status_timer = 0;
 
        /* deinitialize flashlight */
        if (priv->flashlight) {
                flashlight_deinit (priv->flashlight);
        }
+       priv->flashlight = NULL;
+
+       /* deinitialize osso */
+       if (priv->osso_context) {
+               osso_deinitialize (priv->osso_context);
+       }
+       priv->osso_context = NULL;
 
        G_OBJECT_CLASS (flashlight_status_plugin_parent_class)->finalize (object);
 }