/* GKrellM giFT plugin * Copyright (C) 2002, 2003 Tilman Sauerbeck * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * 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, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * */ #include "common.h" #include "gift.h" #include "cfg.h" #define CONFIG_SAVE_STR(key, val) fprintf(fp, PLUGIN_NAME " %s %s\n", (key), (val)) #define CONFIG_SAVE_INT(key, val) fprintf(fp, PLUGIN_NAME " %s %i\n", (key), (val)) #define CONFIG_SAVE_COLOR(key, val) fprintf(fp, PLUGIN_NAME " %s %04hx%04hx%04hx\n", (key), (val).red, (val).green, (val).blue) #define CONFIG_SAVE_CHARTCFG(val) gkrellm_save_chartconfig(fp, (val), PLUGIN_NAME, NULL); #define CONFIG_READ_STR(keyw, dest) \ if (!strcmp(key, (keyw))) \ gkrellm_dup_string(&(dest), value); #define CONFIG_READ_INT(keyw, dest) \ if (!strcmp(key, (keyw))) \ (dest) = atoi(value); #define CONFIG_READ_COLOR(keyw, dest) \ if (!strcmp(key, (keyw))) \ color_copy(&(dest), NULL, value); #define CONFIG_READ_CHARTCFG(keyw, dest) \ if (!strcmp(key, (keyw))) \ gkrellm_load_chartconfig(&(dest), value, 2); static GtkWidget *host_entry[2]; static GtkWidget *color_label[DIR_NUM]; static GtkWidget *max_transfers_spin[DIR_NUM]; static GtkWidget *visibility_checkbtn_chart; static GtkWidget *visibility_checkbtn_panels; static GtkWidget *chart_lbl_fmt_cbo; static GdkColor new_transfer_color[DIR_NUM]; static gchar *color_label_caption[DIR_NUM] = {"Download color", "Upload color"}; extern giFTConfig gift_cfg; static void color_parse(gchar *str, GdkColor *color) { if (!color) return; sscanf(str, "%04hx", (guint16 *) &((*color).red)); str += 4; sscanf(str, "%04hx", (guint16 *) &((*color).green)); str += 4; sscanf(str, "%04hx", (guint16 *) &((*color).blue)); } static void color_copy(GdkColor *dest, GdkColor *src, gchar *str) { GdkColormap *colormap = gift_get_colormap(); if (dest->red || dest->green || dest->blue) gdk_colormap_free_colors(colormap, dest, 1); if (str) color_parse(str, dest); else { dest->red = src->red; dest->green = src->green; dest->blue = src->blue; } gdk_colormap_alloc_color(colormap, dest, FALSE, TRUE); } /* creates a framed box. if @is_vbox is TRUE, a vbox is created, if it's FALSE, * a hbox is created */ static GtkWidget *create_framed_box(GtkWidget *parent, gboolean is_vbox, gchar *caption) { GtkWidget *hbox = gtk_hbox_new(FALSE, 5); GtkWidget *box; GtkWidget *frame; gtk_box_pack_start(GTK_BOX(parent), hbox, FALSE, FALSE, 5); frame = gtk_frame_new(caption); gtk_container_set_border_width(GTK_CONTAINER(frame), 0); gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, TRUE, 0);//5 box = (is_vbox) ? gtk_vbox_new(FALSE, 0) : gtk_hbox_new(FALSE, 0); gtk_container_set_border_width(GTK_CONTAINER(box), 5); gtk_container_add(GTK_CONTAINER(frame), box); return box; } static GtkWidget *create_table(GtkWidget *parent, gint rows, gint cols) { GtkWidget *table = gtk_table_new(rows, cols, FALSE); gtk_table_set_col_spacings(GTK_TABLE(table), 10); gtk_table_set_row_spacings(GTK_TABLE(table), 10); gtk_box_pack_start(GTK_BOX(parent), table, FALSE, FALSE, 5); return table; } static void box_create_target_system(GtkWidget *parent) { GtkWidget *box = create_framed_box(parent, FALSE, "System to connect to"); GtkWidget *label; GtkWidget *table = create_table(box, 2, 2); gchar *captions[] = {"Host", "Port"}; gchar port[6]; gchar *values[] = {gift_cfg.host, port}; gint i; snprintf(port, 6, "%i", gift_cfg.port); for (i = 0; i < 2; i++) { label = gtk_label_new(captions[i]); gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); gtk_table_attach(GTK_TABLE(table), label, 0, 1, i, i + 1, GTK_FILL, GTK_SHRINK, 0, 0); host_entry[i] = gtk_entry_new(); gtk_entry_set_text(GTK_ENTRY(host_entry[i]), values[i]); gtk_table_attach_defaults(GTK_TABLE(table), host_entry[i], 1, 2, i, i + 1); } } static void on_color_changed(GtkColorSelection *select, GdkColor *color) { gtk_color_selection_get_current_color(select, color); } static void show_color_dlg(GtkButton *button, gpointer data) { GtkColorSelection *select; GtkWidget *dlg; GdkColor *color; gchar *buf; gint i = GPOINTER_TO_INT(data); dlg = gtk_color_selection_dialog_new("Choose color"); select = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dlg)->colorsel); color = &new_transfer_color[i]; gtk_color_selection_set_previous_color(select, color); gtk_color_selection_set_current_color(select, color); gtk_color_selection_set_has_palette(select, TRUE); g_signal_connect(G_OBJECT(select), "color_changed", G_CALLBACK(on_color_changed), (gpointer) color); if (gtk_dialog_run(GTK_DIALOG(dlg)) == GTK_RESPONSE_OK) gtk_color_selection_get_current_color(select, color); gtk_widget_hide(dlg); /* update labels */ buf = g_strdup_printf("%s", color->red, color->green, color->blue, color_label_caption[i]); gtk_label_set_markup(GTK_LABEL(color_label[i]), buf); g_free(buf); } static void box_create_transfer_colors(GtkWidget *parent) { GtkWidget *box = create_framed_box(parent, FALSE, "Panel colors"); GtkWidget *table = create_table(box, 2, 2); GtkWidget *button; GtkWidget *img; gchar *buf; gint i; for (i = 0; i < 2; i++) { /* create label */ color_label[i] = gtk_label_new(NULL); gtk_misc_set_alignment(GTK_MISC(color_label[i]), 0, 0.5); color_copy(&new_transfer_color[i], &gift_cfg.transfer_color[i], NULL); buf = g_strdup_printf("%s", new_transfer_color[i].red, new_transfer_color[i].green, new_transfer_color[i].blue, color_label_caption[i]); gtk_label_set_markup(GTK_LABEL(color_label[i]), buf); g_free(buf); gtk_table_attach_defaults(GTK_TABLE(table), color_label[i], 0, 1, i, i + 1); /* create button */ button = gtk_button_new(); img = gtk_image_new_from_stock(GTK_STOCK_COLOR_PICKER, GTK_ICON_SIZE_BUTTON); gtk_container_add(GTK_CONTAINER(button), img); g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(show_color_dlg), GINT_TO_POINTER(i)); gtk_table_attach_defaults(GTK_TABLE(table), button, 1, 2, i, i + 1); } } static void box_create_transfer_limits(GtkWidget *parent) { GtkWidget *box = create_framed_box(parent, TRUE, "Max. transfers to display"); gchar *label[DIR_NUM] = {"Max. downloads to display", "Max. uploads to display"}; gint i; for (i = 0; i < 2; i++) gkrellm_gtk_spin_button(box, &max_transfers_spin[i], (gfloat) gift_cfg.max_transfers[i], 0.0, 10.0, 1.0, 1.0, 0, -1, NULL, NULL, TRUE, label[i]); } static void box_create_visibility(GtkWidget *parent) { GtkWidget *box = create_framed_box(parent, TRUE, "Visibility"); gkrellm_gtk_check_button_connected(box, &visibility_checkbtn_chart, gift_cfg.show_chart, FALSE, FALSE, 0, NULL, NULL, "Show chart"); gkrellm_gtk_check_button_connected(box, &visibility_checkbtn_panels, gift_cfg.show_panels, FALSE, FALSE, 0, NULL, NULL, "Show transfer panels"); } static void box_create_chart_lbl_fmt(GtkWidget *parent) { GtkWidget *box = create_framed_box(parent, TRUE, "Format string for chart label"); GList *list; /* create list with combo entries */ list = g_list_append(NULL, DEFAULT_CHART_LBL_FMT); list = g_list_append(list, gift_cfg.chart_lbl_fmt); chart_lbl_fmt_cbo = gtk_combo_new(); gtk_combo_set_popdown_strings(GTK_COMBO(chart_lbl_fmt_cbo), list); /* set the currently selected item */ gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(chart_lbl_fmt_cbo)->entry), gift_cfg.chart_lbl_fmt); gtk_box_pack_start(GTK_BOX(box), chart_lbl_fmt_cbo, FALSE, FALSE, 0); } static void tab_fill_setup_options(GtkWidget *parent) { GtkWidget *vbox = gtk_vbox_new(TRUE, 5); GtkWidget *hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(parent), vbox, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); box_create_transfer_colors(hbox); box_create_transfer_limits(hbox); hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); box_create_chart_lbl_fmt(hbox); } static void tab_fill_setup_general(GtkWidget *parent) { GtkWidget *vbox = gtk_vbox_new(TRUE, 5); GtkWidget *hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(parent), vbox, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); box_create_target_system(hbox); hbox = gtk_hbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0); box_create_visibility(hbox); } static void tab_fill_info(GtkWidget *box) { GtkWidget *txt_view; gchar *info_txt[] = { "GKrellM giFT\n\n", "This plugin monitors your active giFT transfers and their progress.\n", "Each transfer is displayed in a panel showing the filename and\n", "the percentage of transmitted bytes.\n" "In addition, a small image is displayed indicating the direction\n", "of the transfer.\n", "There's also a chart showing download and upload speed.\n", "By clicking the chart with the left mouse button, you can toggle\n", "the chart label's visibility state.\n\n", "Configuration\n", "Host ", "and ", "port ", "should be self-explanatory. These describe the\n", "system to connect to. When you first run the plugin, it will try\n", "to read ", "~/.giFT/ui/ui.conf ", "to gather this information.\n", "If that file isn't readable, the default host and port will be used,\n", "which is ", "localhost:1213.\n\n", "Download ", "and ", "upload color", ": These are the colors used to draw\n", "the texts in the panels.\n\n", "Max. downloads/uploads to display", ": If there are more\n", "downloads/uploads than the value you set here, these transfers\n", "won't be shown until some other transfer finishes.\n\n", "Format string for chart label", ":\nDefines which information is displayed in the chart.\n", "Available variables/control characters include:\n", "* GKrellM's standard variables\n", "* $Sd: downloads speed\n", "* $Su: uploads speed\n", "* $Td: number of current downloads\n", "* $Tu: number of current uploads\n", "* $Tt: number of downloads and uploads\n", "* $Us: Number of users\n\n", "Both the transfer panels and the chart can be turned off.\n"}; txt_view = gkrellm_gtk_scrolled_text_view(box, NULL, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gkrellm_gtk_text_view_append_strings(txt_view, info_txt, G_N_ELEMENTS(info_txt)); } static void tab_fill_about(GtkWidget *box) { GtkWidget *label; gchar *buf = g_strdup_printf("GKrellM giFT %s\n\n" "Copyright (C) 2002, 2003 by Tilman Sauerbeck\n" "tilman@code-monkey.de\n" "www.code-monkey.de/gkrellm-gift.html\n\n" "Released under the GNU General Public Licence.\n", VERSION); label = gtk_label_new(buf); gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER); g_free(buf); gtk_container_add(GTK_CONTAINER(box), label); } void gift_config_create(GtkWidget *box) { GtkWidget *notebook = gtk_notebook_new(); gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP); gtk_container_add(GTK_CONTAINER(box), notebook); tab_fill_setup_general(gkrellm_gtk_framed_notebook_page(notebook, "General")); tab_fill_setup_options(gkrellm_gtk_framed_notebook_page(notebook, "Options")); tab_fill_info(gkrellm_gtk_framed_notebook_page(notebook, "Info")); tab_fill_about(gkrellm_gtk_framed_notebook_page(notebook, "About")); } void gift_config_apply() { gchar *host; gchar *fmt; gboolean reconnect = FALSE; gboolean active; gint port; gint num; gint i; for (i = 0; i < 2; i++) { if (!gdk_color_equal(&new_transfer_color[i], &gift_cfg.transfer_color[i])) { color_copy(&gift_cfg.transfer_color[i], &new_transfer_color[i], NULL); reconnect = TRUE; } num = (gint) gtk_spin_button_get_value(GTK_SPIN_BUTTON(max_transfers_spin[i])); if (gift_cfg.max_transfers[i] != num) { gift_cfg.max_transfers[i] = num; reconnect = TRUE; } } host = gkrellm_gtk_entry_get_text(&host_entry[0]); port = atoi(gkrellm_gtk_entry_get_text(&host_entry[1])); if (strcmp(host, gift_cfg.host)) { gkrellm_dup_string(&gift_cfg.host, host); reconnect = TRUE; } if (port != gift_cfg.port) { gift_cfg.port = port; reconnect = TRUE; } /* update visibility */ active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(visibility_checkbtn_chart)); if (active != gift_cfg.show_chart) { gift_cfg.show_chart = active; gift_chart_set_visibility(); } active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(visibility_checkbtn_panels)); if (active != gift_cfg.show_panels) { gift_cfg.show_panels = active; gift_panels_set_visibility(active); } /* update chart label format string */ fmt = gkrellm_gtk_entry_get_text(&(GTK_COMBO(chart_lbl_fmt_cbo)->entry)); gkrellm_dup_string(&gift_cfg.chart_lbl_fmt, fmt); if (reconnect) { /* settings were changed, so reconnect. kinda lame, but effective * FIXME: find nicer solution */ gift_monitor_disabled(); gift_try_connect(); } } void gift_config_save(FILE *fp) { gchar *color[DIR_NUM] = {"color_download", "color_upload"}; gchar *max_transfers[DIR_NUM] = {"max_transfers_download", "max_transfers_upload"}; gint i; CONFIG_SAVE_STR("host", gift_cfg.host); CONFIG_SAVE_STR("chart_lbl_fmt", gift_cfg.chart_lbl_fmt); CONFIG_SAVE_INT("port", gift_cfg.port); CONFIG_SAVE_INT("show_chart", gift_cfg.show_chart); CONFIG_SAVE_INT("show_panels", gift_cfg.show_panels); CONFIG_SAVE_INT("show_chart_lbl", gift_cfg.show_chart_lbl); CONFIG_SAVE_CHARTCFG(gift_cfg.chart_cfg); for (i = 0; i < 2; i++) { CONFIG_SAVE_COLOR(color[i], gift_cfg.transfer_color[i]); CONFIG_SAVE_INT(max_transfers[i], gift_cfg.max_transfers[i]); } } void gift_config_load(gchar *arg) { gchar key[32]; gchar value[CFG_BUFSIZE]; if (sscanf(arg, "%31s %[^\n]", key, value) != 2) return; CONFIG_READ_STR("host", gift_cfg.host); CONFIG_READ_INT("port", gift_cfg.port); CONFIG_READ_INT("show_chart", gift_cfg.show_chart); CONFIG_READ_INT("show_panels", gift_cfg.show_panels); CONFIG_READ_INT("show_chart_lbl", gift_cfg.show_chart_lbl); CONFIG_READ_STR("chart_lbl_fmt", gift_cfg.chart_lbl_fmt); CONFIG_READ_COLOR("color_upload", gift_cfg.transfer_color[DIR_UP]); CONFIG_READ_COLOR("color_download", gift_cfg.transfer_color[DIR_DOWN]); CONFIG_READ_INT("max_transfers_upload", gift_cfg.max_transfers[DIR_UP]); CONFIG_READ_INT("max_transfers_download", gift_cfg.max_transfers[DIR_DOWN]); CONFIG_READ_CHARTCFG(GKRELLM_CHARTCONFIG_KEYWORD, gift_cfg.chart_cfg); } /* reads giFT's ui.conf and saves the host information to *cfg */ void gift_read_gift_config(giFTConfig *dest) { Config *src = gift_config_new("ui"); if (src) { gkrellm_dup_string(&dest->host, config_get_str(src, "daemon/host=localhost")); dest->port = config_get_int(src, "daemon/port=1213"); } else { gkrellm_dup_string(&dest->host, "localhost"); dest->port = 1213; } }