X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fui_appindicator.c;h=4b4f9d6d8d9e9c7081b3ddc94aeb7cc8b0990855;hb=01515c2f46a2b0cc70c0ef54f49d16493a2b5ce2;hp=7536952ba9b8e749b880198b73575ef340d54589;hpb=dcf1b53921f5f9fdab59295be938c766bb572793;p=psensor.git diff --git a/src/ui_appindicator.c b/src/ui_appindicator.c index 7536952..4b4f9d6 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,56 +74,6 @@ 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, "_Quit", NULL, "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) { @@ -157,39 +102,25 @@ static void update_menu_items(int use_celcius) update_menu_item(*m, *s, use_celcius); } -static GtkWidget *get_menu(struct ui_psensor *ui) +static void +build_sensor_menu_items(const struct ui_psensor *ui, + GtkMenu *menu) { - GtkActionGroup *action_group; - GtkUIManager *menu_manager; - GError *error; - GtkMenu *menu; - int i, n, j; - int celcius; + int i, j, n, celcius; const char *name; + struct psensor **sorted_sensors; - 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); - - 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")); + free(menu_items); celcius = ui->config->temperature_unit == CELCIUS; - n = psensor_list_size(ui->sensors); + 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(ui->sensors[i]->id)) { - sensors[j] = ui->sensors[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 @@ -207,18 +138,75 @@ static GtkWidget *get_menu(struct ui_psensor *ui) sensors[j] = NULL; + free(sorted_sensors); +} + +static GtkWidget *get_menu(struct ui_psensor *ui) +{ + GError *error; + GtkMenu *menu; + guint ok; + GtkBuilder *builder; + + builder = gtk_builder_new(); + + error = NULL; + 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); } void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) { AppIndicatorStatus status; + char *label, *str, *tmp; + struct psensor **p; if (!indicator) return; status = app_indicator_get_status(indicator); + p = ui_get_sensors_ordered_by_position(ui); + label = NULL; + while (*p) { + if (config_is_appindicator_label_enabled((*p)->id)) { + str = psensor_current_value_to_str + (*p, ui->config->temperature_unit == CELCIUS); + 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; + } + } + p++; + } + + app_indicator_set_label(indicator, label, NULL); + if (!attention && status == APP_INDICATOR_STATUS_ATTENTION) app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE); @@ -234,7 +222,7 @@ static GtkStatusIcon *unity_fallback(AppIndicator *indicator) { GtkStatusIcon *ico; - log_debug("ui_appindicator#unity_fallback"); + log_debug("ui_appindicator.unity_fallback()"); appindicator_supported = 0; @@ -248,7 +236,7 @@ static GtkStatusIcon *unity_fallback(AppIndicator *indicator) static void unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon) { - log_debug("ui_appindicator#unity_unfallback"); + log_debug("ui_appindicator.unity_unfallback()"); ui_status_set_visible(0); @@ -291,5 +279,4 @@ int is_appindicator_supported() void ui_appindicator_cleanup() { free(sensors); - /* TODO: cleanup menu items. */ }