Fixed crash of applet during the package update, if HAL wasn't previously initialized.
[flashlight-appl] / src / flashlight_applet.c
index 055dc49..cb9280a 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
@@ -69,7 +69,7 @@ flashlight_status_plugin_show_notification (FlashlightPlugin *plugin,
        g_return_if_fail (priv);
 
        banner = hildon_banner_show_information (GTK_WIDGET (priv->button), NULL, text);
-       hildon_banner_set_timeout (HILDON_BANNER (banner), 9000);
+       hildon_banner_set_timeout (HILDON_BANNER (banner), 3000);
 }
 
 static void
@@ -84,12 +84,14 @@ flashlight_status_plugin_enable (FlashlightPlugin *plugin,
                if (flashlight_open (priv->flashlight, "/dev/video0") < 0) {
                        flashlight_status_plugin_show_notification (plugin,
                                _("Unable to initialize flashlight.\nCamera in use by another application."));
+                       flashlight_close (priv->flashlight);
                        return;
                }
 
                if (flashlight_set_intensity (priv->flashlight, 1) < 0) {
                        flashlight_status_plugin_show_notification (plugin,
                                _("Unable to turn on flashlight."));
+                       flashlight_close (priv->flashlight);
                        return;
                }
 
@@ -218,6 +220,8 @@ flashlight_status_plugin_ui (FlashlightPlugin *plugin)
 {
        GtkWidget *button;
 
+       g_return_val_if_fail (plugin, NULL);
+
        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_title (HILDON_BUTTON (button), _("Flashlight"));
@@ -293,6 +297,8 @@ flashlight_status_plugin_init (FlashlightPlugin *plugin)
                }
                libhal_ctx_set_user_data (priv->hal, NULL);
                libhal_ctx_free (priv->hal);
+               priv->hal = NULL;
+               return;
        }
 
        libhal_device_add_property_watch (priv->hal, CAM_COVER_UDI, NULL);
@@ -313,12 +319,16 @@ flashlight_status_plugin_finalize (GObject *object)
        FlashlightPlugin *plugin = FLASHLIGHT_STATUS_PLUGIN (object);
        FlashlightPluginPrivate *priv = FLASHLIGHT_STATUS_PLUGIN_GET_PRIVATE (plugin);
 
+       g_return_if_fail (priv);
+
        /* deinitialize hal */
        if (priv->hal) {
                libhal_device_remove_property_watch (priv->hal, CAM_COVER_UDI, NULL);
                libhal_ctx_set_user_data (priv->hal, NULL);
+               libhal_ctx_shutdown (priv->hal, NULL);
                libhal_ctx_free (priv->hal);
        }
+       priv->hal = NULL;
 
        /* cancel status timer */
        if (priv->status_timer) {