Update Free Software Foundation address.
[metze/wireshark/wip.git] / ui / qt / qt_ui_utils.cpp
1 /* qt_ui_utils.cpp
2  *
3  * $Id$
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "qt_ui_utils.h"
28
29 #include "ui/recent.h"
30 #include "ui/ui_util.h"
31
32 // XXX - Copied from ui/gtk/gui_utils.c
33
34 #define WINDOW_GEOM_KEY "window_geom"
35
36 /* load the geometry values for a window from previously saved values */
37 static gboolean window_geom_load(const gchar *name, window_geometry_t *geom);
38
39 /* the geometry hashtable for all known window classes,
40  * the window name is the key, and the geometry struct is the value */
41 static GHashTable *window_geom_hash = NULL;
42
43 /* save the window and it's current geometry into the geometry hashtable */
44 static void
45 window_geom_save(const gchar *name, window_geometry_t *geom)
46 {
47     gchar *key;
48     window_geometry_t *work;
49
50     /* init hashtable, if not already done */
51     if(!window_geom_hash) {
52         window_geom_hash = g_hash_table_new (g_str_hash, g_str_equal);
53     }
54     /* if we have an old one, remove and free it first */
55     work = (window_geometry_t *) g_hash_table_lookup(window_geom_hash, name);
56     if(work) {
57         g_hash_table_remove(window_geom_hash, name);
58         g_free(work->key);
59         g_free(work);
60     }
61
62     /* g_malloc and insert the new one */
63     work = (window_geometry_t *) g_malloc(sizeof(*geom));
64     *work = *geom;
65     key = g_strdup(name);
66     work->key = key;
67     g_hash_table_insert(window_geom_hash, key, work);
68 }
69
70
71 /* load the desired geometry for this window from the geometry hashtable */
72 static gboolean
73 window_geom_load(const gchar *name, window_geometry_t *geom)
74 {
75     window_geometry_t *p;
76
77     /* init hashtable, if not already done */
78     if(!window_geom_hash) {
79         window_geom_hash = g_hash_table_new (g_str_hash, g_str_equal);
80     }
81
82     p = (window_geometry_t *) g_hash_table_lookup(window_geom_hash, name);
83     if(p) {
84         *geom = *p;
85         return TRUE;
86     } else {
87         return FALSE;
88     }
89 }
90
91
92 /* read in a single key value pair from the recent file into the geometry hashtable */
93 extern "C" void
94 window_geom_recent_read_pair(const char *name, const char *key, const char *value)
95 {
96     window_geometry_t geom;
97
98
99     /* find window geometry maybe already in hashtable */
100     if(!window_geom_load(name, &geom)) {
101         /* not in table, init geom with "basic" values */
102         geom.key        = NULL;    /* Will be set in window_geom_save () */
103         geom.set_pos    = FALSE;
104         geom.x          = -1;
105         geom.y          = -1;
106         geom.set_size   = FALSE;
107         geom.width      = -1;
108         geom.height     = -1;
109
110         geom.set_maximized = FALSE;/* this is valid in GTK2 only */
111         geom.maximized  = FALSE;   /* this is valid in GTK2 only */
112     }
113
114     if (strcmp(key, "x") == 0) {
115         geom.x = strtol(value, NULL, 10);
116         geom.set_pos = TRUE;
117     } else if (strcmp(key, "y") == 0) {
118         geom.y = strtol(value, NULL, 10);
119         geom.set_pos = TRUE;
120     } else if (strcmp(key, "width") == 0) {
121         geom.width = strtol(value, NULL, 10);
122         geom.set_size = TRUE;
123     } else if (strcmp(key, "height") == 0) {
124         geom.height = strtol(value, NULL, 10);
125         geom.set_size = TRUE;
126     } else if (strcmp(key, "maximized") == 0) {
127         if (g_ascii_strcasecmp(value, "true") == 0) {
128             geom.maximized = TRUE;
129         }
130         else {
131             geom.maximized = FALSE;
132         }
133         geom.set_maximized = TRUE;
134     } else {
135         /*
136          * Silently ignore the bogus key.  We shouldn't abort here,
137          * as this could be due to a corrupt recent file.
138          *
139          * XXX - should we print a message about this?
140          */
141         return;
142     }
143
144     /* save / replace geometry in hashtable */
145     window_geom_save(name, &geom);
146 }
147
148 /* write all geometry values of all windows from the hashtable to the recent file */
149 extern "C" void
150 window_geom_recent_write_all(gpointer rf)
151 {
152     /* init hashtable, if not already done */
153     if(!window_geom_hash) {
154         window_geom_hash = g_hash_table_new (g_str_hash, g_str_equal);
155     }
156
157     g_hash_table_foreach(window_geom_hash, write_recent_geom, rf);
158 }