summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann <sandmann@redhat.com>2008-03-09 14:14:57 -0400
committerSøren Sandmann <sandmann@redhat.com>2008-03-09 14:14:57 -0400
commitf81f250c08495e1b6e6fe22f6d2213ef1e6351bf (patch)
treebd1f2a33842fff7447d7e7af680363d48fd2683a
parent26a330e72339712bee0bf135d43f41812532609f (diff)
Add metadata to app
-rw-r--r--metadata.c166
-rw-r--r--metadata.h42
-rw-r--r--siv.c159
-rw-r--r--siv.h43
4 files changed, 190 insertions, 220 deletions
diff --git a/metadata.c b/metadata.c
deleted file mode 100644
index bc50540..0000000
--- a/metadata.c
+++ /dev/null
@@ -1,166 +0,0 @@
-#include "metadata.h"
-
-struct MetaData
-{
- GHashTable *info; /* filename=>MetaInfo */
-};
-
-static gchar *
-make_filename (void)
-{
- return g_build_filename (
- g_get_home_dir(), ".gnome2", "siv.metadata", NULL);
-}
-
-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 void
-populate_meta_data_from_keyfile (MetaData *data, GKeyFile *keyfile)
-{
- gchar **groups;
- gsize n_groups;
- int i;
-
- groups = g_key_file_get_groups (keyfile, &n_groups);
-
- for (i = 0; i < n_groups; ++i)
- {
- MetaInfo *info = g_new0 (MetaInfo, 1);
- char *group = groups[i];
- int b;
-
- if (!get_int (keyfile, group, "window_x", &(info->window_x)))
- info->window_x = 0;
-
- if (!get_int (keyfile, group, "window_y", &(info->window_y)))
- info->window_y = 0;
-
- if (!get_int (keyfile, group, "window_width", &(info->window_width)))
- info->window_width = 0;
-
- if (!get_int (keyfile, group, "window_height", &(info->window_height)))
- info->window_height = 0;
-
- if (!get_int (keyfile, group, "background", &b))
- info->background = BG_NONE;
- else
- info->background = b;
-
- if (info->background >= BG_LAST || info->background < BG_FIRST)
- info->background = BG_NONE;
-
- if (!get_int (keyfile, group, "smooth_image", &(info->smooth_image)))
- info->smooth_image = TRUE;
-
- if (!get_int (keyfile, group, "zoom_level", &(info->zoom_level)))
- info->zoom_level = 0;
-
- g_hash_table_insert (data->info, group, info);
- }
-
- g_strfreev (groups);
-}
-
-MetaData *
-meta_data_new (void)
-{
- GKeyFile *keyfile;
- char *filename;
- MetaData *meta_data;
-
- keyfile = g_key_file_new ();
- filename = make_filename();
-
- meta_data = g_new0 (MetaData, 1);
- meta_data->info = g_hash_table_new (g_str_hash, g_str_equal);
-
- if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, NULL))
- populate_meta_data_from_keyfile (meta_data, keyfile);
-
- g_free (filename);
- g_key_file_free (keyfile);
-
- return meta_data;
-}
-
-const MetaInfo *
-meta_data_get_info (MetaData *data,
- const char *filename)
-{
- const MetaInfo *info = g_hash_table_lookup (data->info, filename);
-
- return info;
-}
-
-static void
-foreach (gpointer key, gpointer value, gpointer user_data)
-{
-
-
-}
-
-static void
-meta_data_save (MetaData *meta_data)
-{
- char *filename;
- GKeyFile *keyfile = g_key_file_new ();
- char *data;
-
- g_hash_table_foreach (meta_data->info, foreach, keyfile);
-
- filename = make_filename ();
-
- data = g_key_file_to_data (keyfile, NULL, NULL);
- if (data)
- {
- g_file_set_contents (filename, data, -1, NULL);
- g_free (data);
- }
-
- g_free (filename);
-}
-
-void
-meta_data_add_info (MetaData *data,
- const char *filename,
- int window_x,
- int window_y,
- int window_width,
- int window_height,
- int smooth_image,
- BackgroundType background,
- int zoom_level)
-{
- MetaInfo *info = g_hash_table_lookup (data->info, filename);
-
- if (!info)
- info = g_new0 (MetaInfo, 1);
-
- info->window_x = window_x;
- info->window_y = window_y;
- info->window_width = window_width;
- info->window_height = window_height;
- info->background = background;
- info->smooth_image = smooth_image;
- info->zoom_level = zoom_level;
-
- meta_data_save (data);
-}
diff --git a/metadata.h b/metadata.h
deleted file mode 100644
index 5f7a762..0000000
--- a/metadata.h
+++ /dev/null
@@ -1,42 +0,0 @@
-#ifndef META_DATA_H
-#define META_DATA_H
-
-#include <glib.h>
-
-typedef struct MetaData MetaData;
-typedef struct MetaInfo MetaInfo;
-typedef enum
-{
- BG_FIRST,
- BG_NONE = BG_FIRST,
- BG_CHECKERBAORD,
- BG_WHITE,
- BG_LAST
-} BackgroundType;
-
-
-struct MetaInfo
-{
- int window_x;
- int window_y;
- int window_width;
- int window_height;
- BackgroundType background;
- gboolean smooth_image;
- int zoom_level;
-};
-
-MetaData * meta_data_new (void);
-const MetaInfo *meta_data_get_info (MetaData *data,
- const char *filename);
-void meta_data_add_info (MetaData *data,
- const char *filename,
- int window_x,
- int window_y,
- int window_height,
- int window_width,
- gboolean smooth_image,
- BackgroundType background,
- int zoom_level);
-
-#endif
diff --git a/siv.c b/siv.c
index 2884951..1d1a58b 100644
--- a/siv.c
+++ b/siv.c
@@ -5,15 +5,14 @@
#include <string.h>
#include <math.h>
#include "siv.h"
-#include "metadata.h"
#define _(a) a
struct App
{
- MetaData *meta_data;
-
int n_windows;
+
+ GHashTable *meta_data;
};
static void
@@ -54,12 +53,161 @@ app_unregister_window (App *app, Window *window)
gtk_main_quit ();
}
-MetaInfo *
+MetaData *
app_get_meta_info (App *app, const char *file)
{
/* FIXME */
}
+MetaData *
+app_get_meta_data (App *app,
+ const char *file)
+{
+ MetaData *data = g_hash_table_lookup (app->meta_data, file);
+
+ return data;
+}
+
+static gchar *
+make_filename (void)
+{
+ return g_build_filename (
+ g_get_home_dir(), ".gnome2", "siv.metadata", NULL);
+}
+
+static void
+foreach (gpointer key, gpointer value, gpointer user_data)
+{
+ /* FIXME */
+}
+
+void
+app_set_meta_data (App *app,
+ const char *filename,
+ int window_x,
+ int window_y,
+ int window_height,
+ int window_width,
+ gboolean smooth_image,
+ BackgroundType background,
+ int zoom_level)
+{
+ GKeyFile *keyfile = g_key_file_new ();
+ MetaData *data = g_hash_table_lookup (app->meta_data, filename);
+ char *key_filename = make_filename();
+ char *output;
+
+ if (!data)
+ {
+ data = g_new0 (MetaData, 1);
+
+ g_hash_table_insert (app->meta_data, g_strdup (filename), 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;
+
+ g_hash_table_foreach (app->meta_data, foreach, keyfile);
+
+ output = g_key_file_to_data (keyfile, NULL, NULL);
+ if (output)
+ {
+ g_file_set_contents (filename, output, -1, NULL);
+ g_free (output);
+ }
+
+ g_key_file_free (keyfile);
+ g_free (key_filename);
+}
+
+
+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 (g_str_hash, g_str_equal);
+
+ 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;
+
+ g_hash_table_insert (result, group, data);
+ }
+
+ g_strfreev (groups);
+ }
+
+ g_free (filename);
+ g_key_file_free (keyfile);
+
+ return result;
+}
+
static void
app_new (int argc, char **argv)
{
@@ -88,7 +236,8 @@ app_new (int argc, char **argv)
filenames = g_list_reverse (filenames);
- app->meta_data = meta_data_new ();
+ app->meta_data = load_meta_data ();
+ g_hash_table_new (g_str_hash, g_str_equal);
for (list = filenames; list != NULL; list = list->next)
{
diff --git a/siv.h b/siv.h
index 80a9eb5..f842a5e 100644
--- a/siv.h
+++ b/siv.h
@@ -3,7 +3,6 @@
#include <gtk/gtk.h>
#include <glade/glade.h>
-#include "metadata.h"
#define APPLICATION_NAME "Simple Image Viewer"
#define PACKAGE_VERSION "0.0.1"
@@ -11,15 +10,45 @@
typedef struct Window Window;
typedef struct App App;
+typedef struct MetaData MetaData;
+typedef enum
+{
+ BG_FIRST,
+ BG_NONE = BG_FIRST,
+ BG_CHECKERBAORD,
+ BG_WHITE,
+ BG_LAST
+} BackgroundType;
+
+struct MetaData
+{
+ int window_x;
+ int window_y;
+ int window_width;
+ int window_height;
+ BackgroundType background;
+ gboolean smooth_image;
+ int zoom_level;
+};
/* App */
-void app_register_window (App *app,
- Window *window);
-void app_unregister_window (App *app,
- Window *window);
-MetaInfo *app_get_meta_info (App *app,
- const char *file);
+void app_register_window (App *app,
+ Window *window);
+void app_unregister_window (App *app,
+ Window *window);
+MetaData *app_get_meta_data (App *app,
+ const char *file);
+void app_set_meta_data (App *data,
+ const char *filename,
+ int window_x,
+ int window_y,
+ int window_height,
+ int window_width,
+ gboolean smooth_image,
+ BackgroundType background,
+ int zoom_level);
+
/* Window */
Window * window_new (App *app);