[gnome-applets: 261/263] netspeed: rewrite preferences
- From: Alberts Muktupāvels <muktupavels src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-applets: 261/263] netspeed: rewrite preferences
- Date: Wed, 25 Mar 2015 20:39:53 +0000 (UTC)
commit 6701d6affce51d98589614f65b2859be2884d2bc
Author: Alberts Muktupāvels <alberts muktupavels gmail com>
Date: Wed Mar 25 22:06:05 2015 +0200
netspeed: rewrite preferences
netspeed/src/Makefile.am | 4 +-
netspeed/src/netspeed.c | 396 +++++++++++++++-----------------------------
netspeed/src/netspeed.h | 8 +
netspeed/src/preferences.c | 336 +++++++++++++++++++++++++++++++++++++
netspeed/src/preferences.h | 34 ++++
5 files changed, 516 insertions(+), 262 deletions(-)
---
diff --git a/netspeed/src/Makefile.am b/netspeed/src/Makefile.am
index 91b07e2..96cc19b 100644
--- a/netspeed/src/Makefile.am
+++ b/netspeed/src/Makefile.am
@@ -16,7 +16,9 @@ netspeed_applet2_SOURCES = \
label.h \
label.c \
netspeed.h \
- netspeed.c
+ netspeed.c \
+ preferences.h \
+ preferences.c
netspeed_applet2_LDADD = \
$(GNOME_APPLETS_LIBS) \
diff --git a/netspeed/src/netspeed.c b/netspeed/src/netspeed.c
index 54cd736..cde7240 100644
--- a/netspeed/src/netspeed.c
+++ b/netspeed/src/netspeed.c
@@ -27,6 +27,7 @@
#include <glib/gi18n.h>
#include "backend.h"
#include "label.h"
+#include "preferences.h"
#include "netspeed.h"
/* Icons for the interfaces */
@@ -102,9 +103,8 @@ struct _NetspeedApplet
GtkWidget *inbytes_text;
GtkWidget *outbytes_text;
GtkDialog *details;
- GtkDialog *settings_dialog;
+ GtkWidget *preferences;
GtkDrawingArea *drawingarea;
- GtkWidget *network_device_combo;
guint index_old;
guint64 in_old[OLD_VALUES];
@@ -126,9 +126,6 @@ G_DEFINE_TYPE (NetspeedApplet, netspeed_applet, PANEL_TYPE_APPLET)
static void
update_tooltip(NetspeedApplet* applet);
-static void
-device_change_cb(GtkComboBox *combo, NetspeedApplet *applet);
-
/* Adds a Pango markup "size" to a bytestring
*/
static void
@@ -600,39 +597,6 @@ timeout_function(NetspeedApplet *applet)
return TRUE;
}
-/* Display a section of netspeed help
- */
-static void
-display_help (GtkWidget *dialog, const gchar *section)
-{
- GError *error = NULL;
- gboolean ret;
- char *uri;
-
- if (section)
- uri = g_strdup_printf ("help:netspeed_applet/%s", section);
- else
- uri = g_strdup ("help:netspeed_applet");
-
- ret = gtk_show_uri (gtk_widget_get_screen (dialog), uri, GDK_CURRENT_TIME, &error);
- g_free (uri);
- if (ret == FALSE) {
- GtkWidget *error_dialog = gtk_message_dialog_new (NULL,
- GTK_DIALOG_MODAL,
- GTK_MESSAGE_ERROR,
- GTK_BUTTONS_OK,
- _("There was an error displaying
help:\n%s"),
- error->message);
- g_signal_connect (error_dialog, "response",
- G_CALLBACK (gtk_widget_destroy), NULL);
-
- gtk_window_set_resizable (GTK_WINDOW (error_dialog), FALSE);
- gtk_window_set_screen (GTK_WINDOW (error_dialog), gtk_widget_get_screen (dialog));
- gtk_widget_show (error_dialog);
- g_error_free (error);
- }
-}
-
/* Opens gnome help application
*/
static void
@@ -640,8 +604,11 @@ help_cb (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
- NetspeedApplet *ap = NETSPEED_APPLET (user_data);
- display_help (GTK_WIDGET (ap), NULL);
+ NetspeedApplet *netspeed;
+
+ netspeed = NETSPEED_APPLET (user_data);
+
+ netspeed_applet_display_help (netspeed, NULL);
}
/* Just the about window... If it's already open, just fokus it
@@ -674,66 +641,6 @@ about_cb (GSimpleAction *action,
}
-/* this basically just retrieves the new devicestring
- * and then calls applet_device_change() and change_icons()
- */
-static void
-device_change_cb(GtkComboBox *combo, NetspeedApplet *applet)
-{
- GList *devices;
- int i, active;
-
- g_assert(combo);
- devices = g_object_get_data(G_OBJECT(combo), "devices");
- active = gtk_combo_box_get_active(combo);
- g_assert(active > -1);
-
- if (0 == active) {
- if (applet->auto_change_device)
- return;
- applet->auto_change_device = TRUE;
- } else {
- applet->auto_change_device = FALSE;
- for (i = 1; i < active; i++) {
- devices = g_list_next(devices);
- }
- if (g_str_equal(devices->data, applet->devinfo.name))
- return;
- free_device_info(&applet->devinfo);
- get_device_info(devices->data, &applet->devinfo);
- }
-
- applet->device_has_changed = TRUE;
- update_applet(applet);
-}
-
-
-/* Handle preference dialog response event
- */
-static void
-pref_response_cb (GtkDialog *dialog,
- gint id,
- gpointer user_data)
-{
- NetspeedApplet *netspeed;
-
- netspeed = NETSPEED_APPLET (user_data);
-
- if (id == GTK_RESPONSE_HELP) {
- display_help (GTK_WIDGET (dialog), "netspeed_applet-settings");
- return;
- }
-
- g_settings_set_string (netspeed->settings, "device", netspeed->devinfo.name);
- g_settings_set_boolean (netspeed->settings, "show-sum", netspeed->show_sum);
- g_settings_set_boolean (netspeed->settings, "show-bits", netspeed->show_bits);
- g_settings_set_boolean (netspeed->settings, "change-icon", netspeed->change_icon);
- g_settings_set_boolean (netspeed->settings, "auto-change-device", netspeed->auto_change_device);
-
- gtk_widget_destroy (GTK_WIDGET (netspeed->settings_dialog));
- netspeed->settings_dialog = NULL;
-}
-
/* Called when the showsum checkbutton is toggled...
*/
static void
@@ -761,145 +668,34 @@ changeicon_change_cb(GtkToggleButton *togglebutton, NetspeedApplet *applet)
change_icons(applet);
}
-/* Creates the settings dialog
- * After its been closed, take the new values and store
- * them in the gconf database
- */
+static void
+netspeed_applet_destory_preferences (GtkWidget *widget,
+ gpointer user_data)
+{
+ NetspeedApplet *netspeed;
+
+ netspeed = NETSPEED_APPLET (user_data);
+
+ netspeed->preferences = NULL;
+}
static void
preferences_cb (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
- NetspeedApplet *applet = (NetspeedApplet*)user_data;
- GtkWidget *vbox;
- GtkWidget *hbox;
- GtkWidget *categories_vbox;
- GtkWidget *category_vbox;
- GtkWidget *controls_vbox;
- GtkWidget *category_header_label;
- GtkWidget *network_device_hbox;
- GtkWidget *network_device_label;
- GtkWidget *indent_label;
- GtkWidget *show_sum_checkbutton;
- GtkWidget *show_bits_checkbutton;
- GtkWidget *change_icon_checkbutton;
- GtkWidget *dialog_vbox;
- GtkSizeGroup *category_label_size_group;
- GtkSizeGroup *category_units_size_group;
- gchar *header_str;
- GList *ptr, *devices;
- int i, active = -1;
-
- g_assert(applet);
-
- if (applet->settings_dialog)
- {
- gtk_window_present (GTK_WINDOW (applet->settings_dialog));
- return;
- }
-
- category_label_size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
- category_units_size_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
-
- applet->settings_dialog = GTK_DIALOG (gtk_dialog_new_with_buttons(_("Netspeed Preferences"),
- NULL,
- GTK_DIALOG_DESTROY_WITH_PARENT,
- GTK_STOCK_HELP, GTK_RESPONSE_HELP,
- GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT,
- NULL));
-
- gtk_window_set_resizable (GTK_WINDOW (applet->settings_dialog), FALSE);
- gtk_window_set_screen (GTK_WINDOW (applet->settings_dialog),
- gtk_widget_get_screen (GTK_WIDGET (applet->settings_dialog)));
-
- gtk_dialog_set_default_response (GTK_DIALOG (applet->settings_dialog), GTK_RESPONSE_CLOSE);
-
- vbox = gtk_vbox_new(FALSE, 0);
- gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
-
- categories_vbox = gtk_vbox_new(FALSE, 18);
- gtk_box_pack_start(GTK_BOX (vbox), categories_vbox, TRUE, TRUE, 0);
-
- category_vbox = gtk_vbox_new(FALSE, 6);
- gtk_box_pack_start(GTK_BOX (categories_vbox), category_vbox, TRUE, TRUE, 0);
-
- header_str = g_strconcat("<span weight=\"bold\">", _("General Settings"), "</span>", NULL);
- category_header_label = gtk_label_new(header_str);
- gtk_label_set_use_markup(GTK_LABEL(category_header_label), TRUE);
- gtk_label_set_justify(GTK_LABEL(category_header_label), GTK_JUSTIFY_LEFT);
- gtk_misc_set_alignment(GTK_MISC (category_header_label), 0, 0.5);
- gtk_box_pack_start(GTK_BOX (category_vbox), category_header_label, FALSE, FALSE, 0);
- g_free(header_str);
-
- hbox = gtk_hbox_new(FALSE, 0);
- gtk_box_pack_start(GTK_BOX (category_vbox), hbox, TRUE, TRUE, 0);
+ NetspeedApplet *netspeed;
- indent_label = gtk_label_new(" ");
- gtk_label_set_justify(GTK_LABEL (indent_label), GTK_JUSTIFY_LEFT);
- gtk_box_pack_start(GTK_BOX (hbox), indent_label, FALSE, FALSE, 0);
-
- controls_vbox = gtk_vbox_new(FALSE, 10);
- gtk_box_pack_start(GTK_BOX(hbox), controls_vbox, TRUE, TRUE, 0);
+ netspeed = NETSPEED_APPLET (user_data);
- network_device_hbox = gtk_hbox_new(FALSE, 6);
- gtk_box_pack_start(GTK_BOX(controls_vbox), network_device_hbox, TRUE, TRUE, 0);
-
- network_device_label = gtk_label_new_with_mnemonic(_("Network _device:"));
- gtk_label_set_justify(GTK_LABEL(network_device_label), GTK_JUSTIFY_LEFT);
- gtk_misc_set_alignment(GTK_MISC(network_device_label), 0.0f, 0.5f);
- gtk_size_group_add_widget(category_label_size_group, network_device_label);
- gtk_box_pack_start(GTK_BOX (network_device_hbox), network_device_label, FALSE, FALSE, 0);
-
- applet->network_device_combo = gtk_combo_box_text_new();
- gtk_label_set_mnemonic_widget(GTK_LABEL(network_device_label), applet->network_device_combo);
- gtk_box_pack_start (GTK_BOX (network_device_hbox), applet->network_device_combo, TRUE, TRUE, 0);
-
- /* Default means device with default route set */
- gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(applet->network_device_combo), _("Default"));
- ptr = devices = get_available_devices();
- for (i = 1; ptr; ptr = g_list_next(ptr)) {
- gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(applet->network_device_combo), ptr->data);
- if (g_str_equal(ptr->data, applet->devinfo.name)) active = i;
- ++i;
- }
- if (active < 0 || applet->auto_change_device) {
- active = 0;
+ if (netspeed->preferences) {
+ gtk_window_present (GTK_WINDOW (netspeed->preferences));
+ return;
}
- gtk_combo_box_set_active(GTK_COMBO_BOX(applet->network_device_combo), active);
- g_object_set_data_full(G_OBJECT(applet->network_device_combo), "devices", devices,
(GDestroyNotify)free_devices_list);
- show_sum_checkbutton = gtk_check_button_new_with_mnemonic(_("Show _sum instead of in & out"));
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(show_sum_checkbutton), applet->show_sum);
- gtk_box_pack_start(GTK_BOX(controls_vbox), show_sum_checkbutton, FALSE, FALSE, 0);
-
- show_bits_checkbutton = gtk_check_button_new_with_mnemonic(_("Show _bits instead of bytes"));
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(show_bits_checkbutton), applet->show_bits);
- gtk_box_pack_start(GTK_BOX(controls_vbox), show_bits_checkbutton, FALSE, FALSE, 0);
-
- change_icon_checkbutton = gtk_check_button_new_with_mnemonic(_("Change _icon according to the
selected device"));
- gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(change_icon_checkbutton), applet->change_icon);
- gtk_box_pack_start(GTK_BOX(controls_vbox), change_icon_checkbutton, FALSE, FALSE, 0);
-
- g_signal_connect(G_OBJECT (applet->network_device_combo), "changed",
- G_CALLBACK(device_change_cb), (gpointer)applet);
-
- g_signal_connect(G_OBJECT (show_sum_checkbutton), "toggled",
- G_CALLBACK(showsum_change_cb), (gpointer)applet);
-
- g_signal_connect(G_OBJECT (show_bits_checkbutton), "toggled",
- G_CALLBACK(showbits_change_cb), (gpointer)applet);
-
- g_signal_connect(G_OBJECT (change_icon_checkbutton), "toggled",
- G_CALLBACK(changeicon_change_cb), (gpointer)applet);
-
- g_signal_connect (applet->settings_dialog, "response",
- G_CALLBACK (pref_response_cb), applet);
-
- dialog_vbox = gtk_dialog_get_content_area (GTK_DIALOG (applet->settings_dialog));
- gtk_container_add(GTK_CONTAINER(dialog_vbox), vbox);
-
- gtk_widget_show_all (GTK_WIDGET (applet->settings_dialog));
+ netspeed->preferences = netspeed_preferences_new (netspeed);
+ g_signal_connect (netspeed->preferences, "destroy",
+ G_CALLBACK (netspeed_applet_destory_preferences), netspeed);
}
/* Redraws the graph drawingarea
@@ -1060,14 +856,13 @@ outcolor_changed_cb (GtkColorButton *button,
static void
info_response_cb (GtkDialog *dialog, gint id, NetspeedApplet *applet)
{
-
- if(id == GTK_RESPONSE_HELP){
- display_help (GTK_WIDGET (dialog), "netspeed_applet-details");
+ if (id == GTK_RESPONSE_HELP) {
+ netspeed_applet_display_help (applet, "netspeed_applet-details");
return;
}
-
+
gtk_widget_destroy(GTK_WIDGET(applet->details));
-
+
applet->details = NULL;
applet->inbytes_text = NULL;
applet->outbytes_text = NULL;
@@ -1341,6 +1136,8 @@ netspeed_applet_finalize (GObject *object)
g_free (netspeed->down_cmd);
free_device_info (&netspeed->devinfo);
+
+ G_OBJECT_CLASS (netspeed_applet_parent_class)->finalize (object);
}
static gboolean
@@ -1504,11 +1301,20 @@ device_changed (NetspeedApplet *netspeed)
device = g_settings_get_string (netspeed->settings, "device");
- if (g_strcmp0 (device, "") == 0)
+ if (g_strcmp0 (device, netspeed->devinfo.name) == 0) {
+ g_free (device);
return;
+ }
+
+ if (g_strcmp0 (device, "") == 0) {
+ g_free (device);
+ device = netspeed_applet_get_auto_device_name ();
+ }
get_device_info (device, &netspeed->devinfo);
g_free (device);
+
+ netspeed->device_has_changed = TRUE;
}
static void
@@ -1560,37 +1366,50 @@ netspeed_applet_settings_changed (GSettings *settings,
netspeed = NETSPEED_APPLET (user_data);
- if (key == NULL || g_strcmp0 (key, "refresh-time")) {
+ if (key == NULL || g_strcmp0 (key, "refresh-time") == 0) {
netspeed->refresh_time = g_settings_get_int (netspeed->settings, "refresh-time");
netspeed_applet_setup_timeout (netspeed);
}
- if (key == NULL || g_strcmp0 (key, "show-sum"))
+ if (key == NULL || g_strcmp0 (key, "show-sum") == 0)
netspeed->show_sum = g_settings_get_boolean (netspeed->settings, "show-sum");
- if (key == NULL || g_strcmp0 (key, "show-bits"))
+ if (key == NULL || g_strcmp0 (key, "show-bits") == 0)
netspeed->show_bits = g_settings_get_boolean (netspeed->settings, "show-bits");
- if (key == NULL || g_strcmp0 (key, "change-icon"))
+ if (key == NULL || g_strcmp0 (key, "change-icon") == 0) {
netspeed->change_icon = g_settings_get_boolean (netspeed->settings, "change-icon");
- if (key == NULL || g_strcmp0 (key, "auto-change-device"))
+ if (key != NULL)
+ change_icons (netspeed);
+ }
+
+ if (key == NULL || g_strcmp0 (key, "auto-change-device") == 0)
netspeed->auto_change_device = g_settings_get_boolean (netspeed->settings,
"auto-change-device");
- if (key == NULL || g_strcmp0 (key, "device"))
+ if (key == NULL || g_strcmp0 (key, "device") == 0) {
device_changed (netspeed);
- if (key == NULL || g_strcmp0 (key, "up-command"))
+ if (key != NULL)
+ change_icons (netspeed);
+ }
+
+ if (key == NULL || g_strcmp0 (key, "up-command") == 0)
up_command_changed (netspeed);
- if (key == NULL || g_strcmp0 (key, "down-command"))
+ if (key == NULL || g_strcmp0 (key, "down-command") == 0)
down_command_changed (netspeed);
- if (key == NULL || g_strcmp0 (key, "in-color"))
+ if (key == NULL || g_strcmp0 (key, "in-color") == 0)
in_color_changed (netspeed);
- if (key == NULL || g_strcmp0 (key, "out-color"))
+ if (key == NULL || g_strcmp0 (key, "out-color") == 0)
out_color_changed (netspeed);
+
+ if (key != NULL) {
+ applet_change_size_or_orient (PANEL_APPLET (netspeed), -1, netspeed);
+ update_applet (netspeed);
+ }
}
static gboolean
@@ -1629,22 +1448,6 @@ netspeed_applet_factory (PanelApplet *applet,
G_CALLBACK (netspeed_applet_settings_changed),
netspeed);
- if (!netspeed->devinfo.name) {
- GList *ptr, *devices = get_available_devices();
- ptr = devices;
- while (ptr) {
- if (!g_str_equal(ptr->data, "lo")) {
- get_device_info (ptr->data, &netspeed->devinfo);
- break;
- }
- ptr = g_list_next(ptr);
- }
- free_devices_list(devices);
- }
- if (!netspeed->devinfo.name)
- get_device_info ("lo", &netspeed->devinfo);
- netspeed->device_has_changed = TRUE;
-
netspeed->in_label = netspeed_label_new ();
netspeed->out_label = netspeed_label_new ();
netspeed->sum_label = netspeed_label_new ();
@@ -1693,6 +1496,77 @@ netspeed_applet_factory (PanelApplet *applet,
return TRUE;
}
+void
+netspeed_applet_display_help (NetspeedApplet *netspeed,
+ const gchar *section)
+{
+ GError *error;
+ gboolean ret;
+ gchar *uri;
+
+ if (section)
+ uri = g_strdup_printf ("help:netspeed_applet/%s", section);
+ else
+ uri = g_strdup ("help:netspeed_applet");
+
+ error = NULL;
+ ret = gtk_show_uri (gtk_widget_get_screen (GTK_WIDGET (netspeed)),
+ uri, GDK_CURRENT_TIME, &error);
+ g_free (uri);
+
+ if (ret == FALSE) {
+ GtkWidget *error_dialog;
+
+ error_dialog = gtk_message_dialog_new (NULL,
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_OK,
+ _("There was an error displaying help:\n%s"),
+ error->message);
+ g_error_free (error);
+
+ g_signal_connect (error_dialog, "response",
+ G_CALLBACK (gtk_widget_destroy), NULL);
+
+ gtk_window_set_resizable (GTK_WINDOW (error_dialog), FALSE);
+ gtk_widget_show (error_dialog);
+ }
+}
+
+GSettings *
+netspeed_applet_get_settings (NetspeedApplet *netspeed)
+{
+ return netspeed->settings;
+}
+
+const gchar *
+netspeed_applet_get_current_device_name (NetspeedApplet *netspeed)
+{
+ return netspeed->devinfo.name;
+}
+
+gchar *
+netspeed_applet_get_auto_device_name (void)
+{
+ GList *devices;
+ GList *ptr;
+ gchar *device = NULL;
+
+ for (; ptr; ptr = ptr->next) {
+ if (g_strcmp0 (ptr->data, "lo") != 0) {
+ device = g_strdup (ptr->data);
+ break;
+ }
+ }
+
+ free_devices_list (devices);
+
+ if (device != NULL)
+ return device;
+
+ return g_strdup ("lo");
+}
+
PANEL_APPLET_OUT_PROCESS_FACTORY ("NetspeedAppletFactory",
NETSPEED_TYPE_APPLET,
netspeed_applet_factory,
diff --git a/netspeed/src/netspeed.h b/netspeed/src/netspeed.h
index dca1b14..7991af3 100644
--- a/netspeed/src/netspeed.h
+++ b/netspeed/src/netspeed.h
@@ -28,4 +28,12 @@ G_DECLARE_FINAL_TYPE (NetspeedApplet, netspeed_applet,
NETSPEED, APPLET,
PanelApplet)
+void netspeed_applet_display_help (NetspeedApplet *netspeed,
+ const gchar *section);
+
+GSettings *netspeed_applet_get_settings (NetspeedApplet *netspeed);
+
+const gchar *netspeed_applet_get_current_device_name (NetspeedApplet *netspeed);
+gchar *netspeed_applet_get_auto_device_name (void);
+
#endif
diff --git a/netspeed/src/preferences.c b/netspeed/src/preferences.c
new file mode 100644
index 0000000..3681c1f
--- /dev/null
+++ b/netspeed/src/preferences.c
@@ -0,0 +1,336 @@
+/*
+ * Copyright (C) 2015 Alberts Muktupāvels
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Alberts Muktupāvels <alberts muktupavels gmail com>
+ * Jörgen Scheibengruber <mfcn gmx de>
+ */
+
+#include <glib/gi18n.h>
+#include "backend.h"
+#include "preferences.h"
+
+struct _NetspeedPreferences
+{
+ GtkDialog parent;
+
+ NetspeedApplet *netspeed;
+ GSettings *settings;
+};
+
+G_DEFINE_TYPE (NetspeedPreferences, netspeed_preferences, GTK_TYPE_DIALOG)
+
+static void
+device_change_cb (GtkComboBox *combo_box,
+ gpointer user_data)
+{
+ NetspeedPreferences *preferences;
+ gboolean auto_change_device;
+ gint active;
+ gint i;
+
+ preferences = NETSPEED_PREFERENCES (user_data);
+
+ auto_change_device = g_settings_get_boolean (preferences->settings, "auto-change-device");
+ active = gtk_combo_box_get_active (combo_box);
+
+ if (active == 0) {
+ if (auto_change_device)
+ return;
+
+ auto_change_device = TRUE;
+
+ g_settings_set_string (preferences->settings, "device", "");
+ } else {
+ GList *devices;
+
+ auto_change_device = FALSE;
+ devices = g_object_get_data (G_OBJECT (combo_box), "devices");
+
+ for (i = 1; i < active; i++)
+ devices = devices->next;
+
+ g_settings_set_string (preferences->settings, "device", devices->data);
+ }
+
+ g_settings_set_boolean (preferences->settings, "auto-change-device", auto_change_device);
+}
+
+static void
+auto_change_device_settings_changed (GSettings *settings,
+ const gchar *key,
+ gpointer user_data)
+{
+ gboolean auto_change_device;
+ gchar *device;
+
+ auto_change_device = g_settings_get_boolean (settings, "auto-change-device");
+ device = g_settings_get_string (settings, "device");
+
+ if (auto_change_device) {
+ if (g_strcmp0 (device, "") != 0)
+ g_settings_set_string (settings, "device", "");
+ } else {
+ if (g_strcmp0 (device, "") == 0) {
+ gchar *tmp_device;
+
+ tmp_device = netspeed_applet_get_auto_device_name ();
+ g_settings_set_string (settings, "device", tmp_device);
+ g_free (tmp_device);
+ }
+ }
+
+ g_free (device);
+}
+
+static void
+device_settings_changed (GSettings *settings,
+ const gchar *key,
+ gpointer user_data)
+{
+ GtkComboBox *combo_box;
+ GList *devices;
+ GList *ptr;
+ gchar *device;
+ gint active;
+ gint i;
+
+ if (g_strcmp0 (key, "device") != 0)
+ return;
+
+ combo_box = GTK_COMBO_BOX (user_data);
+ device = g_settings_get_string (settings, "device");
+ ptr = devices = get_available_devices ();
+
+ active = 0;
+ for (i = 1; ptr; ptr = ptr->next) {
+ if (g_str_equal (ptr->data, device)) {
+ active = i;
+ break;
+ }
+
+ i++;
+ }
+
+ gtk_combo_box_set_active (combo_box, active);
+
+ if (active == 0 && g_strcmp0 (device, "") != 0) {
+ g_settings_set_string (settings, "device", "");
+ g_settings_set_boolean (settings, "auto-change-device", TRUE);
+ }
+
+ free_devices_list (devices);
+ g_free (device);
+}
+
+GtkWidget *
+create_network_hbox (NetspeedPreferences *preferences)
+{
+ GtkWidget *network_device_hbox;
+ GtkWidget *network_device_label;
+ GtkWidget *network_device_combo;
+ GList *ptr;
+ GList *devices;
+ gint i;
+ gint active;
+
+ network_device_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
+
+ network_device_label = gtk_label_new_with_mnemonic (_("Network _device:"));
+ gtk_label_set_justify (GTK_LABEL (network_device_label), GTK_JUSTIFY_LEFT);
+ gtk_label_set_xalign (GTK_LABEL (network_device_label), 0.0);
+ gtk_box_pack_start (GTK_BOX (network_device_hbox), network_device_label, FALSE, FALSE, 0);
+
+ network_device_combo = gtk_combo_box_text_new ();
+ gtk_label_set_mnemonic_widget (GTK_LABEL (network_device_label), network_device_combo);
+ gtk_box_pack_start (GTK_BOX (network_device_hbox), network_device_combo, TRUE, TRUE, 0);
+
+ /* Default means device with default route set */
+ gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (network_device_combo), _("Default"));
+
+ active = 0;
+ ptr = devices = get_available_devices ();
+ for (i = 1; ptr; ptr = ptr->next) {
+ gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (network_device_combo), ptr->data);
+ if (g_str_equal (ptr->data, netspeed_applet_get_current_device_name (preferences->netspeed)))
+ active = i;
+ i++;
+ }
+
+ if (g_settings_get_boolean (preferences->settings, "auto-change-device"))
+ active = 0;
+
+ gtk_combo_box_set_active (GTK_COMBO_BOX (network_device_combo), active);
+
+ g_object_set_data_full (G_OBJECT (network_device_combo),
+ "devices", devices,
+ (GDestroyNotify) free_devices_list);
+
+ g_signal_connect (network_device_combo, "changed",
+ G_CALLBACK (device_change_cb), preferences);
+ g_signal_connect (preferences->settings, "changed::auto-change-device",
+ G_CALLBACK (auto_change_device_settings_changed), network_device_combo);
+ g_signal_connect (preferences->settings, "changed::device",
+ G_CALLBACK (device_settings_changed), network_device_combo);
+
+ return network_device_hbox;
+}
+
+static void
+netspeed_preferences_setup (NetspeedPreferences *preferences)
+{
+ GtkDialog *dialog;
+ GtkWidget *vbox;
+ GtkWidget *categories;
+ GtkWidget *category;
+ gchar *header_title;
+ GtkWidget *category_label;
+ GtkWidget *hbox;
+ GtkWidget *indent_label;
+ GtkWidget *controls;
+ GtkWidget *show_sum_checkbutton;
+ GtkWidget *show_bits_checkbutton;
+ GtkWidget *change_icon_checkbutton;
+
+ dialog = GTK_DIALOG (preferences);
+
+ vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
+ gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
+
+ categories = gtk_box_new (GTK_ORIENTATION_VERTICAL, 18);
+ gtk_box_pack_start (GTK_BOX (vbox), categories, TRUE, TRUE, 0);
+
+ category = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
+ gtk_box_pack_start (GTK_BOX (categories), category, TRUE, TRUE, 0);
+
+ header_title = g_strconcat ("<span weight=\"bold\">", _("General Settings"), "</span>", NULL);
+ category_label = gtk_label_new (header_title);
+ g_free (header_title);
+
+ gtk_label_set_use_markup (GTK_LABEL (category_label), TRUE);
+ gtk_label_set_justify (GTK_LABEL (category_label), GTK_JUSTIFY_LEFT);
+ gtk_label_set_xalign (GTK_LABEL (category_label), 0.0);
+ gtk_box_pack_start (GTK_BOX (category), category_label, FALSE, FALSE, 0);
+
+ hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+ gtk_box_pack_start (GTK_BOX (category), hbox, TRUE, TRUE, 0);
+
+ indent_label = gtk_label_new (" ");
+ gtk_label_set_justify (GTK_LABEL (indent_label), GTK_JUSTIFY_LEFT);
+ gtk_box_pack_start (GTK_BOX (hbox), indent_label, FALSE, FALSE, 0);
+
+ controls = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
+ gtk_box_pack_start (GTK_BOX (hbox), controls, TRUE, TRUE, 0);
+
+ gtk_box_pack_start (GTK_BOX (controls), create_network_hbox (preferences), TRUE, TRUE, 0);
+
+ show_sum_checkbutton = gtk_check_button_new_with_mnemonic (_("Show _sum instead of in & out"));
+ gtk_box_pack_start (GTK_BOX (controls), show_sum_checkbutton, FALSE, FALSE, 0);
+ g_settings_bind (preferences->settings, "show-sum",
+ show_sum_checkbutton, "active",
+ G_SETTINGS_BIND_DEFAULT);
+
+ show_bits_checkbutton = gtk_check_button_new_with_mnemonic (_("Show _bits instead of bytes"));
+ gtk_box_pack_start (GTK_BOX (controls), show_bits_checkbutton, FALSE, FALSE, 0);
+ g_settings_bind (preferences->settings, "show-bits",
+ show_bits_checkbutton, "active",
+ G_SETTINGS_BIND_DEFAULT);
+
+ change_icon_checkbutton = gtk_check_button_new_with_mnemonic (_("Change _icon according to the selected
device"));
+ gtk_box_pack_start (GTK_BOX (controls), change_icon_checkbutton, FALSE, FALSE, 0);
+ g_settings_bind (preferences->settings, "change-icon",
+ change_icon_checkbutton, "active",
+ G_SETTINGS_BIND_DEFAULT);
+
+ gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (dialog)), vbox);
+}
+
+static void
+netspeed_preferences_finalize (GObject *object)
+{
+ G_OBJECT_CLASS (netspeed_preferences_parent_class)->finalize (object);
+}
+
+static void
+netspeed_preferences_response (GtkDialog *dialog,
+ gint response_id)
+{
+ NetspeedPreferences *preferences;
+
+ preferences = NETSPEED_PREFERENCES (dialog);
+
+ switch (response_id) {
+ case GTK_RESPONSE_HELP:
+ netspeed_applet_display_help (preferences->netspeed, "netspeed_applet-settings");
+ break;
+ default:
+ gtk_widget_destroy (GTK_WIDGET (preferences));
+ break;
+ }
+}
+
+static void
+netspeed_preferences_class_init (NetspeedPreferencesClass *preferences_class)
+{
+ GObjectClass *object_class;
+ GtkDialogClass *dialog_class;
+
+ object_class = G_OBJECT_CLASS (preferences_class);
+ dialog_class = GTK_DIALOG_CLASS (preferences_class);
+
+ object_class->finalize = netspeed_preferences_finalize;
+
+ dialog_class->response = netspeed_preferences_response;
+}
+
+static void
+netspeed_preferences_init (NetspeedPreferences *preferences)
+{
+}
+
+GtkWidget *
+netspeed_preferences_new (NetspeedApplet *netspeed)
+{
+ NetspeedPreferences *preferences;
+ GtkDialog *dialog;
+ GtkWidget *widget;
+
+ preferences = g_object_new (NETSPEED_TYPE_PREFERENCES,
+ "title", _("Netspeed Preferences"),
+ "screen", gtk_widget_get_screen (GTK_WIDGET (netspeed)),
+ "resizable", FALSE,
+ NULL);
+
+ preferences->netspeed = netspeed;
+ preferences->settings = netspeed_applet_get_settings (netspeed);
+
+ dialog = GTK_DIALOG (preferences);
+ widget = GTK_WIDGET (dialog);
+
+ gtk_dialog_add_buttons (dialog,
+ _("_Help"), GTK_RESPONSE_HELP,
+ _("_Close"), GTK_RESPONSE_ACCEPT,
+ NULL);
+ gtk_dialog_set_default_response (dialog, GTK_RESPONSE_CLOSE);
+
+ gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
+
+ netspeed_preferences_setup (preferences);
+
+ gtk_widget_show_all (widget);
+
+ return widget;
+}
diff --git a/netspeed/src/preferences.h b/netspeed/src/preferences.h
new file mode 100644
index 0000000..a716fc7
--- /dev/null
+++ b/netspeed/src/preferences.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2015 Alberts Muktupāvels
+ *
+ * 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ * Alberts Muktupāvels <alberts muktupavels gmail com>
+ */
+
+#ifndef NETSPEED_PREFERENCES_H
+#define NETSPEED_PREFERENCES_H
+
+#include <gtk/gtk.h>
+#include "netspeed.h"
+
+#define NETSPEED_TYPE_PREFERENCES netspeed_preferences_get_type ()
+G_DECLARE_FINAL_TYPE (NetspeedPreferences, netspeed_preferences,
+ NETSPEED, PREFERENCES,
+ GtkDialog)
+
+GtkWidget *netspeed_preferences_new (NetspeedApplet *netspeed);
+
+#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]