X-Git-Url: http://wpitchoune.net/gitweb/?p=psensor-pkg-debian.git;a=blobdiff_plain;f=src%2Fui_appindicator.c;h=c8887c239c5b857043494d12e231b752cc1e3352;hp=31616a1dcd2ce3af3de03149a7d16093a35ebb2d;hb=ecd968930278d22ce234485d1bddced831c82f0f;hpb=ee0b7212d29624b6321a0eda9c7ba1e17817ba25;ds=sidebyside diff --git a/src/ui_appindicator.c b/src/ui_appindicator.c index 31616a1..c8887c2 100644 --- a/src/ui_appindicator.c +++ b/src/ui_appindicator.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2013 jeanfi@gmail.com + * Copyright (C) 2010-2014 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 @@ -40,17 +40,12 @@ static int appindicator_supported = 1; static AppIndicator *indicator; static struct ui_psensor *ui_psensor; -static void cb_menu_show(GtkMenuItem *mi, gpointer data) +void cb_menu_show(GtkMenuItem *mi, gpointer data) { ui_window_show((struct ui_psensor *)data); } -static void cb_menu_quit(GtkMenuItem *mi, gpointer data) -{ - ui_psensor_quit(data); -} - -static void cb_menu_preferences(GtkMenuItem *mi, gpointer data) +void ui_appindicator_cb_preferences(GtkMenuItem *mi, gpointer data) { #ifdef HAVE_APPINDICATOR_029 gdk_threads_enter(); @@ -63,7 +58,7 @@ static void cb_menu_preferences(GtkMenuItem *mi, gpointer data) #endif } -static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data) +void ui_appindicator_cb_sensor_preferences(GtkMenuItem *mi, gpointer data) { struct ui_psensor *ui = data; @@ -79,66 +74,13 @@ static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data) #endif } -static void cb_about(GtkMenuItem *mi, gpointer data) -{ - ui_show_about_dialog(); -} - -static const char *menu_desc = -"" -" " -" " -" " -" " -" " -" " -" " -" " -" " -" " -" " -""; - -static GtkActionEntry entries[] = { - { "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, - N_("_Quit"), - NULL, N_("Quit"), - G_CALLBACK(cb_menu_quit) } -}; -static guint n_entries = G_N_ELEMENTS(entries); - static void -update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celcius) +update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celsius) { gchar *str; char *v; - v = psensor_current_value_to_str(s, use_celcius); + v = psensor_current_value_to_str(s, use_celsius); str = g_strdup_printf("%s: %s", s->name, v); @@ -148,7 +90,7 @@ update_menu_item(GtkMenuItem *item, struct psensor *s, int use_celcius) g_free(str); } -static void update_menu_items(int use_celcius) +static void update_menu_items(int use_celsius) { struct psensor **s; GtkMenuItem **m; @@ -157,20 +99,19 @@ static void update_menu_items(int use_celcius) return ; for (s = sensors, m = menu_items; *s; s++, m++) - update_menu_item(*m, *s, use_celcius); + update_menu_item(*m, *s, use_celsius); } static void -build_sensor_menu_items(const struct ui_psensor *ui, - GtkMenu *menu) +build_sensor_menu_items(const struct ui_psensor *ui, GtkMenu *menu) { - int i, j, n, celcius; + int i, j, n, celsius; const char *name; struct psensor **sorted_sensors; free(menu_items); - celcius = ui->config->temperature_unit == CELCIUS; + celsius = ui->config->temperature_unit == CELSIUS; sorted_sensors = ui_get_sensors_ordered_by_position(ui); n = psensor_list_size(sorted_sensors); @@ -188,7 +129,7 @@ build_sensor_menu_items(const struct ui_psensor *ui, GTK_WIDGET(menu_items[j]), j+2); - update_menu_item(menu_items[j], sensors[j], celcius); + update_menu_item(menu_items[j], sensors[j], celsius); j++; } @@ -201,31 +142,87 @@ build_sensor_menu_items(const struct ui_psensor *ui, static GtkWidget *get_menu(struct ui_psensor *ui) { - GtkActionGroup *action_group; - GtkUIManager *menu_manager; GError *error; GtkMenu *menu; + guint ok; + GtkBuilder *builder; - action_group = gtk_action_group_new("PsensorActions"); - gtk_action_group_set_translation_domain(action_group, PACKAGE); - menu_manager = gtk_ui_manager_new(); - - gtk_action_group_add_actions(action_group, entries, n_entries, ui); - gtk_ui_manager_insert_action_group(menu_manager, action_group, 0); + builder = gtk_builder_new(); error = NULL; - gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error); - - if (error) - g_error(_("building menus failed: %s"), error->message); - - menu = GTK_MENU(gtk_ui_manager_get_widget(menu_manager, "/MainMenu")); + ok = gtk_builder_add_from_file + (builder, + PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade", + &error); + + if (!ok) { + log_printf(LOG_ERR, error->message); + g_error_free(error); + return NULL; + } + menu = GTK_MENU(gtk_builder_get_object(builder, "appindicator_menu")); build_sensor_menu_items(ui, menu); + gtk_builder_connect_signals(builder, ui); + + g_object_ref(G_OBJECT(menu)); + g_object_unref(G_OBJECT(builder)); return GTK_WIDGET(menu); } +static void update_label(struct ui_psensor *ui) +{ + char *label, *str, *tmp, *guide; + struct psensor **p; + + p = ui_get_sensors_ordered_by_position(ui); + label = NULL; + guide = NULL; + while (*p) { + if (config_is_appindicator_label_enabled((*p)->id)) { + str = psensor_current_value_to_str + (*p, ui->config->temperature_unit == CELSIUS); + + if (label == NULL) { + label = str; + } else { + tmp = malloc(strlen(label) + + 1 + + strlen(str) + + 1); + sprintf(tmp, "%s %s", label, str); + free(label); + free(str); + label = tmp; + } + + if (is_temp_type((*p)->type)) + str = "999UUU"; + else if (is_fan_type((*p)->type)) + str = "999UUU"; + else /* cpu load */ + str = "999%"; + + if (guide == NULL) { + guide = strdup(str); + } else { + tmp = malloc(strlen(guide) + + 1 + + strlen(str) + + 1); + sprintf(tmp, "%sW%s", guide, str); + free(guide); + guide = tmp; + } + + } + p++; + } + + app_indicator_set_label(indicator, label, guide); +} + void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) { AppIndicatorStatus status; @@ -233,6 +230,8 @@ void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) if (!indicator) return; + update_label(ui); + status = app_indicator_get_status(indicator); if (!attention && status == APP_INDICATOR_STATUS_ATTENTION) @@ -243,7 +242,7 @@ void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ATTENTION); - update_menu_items(ui->config->temperature_unit == CELCIUS); + update_menu_items(ui->config->temperature_unit == CELSIUS); } static GtkStatusIcon *unity_fallback(AppIndicator *indicator) @@ -307,5 +306,4 @@ int is_appindicator_supported() void ui_appindicator_cleanup() { free(sensors); - /* TODO: cleanup menu items. */ }