1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "modest-widget-memory.h"
31 #include <widgets/modest-header-view.h>
36 #define PARAM_HEIGHT "height"
37 #define PARAM_WIDTH "width"
38 #define PARAM_POS "pos"
39 #define PARAM_COLUMN_WIDTH "column-width"
42 get_keyname (ModestConf *conf, const gchar *name, const gchar *param)
44 gchar *esc_name, *keyname;
45 esc_name = modest_conf_key_escape (conf, name);
47 keyname = g_strdup_printf ("%s/%s/%s",
48 MODEST_CONF_WIDGET_NAMESPACE,
57 save_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
61 key = get_keyname (conf, name, PARAM_HEIGHT);
62 modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.height, NULL);
65 key = get_keyname (conf, name, PARAM_WIDTH);
66 modest_conf_set_int (conf, key, GTK_WIDGET(widget)->allocation.width, NULL);
74 restore_settings_widget (ModestConf *conf, GtkWidget *widget, const gchar *name)
79 key = get_keyname (conf, name, PARAM_HEIGHT);
81 if (modest_conf_key_exists (conf, key, NULL))
82 req.height = modest_conf_get_int (conf, key, NULL);
86 key = get_keyname (conf, name, PARAM_WIDTH);
87 if (modest_conf_key_exists (conf, key, NULL))
88 req.width = modest_conf_get_int (conf, key, NULL);
91 if (req.height && req.width)
92 gtk_widget_size_request (widget, &req);
101 save_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
106 gtk_window_get_size (win, &width, &height);
108 key = get_keyname (conf, name, PARAM_HEIGHT);
109 modest_conf_set_int (conf, key, height, NULL);
112 key = get_keyname (conf, name, PARAM_WIDTH);
113 modest_conf_set_int (conf, key, width, NULL);
121 restore_settings_window (ModestConf *conf, GtkWindow *win, const gchar *name)
124 int height = 0, width = 0;
126 key = get_keyname (conf, name, PARAM_HEIGHT);
128 if (modest_conf_key_exists (conf, key, NULL))
129 height = modest_conf_get_int (conf, key, NULL);
133 key = get_keyname (conf, name, PARAM_WIDTH);
134 if (modest_conf_key_exists (conf, key, NULL))
135 width = modest_conf_get_int (conf, key, NULL);
140 gtk_window_set_default_size (win, width, height);
147 save_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
152 pos = gtk_paned_get_position (paned);
154 key = get_keyname (conf, name, PARAM_POS);
155 modest_conf_set_int (conf, key, pos, NULL);
163 restore_settings_paned (ModestConf *conf, GtkPaned *paned, const gchar *name)
168 key = get_keyname (conf, name, PARAM_POS);
170 if (modest_conf_key_exists (conf, key, NULL)) {
171 pos = modest_conf_get_int (conf, key, NULL);
172 gtk_paned_set_position (paned, pos);
181 save_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
186 GList *cols, *cursor;
188 cursor = cols = modest_header_view_get_columns (header_view);
189 str = g_string_new (NULL);
194 GtkTreeViewColumn *col;
196 col = GTK_TREE_VIEW_COLUMN (cursor->data);
197 col_id = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(col),
198 MODEST_HEADER_VIEW_COLUMN));
199 width = gtk_tree_view_column_get_width (col);
201 g_string_append_printf (str, "%d:%d ", col_id, width);
203 cursor = g_list_next (cursor);
206 key = get_keyname (conf, name, PARAM_COLUMN_WIDTH);
207 modest_conf_set_string (conf, key, str->str, NULL);
209 g_string_free (str, TRUE);
218 restore_settings_header_view (ModestConf *conf, ModestHeaderView *header_view,
222 key = get_keyname (conf, name, PARAM_COLUMN_WIDTH);
224 if (modest_conf_key_exists (conf, key, NULL)) {
226 gchar *data, *cursor;
229 GList *colwidths = NULL;
231 cursor = data = modest_conf_get_string (conf, key, NULL);
232 while (cursor && sscanf (cursor, "%u:%u ", &col, &width) == 2) {
233 cols = g_list_append (cols, GINT_TO_POINTER(col));
234 colwidths = g_list_append (colwidths, GINT_TO_POINTER(width));
235 cursor = strchr (cursor + 1, ' ');
240 GList *viewcolumns, *colcursor, *widthcursor;
241 modest_header_view_set_columns (header_view, cols);
243 widthcursor = colwidths;
244 colcursor = viewcolumns = gtk_tree_view_get_columns (GTK_TREE_VIEW(header_view));
245 while (colcursor && widthcursor) {
246 gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(colcursor->data),
247 GPOINTER_TO_INT(widthcursor->data));
248 colcursor = g_list_next (colcursor);
249 widthcursor = g_list_next (widthcursor);
252 g_list_free (colwidths);
253 g_list_free (viewcolumns);
263 modest_widget_memory_save_settings (ModestConf *conf, GtkWidget *widget,
266 g_return_val_if_fail (conf, FALSE);
267 g_return_val_if_fail (widget, FALSE);
268 g_return_val_if_fail (name, FALSE);
270 if (GTK_IS_WINDOW(widget))
271 return save_settings_window (conf, GTK_WINDOW(widget), name);
272 else if (GTK_IS_PANED(widget))
273 return save_settings_paned (conf, GTK_PANED(widget), name);
274 else if (MODEST_IS_HEADER_VIEW(widget))
275 return save_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
276 else if (GTK_IS_WIDGET(widget))
277 return save_settings_widget (conf, widget, name);
279 g_printerr ("modest: %p is not a known widget\n", widget);
286 modest_widget_memory_restore_settings (ModestConf *conf, GtkWidget *widget,
289 g_return_val_if_fail (conf, FALSE);
290 g_return_val_if_fail (widget, FALSE);
291 g_return_val_if_fail (name, FALSE);
293 if (GTK_IS_WINDOW(widget))
294 return restore_settings_window (conf, GTK_WINDOW(widget), name);
295 else if (GTK_IS_PANED(widget))
296 return restore_settings_paned (conf, GTK_PANED(widget), name);
297 else if (MODEST_IS_HEADER_VIEW(widget))
298 return restore_settings_header_view (conf, MODEST_HEADER_VIEW(widget), name);
299 else if (GTK_IS_WIDGET(widget))
300 return restore_settings_widget (conf, widget, name);
302 g_printerr ("modest: %p is not a known widget\n", widget);