/* * File: dillocfg-gtk.c * * Copyright (C) 2003 Kiyo * * 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. */ #include #include #include #include #include #include #include "../config.h" #include "../src/intl.h" #include "dillocfg.h" #include "dillocfg_option.h" #include "dillocfg_token.h" static gboolean modified_ = FALSE; #define IS_LINE_END(c) (c == '\0' || c == '#') #define SKIPSPACE(buf, i) while (!IS_LINE_END(buf[i]) && isspace(buf[i])) i++ DillocfgWin *DillocfgWin_new(); void DillocfgWin_init(DillocfgWin *d, gboolean use_default); void DillocfgWin_free(GtkWidget *widget, DillocfgWin *d); void DillocfgWin_callback_reset(GtkButton *button, gpointer user_data); void DillocfgWin_callback_reload(GtkButton *button, gpointer user_data); void DillocfgWin_callback_save(GtkButton *button, gpointer user_data); DillocfgInterface *DillocfgInterface_new(DillocfgWin *w, const gchar *label); DillocfgFont *DillocfgFont_new(DillocfgWin *w, const gchar *label); DillocfgColor *DillocfgColor_new(DillocfgWin *w, const gchar *label); DillocfgNetwork *DillocfgNetwork_new(DillocfgWin *w, const gchar *label); #ifndef DISABLE_TABS DillocfgTab *DillocfgTab_new(DillocfgWin *w, const gchar *label); #endif DillocfgOther *DillocfgOther_new(DillocfgWin *w, const gchar *label); DillocfgCookie *DillocfgCookie_new(DillocfgWin *w, const gchar *label); void ReadUserconfig(DillocfgWin *d); void WriteUserconfig(DillocfgWin *d); /****************************** M A I N ********************************/ int main(int argc, char **argv) { DillocfgWin *dialog; #ifdef ENABLE_NLS /* set locale */ gtk_set_locale(); /* Initialize Gettext */ bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); #endif /* Initialize GUI and parse GTK related args */ gtk_init(&argc, &argv); gdk_rgb_init(); dialog = DillocfgWin_new(); ReadUserconfig(dialog); /* Start the GTK+ cycle */ gtk_main(); //g_print("Dillocfg:normal exit\n"); gtk_exit (0); return 0; } DillocfgWin *DillocfgWin_new() { DillocfgWin *d = g_new(DillocfgWin, 1); GtkWidget *dialog_main = gtk_window_new(GTK_WINDOW_TOPLEVEL);//DIALOG); GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *button_hbox = gtk_hbox_new(FALSE, 0); GtkWidget *button_reset = gtk_button_new_with_label(_("Reset")); GtkWidget *button_reload = gtk_button_new_with_label(_("Reload")); GtkWidget *button_save = gtk_button_new_with_label(_("Save")); GtkWidget *button_quit = gtk_button_new_with_label(_("Quit")); gtk_window_set_title(GTK_WINDOW(dialog_main), _("Dillo Preferences")); gtk_window_set_policy(GTK_WINDOW(dialog_main), TRUE, TRUE, FALSE); gtk_signal_connect(GTK_OBJECT(dialog_main), "delete_event", GTK_SIGNAL_FUNC(gtk_widget_destroy), d); gtk_signal_connect(GTK_OBJECT(dialog_main), "destroy", GTK_SIGNAL_FUNC(DillocfgWin_free), d); gtk_window_set_wmclass(GTK_WINDOW(dialog_main), "dillocfg", _("Dillo Preferences")); gtk_widget_set_usize(dialog_main, WIDTH, HEIGHT); d->notebook = gtk_notebook_new(); gtk_notebook_set_homogeneous_tabs(GTK_NOTEBOOK(d->notebook), FALSE); gtk_container_add(GTK_CONTAINER(dialog_main), vbox); gtk_container_add(GTK_CONTAINER(vbox), d->notebook); gtk_signal_connect(GTK_OBJECT(button_reset), "clicked", GTK_SIGNAL_FUNC(DillocfgWin_callback_reset), d); gtk_signal_connect(GTK_OBJECT(button_reload), "clicked", GTK_SIGNAL_FUNC(DillocfgWin_callback_reload), d); gtk_signal_connect(GTK_OBJECT(button_save), "clicked", GTK_SIGNAL_FUNC(DillocfgWin_callback_save), d); gtk_signal_connect(GTK_OBJECT(button_quit), "clicked", GTK_SIGNAL_FUNC(DillocfgWin_free), d); gtk_box_set_spacing(GTK_BOX(button_hbox), 5); gtk_container_add(GTK_CONTAINER(button_hbox), button_reset); gtk_container_add(GTK_CONTAINER(button_hbox), button_reload); gtk_container_add(GTK_CONTAINER(button_hbox), button_save); gtk_container_add(GTK_CONTAINER(button_hbox), button_quit); gtk_box_pack_end(GTK_BOX(vbox), button_hbox, FALSE, FALSE, 5); d->list = NULL; d->interface = DillocfgInterface_new(d, _("Interface")); d->font = DillocfgFont_new(d, _(" Font ")); d->color = DillocfgColor_new(d, _(" Color ")); d->network = DillocfgNetwork_new(d, _(" Network ")); #ifndef DISABLE_TABS d->tab = DillocfgTab_new(d, _(" Tab ")); #endif d->cookie = NULL;//DillocfgCookie_new(d, _(" Cookie ")); d->other = DillocfgOther_new(d, _(" Other ")); gtk_widget_show_all(dialog_main); return d; } void DillocfgWin_init(DillocfgWin *d, gboolean use_default) { g_list_foreach(d->list, (GFunc)Option_init, (gpointer)use_default); } void DillocfgWin_free(GtkWidget *widget, DillocfgWin *d) { g_list_foreach(d->list, (GFunc)Option_free, (gpointer)&d->list); g_free(d->interface); g_free(d->font); g_free(d->color); g_free(d->network); #ifndef DISABLE_TABS g_free(d->tab); #endif g_free(d->cookie); g_free(d->other); g_list_free(d->list); g_free(d); gtk_main_quit(); } #ifndef DISABLE_TABS DillocfgTab *DillocfgTab_new(DillocfgWin *w, const gchar *label) { DillocfgTab *t = g_new(DillocfgTab, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *table = gtk_table_new(4, 2, FALSE); GtkWidget *table2 = gtk_table_new(2, 1, FALSE); gtk_table_set_homogeneous(GTK_TABLE(table), TRUE); gtk_table_set_homogeneous(GTK_TABLE(table2), TRUE); gtk_container_add(GTK_CONTAINER(vbox), table); gtk_container_add(GTK_CONTAINER(vbox), table2); x = 0; y = 0; t->tab_bar_homogeneous = Option_new(TOKEN_TAB_BAR_HOMOGENEOUS, &w->list, table, x, y++); t->tab_bar_scroller = Option_new(TOKEN_TAB_BAR_SCROLLER, &w->list, table, x, y++); t->tab_bar_show_fullscreen = Option_new(TOKEN_TAB_BAR_SHOW_FULLSCREEN, &w->list, table, x, y++); t->tab_close_middle_click = Option_new(TOKEN_TAB_CLOSE_MIDDLE_CLICK, &w->list, table, x, y++); x++; y = 0; t->tab_bar_show_single_tab = Option_new(TOKEN_TAB_BAR_SHOW_SINGLE_TAB, &w->list, table, x, y++); t->tab_instead_of_window = Option_new(TOKEN_TAB_INSTEAD_OF_WINDOW, &w->list, table, x, y++); t->tab_load_in_background = Option_new(TOKEN_TAB_LOAD_IN_BACKGROUND, &w->list, table, x, y++); t->tab_title_compress = Option_new(TOKEN_TAB_TITLE_COMPRESS, &w->list, table, x, y++); x = 0; y = 0; t->compress_vowels = Option_new(TOKEN_COMPRESS_VOWELS, &w->list, table2, x, y++); t->compress_common_prefixes = Option_new(TOKEN_COMPRESS_COMMON_PREFIXES, &w->list, table2, x, y++); gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return t; } #endif DillocfgOther *DillocfgOther_new(DillocfgWin *w, const gchar *label) { DillocfgOther *g = g_new(DillocfgOther, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *table = gtk_table_new(9, 1, FALSE); gtk_table_set_homogeneous(GTK_TABLE(table), TRUE); gtk_container_add(GTK_CONTAINER(vbox), table); x = 0; y = 0; g->w3c_plus_heuristics = Option_new(TOKEN_W3C_PLUS_HEURISTICS, &w->list, table, x, y++); g->use_dicache = Option_new(TOKEN_USE_DICACHE, &w->list, table, x, y++); g->focus_location_on_new = Option_new(TOKEN_FOCUS_LOCATION_ON_NEW, &w->list, table, x, y++); g->enterpress_forces_submit = Option_new(TOKEN_ENTERPRESS_FORCES_SUBMIT, &w->list, table, x, y++); g->generate_submit = Option_new(TOKEN_GENERATE_SUBMIT, &w->list, table, x, y++); g->limit_text_width = Option_new(TOKEN_LIMIT_TEXT_WIDTH, &w->list, table, x, y++); g->search_url = Option_new(TOKEN_SEARCH_URL, &w->list, table, x, y++); g->search_charset = Option_new(TOKEN_SEARCH_CHARSET, &w->list, table, x, y++); g->accept_language = Option_new(TOKEN_ACCEPT_LANGUAGE, &w->list, table, x, y++); g->show_msg = Option_new(TOKEN_SHOW_MSG, &w->list, table, x, y++); g->show_extra_warnings = Option_new(TOKEN_SHOW_EXTRA_WARNINGS, &w->list, table, x, y++); gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return g; } DillocfgInterface *DillocfgInterface_new(DillocfgWin *w, const gchar *label) { DillocfgInterface *di = g_new(DillocfgInterface, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *table = gtk_table_new(10, 2, FALSE); gtk_table_set_homogeneous (GTK_TABLE(table), FALSE); gtk_container_add(GTK_CONTAINER(vbox), table); x = 0; y = 0; di->geometry = Option_new(TOKEN_GEOMETRY, &w->list, table, x, y++); di->panel_size = Option_new(TOKEN_PANEL_SIZE, &w->list, table, x, y++); di->show_back = Option_new(TOKEN_SHOW_BACK, &w->list, table, x, y++); di->show_forw = Option_new(TOKEN_SHOW_FORW, &w->list, table, x, y++); di->show_bookmarks = Option_new(TOKEN_SHOW_BOOKMARKS, &w->list, table, x, y++); di->show_clear_url = Option_new(TOKEN_SHOW_CLEAR_URL, &w->list, table, x, y++); di->show_home = Option_new(TOKEN_SHOW_HOME, &w->list, table, x, y++); di->show_menubar = Option_new(TOKEN_SHOW_MENUBAR, &w->list, table, x, y++); di->show_progress_box = Option_new(TOKEN_SHOW_PROGRESS_BOX, &w->list, table, x, y++); di->show_reload = Option_new(TOKEN_SHOW_RELOAD, &w->list, table, x, y++); x++; y = 0; di->show_save = Option_new(TOKEN_SHOW_SAVE, &w->list, table, x, y++); di->show_stop = Option_new(TOKEN_SHOW_STOP, &w->list, table, x, y++); di->show_tooltip = Option_new(TOKEN_SHOW_TOOLTIP, &w->list, table, x, y++); di->show_url = Option_new(TOKEN_SHOW_URL, &w->list, table, x, y++); di->show_search = Option_new(TOKEN_SHOW_SEARCH, &w->list, table, x, y++); di->small_icons = Option_new(TOKEN_SMALL_ICONS, &w->list, table, x, y++); di->fullwindow_start = Option_new(TOKEN_FULLWINDOW_START, &w->list, table, x, y++); di->transient_dialogs = Option_new(TOKEN_TRANSIENT_DIALOGS, &w->list, table, x, y++); di->popup_dialogs = Option_new(TOKEN_POPUP_DIALOGS, &w->list, table, x, y++); di->show_popup_navigation = Option_new(TOKEN_SHOW_POPUP_NAVIGATION, &w->list, table, x, y++); gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return di; } DillocfgFont *DillocfgFont_new(DillocfgWin *w, const gchar *label) { DillocfgFont *r = g_new(DillocfgFont, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 5); GtkWidget *table = gtk_table_new(4, 1, FALSE); GtkWidget *table2 = gtk_table_new(1, 2, FALSE); gtk_table_set_homogeneous (GTK_TABLE(table), TRUE); gtk_table_set_row_spacings(GTK_TABLE(table), 5); gtk_table_set_homogeneous (GTK_TABLE(table2), TRUE); //gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, FALSE, 5); //gtk_box_pack_start(GTK_BOX(vbox), table2, TRUE, FALSE, 5); gtk_container_add(GTK_CONTAINER(vbox), table); gtk_container_add(GTK_CONTAINER(vbox), table2); x=0; y=0; #ifdef DISABLE_ANTI_ALIAS r->vw_fontname = Option_new(TOKEN_VW_FONT, &w->list, table, x, y++); #else r->vw_aafontname = Option_new(TOKEN_VW_AAFONT, &w->list, table, x, y++); #endif #ifdef DISABLE_ANTI_ALIAS r->fw_fontname = Option_new(TOKEN_FW_FONT, &w->list, table, x, y++); #else r->fw_aafontname = Option_new(TOKEN_FW_AAFONT, &w->list, table, x, y++); #endif r->font_sizes = Option_new(TOKEN_FONT_SIZES, &w->list, table, x, y++); r->font_factor = Option_new(TOKEN_FONT_FACTOR, &w->list, table, x, y++); x=0; y=0; r->use_oblique = Option_new(TOKEN_USE_OBLIQUE, &w->list, table2, x, y++); x++; y=0; r->limit_font_decoration = Option_new(TOKEN_LIMIT_FONT_DECORATION, &w->list, table2, x, y++); gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return r; } DillocfgColor *DillocfgColor_new(DillocfgWin *w, const gchar *label) { DillocfgColor *c = g_new(DillocfgColor, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *table = gtk_table_new(3, 1, FALSE); GtkWidget *table2 = gtk_table_new(2, 2, TRUE); gtk_table_set_homogeneous (GTK_TABLE(table), TRUE); gtk_table_set_homogeneous (GTK_TABLE(table2), FALSE); gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(vbox), table2, FALSE, FALSE, 5); x = 0; y = 0; c->force_my_colors = Option_new(TOKEN_FORCE_MY_COLORS, &w->list, table, x, y++); c->contrast_visited_color = Option_new(TOKEN_CONTRAST_VISITED_COLOR, &w->list, table, x, y++); c->allow_white_bg = Option_new(TOKEN_ALLOW_WHITE_BG, &w->list, table, x, y++); x = 0; y = 0; c->link_color = Option_new(TOKEN_LINK_COLOR, &w->list, table2, x, y++); c->bg_color = Option_new(TOKEN_BG_COLOR, &w->list, table2, x, y++); x++; y = 0; c->visited_color = Option_new(TOKEN_VISITED_COLOR, &w->list, table2, x, y++); c->text_color = Option_new(TOKEN_TEXT_COLOR, &w->list, table2, x, y++); gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return c; } DillocfgNetwork *DillocfgNetwork_new(DillocfgWin *w, const gchar *label) { DillocfgNetwork *n = g_new(DillocfgNetwork, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *table = gtk_table_new(10, 2, FALSE); gtk_container_add(GTK_CONTAINER(vbox), table); x = 0; y = 0; n->user_agent = Option_new(TOKEN_USER_AGENT, &w->list, table, x, y++); n->home = Option_new(TOKEN_HOME, &w->list, table, x, y++); n->start_page = Option_new(TOKEN_START_PAGE, &w->list, table, x, y++);; n->http_proxy = Option_new(TOKEN_PROXY, &w->list, table, x, y++);; n->http_proxyuser = Option_new(TOKEN_PROXYUSER, &w->list, table, x, y++);; n->no_proxy = Option_new(TOKEN_NOPROXY, &w->list, table, x, y++);; n->send_referer = Option_new(TOKEN_SEND_REFERER, &w->list, table, x, y++);; gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return n; } DillocfgCookie *DillocfgCookie_new(DillocfgWin *w, const gchar *label) { DillocfgCookie *c = g_new(DillocfgCookie, 1); gint x, y; GtkWidget *vbox = gtk_vbox_new(FALSE, 0); GtkWidget *hbox = gtk_hbox_new(FALSE, 0); GtkWidget *table = gtk_table_new(1, 1, TRUE); GtkWidget *clist = gtk_clist_new(2); GtkWidget *add_btn= gtk_button_new_with_label(_("Add")); GtkWidget *del_btn= gtk_button_new_with_label(_("Delete")); gtk_table_set_homogeneous(GTK_TABLE(table), TRUE); gtk_container_add(GTK_CONTAINER(hbox), add_btn); gtk_container_add(GTK_CONTAINER(hbox), del_btn); x = 0; y = 0; c->cookie_default = Option_new(TOKEN_COOKIE_DEFAULT, &w->list, table, x, y++); gtk_clist_set_column_title(GTK_CLIST(clist), 0, _("Address")); gtk_clist_set_column_title(GTK_CLIST(clist), 1, _("Rule")); gtk_clist_column_titles_show(GTK_CLIST(clist)); gtk_clist_set_column_width(GTK_CLIST(clist), 0, 200); /* gtk_clist_insert(GTK_CLIST(clist), 1, NULL); gtk_clist_set_text(GTK_CLIST(clist), 0, 0, "DEFAULT"); gtk_clist_set_text(GTK_CLIST(clist), 0, 1, "ACCEPT"); */ gtk_widget_set_usize(table, 320, 32); gtk_box_pack_start(GTK_BOX(vbox), clist, TRUE, TRUE, 10); gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 10); gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 10); //gtk_table_attach_defaults(GTK_TABLE(table), clist, x, x + 1, y, y + 1); gtk_notebook_append_page(GTK_NOTEBOOK(w->notebook), vbox, gtk_label_new(label)); return c; } #define BUFSIZE 4096 void ReadUserconfig(DillocfgWin *d) { FILE *usercfg, *usercookiecfg; gchar *filepass = g_strconcat(g_get_home_dir(), "/", USER_CFG, NULL); char buf[BUFSIZE], name[BUFSIZE], value[BUFSIZE]; gchar *locale = g_strdup (setlocale (LC_NUMERIC, NULL));; setlocale (LC_NUMERIC, "C"); if (!(usercfg = fopen(filepass, "r"))) { g_print(_("Read failed <%s>\n"), filepass); return; } g_free(filepass); filepass = g_strconcat(g_get_home_dir(), "/", USER_COOKIE_CFG, NULL); if (!(usercookiecfg = fopen(filepass, "r"))) { g_print(_("Read failed <%s>\n"), filepass); return; } g_free(filepass); /* First, set default data. */ DillocfgWin_init(d, TRUE); /* Read dillorc. */ while (fgets(buf, BUFSIZE, usercfg)) { /* Parse name and value. */ int i = 0, j = 0, k = 0; guint token; SKIPSPACE(buf, i); if (buf[i] == '#') continue; SKIPSPACE(buf, i); while (!IS_LINE_END(buf[i]) && buf[i] != '=' && !isspace(buf[i])) name[j++] = buf[i++]; name[j] = '\0'; SKIPSPACE(buf, i); if (buf[i++] != '=') continue; SKIPSPACE(buf, i); if (buf[i] == '"') { i++; while (!IS_LINE_END(buf[i]) && buf[i] != '"') value[k++] = buf[i++]; } else while (!IS_LINE_END(buf[i]) && !isspace(buf[i])) value[k++] = buf[i++]; value[k] = '\0'; if (strlen(value) == 0) continue; token = name2token(name); /* Set value */ Option_set(get_token_object(token), value); } /* Read cookiesrc */ while (fgets(buf, BUFSIZE, usercookiecfg)) { /* Parse name and value. */ int i = 0, j = 0, k = 0; guint token; SKIPSPACE(buf, i); if (buf[i] == '#') continue; SKIPSPACE(buf, i); while (!IS_LINE_END(buf[i]) && !isspace(buf[i])) name[j++] = buf[i++]; name[j] = '\0'; SKIPSPACE(buf, i); while (!IS_LINE_END(buf[i]) && !isspace(buf[i])) value[k++] = buf[i++]; value[k] = '\0'; if (strlen(value) == 0) continue; if (strcmp(name, "DEFAULT") == 0) token = TOKEN_COOKIE_DEFAULT; else token = TOKEN_COOKIE; /* Set value */ Option_set(get_token_object(token), value); } fclose(usercfg); fclose(usercookiecfg); setlocale(LC_NUMERIC, locale); DillocfgWin_init(d, FALSE); g_free (locale); } void DillocfgWin_callback_reset(GtkButton *button, gpointer user_data) { DillocfgWin *d = (DillocfgWin *)user_data; DillocfgWin_init(d, TRUE); modified_ = TRUE; } void DillocfgWin_callback_reload(GtkButton *button, gpointer user_data) { DillocfgWin *d = (DillocfgWin *)user_data; ReadUserconfig(d); modified_ = FALSE; } void WriteUserconfig(DillocfgWin *d) { FILE *usercfg; //FILE *usercookiecfg; GList *list; gchar *filepass = g_strconcat(g_get_home_dir(), "/", USER_CFG, NULL); if (!(usercfg = fopen(filepass, "w"))) { g_print(_("can't write <%s>\n"), filepass); return; } g_free(filepass); fprintf(usercfg, "#This file is written by Dillocfg\n"); #if 0 filepass = g_strconcat(g_get_home_dir(), "/", USER_COOKIE_CFG, NULL); if (!(usercookiecfg = fopen(filepass, "w"))) { g_print(_("can't write <%s>\n"), filepass); fclose(usercfg); return; } g_free(filepass); fprintf(usercookiecfg, "#This file is written by Dillocfg\n"); #endif for (list = g_list_first(d->list); list; list = g_list_next(list)) { Option *o = (Option *)list->data; if (get_token_type(o->base.token) == COOKIE) ;//Option_get(o, usercookiecfg); else Option_get(o, usercfg); } fclose(usercfg); //fclose(usercookiecfg); } void DillocfgWin_callback_save(GtkButton *button, gpointer user_data) { DillocfgWin *d = (DillocfgWin *)user_data; WriteUserconfig(d); modified_ = FALSE; } /* vim: set ts=3 sw=3 sts=3 expandtab:*/