sort by position the list of sensors in the ubuntu application indicator
[psensor.git] / src / ui_appindicator.c
index f723528..9fbeaa7 100644 (file)
@@ -157,39 +157,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,6 +193,33 @@ static GtkWidget *get_menu(struct ui_psensor *ui)
 
        sensors[j] = NULL;
 
+       free(sorted_sensors);
+}
+
+static GtkWidget *get_menu(struct ui_psensor *ui)
+{
+       GtkActionGroup *action_group;
+       GtkUIManager *menu_manager;
+       GError *error;
+       GtkMenu *menu;
+
+       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"));
+
+       build_sensor_menu_items(ui, menu);
+
        return GTK_WIDGET(menu);
 }