X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Fui_appindicator.c;h=ed4cc6418ab6d577d8b39c4a9a511dea5fab0f88;hp=f9f1a598d847f8b2abec01639e0405419f196540;hb=c1e20f2631a1249720e9c75d753eacfcb0f6c7b9;hpb=bf8b896c257cd142668525369508f8d9135ff0f5 diff --git a/src/ui_appindicator.c b/src/ui_appindicator.c index f9f1a59..ed4cc64 100644 --- a/src/ui_appindicator.c +++ b/src/ui_appindicator.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2014 jeanfi@gmail.com + * Copyright (C) 2010-2016 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 @@ -23,17 +23,20 @@ #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" +#include +#include +#include +#include +#include +#include +#include static const char *ICON = "psensor_normal"; static const char *ATTENTION_ICON = "psensor_hot"; +static const char *GLADE_FILE += PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-appindicator.glade"; + static struct psensor **sensors; static GtkMenuItem **menu_items; static bool appindicator_supported = true; @@ -87,19 +90,21 @@ static void update_menu_items(int use_celsius) } static void -build_sensor_menu_items(const struct ui_psensor *ui, GtkMenu *menu) +create_sensor_menu_items(const struct ui_psensor *ui, GtkMenu *menu) { int i, j, n, celsius; const char *name; struct psensor **sorted_sensors; - free(menu_items); - - celsius = ui->config->temperature_unit == CELSIUS; + if (config_get_temperature_unit() == CELSIUS) + celsius = 1; + else + celsius = 0; - sorted_sensors = ui_get_sensors_ordered_by_position(ui); + sorted_sensors = ui_get_sensors_ordered_by_position(ui->sensors); n = psensor_list_size(sorted_sensors); - menu_items = malloc(n * sizeof(GtkWidget *)); + menu_items = malloc((n + 1) * 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)) { @@ -120,11 +125,12 @@ build_sensor_menu_items(const struct ui_psensor *ui, GtkMenu *menu) } sensors[j] = NULL; + menu_items[j] = NULL; free(sorted_sensors); } -static GtkWidget *get_menu(struct ui_psensor *ui) +static GtkMenu *load_menu(struct ui_psensor *ui) { GError *error; GtkMenu *menu; @@ -136,21 +142,18 @@ static GtkWidget *get_menu(struct ui_psensor *ui) builder = gtk_builder_new(); error = NULL; - ok = gtk_builder_add_from_file - (builder, - PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-appindicator.glade", - &error); + ok = gtk_builder_add_from_file(builder, GLADE_FILE, &error); if (!ok) { log_err(_("Failed to load glade file %s: %s"), - PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor-appindicator.glade", + GLADE_FILE, error->message); g_error_free(error); return NULL; } menu = GTK_MENU(gtk_builder_get_object(builder, "appindicator_menu")); - build_sensor_menu_items(ui, menu); + create_sensor_menu_items(ui, menu); gtk_builder_connect_signals(builder, ui); g_object_ref(G_OBJECT(menu)); @@ -158,21 +161,27 @@ static GtkWidget *get_menu(struct ui_psensor *ui) log_fct_exit(); - return GTK_WIDGET(menu); + return menu; } static void update_label(struct ui_psensor *ui) { char *label, *str, *tmp, *guide; struct psensor **p; + int use_celsius; - p = ui_get_sensors_ordered_by_position(ui); + p = ui_get_sensors_ordered_by_position(ui->sensors); label = NULL; guide = NULL; + + if (config_get_temperature_unit() == CELSIUS) + use_celsius = 1; + else + use_celsius = 0; + while (*p) { if (config_is_appindicator_label_enabled((*p)->id)) { - str = psensor_current_value_to_str - (*p, ui->config->temperature_unit == CELSIUS); + str = psensor_current_value_to_str(*p, use_celsius); if (label == NULL) { label = str; @@ -213,7 +222,7 @@ static void update_label(struct ui_psensor *ui) app_indicator_set_label(indicator, label, guide); } -void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) +void ui_appindicator_update(struct ui_psensor *ui, bool attention) { AppIndicatorStatus status; @@ -230,9 +239,12 @@ void ui_appindicator_update(struct ui_psensor *ui, unsigned int attention) if (attention && status == APP_INDICATOR_STATUS_ACTIVE) app_indicator_set_status(indicator, - APP_INDICATOR_STATUS_ATTENTION); + APP_INDICATOR_STATUS_ATTENTION); - update_menu_items(ui->config->temperature_unit == CELSIUS); + if (config_get_temperature_unit() == CELSIUS) + update_menu_items(1); + else + update_menu_items(0); } static GtkStatusIcon *unity_fallback(AppIndicator *indicator) @@ -260,14 +272,44 @@ unity_unfallback(AppIndicator *indicator, GtkStatusIcon *status_icon) appindicator_supported = true; } +static void remove_sensor_menu_items(GtkMenu *menu) +{ + GtkMenuItem **items; + + if (!menu_items) + return; + + items = menu_items; + while (*items) { + gtk_container_remove(GTK_CONTAINER(menu), GTK_WIDGET(*items)); + + items++; + } + + free(menu_items); + free(sensors); +} + void ui_appindicator_update_menu(struct ui_psensor *ui) { - GtkWidget *menu; + GtkMenu *menu; + + menu = GTK_MENU(app_indicator_get_menu(indicator)); + + if (menu) { + remove_sensor_menu_items(menu); + create_sensor_menu_items(ui, menu); + } else { + menu = load_menu(ui); - menu = get_menu(ui); - app_indicator_set_menu(indicator, GTK_MENU(menu)); + if (menu) { + app_indicator_set_menu(indicator, menu); + g_object_unref(G_OBJECT(menu)); + } + } - gtk_widget_show_all(menu); + if (menu) + gtk_widget_show_all(GTK_WIDGET(menu)); } void ui_appindicator_init(struct ui_psensor *ui)