aboutsummaryrefslogtreecommitdiffstats
path: root/mail/em-folder-tree-model.c
diff options
context:
space:
mode:
authorJeffrey Stedfast <fejj@ximian.com>2003-12-03 01:50:38 +0800
committerJeffrey Stedfast <fejj@src.gnome.org>2003-12-03 01:50:38 +0800
commit8f446ab62628c20b9e745f2a9bcea520ab578b9d (patch)
tree54793330b3fc74ea3ecb4897cba357da4f6be893 /mail/em-folder-tree-model.c
parentf22ee6ae4a87bf23c1bfbe2daa5ddd2fc50e3d9e (diff)
downloadgsoc2013-evolution-8f446ab62628c20b9e745f2a9bcea520ab578b9d.tar.gz
gsoc2013-evolution-8f446ab62628c20b9e745f2a9bcea520ab578b9d.tar.zst
gsoc2013-evolution-8f446ab62628c20b9e745f2a9bcea520ab578b9d.zip
Fixed to pass the evolution dir into em_folder_tree_model_new().
2003-12-02 Jeffrey Stedfast <fejj@ximian.com> * em-folder-tree.c (em_folder_tree_new): Fixed to pass the evolution dir into em_folder_tree_model_new(). (model_save_state): Removed. (em_folder_tree_save_state): Moved all the saving logic into em_folder_tree_model_save_expanded(). * em-folder-tree-model.c (em_folder_tree_model_new): Load the expanded state off disk. (em_folder_tree_model_save_expanded): New function to save expanded state. (em_folder_tree_model_get_expanded): new function to get if a node should be expanded. (em_folder_tree_model_set_expanded): new function to set the expanded state of a node. * mail-component.c (mail_component_init): Pass the evo dir to em_folder_tree_model_new() which now requires it. svn path=/trunk/; revision=23585
Diffstat (limited to 'mail/em-folder-tree-model.c')
-rw-r--r--mail/em-folder-tree-model.c130
1 files changed, 129 insertions, 1 deletions
diff --git a/mail/em-folder-tree-model.c b/mail/em-folder-tree-model.c
index 677ab8d0fe..e9819346be 100644
--- a/mail/em-folder-tree-model.c
+++ b/mail/em-folder-tree-model.c
@@ -25,7 +25,14 @@
#include <config.h>
#endif
+#include <stdio.h>
#include <string.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include <camel/camel-file-utils.h>
#include "mail-config.h"
@@ -201,6 +208,7 @@ em_folder_tree_model_init (EMFolderTreeModel *model)
{
model->store_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
model->uri_hash = g_hash_table_new (g_str_hash, g_str_equal);
+ model->expanded = g_hash_table_new (g_str_hash, g_str_equal);
}
static void
@@ -241,6 +249,12 @@ uri_hash_free (gpointer key, gpointer value, gpointer user_data)
gtk_tree_row_reference_free (value);
}
+static gboolean
+expanded_free (gpointer key, gpointer value, gpointer user_data)
+{
+ g_free (key);
+ return TRUE;
+}
static void
em_folder_tree_model_finalize (GObject *obj)
@@ -253,6 +267,11 @@ em_folder_tree_model_finalize (GObject *obj)
g_hash_table_foreach (model->uri_hash, uri_hash_free, NULL);
g_hash_table_destroy (model->uri_hash);
+ g_hash_table_foreach (model->expanded, (GHFunc) expanded_free, NULL);
+ g_hash_table_destroy (model->expanded);
+
+ g_free (model->filename);
+
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
@@ -335,14 +354,37 @@ model_drag_data_delete (GtkTreeDragSource *drag_source, GtkTreePath *src_path)
}
+static void
+em_folder_tree_model_load_state (EMFolderTreeModel *model, const char *filename)
+{
+ char *node;
+ FILE *fp;
+
+ g_hash_table_foreach_remove (model->expanded, expanded_free, NULL);
+
+ if ((fp = fopen (filename, "r")) == NULL)
+ return;
+
+ while (camel_file_util_decode_string (fp, &node) != -1)
+ g_hash_table_insert (model->expanded, node, GINT_TO_POINTER (TRUE));
+
+ fclose (fp);
+}
+
+
EMFolderTreeModel *
-em_folder_tree_model_new (void)
+em_folder_tree_model_new (const char *evolution_dir)
{
EMFolderTreeModel *model;
+ char *filename;
model = g_object_new (EM_TYPE_FOLDER_TREE_MODEL, NULL);
gtk_tree_store_set_column_types ((GtkTreeStore *) model, NUM_COLUMNS, col_types);
+ filename = g_build_filename (evolution_dir, "mail", "config", "folder-tree.state", NULL);
+ em_folder_tree_model_load_state (model, filename);
+ model->filename = filename;
+
return model;
}
@@ -767,3 +809,89 @@ em_folder_tree_model_remove_store (EMFolderTreeModel *model, CamelStore *store)
/* recursively remove subfolders and finally the toplevel store */
em_folder_tree_model_remove_folders (model, si, &iter);
}
+
+
+gboolean
+em_folder_tree_model_get_expanded (EMFolderTreeModel *model, const char *key)
+{
+ if (g_hash_table_lookup (model->expanded, key))
+ return TRUE;
+
+ return FALSE;
+}
+
+
+void
+em_folder_tree_model_set_expanded (EMFolderTreeModel *model, const char *key, gboolean expanded)
+{
+ gpointer okey, oval;
+
+ if (g_hash_table_lookup_extended (model->expanded, key, &okey, &oval)) {
+ g_hash_table_remove (model->expanded, okey);
+ g_free (okey);
+ }
+
+ if (expanded)
+ g_hash_table_insert (model->expanded, g_strdup (key), GINT_TO_POINTER (TRUE));
+}
+
+
+static void
+expanded_save (gpointer key, gpointer value, FILE *fp)
+{
+ if (!GPOINTER_TO_INT (value))
+ return;
+
+ camel_file_util_encode_string (fp, key);
+}
+
+void
+em_folder_tree_model_save_expanded (EMFolderTreeModel *model)
+{
+ char *dirname, *tmpname;
+ FILE *fp;
+ int fd;
+
+ dirname = g_path_get_dirname (model->filename);
+ if (camel_mkdir (dirname, 0777) == -1 && errno != EEXIST) {
+ g_free (dirname);
+ return;
+ }
+
+ g_free (dirname);
+ tmpname = g_strdup_printf ("%s~", model->filename);
+
+ if (!(fp = fopen (tmpname, "w+"))) {
+ g_free (tmpname);
+ return;
+ }
+
+ g_hash_table_foreach (model->expanded, (GHFunc) expanded_save, fp);
+
+ if (fflush (fp) != 0)
+ goto exception;
+
+ if ((fd = fileno (fp)) == -1)
+ goto exception;
+
+ if (fsync (fd) == -1)
+ goto exception;
+
+ fclose (fp);
+ fp = NULL;
+
+ if (rename (tmpname, model->filename) == -1)
+ goto exception;
+
+ g_free (tmpname);
+
+ return;
+
+ exception:
+
+ if (fp != NULL)
+ fclose (fp);
+
+ unlink (tmpname);
+ g_free (tmpname);
+}