home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / yagirc-0.51.tar.gz / yagirc-0.51.tar / yagirc-0.51 / gui_setup.c < prev    next >
C/C++ Source or Header  |  1998-05-11  |  24KB  |  813 lines

  1. /*
  2.  
  3.  gui_setup.c : Setup dialog
  4.  
  5.     Copyright (C) 1998 Timo Sirainen
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26.  
  27. #include <pwd.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30.  
  31. #include <gtk/gtk.h>
  32. #include <glib.h>
  33.  
  34. #include "os.h"
  35. #include "irc.h"
  36. #include "gui.h"
  37. #include "misc.h"
  38.  
  39. #include "gui_setup_alias.h"
  40. #include "gui_setup_server.h"
  41. #include "gui_setup_dcc.h"
  42. #include "gui_setup_lists.h"
  43.  
  44.  
  45. char *default_nick;
  46. char *real_name;
  47. char *user_name;
  48.  
  49. char *default_text_pixmap;
  50.  
  51. SETUP_THEME_REC *default_theme;
  52. static SETUP_THEME_REC deftheme;
  53.  
  54. enum
  55. {
  56.     SETUP_TYPE_UNKNOWN,
  57.     SETUP_TYPE_STR,
  58.     SETUP_TYPE_INT,
  59.     SETUP_TYPE_COLOR,
  60.     SETUP_TYPE_COLORS,
  61.     SETUP_TYPE_SERVER,
  62.     SETUP_TYPE_LIST,
  63.     SETUP_TYPE_ALIAS,
  64.     SETUP_TYPE_THEME,
  65. };
  66.  
  67. typedef struct
  68. {
  69.     char *name;
  70.     int type;
  71.     int multiple;
  72.     void *ptr;
  73. }
  74. SETUP_REC;
  75.  
  76. static SETUP_REC setups[] =
  77. {
  78.     { "Nick", SETUP_TYPE_STR, 0, &default_nick },
  79.     { "RealName", SETUP_TYPE_STR, 0, &real_name },
  80.     { "GlobalServer", SETUP_TYPE_SERVER, 1, &globalservers },
  81.     { "LocalServer", SETUP_TYPE_SERVER, 1, &servers },
  82.     { "Ignore", SETUP_TYPE_LIST, 1, &ignores },
  83.     { "Notify", SETUP_TYPE_LIST, 1, ¬ifies },
  84.     { "Channel", SETUP_TYPE_LIST, 1, &joinlist },
  85.     { "Alias", SETUP_TYPE_ALIAS, 1, NULL },
  86.     { "Theme", SETUP_TYPE_THEME, 1, &themes },
  87.     { NULL, 0, 0, NULL }
  88. };
  89.  
  90. static GtkWidget *setupwin = NULL; /* setup window widget */
  91. static int setup_open = 0; /* setup window is opened */
  92.  
  93. /* create text field with label */
  94. GtkWidget *gui_add_label(GtkWidget *box, char *text, char *field)
  95. {
  96.     GtkWidget *label;
  97.     GtkWidget *entry;
  98.     GtkWidget *hbox;
  99.  
  100.     g_return_val_if_fail(box != NULL, NULL);
  101.     g_return_val_if_fail(text != NULL, NULL);
  102.  
  103.     hbox = gtk_hbox_new(FALSE, 0);
  104.     gtk_container_border_width (GTK_CONTAINER (hbox), 4);
  105.     gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
  106.     gtk_widget_show (hbox);
  107.  
  108.     label = gtk_label_new(text);
  109.     gtk_widget_set_usize (label, 60, -1);
  110.     gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  111.     gtk_widget_show (label);
  112.  
  113.     entry = gtk_entry_new();
  114.     if (field != NULL) gtk_entry_set_text (GTK_ENTRY (entry), field);
  115.     gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
  116.     gtk_widget_show (entry);
  117.  
  118.     return entry;
  119. }
  120.  
  121. void gui_setup_init(void)
  122. {
  123.     struct passwd *entry;
  124.     char *ptr;
  125.     int n;
  126.  
  127.     if (setup_open)
  128.     {
  129.         /* setup dialog is open - don't initialize. */
  130.         return;
  131.     }
  132.  
  133.     for (n = 0; setups[n].name != NULL; n++)
  134.     {
  135.         switch (setups[n].type)
  136.         {
  137.             case SETUP_TYPE_INT:
  138.                 *((int *) setups[n].ptr) = 0;
  139.                 break;
  140.             case SETUP_TYPE_STR:
  141.                 *((char **) setups[n].ptr) = NULL;
  142.                 break;
  143.         }
  144.     }
  145.  
  146.     /* get user information */
  147.     user_name = getenv("IRCUSER");
  148.     real_name = getenv("IRCNAME");
  149.  
  150.     globalservers = servers = NULL;
  151.  
  152.     aliases = NULL;
  153.     ignores = NULL;
  154.     notifies = NULL;
  155.     themes = NULL;
  156.  
  157.     default_theme = NULL;
  158.     deftheme.name = _("default theme");
  159.     deftheme.pixmap = NULL;
  160.     deftheme.fg = colors[BLACK];
  161.     deftheme.bg = colors[BWHITE];
  162.     deftheme.colors[0] = colors[CYAN];
  163.     deftheme.colors[1] = colors[BMAGENTA];
  164.     deftheme.colors[2] = colors[GREEN];
  165.     deftheme.colors[3] = colors[BMAGENTA];
  166.     deftheme.colors[4] = colors[MAGENTA];
  167.     deftheme.colors[5] = colors[RED];
  168.     deftheme.colors[6] = colors[BRED];
  169.     deftheme.colors[7] = colors[BMAGENTA];
  170.     deftheme.colors[8] = colors[MAGENTA];
  171.     deftheme.colors[9] = colors[GREEN];
  172.     deftheme.colors[10] = colors[BLACK];
  173.     deftheme.colors[11] = colors[BRED];
  174.     deftheme.colors[12] = colors[BBLUE];
  175.     deftheme.colors[13] = colors[GREEN];
  176.     deftheme.colors[14] = deftheme.fg;
  177.  
  178.     /* get passwd-entry */
  179.     entry = getpwuid(getuid());
  180.     if (entry != NULL)
  181.     {
  182.         user_name = entry->pw_name;
  183.         if (real_name == NULL) real_name = entry->pw_gecos;
  184.     }
  185.  
  186.     ptr = getenv("IRCNICK");
  187.     if (ptr == NULL)
  188.     {
  189.         if (user_name != NULL)
  190.             ptr = user_name;
  191.         else
  192.             ptr = "someone";
  193.     }
  194.  
  195.     default_nick = ptr;
  196.     if (user_name == NULL || *user_name == '\0') user_name = default_nick;
  197.     if (real_name == NULL || *real_name == '\0') real_name = user_name;
  198.  
  199.     user_name = g_strdup(user_name);
  200.     real_name = g_strdup(real_name);
  201.     default_nick = g_strdup(default_nick);
  202. }
  203.  
  204. void gui_setup_deinit(void)
  205. {
  206.     int n;
  207.  
  208.     if (setup_open)
  209.     {
  210.         /* setup dialog is open - don't deinitialize. */
  211.         return;
  212.     }
  213.  
  214.     for (n = 0; setups[n].name != NULL; n++)
  215.     {
  216.         char **str;
  217.  
  218.         str = setups[n].ptr;
  219.         if (setups[n].type == SETUP_TYPE_STR && *str != NULL)
  220.         {
  221.             g_free(*str);
  222.             *str = NULL;
  223.         }
  224.     }
  225.  
  226.     while (servers != NULL)
  227.         remove_server_list(&servers, (SETUP_SERVER_REC *) servers->data);
  228.     while (globalservers != NULL)
  229.         remove_server_list(&globalservers, (SETUP_SERVER_REC *) globalservers->data);
  230.  
  231.     while (aliases != NULL)
  232.         remove_alias_list((ALIAS_REC *) aliases->data);
  233.  
  234.     while (ignores != NULL)
  235.     {
  236.         g_free(ignores->data);
  237.         ignores = g_list_remove_link(ignores, ignores);
  238.     }
  239.  
  240.     while (notifies != NULL)
  241.     {
  242.         g_free(notifies->data);
  243.         notifies = g_list_remove_link(notifies, notifies);
  244.     }
  245.  
  246.     if (default_text_pixmap != NULL) g_free(default_text_pixmap);
  247.     while (themes != NULL)
  248.         gui_remove_theme_rec(&themes, themes->data);
  249.  
  250.     g_free(user_name);
  251.     g_free(real_name);
  252.     g_free(default_nick);
  253. }
  254.  
  255. /* Get next count parameters from data */
  256. static char *get_params(char *data, int count, ...)
  257. {
  258.     char **str, *tmp;
  259.  
  260.     va_list args;
  261.     va_start(args, count);
  262.  
  263.     while (count-- > 0)
  264.     {
  265.         str = (char **) va_arg(args, char **);
  266.  
  267.         tmp = data;
  268.         while (*data != '\0' && *data != '\t') data++;
  269.         if (*data == '\t') *data++ = '\0';
  270.  
  271.         if (str != NULL) *str = tmp;
  272.     }
  273.     va_end(args);
  274.  
  275.     return data;
  276. }
  277.  
  278. static void mkcolor(char *str, GdkColor *color)
  279. {
  280.     unsigned long num;
  281.  
  282.     if (sscanf(str, "%lu", &num) != 1) num = 0;
  283.     color->blue = (num & 0xff)*256;
  284.     color->green = num & 0xff00;
  285.     color->red = (num & 0xff0000)/256;
  286.     color->pixel = num;
  287. }
  288.  
  289. /* Read yagIRC's configuration file */
  290. static int read_config_file(char *fname)
  291. {
  292.     char *tmp, str[500], *cmd, *value;
  293.     long linenum;
  294.     int n;
  295.     FILE *f;
  296.  
  297.     tmp = convhome(fname);
  298.     f = fopen(tmp, "rb");
  299.     g_free(tmp);
  300.     if (f == NULL) return 0;
  301.  
  302.     linenum = 0;
  303.     while (fgets(str, sizeof(str), f) != NULL)
  304.     {
  305.         linenum++;
  306.         if (str[strlen(str)-1] == '\n') str[strlen(str)-1] = '\0';
  307.         if (str[0] == '\0' || str[0] == '#') continue;
  308.         cmd = str;
  309.         while (*cmd == ' ') cmd++;
  310.         if (*cmd == '\0') continue;
  311.  
  312.         value = strchr(cmd, '=');
  313.         if (value == NULL)
  314.         {
  315.             g_warning("%s:%ld Error in configuration file: missing '=', line ignored", fname, linenum);
  316.             continue;
  317.         }
  318.         if (value == cmd)
  319.         {
  320.             g_warning("%s:%ld Error in configuration file: missing command, line ignored", fname, linenum);
  321.             continue;
  322.         }
  323.  
  324.         /* remove whitespace */
  325.         tmp = value-1;
  326.         while (*tmp == ' ') tmp--; *(tmp+1) = '\0';
  327.         while (*(++value) == ' ') ;
  328.  
  329.         for (n = 0; setups[n].name != NULL; n++)
  330.         {
  331.             if (strcasecmp(cmd, setups[n].name) == 0)
  332.             {
  333.                 switch (setups[n].type)
  334.                 {
  335.                     case SETUP_TYPE_STR:
  336.                         {
  337.                             char **str = (char **) setups[n].ptr;
  338.                             if (*str != NULL) g_free(*str);
  339.                             *str = g_strdup(value);
  340.                         }
  341.                         break;
  342.                     case SETUP_TYPE_INT:
  343.                         {
  344.                             int *num = (int *) setups[n].ptr;
  345.                             if (sscanf(value, "%d", num) != 1)
  346.                                 g_warning("%s:%ld Error in configuration file: value should be numeric, line ignored", fname, linenum);
  347.                         }
  348.                         break;
  349.                     case SETUP_TYPE_COLOR:
  350.                         {
  351.                             unsigned long num;
  352.                             GdkColor *color = (GdkColor *) setups[n].ptr;
  353.  
  354.                             if (sscanf(value, "%lu", &num) != 1)
  355.                                 g_warning("%s:%ld Error in configuration file: value should be numeric, line ignored", fname, linenum);
  356.                             color->blue = (num & 0xff)*256;
  357.                             color->green = num & 0xff00;
  358.                             color->red = (num & 0xff0000)/256;
  359.                             color->pixel = num;
  360.                         }
  361.                         break;
  362.                     case SETUP_TYPE_COLORS:
  363.                         {
  364.                             unsigned long num;
  365.                             int i;
  366.                             GdkColor *color = (GdkColor *) setups[n].ptr;
  367.  
  368.                             for (i = 0; value != NULL; i++)
  369.                             {
  370.                                 char *ptr;
  371.  
  372.                                 ptr = strchr(value, ' ');
  373.                                 if (ptr != NULL) *ptr++ = '\0';
  374.                                 if (sscanf(value, "%lu", &num) != 1)
  375.                                     g_warning("%s:%ld Error in configuration file: value should be numeric, line ignored", fname, linenum);
  376.                                 color[i].blue = (num & 0xff)*256;
  377.                                 color[i].green = num & 0xff00;
  378.                                 color[i].red = (num & 0xff0000)/256;
  379.                                 color[i].pixel = num;
  380.                                 value = ptr;
  381.                             }
  382.                         }
  383.                         break;
  384.                     case SETUP_TYPE_SERVER:
  385.                         {
  386.                             GList **servers = (GList **) setups[n].ptr;
  387.                             char *net, *name, *ports, *location;
  388.                             int port;
  389.  
  390.                             get_params(value, 4, &net, &name, &ports, &location);
  391.                             if (sscanf(ports, "%d", &port) != 1) port = 6667;
  392.  
  393.                             add_server_list(servers, net, name, port, location);
  394.                         }
  395.                         break;
  396.                     case SETUP_TYPE_ALIAS:
  397.                         {
  398.                             char *alias, *cmd;
  399.  
  400.                             alias = value;
  401.                             while (*value != '\0' && *value != '\t') value++;
  402.                             if (*value == '\t') *value++ = '\0';
  403.                             cmd = value;
  404.  
  405.                             add_alias_list(alias, cmd);
  406.                         }
  407.                         break;
  408.                     case SETUP_TYPE_LIST:
  409.                         {
  410.                             GList **list = (GList **) setups[n].ptr;
  411.  
  412.                             if (glist_case_find_string(*list, value) == NULL)
  413.                                 *list = g_list_append(*list, g_strdup(value));
  414.                         }
  415.                         break;
  416.                     case SETUP_TYPE_THEME:
  417.                         {
  418.                             GList **list = (GList **) setups[n].ptr;
  419.                             char *def, *name, *fgs, *bgs, *colors[15];
  420.                             int n;
  421.  
  422.                             get_params(value, 20, &def, &name, &default_text_pixmap, &fgs, &bgs,
  423.                                        &colors[0], &colors[1], &colors[2],
  424.                                        &colors[3], &colors[4], &colors[5],
  425.                                        &colors[6], &colors[7], &colors[8],
  426.                                        &colors[9], &colors[10], &colors[11],
  427.                                        &colors[12], &colors[13], &colors[14]);
  428.  
  429.                             mkcolor(fgs, &default_color);
  430.                             mkcolor(bgs, &default_bgcolor);
  431.                             if (*default_text_pixmap == '\0')
  432.                                 default_text_pixmap = NULL;
  433.                             for (n = 0; n < 15; n++)
  434.                                 mkcolor(colors[n], &yag_colors[n]);
  435.  
  436.                             n = gui_add_theme_rec(list, name);
  437.                             if (*def == '*')
  438.                             {
  439.                                 /* this theme is the default */
  440.                                 default_theme = g_list_nth(*list, n)->data;
  441.                             }
  442.                         }
  443.                         break;
  444.                 }
  445.                 break;
  446.             }
  447.         }
  448.         if (setups[n].name == NULL)
  449.             g_warning("%s:%ld Error in configuration file: unknown command '%s', line ignored", fname, linenum, cmd);
  450.     }
  451.  
  452.     fclose(f);
  453.     return 1;
  454. }
  455.  
  456. int gui_read_config(void)
  457. {
  458.     int ret;
  459.  
  460.     if (setup_open)
  461.     {
  462.         /* setup dialog is open - don't read anything.. */
  463.         return 0;
  464.     }
  465.  
  466.     read_config_file(GLOBAL_CONFIG_FILE);
  467.     read_config_file(SITE_CONFIG_FILE);
  468.     ret = read_config_file(CONFIG_FILE);
  469.  
  470.     if (default_theme == NULL)
  471.     {
  472.         if (themes != NULL)
  473.             default_theme = g_list_first(themes)->data;
  474.         else
  475.         {
  476.             SETUP_THEME_REC *theme;
  477.  
  478.             theme = (SETUP_THEME_REC *) g_malloc(sizeof(SETUP_THEME_REC));
  479.             memcpy(theme, &deftheme, sizeof(deftheme));
  480.             theme->name = g_strdup(deftheme.name);
  481.             theme->pixmap = NULL;
  482.             themes = g_list_append(NULL, theme);
  483.             default_theme = theme;
  484.         }
  485.     }
  486.  
  487.     default_text_pixmap = g_strdup(default_theme->pixmap);
  488.     default_color = default_theme->fg;
  489.     default_bgcolor = default_theme->bg;
  490.     memcpy(yag_colors, default_theme->colors, sizeof(yag_colors));
  491.  
  492.     return ret;
  493. }
  494.  
  495. /* read all data from configuration file to linked list */
  496. GList *init_config_write(GList *list, char *fname)
  497. {
  498.     FILE *f;
  499.     char *tmp, *str;
  500.  
  501.     tmp = convhome(fname);
  502.     f = fopen(tmp, "rb");
  503.     g_free(tmp);
  504.     if (f == NULL) return list;
  505.  
  506.     str = g_malloc(1000);
  507.     tmp = g_malloc(1000);
  508.     while (fgets(str, 1000, f) != NULL)
  509.     {
  510.         char *cmd, *value;
  511.  
  512.         if (str[strlen(str)-1] == '\n') str[strlen(str)-1] = '\0';
  513.         if (str[0] == '\0' || str[0] == '#') continue;
  514.  
  515.         cmd = str;
  516.         while (*cmd == ' ') cmd++;
  517.         if (*cmd == '\0') continue;
  518.  
  519.         /* get command name */
  520.         value = strchr(cmd, '=');
  521.         if (value == NULL || value == cmd) continue;
  522.  
  523.         /* remove whitespace */
  524.         {
  525.             char *tmp;
  526.  
  527.             tmp = value-1;
  528.             while (*tmp == ' ') tmp--; *(tmp+1) = '\0';
  529.             while (*(++value) == ' ') ;
  530.         }
  531.  
  532.         sprintf(tmp, "%s = %s", cmd, value);
  533.         list = g_list_append(list, g_strdup(tmp));
  534.     }
  535.     g_free(str);
  536.     g_free(tmp);
  537.     fclose(f);
  538.  
  539.     return list;
  540. }
  541.  
  542. /* check if this line is already found from global/site configs, if not,
  543.    write it to user config file .. */
  544. static void write_config_line(FILE *f, GList *list, char *str)
  545. {
  546.     while (list != NULL)
  547.     {
  548.         if (strcmp((char *) list->data, str) == 0)
  549.             return;
  550.         list = list->next;
  551.     }
  552.  
  553.     fprintf(f, "%s\n", str);
  554. }
  555.  
  556. int gui_write_config(void)
  557. {
  558.     GList *tmp, *config;
  559.     char *str;
  560.     int n;
  561.     FILE *f;
  562.  
  563.     if (setup_open)
  564.     {
  565.         /* setup dialog is open - don't save changes. */
  566.         return 0;
  567.     }
  568.  
  569.     str = convhome(CONFIG_FILE);
  570.     f = fopen(str, "w+t");
  571.     g_free(str);
  572.     if (f == NULL)
  573.     {
  574.         /* couldn't create config file */
  575.         return 0;
  576.     }
  577.  
  578.     config = init_config_write(NULL, GLOBAL_CONFIG_FILE);
  579.     config = init_config_write(config, SITE_CONFIG_FILE);
  580.     config = g_list_first(config);
  581.  
  582.     /* save generic things.. */
  583.     str = g_malloc(1000); /* enough? */
  584.     for (n = 0; setups[n].name != NULL; n++)
  585.     {
  586.         *str = '\0';
  587.  
  588.         switch (setups[n].type)
  589.         {
  590.             case SETUP_TYPE_STR:
  591.                 {
  592.                     char **s = (char **) setups[n].ptr;
  593.                     if (*s != NULL)
  594.                         sprintf(str, "%s = %s", setups[n].name, *s);
  595.                 }
  596.                 break;
  597.             case SETUP_TYPE_INT:
  598.                 sprintf(str, "%s = %d", setups[n].name, *((int *) setups[n].ptr));
  599.                 break;
  600.             case SETUP_TYPE_COLOR:
  601.                 {
  602.                     unsigned long num;
  603.                     GdkColor *color = (GdkColor *) setups[n].ptr;
  604.  
  605.                     num =
  606.                         (color->blue & 0xff00)/256 |
  607.                         (color->green & 0xff00) |
  608.                         (color->red & 0xff00)*256;
  609.                     sprintf(str, "%s = %lu", setups[n].name, num);
  610.                 }
  611.                 break;
  612.             case SETUP_TYPE_LIST:
  613.                 {
  614.                     GList **list = (GList **) setups[n].ptr;
  615.  
  616.                     for (tmp = g_list_first(*list); tmp != NULL; tmp = tmp->next)
  617.                     {
  618.                         sprintf(str, "%s = %s", setups[n].name, (char *) tmp->data);
  619.                         write_config_line(f, config, str);
  620.                     }
  621.                     *str = '\0';
  622.                 }
  623.                 break;
  624.         }
  625.  
  626.         if (*str != '\0')
  627.             write_config_line(f, config, str);
  628.     }
  629.  
  630.     /* save global servers list */
  631.     /*for (tmp = g_list_first(globalservers); tmp != NULL; tmp = tmp->next)
  632.     {
  633.         SETUP_SERVER_REC *serv;
  634.  
  635.         serv = (SETUP_SERVER_REC *) tmp->data;
  636.         sprintf(str, "GlobalServer = %s\t%s\t%d\t%s", serv->ircnet, serv->name, serv->port, serv->location);
  637.         write_config_line(f, config, str);
  638.     }*/
  639.  
  640.     /* save list of local servers list */
  641.     for (tmp = g_list_first(servers); tmp != NULL; tmp = tmp->next)
  642.     {
  643.         SETUP_SERVER_REC *serv;
  644.  
  645.         serv = (SETUP_SERVER_REC *) tmp->data;
  646.         sprintf(str, "LocalServer = %s\t%s\t%d\t%s", serv->ircnet, serv->name, serv->port, serv->location);
  647.         write_config_line(f, config, str);
  648.     }
  649.  
  650.     /* save alias list */
  651.     for (tmp = g_list_first(aliases); tmp != NULL; tmp = tmp->next)
  652.     {
  653.         ALIAS_REC *al;
  654.  
  655.         al = (ALIAS_REC *) tmp->data;
  656.         sprintf(str, "Alias = %s\t%s", al->alias, al->cmd);
  657.         write_config_line(f, config, str);
  658.     }
  659.  
  660.     /* save themes */
  661.     for (tmp = g_list_first(themes); tmp != NULL; tmp = tmp->next)
  662.     {
  663.         SETUP_THEME_REC *theme;
  664.         unsigned long num, num2;
  665.         int n, pos;
  666.  
  667.         theme = (SETUP_THEME_REC *) tmp->data;
  668.  
  669.         num = (theme->fg.blue & 0xff00)/256 | (theme->fg.green & 0xff00) |
  670.             (theme->fg.red & 0xff00)*256;
  671.         num2 = (theme->bg.blue & 0xff00)/256 | (theme->bg.green & 0xff00) |
  672.             (theme->bg.red & 0xff00)*256;
  673.  
  674.         pos = sprintf(str, "Theme = %s\t%s\t%s\t%lu\t%lu",
  675.                       theme == default_theme ? "*" : "",
  676.                       theme->name,
  677.                       theme->pixmap == NULL ? "" : theme->pixmap,
  678.                       num, num2);
  679.         for (n = 0; n < 15; n++)
  680.         {
  681.             num =
  682.                 (theme->colors[n].blue & 0xff00)/256 |
  683.                 (theme->colors[n].green & 0xff00) |
  684.                 (theme->colors[n].red & 0xff00)*256;
  685.             pos += sprintf(str+pos, "\t%lu", num);
  686.         }
  687.         write_config_line(f, config, str);
  688.     }
  689.  
  690.     g_free(str);
  691.  
  692.     fclose(f);
  693.     return 1;
  694. }
  695.  
  696. /* signal: Apply button pressed */
  697. static void gui_setup_apply(GtkWidget *win)
  698. {
  699.     setup_open = 0;
  700.     gui_setup_servers_accept();
  701.     gui_setup_outlook_accept();
  702.  
  703.     gui_write_config();
  704.     setup_open = 1;
  705.  
  706.     gui_refresh_windows();
  707. }
  708.  
  709. /* signal: OK button pressed */
  710. static void gui_setup_ok(GtkWidget *win)
  711. {
  712.     gui_setup_apply(win);
  713.  
  714.     gtk_widget_destroy(win);
  715. }
  716.  
  717. /* signal: CANCEL button pressed */
  718. static void gui_setup_cancel(GtkWidget *win)
  719. {
  720.     setup_open = 0;
  721.     gui_setup_deinit();
  722.     gui_setup_init();
  723.  
  724.     gui_read_config();
  725.     gtk_widget_destroy(win);
  726. }
  727.  
  728. /* signal: setup window gets destroyed */
  729. static void setup_destroy(GtkWidget *window)
  730. {
  731.     setup_open = 0;
  732. }
  733.  
  734. /* signal: setup window is closed */
  735. static int setup_delete_event(GtkWidget *window)
  736. {
  737.     setup_open = 0;
  738.     return 0;
  739. }
  740.  
  741. static void add_page(GtkWidget *notebook, char *text, GUI_SETUP_FUNC func)
  742. {
  743.     GtkWidget *vbox;
  744.     GtkWidget *label;
  745.  
  746.     label = gtk_label_new(text);
  747.  
  748.     vbox = gtk_vbox_new(FALSE, 0);
  749.     gtk_container_border_width(GTK_CONTAINER(vbox), 10);
  750.     gtk_widget_show(vbox);
  751.  
  752.     gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);
  753.     func(vbox);
  754. }
  755.  
  756. /* Run setup */
  757. void gui_setup(void)
  758. {
  759.     GtkWidget *notebook;
  760.     GtkWidget *button;
  761.  
  762.     if (setup_open)
  763.     {
  764.         /* setup window already created */
  765.         gdk_window_raise(setupwin->window);
  766.         return;
  767.     }
  768.     setup_open = 1;
  769.  
  770.     setupwin = gtk_dialog_new();
  771.     gtk_signal_connect (GTK_OBJECT (setupwin), "delete_event",
  772.                         GTK_SIGNAL_FUNC(setup_delete_event), NULL);
  773.     gtk_signal_connect (GTK_OBJECT (setupwin), "destroy",
  774.                         GTK_SIGNAL_FUNC(setup_destroy), NULL);
  775.     gtk_window_set_title (GTK_WINDOW (setupwin), "yagIRC setup");
  776.     gtk_window_position (GTK_WINDOW (setupwin), GTK_WIN_POS_CENTER);
  777.  
  778.     notebook = gtk_notebook_new();
  779.     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(setupwin)->vbox), notebook);
  780.     gtk_widget_show(notebook);
  781.  
  782.     add_page(notebook, _("Servers"), (GUI_SETUP_FUNC) gui_setup_servers);
  783.     add_page(notebook, _("Aliases"), (GUI_SETUP_FUNC) gui_setup_aliases);
  784.     add_page(notebook, _("Ignore list"), (GUI_SETUP_FUNC) gui_setup_ignorelist);
  785.     add_page(notebook, _("Notify list"), (GUI_SETUP_FUNC) gui_setup_notifylist);
  786.     add_page(notebook, _("DCC"), (GUI_SETUP_FUNC) gui_setup_dcc);
  787.     add_page(notebook, _("Outlook"), (GUI_SETUP_FUNC) gui_setup_outlook);
  788.  
  789.     button = gtk_button_new_with_label (_("Ok"));
  790.     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(setupwin)->action_area), button, TRUE, TRUE, 10);
  791.     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  792.                                GTK_SIGNAL_FUNC (gui_setup_ok), GTK_OBJECT(setupwin));
  793.     /*GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  794.     gtk_widget_grab_default (button);*/
  795.     gtk_widget_show (button);
  796.  
  797.     button = gtk_button_new_with_label (_("Cancel"));
  798.     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(setupwin)->action_area), button, TRUE, TRUE, 10);
  799.     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  800.                                GTK_SIGNAL_FUNC (gui_setup_cancel),
  801.                                GTK_OBJECT(setupwin));
  802.     gtk_widget_show (button);
  803.  
  804.     button = gtk_button_new_with_label (_("Apply"));
  805.     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(setupwin)->action_area), button, TRUE, TRUE, 10);
  806.     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
  807.                                GTK_SIGNAL_FUNC (gui_setup_apply),
  808.                                GTK_OBJECT(setupwin));
  809.     gtk_widget_show (button);
  810.  
  811.     gtk_widget_show(setupwin);
  812. }
  813.