style
[psensor.git] / src / ui_appindicator.c
index 153046e..2c42f70 100644 (file)
@@ -1,22 +1,21 @@
 /*
-    Copyright (C) 2010-2011 jeanfi@gmail.com
-
-    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., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2011 jeanfi@gmail.com
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "ui.h"
 #include "ui_appindicator.h"
 #include "ui_sensorpref.h"
+#include "ui_status.h"
 #include "ui_pref.h"
 
 static GtkMenuItem **sensor_menu_items;
+static GtkWidget *main_window;
+static int appindicator_supported = 1;
+static AppIndicator *indicator;
+static struct ui_psensor *ui_psensor;
 
 static void cb_menu_show(GtkMenuItem *mi, gpointer data)
 {
-       struct ui_psensor *ui = (struct ui_psensor *)data;
-
-       gtk_window_present(GTK_WINDOW(ui->main_window));
+       ui_window_show((struct ui_psensor *)data);
 }
 
 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
@@ -44,8 +46,7 @@ static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
        ui_psensor_quit(data);
 }
 
-static void
-cb_menu_preferences(GtkMenuItem *mi, gpointer data)
+static void cb_menu_preferences(GtkMenuItem *mi, gpointer data)
 {
 #ifdef HAVE_APPINDICATOR_029
        gdk_threads_enter();
@@ -58,13 +59,25 @@ cb_menu_preferences(GtkMenuItem *mi, gpointer data)
 #endif
 }
 
-static void
-cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
+static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
 {
        struct ui_psensor *ui = data;
 
+#ifdef HAVE_APPINDICATOR_029
+       gdk_threads_enter();
+#endif
+
        if (ui->sensors && *ui->sensors)
                ui_sensorpref_dialog_run(*ui->sensors, ui);
+
+#ifdef HAVE_APPINDICATOR_029
+       gdk_threads_leave();
+#endif
+}
+
+static void cb_about(GtkMenuItem *mi, gpointer data)
+{
+       ui_show_about_dialog();
 }
 
 static const char *menu_desc =
@@ -76,6 +89,8 @@ static const char *menu_desc =
 "      <menuitem name='Preferences' action='PreferencesAction' />"
 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
 "      <separator />"
+"      <menuitem name='About' action='AboutAction' />"
+"      <separator />"
 "      <menuitem name='Quit' action='QuitAction' />"
 "  </popup>"
 "</ui>";
@@ -99,6 +114,12 @@ static GtkActionEntry entries[] = {
     "SensorPreferences",
     G_CALLBACK(cb_sensor_preferences) },
 
+  { "AboutAction", NULL,
+    "_About",
+    NULL,
+    "About",
+    G_CALLBACK(cb_about) },
+
   { "QuitAction",
     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
 };
@@ -108,9 +129,10 @@ static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
 {
        gchar *str;
 
-       str = g_strdup_printf("%s: %2.f",
+       str = g_strdup_printf("%s: %2.f %s",
                              s->name,
-                             psensor_get_current_value(s));
+                             psensor_get_current_value(s),
+                             psensor_type_to_unit_str(s->type));
 
        gtk_menu_item_set_label(item, str);
 
@@ -119,12 +141,11 @@ static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s)
 
 static void update_sensor_menu_items(struct psensor **sensors)
 {
-       int n = psensor_list_size(sensors);
-       int i;
+       int n, i;
 
+       n = psensor_list_size(sensors);
        for (i = 0; i < n; i++)
-               update_sensor_menu_item(sensor_menu_items[i],
-                                       sensors[i]);
+               update_sensor_menu_item(sensor_menu_items[i], sensors[i]);
 }
 
 static GtkWidget *get_menu(struct ui_psensor *ui)
@@ -172,55 +193,73 @@ static GtkWidget *get_menu(struct ui_psensor *ui)
        return GTK_WIDGET(menu);
 }
 
-
-void ui_appindicator_update(struct ui_psensor *ui)
+void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention)
 {
-       struct psensor **sensor_cur = ui->sensors;
        AppIndicatorStatus status;
-       int attention = 0;
 
-       if (!ui->indicator)
+       if (!indicator)
                return;
 
-       while (*sensor_cur) {
-               struct psensor *s = *sensor_cur;
-
-               if (s->alarm_enabled && s->alarm_raised) {
-                       attention = 1;
-                       break;
-               }
-
-               sensor_cur++;
-       }
-
-       status = app_indicator_get_status(ui->indicator);
+       status = app_indicator_get_status(indicator);
 
        if (!attention && status == APP_INDICATOR_STATUS_ATTENTION)
                app_indicator_set_status
-                   (ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
+                       (indicator, APP_INDICATOR_STATUS_ACTIVE);
 
        if (attention && status == APP_INDICATOR_STATUS_ACTIVE)
                app_indicator_set_status
-                   (ui->indicator, APP_INDICATOR_STATUS_ATTENTION);
+                   (indicator, APP_INDICATOR_STATUS_ATTENTION);
 
        update_sensor_menu_items(ui->sensors);
 }
 
+static GtkStatusIcon *unity_fallback(AppIndicator *indicator)
+{
+       log_debug("ui_appindicator#unity_fallback");
+
+       appindicator_supported = 0;
+
+       return ui_status_get_icon(ui_psensor);
+}
+
+static void
+unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon)
+{
+       log_debug("ui_appindicator#unity_unfallback");
+
+       appindicator_supported = 1;
+}
+
 void ui_appindicator_init(struct ui_psensor *ui)
 {
-       GtkWidget *indicatormenu;
+       GtkWidget *menu;
+
+       ui_psensor = ui;
+       main_window = ui->main_window;
 
-       ui->indicator
-           = app_indicator_new("psensor",
-                               "psensor",
-                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
+       indicator = app_indicator_new
+               ("psensor",
+                "psensor_normal",
+                APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
 
-       app_indicator_set_status(ui->indicator, APP_INDICATOR_STATUS_ACTIVE);
-       app_indicator_set_attention_icon(ui->indicator, "psensor_hot");
+       APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback;
+       APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback;
 
-       indicatormenu = get_menu(ui);
+       app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
+       app_indicator_set_attention_icon(indicator, "psensor_hot");
 
-       gtk_widget_show_all(indicatormenu);
+       menu = get_menu(ui);
+       app_indicator_set_menu(indicator, GTK_MENU(menu));
 
-       app_indicator_set_menu(ui->indicator, GTK_MENU(indicatormenu));
+       gtk_widget_show_all(menu);
+}
+
+int is_appindicator_supported()
+{
+       return appindicator_supported;
+}
+
+void ui_appindicator_cleanup()
+{
+       /* TODO: cleanup menu items. */
 }