Found some erroneous usages of gtk_signal_connect_object. I always wondered
[obnox/wireshark/wip.git] / prefs.c
1 /* prefs.c
2  * Routines for handling preferences
3  *
4  * $Id: prefs.c,v 1.18 1999/06/21 19:04:35 gram Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #include <gtk/gtk.h>
35
36 #include <stdlib.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <sys/stat.h>
41
42 #include "ethereal.h"
43 #include "packet.h"
44 #include "file.h"
45 #include "prefs.h"
46 #include "column.h"
47 #include "print.h"
48 #include "filter.h"
49 #include "util.h"
50
51 /* Internal functions */
52 static int    set_pref(gchar*, gchar*);
53 static void   write_prefs();
54 static void   prefs_main_ok_cb(GtkWidget *, gpointer);
55 static void   prefs_main_save_cb(GtkWidget *, gpointer);
56 static void   prefs_main_cancel_cb(GtkWidget *, gpointer);
57 static GList *get_string_list(gchar *);
58 static void   clear_string_list(GList *);
59
60 e_prefs prefs;
61 static int init_prefs = 1;
62
63 #define PF_NAME "preferences"
64
65 #define E_PRINT_PAGE_KEY  "printer_options_page"
66 #define E_FILTER_PAGE_KEY "filter_options_page"
67 #define E_COLUMN_PAGE_KEY "column_options_page"
68
69 static gchar *pf_path = NULL;
70
71 void
72 prefs_cb(GtkWidget *w, gpointer sp) {
73   GtkWidget *prefs_w, *main_vb, *top_hb, *bbox, *prefs_nb,
74             *ok_bt, *save_bt, *cancel_bt;
75   GtkWidget *print_pg, *filter_pg, *column_pg, *filter_te, *label;
76
77 /*  GtkWidget *nlabel; */
78   gint       start_page = (gint) sp;
79
80   filter_pg = NULL;
81   filter_te = NULL;
82
83   prefs_w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
84   gtk_window_set_title(GTK_WINDOW(prefs_w), "Ethereal: Preferences");
85   
86   /* Container for each row of widgets */
87   main_vb = gtk_vbox_new(FALSE, 5);
88   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
89   gtk_container_add(GTK_CONTAINER(prefs_w), main_vb);
90   gtk_widget_show(main_vb);
91   
92   /* Top row: Preferences notebook */
93   top_hb = gtk_hbox_new(FALSE, 1);
94   gtk_container_add(GTK_CONTAINER(main_vb), top_hb);
95   gtk_widget_show(top_hb);
96   
97   prefs_nb = gtk_notebook_new();
98   gtk_container_add(GTK_CONTAINER(main_vb), prefs_nb);
99   gtk_widget_show(prefs_nb);
100   
101   /* General prefs */
102 /*   nlabel = gtk_label_new("Nothing here yet...");
103   gtk_widget_show (nlabel);
104
105   label = gtk_label_new ("General");
106   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), nlabel, label);
107  */  
108   /* Printing prefs */
109   print_pg = printer_prefs_show();
110   gtk_object_set_data(GTK_OBJECT(prefs_w), E_PRINT_PAGE_KEY, print_pg);
111   label = gtk_label_new ("Printing");
112   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), print_pg, label);
113     
114   /* Filter prefs */
115   if (w)
116     filter_te = gtk_object_get_data(GTK_OBJECT(w), E_FILT_TE_PTR_KEY);
117   filter_pg = filter_prefs_show(filter_te);
118
119   /* Pass along the entry widget pointer from the calling widget */
120   gtk_object_set_data(GTK_OBJECT(filter_pg), E_FILT_TE_PTR_KEY, filter_te);
121   gtk_object_set_data(GTK_OBJECT(prefs_w), E_FILTER_PAGE_KEY, filter_pg);
122   label = gtk_label_new ("Filters");
123   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), filter_pg, label);
124   /* Column prefs */
125   column_pg = column_prefs_show();
126   gtk_object_set_data(GTK_OBJECT(prefs_w), E_COLUMN_PAGE_KEY, column_pg);
127   label = gtk_label_new ("Columns");
128   gtk_notebook_append_page (GTK_NOTEBOOK(prefs_nb), column_pg, label);
129   
130   /* Jump to the specified page, if it was supplied */
131   if (start_page > E_PR_PG_NONE)
132     gtk_notebook_set_page(GTK_NOTEBOOK(prefs_nb), start_page);
133     
134   /* Button row: OK and cancel buttons */
135   bbox = gtk_hbutton_box_new();
136   gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_END);
137   gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
138   gtk_container_add(GTK_CONTAINER(main_vb), bbox);
139   gtk_widget_show(bbox);
140   
141   ok_bt = gtk_button_new_with_label ("OK");
142   gtk_signal_connect(GTK_OBJECT(ok_bt), "clicked",
143     GTK_SIGNAL_FUNC(prefs_main_ok_cb), GTK_OBJECT(prefs_w));
144   GTK_WIDGET_SET_FLAGS(ok_bt, GTK_CAN_DEFAULT);
145   gtk_box_pack_start (GTK_BOX (bbox), ok_bt, TRUE, TRUE, 0);
146   gtk_widget_grab_default(ok_bt);
147   gtk_widget_show(ok_bt);
148
149   save_bt = gtk_button_new_with_label ("Save");
150   gtk_signal_connect(GTK_OBJECT(save_bt), "clicked",
151     GTK_SIGNAL_FUNC(prefs_main_save_cb), GTK_OBJECT(prefs_w));
152   GTK_WIDGET_SET_FLAGS(save_bt, GTK_CAN_DEFAULT);
153   gtk_box_pack_start (GTK_BOX (bbox), save_bt, TRUE, TRUE, 0);
154   gtk_widget_show(save_bt);
155   
156   cancel_bt = gtk_button_new_with_label ("Cancel");
157   gtk_signal_connect(GTK_OBJECT(cancel_bt), "clicked",
158     GTK_SIGNAL_FUNC(prefs_main_cancel_cb), GTK_OBJECT(prefs_w));
159   GTK_WIDGET_SET_FLAGS(cancel_bt, GTK_CAN_DEFAULT);
160   gtk_box_pack_start (GTK_BOX (bbox), cancel_bt, TRUE, TRUE, 0);
161   gtk_widget_show(cancel_bt);
162
163   gtk_widget_show(prefs_w);
164 }
165
166 void
167 prefs_main_ok_cb(GtkWidget *ok_bt, gpointer parent_w)
168 {
169   printer_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_PRINT_PAGE_KEY));
170   filter_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_FILTER_PAGE_KEY));
171   column_prefs_ok(gtk_object_get_data(GTK_OBJECT(parent_w), E_COLUMN_PAGE_KEY));
172   gtk_widget_destroy(GTK_WIDGET(parent_w));
173 }
174
175 void
176 prefs_main_save_cb(GtkWidget *save_bt, gpointer parent_w)
177 {
178   printer_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_PRINT_PAGE_KEY));
179   filter_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_FILTER_PAGE_KEY));
180   column_prefs_save(gtk_object_get_data(GTK_OBJECT(parent_w), E_COLUMN_PAGE_KEY));
181   write_prefs();
182 }
183
184 void
185 prefs_main_cancel_cb(GtkWidget *cancel_bt, gpointer parent_w)
186 {
187   printer_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_PRINT_PAGE_KEY));
188   filter_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_FILTER_PAGE_KEY));
189   column_prefs_cancel(gtk_object_get_data(GTK_OBJECT(parent_w), E_COLUMN_PAGE_KEY));
190   gtk_widget_destroy(GTK_WIDGET(parent_w));
191 }
192
193 /* Parse through a list of comma-separated, quoted strings.  Return a
194    list of the string data */
195 static GList *
196 get_string_list(gchar *str) {
197   enum { PRE_QUOT, IN_QUOT, POST_QUOT };
198
199   gint      state = PRE_QUOT, i = 0, j = 0;
200   gboolean  backslash = FALSE;
201   gchar     cur_c, *slstr = NULL;
202   GList    *sl = NULL;
203   
204   while ((cur_c = str[i]) != '\0') {
205     if (cur_c == '"' && ! backslash) {
206       switch (state) {
207         case PRE_QUOT:
208           state = IN_QUOT;
209           slstr = (gchar *) g_malloc(sizeof(gchar) * COL_MAX_LEN);
210           j = 0;
211           break;
212         case IN_QUOT:
213           state  = POST_QUOT;
214           slstr[j] = '\0';
215           sl = g_list_append(sl, slstr);
216           break;
217         case POST_QUOT:
218           clear_string_list(sl);
219           return NULL;
220           break;
221         default:
222           break;
223       }
224     } else if (cur_c == '\\' && ! backslash) {
225       backslash = TRUE;
226     } else if (cur_c == ',' && state == POST_QUOT) {
227       state = PRE_QUOT;
228     } else if (state == IN_QUOT && j < COL_MAX_LEN) {
229       slstr[j] = str[i];
230       j++;
231     }
232     i++;
233   }
234   if (state != POST_QUOT) {
235     clear_string_list(sl);
236   }
237   return(sl);
238 }
239
240 void
241 clear_string_list(GList *sl) {
242   GList *l = sl;
243   
244   while (l) {
245     g_free(l->data);
246     l = g_list_remove_link(l, l);
247   }
248 }
249
250 /* Preferences file format:
251  * - Configuration directives start at the beginning of the line, and 
252  *   are terminated with a colon.
253  * - Directives can be continued on the next line by preceding them with
254  *   whitespace.
255  *
256  * Example:
257
258 # This is a comment line
259 print.command: lpr
260 print.file: /a/very/long/path/
261         to/ethereal-out.ps
262  *
263  */
264
265 #define MAX_VAR_LEN    32
266 #define MAX_VAL_LEN  1024
267 #define DEF_NUM_COLS    6
268 e_prefs *
269 read_prefs(char **pf_path_return) {
270   enum { START, IN_VAR, PRE_VAL, IN_VAL, IN_SKIP };
271   FILE     *pf;
272   gchar     cur_var[MAX_VAR_LEN], cur_val[MAX_VAL_LEN];
273   int       got_c, state = START, i;
274   gint      var_len = 0, val_len = 0, fline = 1, pline = 1;
275   gboolean  got_val = FALSE;
276   fmt_data *cfmt;
277   gchar    *col_fmt[] = {"No.",      "%m", "Time",        "%t",
278                          "Source",   "%s", "Destination", "%d",
279                          "Protocol", "%p", "Info",        "%i"};
280
281   
282   /* Initialize preferences.  With any luck, these values will be
283      overwritten below. */
284   if (init_prefs) {
285     init_prefs       = 0;
286     prefs.pr_format  = PR_FMT_TEXT;
287     prefs.pr_dest    = PR_DEST_CMD;
288     prefs.pr_file    = g_strdup("ethereal.out");
289     prefs.pr_cmd     = g_strdup("lpr");
290     prefs.col_list = NULL;
291     for (i = 0; i < DEF_NUM_COLS; i++) {
292       cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
293       cfmt->title = g_strdup(col_fmt[i * 2]);
294       cfmt->fmt   = g_strdup(col_fmt[(i * 2) + 1]);
295       prefs.col_list = g_list_append(prefs.col_list, cfmt);
296     }
297     prefs.num_cols  = DEF_NUM_COLS;
298   }
299
300   if (! pf_path) {
301     pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
302       strlen(PF_NAME) + 4);
303     sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
304   }
305     
306   *pf_path_return = NULL;
307   if ((pf = fopen(pf_path, "r")) == NULL) {
308     if (errno != ENOENT)
309       *pf_path_return = pf_path;
310     return &prefs;
311   }
312     
313   while ((got_c = getc(pf)) != EOF) {
314     if (got_c == '\n') {
315       state = START;
316       fline++;
317       continue;
318     }
319     if (var_len >= MAX_VAR_LEN) {
320       g_warning ("%s line %d: Variable too long", pf_path, fline);
321       state = IN_SKIP;
322       var_len = 0;
323       continue;
324     }
325     if (val_len >= MAX_VAL_LEN) {
326       g_warning ("%s line %d: Value too long", pf_path, fline);
327       state = IN_SKIP;
328       var_len = 0;
329       continue;
330     }
331     
332     switch (state) {
333       case START:
334         if (isalnum(got_c)) {
335           if (var_len > 0) {
336             if (got_val) {
337               cur_var[var_len] = '\0';
338               cur_val[val_len] = '\0';
339               if (! set_pref(cur_var, cur_val))
340                 g_warning ("%s line %d: Bogus preference", pf_path, pline);
341             } else {
342               g_warning ("%s line %d: Incomplete preference", pf_path, pline);
343             }
344           }
345           state      = IN_VAR;
346           got_val    = FALSE;
347           cur_var[0] = got_c;
348           var_len    = 1;
349           pline = fline;
350         } else if (isspace(got_c) && var_len > 0 && got_val) {
351           state = PRE_VAL;
352         } else if (got_c == '#') {
353           state = IN_SKIP;
354         } else {
355           g_warning ("%s line %d: Malformed line", pf_path, fline);
356         }
357         break;
358       case IN_VAR:
359         if (got_c != ':') {
360           cur_var[var_len] = got_c;
361           var_len++;
362         } else {
363           state   = PRE_VAL;
364           val_len = 0;
365           got_val = TRUE;
366         }
367         break;
368       case PRE_VAL:
369         if (!isspace(got_c)) {
370           state = IN_VAL;
371           cur_val[val_len] = got_c;
372           val_len++;
373         }
374         break;
375       case IN_VAL:
376         if (got_c != '#')  {
377           cur_val[val_len] = got_c;
378           val_len++;
379         } else {
380           while (isspace(cur_val[val_len]) && val_len > 0)
381             val_len--;
382           state = IN_SKIP;
383         }
384         break;
385     }
386   }
387   if (var_len > 0) {
388     if (got_val) {
389       cur_var[var_len] = '\0';
390       cur_val[val_len] = '\0';
391       if (! set_pref(cur_var, cur_val))
392         g_warning ("%s line %d: Bogus preference", pf_path, pline);
393     } else {
394       g_warning ("%s line %d: Incomplete preference", pf_path, pline);
395     }
396   }
397   fclose(pf);
398   
399   return &prefs;
400 }
401
402 #define PRS_PRINT_FMT  "print.format"
403 #define PRS_PRINT_DEST "print.destination"
404 #define PRS_PRINT_FILE "print.file"
405 #define PRS_PRINT_CMD  "print.command"
406 #define PRS_COL_FMT    "column.format"
407
408 static gchar *pr_formats[] = { "text", "postscript" };
409 static gchar *pr_dests[]   = { "command", "file" };
410
411 int
412 set_pref(gchar *pref, gchar *value) {
413   GList    *col_l;
414   gint      llen;
415   fmt_data *cfmt;
416
417   if (strcmp(pref, PRS_PRINT_FMT) == 0) {
418     if (strcmp(value, pr_formats[PR_FMT_TEXT]) == 0) {
419       prefs.pr_format = PR_FMT_TEXT;
420     } else if (strcmp(value, pr_formats[PR_FMT_PS]) == 0) {
421       prefs.pr_format = PR_FMT_PS;
422     } else {
423       return 0;
424     }
425   } else if (strcmp(pref, PRS_PRINT_DEST) == 0) {
426     if (strcmp(value, pr_dests[PR_DEST_CMD]) == 0) {
427       prefs.pr_dest = PR_DEST_CMD;
428     } else if (strcmp(value, pr_dests[PR_DEST_FILE]) == 0) {
429       prefs.pr_dest = PR_DEST_FILE;
430     } else {
431       return 0;
432     }
433   } else if (strcmp(pref, PRS_PRINT_FILE) == 0) {
434     if (prefs.pr_file) g_free(prefs.pr_file);
435     prefs.pr_file = g_strdup(value);
436   } else if (strcmp(pref, PRS_PRINT_CMD) == 0) {
437     if (prefs.pr_cmd) g_free(prefs.pr_cmd);
438     prefs.pr_cmd = g_strdup(value);
439   } else if (strcmp(pref, PRS_COL_FMT) == 0) {
440     if ((col_l = get_string_list(value)) && (g_list_length(col_l) % 2) == 0) {
441       while (prefs.col_list) {
442         cfmt = prefs.col_list->data;
443         g_free(cfmt->title);
444         g_free(cfmt->fmt);
445         g_free(cfmt);
446         prefs.col_list = g_list_remove_link(prefs.col_list, prefs.col_list);
447       }
448       llen             = g_list_length(col_l);
449       prefs.num_cols   = llen / 2;
450       col_l = g_list_first(col_l);
451       while(col_l) {
452         cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
453         cfmt->title    = g_strdup(col_l->data);
454         col_l          = col_l->next;
455         cfmt->fmt      = g_strdup(col_l->data);
456         col_l          = col_l->next;
457         prefs.col_list = g_list_append(prefs.col_list, cfmt);
458       }
459       /* To do: else print some sort of error? */
460     }
461     clear_string_list(col_l);
462   } else {
463     return 0;
464   }
465   
466   return 1;
467 }
468
469 void
470 write_prefs() {
471   FILE        *pf;
472   struct stat  s_buf;
473   
474   /* To do:
475    * - Split output lines longer than MAX_VAL_LEN
476    * - Create a function for the preference directory check/creation
477    *   so that duplication can be avoided with filter.c
478    */
479
480   if (! pf_path) {
481     pf_path = (gchar *) g_malloc(strlen(getenv("HOME")) + strlen(PF_DIR) +
482       strlen(PF_NAME) + 4);
483   }
484
485   sprintf(pf_path, "%s/%s", getenv("HOME"), PF_DIR);
486   if (stat(pf_path, &s_buf) != 0)
487     mkdir(pf_path, 0755);
488
489   sprintf(pf_path, "%s/%s/%s", getenv("HOME"), PF_DIR, PF_NAME);
490   if ((pf = fopen(pf_path, "w")) == NULL) {
491      simple_dialog(ESD_TYPE_WARN, NULL,
492       "Can't open preferences file\n\"%s\".", pf_path);
493    return;
494  }
495     
496   fputs("# Configuration file for Ethereal " VERSION ".\n"
497     "#\n"
498     "# This file is regenerated each time preferences are saved within\n"
499     "# Ethereal.  Making manual changes should be safe, however.\n"
500     "\n"
501     "######## Printing ########\n"
502     "\n", pf);
503
504   fprintf (pf, "# Can be one of \"text\" or \"postscript\".\n"
505     "print.format: %s\n\n", pr_formats[prefs.pr_format]);
506
507   fprintf (pf, "# Can be one of \"command\" or \"file\".\n"
508     "print.destination: %s\n\n", pr_dests[prefs.pr_dest]);
509
510   fprintf (pf, "# This is the file that gets written to when the "
511     "destination is set to \"file\"\n"
512     "%s: %s\n\n", PRS_PRINT_FILE, prefs.pr_file);
513
514   fprintf (pf, "# Output gets piped to this command when the destination "
515     "is set to \"command\"\n"
516     "%s: %s\n\n", PRS_PRINT_CMD, prefs.pr_cmd);
517
518   fprintf (pf, "# Packet list column format.  Each pair of strings consists "
519     "of a column title \n# and its format.\n"
520     "%s: %s\n\n", PRS_COL_FMT, col_format_to_pref_str());
521
522   fclose(pf);
523 }