X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fui_appindicator.c;h=9fbeaa782fbb214de629748e3cbac58fcca02ea1;hb=37afe949c5c7aff3abe6e030161c64c8603877b2;hp=e6f37e858f6da5b700adc5340f3d64a239a2335d;hpb=68a02c60b7eed8237652580048825b4ea91a333f;p=psensor.git diff --git a/src/ui_appindicator.c b/src/ui_appindicator.c index e6f37e8..9fbeaa7 100644 --- a/src/ui_appindicator.c +++ b/src/ui_appindicator.c @@ -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-2013 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 #include #include @@ -24,16 +23,22 @@ #include #include +#include "cfg.h" #include "psensor.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 const char *ICON = "psensor_normal"; +static const char *ATTENTION_ICON = "psensor_hot"; + +static struct psensor **sensors; +static GtkMenuItem **menu_items; static int appindicator_supported = 1; static AppIndicator *indicator; +static struct ui_psensor *ui_psensor; static void cb_menu_show(GtkMenuItem *mi, gpointer data) { @@ -95,57 +100,100 @@ static const char *menu_desc = ""; static GtkActionEntry entries[] = { - { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */ - - { "ShowAction", NULL, /* name, stock id */ - "_Show", NULL, /* label, accelerator */ - "Show", /* tooltip */ - G_CALLBACK(cb_menu_show) }, - - { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */ - "_Preferences", NULL, /* label, accelerator */ - "Preferences", /* tooltip */ - G_CALLBACK(cb_menu_preferences) }, - - { "SensorPreferencesAction", GTK_STOCK_PREFERENCES, - "S_ensor Preferences", - NULL, - "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) } + { "PsensorMenuAction", NULL, "_Psensor" }, + + { "ShowAction", NULL, + N_("_Show"), NULL, + N_("Show"), + G_CALLBACK(cb_menu_show) }, + + { "PreferencesAction", GTK_STOCK_PREFERENCES, + N_("_Preferences"), NULL, + N_("Preferences"), + G_CALLBACK(cb_menu_preferences) }, + + { "SensorPreferencesAction", GTK_STOCK_PREFERENCES, + N_("S_ensor Preferences"), + NULL, + N_("SensorPreferences"), + G_CALLBACK(cb_sensor_preferences) }, + + { "AboutAction", NULL, + N_("_About"), + NULL, + N_("About"), + G_CALLBACK(cb_about) }, + + { "QuitAction", + GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) } }; static guint n_entries = G_N_ELEMENTS(entries); -static void update_sensor_menu_item(GtkMenuItem *item, struct psensor *s) +static void +update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celcius) { gchar *str; + char *v; - str = g_strdup_printf("%s: %2.f %s", - s->name, - psensor_get_current_value(s), - psensor_type_to_unit_str(s->type)); + v = psensor_current_value_to_str(s, use_celcius); + + str = g_strdup_printf("%s: %s", s->name, v); gtk_menu_item_set_label(item, str); + free(v); g_free(str); } -static void update_sensor_menu_items(struct psensor **sensors) +static void update_menu_items(int use_celcius) +{ + struct psensor **s; + GtkMenuItem **m; + + if (!sensors) + return ; + + for (s = sensors, m = menu_items; *s; s++, m++) + update_menu_item(*m, *s, use_celcius); +} + +static void +build_sensor_menu_items(const struct ui_psensor *ui, + GtkMenu *menu) { - int n, i; + int i, j, n, celcius; + const char *name; + struct psensor **sorted_sensors; + + free(menu_items); + + celcius = ui->config->temperature_unit == CELCIUS; + + sorted_sensors = ui_get_sensors_ordered_by_position(ui); + n = psensor_list_size(sorted_sensors); + menu_items = malloc(n * sizeof(GtkWidget *)); + sensors = malloc((n + 1) * sizeof(struct psensor *)); + for (i = 0, j = 0; i < n; i++) { + if (config_is_appindicator_enabled(sorted_sensors[i]->id)) { + sensors[j] = sorted_sensors[i]; + name = sensors[j]->name; + + menu_items[j] = GTK_MENU_ITEM + (gtk_menu_item_new_with_label(name)); + + gtk_menu_shell_insert(GTK_MENU_SHELL(menu), + GTK_WIDGET(menu_items[j]), + j+2); + + update_menu_item(menu_items[j], sensors[j], celcius); + + j++; + } + } + + sensors[j] = NULL; - n = psensor_list_size(sensors); - for (i = 0; i < n; i++) - update_sensor_menu_item(sensor_menu_items[i], - sensors[i]); + free(sorted_sensors); } static GtkWidget *get_menu(struct ui_psensor *ui) @@ -154,10 +202,6 @@ static GtkWidget *get_menu(struct ui_psensor *ui) GtkUIManager *menu_manager; GError *error; GtkMenu *menu; - int i; - int n = psensor_list_size(ui->sensors); - struct psensor **sensors = ui->sensors; - action_group = gtk_action_group_new("PsensorActions"); gtk_action_group_set_translation_domain(action_group, PACKAGE); @@ -174,21 +218,7 @@ static GtkWidget *get_menu(struct ui_psensor *ui) menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu")); - sensor_menu_items = malloc(sizeof(GtkWidget *)*n); - for (i = 0; i < n; i++) { - struct psensor *s = sensors[i]; - - sensor_menu_items[i] - = GTK_MENU_ITEM(gtk_menu_item_new_with_label(s->name)); - - gtk_menu_shell_insert(GTK_MENU_SHELL(menu), - GTK_WIDGET(sensor_menu_items[i]), - i+2); - - update_sensor_menu_item(sensor_menu_items[i], - s); - } - + build_sensor_menu_items(ui, menu); return GTK_WIDGET(menu); } @@ -203,56 +233,67 @@ void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) status = app_indicator_get_status(indicator); if (!attention && status == APP_INDICATOR_STATUS_ATTENTION) - app_indicator_set_status - (indicator, APP_INDICATOR_STATUS_ACTIVE); + app_indicator_set_status(indicator, + APP_INDICATOR_STATUS_ACTIVE); if (attention && status == APP_INDICATOR_STATUS_ACTIVE) - app_indicator_set_status - (indicator, APP_INDICATOR_STATUS_ATTENTION); + app_indicator_set_status(indicator, + APP_INDICATOR_STATUS_ATTENTION); - update_sensor_menu_items(ui->sensors); + update_menu_items(ui->config->temperature_unit == CELCIUS); } static GtkStatusIcon *unity_fallback(AppIndicator *indicator) { - log_printf(LOG_DEBUG, "ui_appindicator#unity_fallback"); + GtkStatusIcon *ico; - gtk_widget_show_all(main_window); + log_debug("ui_appindicator.unity_fallback()"); appindicator_supported = 0; - return NULL; + ico = ui_status_get_icon(ui_psensor); + + ui_status_set_visible(1); + + return ico; } static void unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon) { - log_printf(LOG_DEBUG, "ui_appindicator#unity_unfallback"); + log_debug("ui_appindicator.unity_unfallback()"); + + ui_status_set_visible(0); appindicator_supported = 1; } -void ui_appindicator_init(struct ui_psensor *ui) +void ui_appindicator_update_menu(struct ui_psensor *ui) { GtkWidget *menu; - main_window = ui->main_window; + menu = get_menu(ui); + app_indicator_set_menu(indicator, GTK_MENU(menu)); + + gtk_widget_show_all(menu); +} + +void ui_appindicator_init(struct ui_psensor *ui) +{ + ui_psensor = ui; indicator = app_indicator_new ("psensor", - "psensor_normal", + ICON, APP_INDICATOR_CATEGORY_APPLICATION_STATUS); APP_INDICATOR_GET_CLASS(indicator)->fallback = unity_fallback; APP_INDICATOR_GET_CLASS(indicator)->unfallback = unity_unfallback; app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE); - app_indicator_set_attention_icon(indicator, "psensor_hot"); + app_indicator_set_attention_icon(indicator, ATTENTION_ICON); - menu = get_menu(ui); - app_indicator_set_menu(indicator, GTK_MENU(menu)); - - gtk_widget_show_all(menu); + ui_appindicator_update_menu(ui); } int is_appindicator_supported() @@ -262,5 +303,6 @@ int is_appindicator_supported() void ui_appindicator_cleanup() { + free(sensors); /* TODO: cleanup menu items. */ }