diff options
author | Søren Sandmann Pedersen <sandmann@redhat.com> | 2009-08-21 23:10:17 -0400 |
---|---|---|
committer | Søren Sandmann Pedersen <sandmann@redhat.com> | 2009-08-21 23:10:17 -0400 |
commit | 263cf138b25f4b4c21985681ec60bcce65b21fa2 (patch) | |
tree | 0ebd6c9a0d5459a50721612c30f7f41c2a5ef6d9 | |
parent | 60849e381af51d593c081ab3b702e7cac45549aa (diff) |
Port siv over to use external persist.persist
-rw-r--r-- | persist.c | 10 | ||||
-rw-r--r-- | persist.h | 20 | ||||
-rw-r--r-- | siv.c | 213 | ||||
-rw-r--r-- | siv.h | 3 | ||||
-rw-r--r-- | window.c | 153 |
5 files changed, 143 insertions, 256 deletions
@@ -151,8 +151,18 @@ static void save_to_file (persist_t *persist) { GKeyFile *key_file = g_key_file_new (); + char *output; g_hash_table_foreach (persist->groups, foreach_group, key_file); + output = g_key_file_to_data (key_file, NULL, NULL); + + if (output) + { + g_file_set_contents (persist->filename, output, -1, NULL); + g_free (output); + } + + g_key_file_free (key_file); } static gchar * @@ -1,3 +1,23 @@ +/* persist.c - Simple routines for storing key/values persistently + * Copyright (C) 2008, 2009 Soren Sandmann + * Copyright (C) 2009 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + typedef struct persist_t persist_t; persist_t *persist_new (const char *filename); @@ -8,6 +8,7 @@ #include <signal.h> #include <unistd.h> #include "siv.h" +#include "persist.h" #define _(a) a @@ -19,7 +20,7 @@ struct App SivWindow **windows; - GHashTable *meta_data; + persist_t *persist; }; void @@ -92,49 +93,9 @@ app_register_window (App *app, } static void -foreach (gpointer key, gpointer value, gpointer user_data) -{ - gchar *filename = key; - MetaData *meta = value; - GKeyFile *key_file = user_data; - - g_key_file_set_integer (key_file, filename, "window_x", meta->window_x); - g_key_file_set_integer (key_file, filename, "window_y", meta->window_y); - g_key_file_set_integer (key_file, filename, "window_width", meta->window_width); - g_key_file_set_integer (key_file, filename, "window_height", meta->window_height); - g_key_file_set_integer (key_file, filename, "background", meta->background); - g_key_file_set_integer (key_file, filename, "smooth_image", meta->smooth_image); - g_key_file_set_integer (key_file, filename, "zoom_level", meta->zoom_level); - g_key_file_set_integer (key_file, filename, "hadj", meta->hadj); - g_key_file_set_integer (key_file, filename, "vadj", meta->vadj); - g_key_file_set_integer (key_file, filename, "show_toolbar", meta->show_toolbar); -} - -static gchar * -make_filename (void) -{ - return g_build_filename ( - g_get_home_dir(), ".gnome2", "siv.metadata", NULL); -} - -static void quit (App *app) { - char *key_filename = make_filename(); - GKeyFile *keyfile = g_key_file_new (); - char *output; - - g_hash_table_foreach (app->meta_data, foreach, keyfile); - - output = g_key_file_to_data (keyfile, NULL, NULL); - if (output) - { - g_file_set_contents (key_filename, output, -1, NULL); - g_free (output); - } - - g_key_file_free (keyfile); - g_free (key_filename); + persist_save (app->persist); gtk_main_quit(); } @@ -164,136 +125,13 @@ app_get_open_chooser (App *app) return app->chooser; } -static gchar * -encode (const char *filename) -{ - GString *result = g_string_new (NULL); - int i; - - for (i = 0; filename[i] != '\0'; ++i) - { - char c = filename[i]; - - if (!g_ascii_isprint (c) || c == '@' || c == '[' || c == ']') - g_string_append_printf (result, "@%x", c); - else - g_string_append_c (result, c); - } - - return g_string_free (result, FALSE); -} - -static gboolean -get_int (GKeyFile *keyfile, const char *group, const char *key, int *result) -{ - GError *err = NULL; - int d; - - d = g_key_file_get_integer (keyfile, group, key, &err); - - if (err) - { - g_error_free (err); - return FALSE; - } - - if (result) - *result = d; - - return TRUE; -} - -static GHashTable * -load_meta_data (void) -{ - GKeyFile *keyfile; - char *filename; - GHashTable *result; - - keyfile = g_key_file_new (); - filename = make_filename(); - - result = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - - if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL)) - { - gsize n_groups; - char **groups = g_key_file_get_groups (keyfile, &n_groups); - int i; - - for (i = 0; i < n_groups; ++i) - { - MetaData *data = g_new0 (MetaData, 1); - char *group = groups[i]; - int b; - - if (!get_int (keyfile, group, "window_x", &(data->window_x))) - data->window_x = 0; - - if (!get_int (keyfile, group, "window_y", &(data->window_y))) - data->window_y = 0; - - if (!get_int (keyfile, group, "window_width", &(data->window_width))) - data->window_width = 0; - - if (!get_int (keyfile, group, "window_height", &(data->window_height))) - data->window_height = 0; - - if (!get_int (keyfile, group, "background", &b)) - data->background = BG_NONE; - else - data->background = b; - - if (data->background >= BG_LAST || data->background < BG_FIRST) - data->background = BG_NONE; - - if (!get_int (keyfile, group, "smooth_image", &(data->smooth_image))) - data->smooth_image = TRUE; - - if (!get_int (keyfile, group, "zoom_level", &(data->zoom_level))) - data->zoom_level = 0; - - if (!get_int (keyfile, group, "vadj", &(data->vadj))) - data->vadj = 0; - - if (!get_int (keyfile, group, "hadj", &(data->hadj))) - data->hadj = 0; - - if (!get_int (keyfile, group, "show_toolbar", &(data->show_toolbar))) - data->show_toolbar = TRUE; - - g_hash_table_insert (result, g_strdup (group), data); - } - - g_strfreev (groups); - } - - g_free (filename); - g_key_file_free (keyfile); - - return result; -} - gboolean app_get_meta_data (App *app, const char *file, - MetaData *data) + const char *key, + int *value) { - char *encoded = encode (file); - gboolean result = FALSE; - MetaData *d = g_hash_table_lookup (app->meta_data, encoded); - - if (d) - { - if (data) - *data = *d; - - result = TRUE; - } - - g_free (encoded); - - return result; + return persist_get_int (app->persist, file, key, value); } void @@ -310,28 +148,16 @@ app_set_meta_data (App *app, int hadj, int show_toolbar) { - char *encoded = encode (filename); - MetaData *data = g_hash_table_lookup (app->meta_data, encoded); - - if (!data) - { - data = g_new0 (MetaData, 1); - - g_hash_table_insert (app->meta_data, g_strdup (encoded), data); - } - - data->window_x = window_x; - data->window_y = window_y; - data->window_width = window_width; - data->window_height = window_height; - data->background = background; - data->smooth_image = smooth_image; - data->zoom_level = zoom_level; - data->hadj = hadj; - data->vadj = vadj; - data->show_toolbar = show_toolbar; - - g_free (encoded); + persist_set_int (app->persist, filename, "window_x", window_x); + persist_set_int (app->persist, filename, "window_y", window_y); + persist_set_int (app->persist, filename, "window_width", window_width); + persist_set_int (app->persist, filename, "window_height", window_height); + persist_set_int (app->persist, filename, "background", background); + persist_set_int (app->persist, filename, "smooth_image", smooth_image); + persist_set_int (app->persist, filename, "zoom_level", zoom_level); + persist_set_int (app->persist, filename, "hadj", hadj); + persist_set_int (app->persist, filename, "vadj", vadj); + persist_set_int (app->persist, filename, "show_toolbar", show_toolbar); } static char ** @@ -455,16 +281,19 @@ app_new (int argc, char **argv) { char **err_files = nul_array_new (char *); char **filenames; + char *filename; App *app; int i; filenames = process_options (argc, argv); app = g_new0 (App, 1); - app->windows = nul_array_new (SivWindow *); - app->meta_data = load_meta_data (); + filename = g_build_filename (g_get_home_dir(), ".gnome2", "siv.metadata", NULL); + app->persist = persist_new (filename); + g_free (filename); + app_begin_load (app); if (filenames[0]) @@ -42,7 +42,8 @@ void app_unregister_window (App *app, SivWindow *window); gboolean app_get_meta_data (App *app, const char *file, - MetaData *data); + const char *key, + int *value); void app_set_meta_data (App *data, const char *filename, int window_x, @@ -336,7 +336,7 @@ save_meta_data (SivWindow *window) smooth = gtk_check_menu_item_get_active (get_widget (window, "menu_smooth_image")); show_toolbar = gtk_check_menu_item_get_active (get_widget (window, "menu_toolbar")); - + app_set_meta_data (window->app, filename, x, y, width, height, @@ -951,13 +951,16 @@ connect_signals (SivWindow *window) } static void -set_defaults (SivWindow *window) +compute_default_window_geometry (SivWindow *window, + int *x, + int *y, + int *width, + int *height) { GtkWidget *widget = get_widget (window, "main_window"); GdkScreen *screen = gtk_widget_get_screen (widget); GdkRectangle monitor; int monitor_num; - int width, height; monitor_num = gdk_screen_get_monitor_at_window (screen, widget->window); @@ -965,93 +968,117 @@ set_defaults (SivWindow *window) if (window->original) { - width = gdk_pixbuf_get_width (window->original) + 48; - height = gdk_pixbuf_get_height (window->original) + 96; + *width = gdk_pixbuf_get_width (window->original) + 48; + *height = gdk_pixbuf_get_height (window->original) + 96; } else { if (monitor.height < monitor.width) { - height = (monitor.height * 3) / 4; - width = (3 * height) / 4; + *height = (monitor.height * 3) / 4; + *width = (*height * 3) / 4; } else { - width = (monitor.width) / 3; - height = (width * 4) / 3; + *width = (monitor.width) / 3; + *height = (*width * 4) / 3; } } - if (width > monitor.width) - width = monitor.width - 32; + if (*width > monitor.width) + *width = monitor.width - 32; - if (height > monitor.height) - height = monitor.height - 32; + if (*height > monitor.height) + *height = monitor.height - 32; - if (GTK_WIDGET_REALIZED (get_widget (window, "main_window"))) - gtk_window_resize (get_widget (window, "main_window"), width, height); - else - gtk_window_set_default_size (get_widget (window, "main_window"), width, height); - - gtk_window_move (get_widget (window, "main_window"), - (monitor.width - width) / 2, - (monitor.height - height) / 2); - - gtk_check_menu_item_set_active (get_widget (window, "menu_smooth_image"), TRUE); - gtk_check_menu_item_set_active (get_widget (window, "menu_no"), TRUE); - gtk_check_menu_item_set_active (get_widget (window, "menu_toolbar"), TRUE); - + *x = (monitor.width - *width) / 2; + *y = (monitor.height - *height) / 2; } static void apply_meta_data (SivWindow *window, const char *filename) { - - GtkCheckMenuItem *item; MetaData data; + char *item_name; + int b; + App *app = window->app; - if (app_get_meta_data (window->app, filename, &data)) + /* Read values */ + + if (!app_get_meta_data (app, filename, "window_width", &(data.window_width)) || + !app_get_meta_data (app, filename, "window_height", &(data.window_height)) || + !app_get_meta_data (app, filename, "window_x", &(data.window_x)) || + !app_get_meta_data (app, filename, "window_y", &(data.window_y))) + { + compute_default_window_geometry ( + window, &data.window_x, &data.window_y, &data.window_width, &data.window_height); + } + + if (!app_get_meta_data (app, filename, "background", &b)) + b = BG_NONE; + + if (!app_get_meta_data (app, filename, "smooth_image", &(data.smooth_image))) + data.smooth_image = TRUE; + + if (!app_get_meta_data (app, filename, "zoom_level", &(data.zoom_level))) + data.zoom_level = 0; + + if (!app_get_meta_data (app, filename, "vadj", &(data.vadj))) + data.vadj = 0; + + if (!app_get_meta_data (app, filename, "hadj", &(data.hadj))) + data.hadj = 0; + + if (!app_get_meta_data (app, filename, "show_toolbar", &(data.show_toolbar))) + data.show_toolbar = TRUE; + + /* Apply them */ + + if (GTK_WIDGET_REALIZED (get_widget (window, "main_window"))) { - gtk_window_move (get_widget (window, "main_window"), - data.window_x, data.window_y); - gtk_window_resize (get_widget (window, "main_window"), data.window_width, data.window_height); - - switch (data.background) - { - default: - case BG_NONE: - item = get_widget (window, "menu_no"); - break; - - case BG_CHECKERBOARD: - item = get_widget (window, "menu_checkerboard"); - break; - - case BG_WHITE: - item = get_widget (window, "menu_white"); - break; - } - - gtk_check_menu_item_set_active (item, TRUE); - - gtk_check_menu_item_set_active (get_widget (window, "menu_smooth_image"), - data.smooth_image); - - gtk_check_menu_item_set_active (get_widget (window, "menu_toolbar"), - data.show_toolbar); - - window->zoom_level = data.zoom_level; - - /* Adjustments have to be set after the window is shown */ - window->hadj = data.hadj; - window->vadj = data.vadj; } else { - set_defaults (window); + gtk_window_set_default_size (get_widget (window, "main_window"), + data.window_width, data.window_height); + } + + gtk_window_move (get_widget (window, "main_window"), + data.window_x, data.window_y); + + switch (b) + { + default: + case BG_NONE: + item_name = "menu_no"; + break; + + case BG_CHECKERBOARD: + item_name = "menu_checkerboard"; + break; + + case BG_WHITE: + item_name = "menu_white"; + break; } + + gtk_check_menu_item_set_active (get_widget (window, item_name), TRUE); + + gtk_check_menu_item_set_active (get_widget (window, "menu_smooth_image"), + data.smooth_image); + + gtk_check_menu_item_set_active (get_widget (window, "menu_toolbar"), + data.show_toolbar); + + window->zoom_level = data.zoom_level; + + /* Adjustments have to be set after the window is shown, so we + * store them in variables here, and apply them later + */ + window->hadj = data.hadj; + window->vadj = data.vadj; } gboolean |