aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog25
-rw-r--r--embed/ephy-favicon-cache.c60
-rw-r--r--embed/ephy-history.c68
-rw-r--r--lib/ephy-node-db.c152
-rw-r--r--lib/ephy-node-db.h12
-rw-r--r--lib/ephy-node.c6
-rw-r--r--lib/ephy-node.h2
-rw-r--r--lib/ephy-state.c45
-rw-r--r--src/bookmarks/ephy-bookmarks.c75
9 files changed, 206 insertions, 239 deletions
diff --git a/ChangeLog b/ChangeLog
index cbb72dd77..b3d5bb0f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+2003-05-23 Marco Pesenti Gritti <marco@it.gnome.org>
+
+ * embed/ephy-favicon-cache.c: (ephy_favicon_cache_load),
+ (ephy_favicon_cache_save), (ephy_favicon_cache_init),
+ (ephy_favicon_cache_finalize):
+ * embed/ephy-history.c: (ephy_history_load), (ephy_history_save),
+ (ephy_history_init):
+ * lib/ephy-node-db.c: (ephy_node_db_set_version),
+ (ephy_node_db_get_property), (ephy_node_db_set_property),
+ (ephy_node_db_class_init), (ephy_node_db_init),
+ (ephy_node_db_finalize), (ephy_node_db_new),
+ (_ephy_node_db_remove_id), (ephy_node_db_load_from_xml),
+ (ephy_node_db_save_to_xml):
+ * lib/ephy-node-db.h:
+ * lib/ephy-node.c: (ephy_node_to_xml):
+ * lib/ephy-node.h:
+ * lib/ephy-state.c: (ephy_states_load), (ephy_states_save),
+ (ensure_states):
+ * src/bookmarks/ephy-bookmarks.c: (ephy_bookmarks_load),
+ (ephy_bookmarks_save), (ephy_bookmarks_init),
+ (ephy_bookmarks_finalize):
+
+ Implement generic way to save/load db and make it
+ low disk safe.
+
2003-05-22 David Bordoley <bordoley@msu.edu>
* src/bookmarks/ephy-topic-action.c:
diff --git a/embed/ephy-favicon-cache.c b/embed/ephy-favicon-cache.c
index 1391c5f5c..e73a03340 100644
--- a/embed/ephy-favicon-cache.c
+++ b/embed/ephy-favicon-cache.c
@@ -122,30 +122,7 @@ ephy_favicon_cache_new (void)
static void
ephy_favicon_cache_load (EphyFaviconCache *eb)
{
- xmlDocPtr doc;
- xmlNodePtr root, child;
- char *tmp;
-
- if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE)
- return;
-
- doc = xmlParseFile (eb->priv->xml_file);
- g_assert (doc != NULL);
-
- root = xmlDocGetRootElement (doc);
-
- tmp = xmlGetProp (root, "version");
- g_assert (tmp != NULL && strcmp (tmp, EPHY_FAVICON_CACHE_XML_VERSION) == 0);
- g_free (tmp);
-
- for (child = root->children; child != NULL; child = child->next)
- {
- EphyNode *node;
-
- node = ephy_node_new_from_xml (eb->priv->db, child);
- }
-
- xmlFreeDoc (doc);
+ ephy_node_db_load_from_xml (eb->priv->db, eb->priv->xml_file);
}
static gboolean
@@ -229,33 +206,7 @@ remove_obsolete_icons (EphyFaviconCache *eb)
static void
ephy_favicon_cache_save (EphyFaviconCache *eb)
{
- xmlDocPtr doc;
- xmlNodePtr root;
- GPtrArray *children;
- int i;
-
- /* save nodes to xml */
- xmlIndentTreeOutput = TRUE;
- doc = xmlNewDoc ("1.0");
-
- root = xmlNewDocNode (doc, NULL, "ephy_favicons_cache", NULL);
- xmlSetProp (root, "version", EPHY_FAVICON_CACHE_XML_VERSION);
- xmlDocSetRootElement (doc, root);
-
- children = ephy_node_get_children (eb->priv->icons);
- for (i = 0; i < children->len; i++)
- {
- EphyNode *kid;
-
- kid = g_ptr_array_index (children, i);
-
- ephy_node_save_to_xml (kid, root);
- }
- ephy_node_thaw (eb->priv->icons);
-
- xmlSaveFormatFile (eb->priv->xml_file, doc, 1);
-
- xmlFreeDoc (doc);
+ ephy_node_db_save_to_xml (eb->priv->db, eb->priv->xml_file);
}
static void
@@ -265,7 +216,7 @@ ephy_favicon_cache_init (EphyFaviconCache *cache)
cache->priv = g_new0 (EphyFaviconCachePrivate, 1);
- db = ephy_node_db_new ("EphyFaviconCache");
+ db = ephy_node_db_new ("EphyFaviconCache", EPHY_FAVICON_CACHE_XML_VERSION);
cache->priv->db = db;
cache->priv->xml_file = g_build_filename (ephy_dot_dir (),
@@ -351,17 +302,16 @@ ephy_favicon_cache_finalize (GObject *object)
g_return_if_fail (cache->priv != NULL);
- g_object_unref (cache->priv->db);
-
+ ephy_favicon_cache_save (cache);
cleanup_downloads_hash (cache);
remove_obsolete_icons (cache);
- ephy_favicon_cache_save (cache);
g_free (cache->priv->xml_file);
g_free (cache->priv->directory);
g_hash_table_destroy (cache->priv->icons_hash);
g_static_rw_lock_free (cache->priv->icons_hash_lock);
g_hash_table_destroy (cache->priv->downloads_hash);
+ g_object_unref (cache->priv->db);
g_free (cache->priv);
diff --git a/embed/ephy-history.c b/embed/ephy-history.c
index c9541c618..0806ee2f3 100644
--- a/embed/ephy-history.c
+++ b/embed/ephy-history.c
@@ -199,30 +199,7 @@ ephy_history_class_init (EphyHistoryClass *klass)
static void
ephy_history_load (EphyHistory *eb)
{
- xmlDocPtr doc;
- xmlNodePtr root, child;
- char *tmp;
-
- if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE)
- return;
-
- doc = xmlParseFile (eb->priv->xml_file);
- g_return_if_fail (doc != NULL);
-
- root = xmlDocGetRootElement (doc);
-
- tmp = xmlGetProp (root, "version");
- g_assert (tmp != NULL && strcmp (tmp, EPHY_HISTORY_XML_VERSION) == 0);
- g_free (tmp);
-
- for (child = root->children; child != NULL; child = child->next)
- {
- EphyNode *node;
-
- node = ephy_node_new_from_xml (eb->priv->db, child);
- }
-
- xmlFreeDoc (doc);
+ ephy_node_db_load_from_xml (eb->priv->db, eb->priv->xml_file);
}
static gboolean
@@ -271,46 +248,7 @@ remove_obsolete_pages (EphyHistory *eb)
static void
ephy_history_save (EphyHistory *eb)
{
- xmlDocPtr doc;
- xmlNodePtr root;
- GPtrArray *children;
- int i;
-
- LOG ("Saving history")
-
- /* save nodes to xml */
- xmlIndentTreeOutput = TRUE;
- doc = xmlNewDoc ("1.0");
-
- root = xmlNewDocNode (doc, NULL, "ephy_history", NULL);
- xmlSetProp (root, "version", EPHY_HISTORY_XML_VERSION);
- xmlDocSetRootElement (doc, root);
-
- children = ephy_node_get_children (eb->priv->hosts);
- for (i = 0; i < children->len; i++)
- {
- EphyNode *kid;
-
- kid = g_ptr_array_index (children, i);
- if (kid == eb->priv->pages) continue;
-
- ephy_node_save_to_xml (kid, root);
- }
- ephy_node_thaw (eb->priv->hosts);
-
- children = ephy_node_get_children (eb->priv->pages);
- for (i = 0; i < children->len; i++)
- {
- EphyNode *kid;
-
- kid = g_ptr_array_index (children, i);
-
- ephy_node_save_to_xml (kid, root);
- }
- ephy_node_thaw (eb->priv->pages);
-
- xmlSaveFormatFile (eb->priv->xml_file, doc, 1);
- xmlFreeDoc(doc);
+ ephy_node_db_save_to_xml (eb->priv->db, eb->priv->xml_file);
}
static void
@@ -399,7 +337,7 @@ ephy_history_init (EphyHistory *eb)
eb->priv = g_new0 (EphyHistoryPrivate, 1);
- db = ephy_node_db_new ("EphyHistory");
+ db = ephy_node_db_new ("EphyHistory", EPHY_HISTORY_XML_VERSION);
eb->priv->db = db;
eb->priv->xml_file = g_build_filename (ephy_dot_dir (),
diff --git a/lib/ephy-node-db.c b/lib/ephy-node-db.c
index 86913d93c..bb10759ec 100644
--- a/lib/ephy-node-db.c
+++ b/lib/ephy-node-db.c
@@ -19,6 +19,9 @@
*/
#include "ephy-node-db.h"
+#include "ephy-debug.h"
+
+#include <unistd.h>
static void ephy_node_db_class_init (EphyNodeDbClass *klass);
static void ephy_node_db_init (EphyNodeDb *node);
@@ -30,12 +33,14 @@ static void ephy_node_db_finalize (GObject *object);
enum
{
PROP_0,
- PROP_NAME
+ PROP_NAME,
+ PROP_VERSION
};
struct EphyNodeDbPrivate
{
char *name;
+ char *version;
GMutex *id_factory_lock;
long id_factory;
@@ -75,6 +80,12 @@ ephy_node_db_get_type (void)
}
static void
+ephy_node_db_set_version (EphyNodeDb *db, const char *version)
+{
+ db->priv->version = g_strdup (version);
+}
+
+static void
ephy_node_db_set_name (EphyNodeDb *db, const char *name)
{
db->priv->name = g_strdup (name);
@@ -102,6 +113,9 @@ ephy_node_db_get_property (GObject *object,
case PROP_NAME:
g_value_set_string (value, db->priv->name);
break;
+ case PROP_VERSION:
+ g_value_set_string (value, db->priv->version);
+ break;
}
}
@@ -121,6 +135,9 @@ ephy_node_db_set_property (GObject *object,
case PROP_NAME:
ephy_node_db_set_name (db, g_value_get_string (value));
break;
+ case PROP_VERSION:
+ ephy_node_db_set_version (db, g_value_get_string (value));
+ break;
}
}
@@ -142,6 +159,14 @@ ephy_node_db_class_init (EphyNodeDbClass *klass)
"Name",
NULL,
G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_VERSION,
+ g_param_spec_string ("version",
+ "Version",
+ "Version",
+ NULL,
+ G_PARAM_READWRITE));
+
}
static void
@@ -150,6 +175,7 @@ ephy_node_db_init (EphyNodeDb *db)
db->priv = g_new0 (EphyNodeDbPrivate, 1);
db->priv->name = NULL;
+ db->priv->version = NULL;
/* id to node */
db->priv->id_to_node = g_ptr_array_new ();
@@ -186,6 +212,7 @@ ephy_node_db_finalize (GObject *object)
g_mutex_free (db->priv->id_factory_lock);
g_free (db->priv->name);
+ g_free (db->priv->version);
g_free (db->priv);
@@ -203,12 +230,13 @@ ephy_node_db_get_by_name (const char *name)
}
EphyNodeDb *
-ephy_node_db_new (const char *name)
+ephy_node_db_new (const char *name, const char *version)
{
EphyNodeDb *db;
db = EPHY_NODE_DB (g_object_new (EPHY_TYPE_NODE_DB,
"name", name,
+ "version", version,
NULL));
g_return_val_if_fail (db->priv != NULL, NULL);
@@ -295,3 +323,123 @@ _ephy_node_db_remove_id (EphyNodeDb *db,
g_static_rw_lock_writer_unlock (db->priv->id_to_node_lock);
}
+
+gboolean
+ephy_node_db_load_from_xml (EphyNodeDb *db, const char *xml_file)
+{
+ xmlDocPtr doc;
+ xmlNodePtr root, child;
+
+ if (g_file_test (xml_file, G_FILE_TEST_EXISTS) == FALSE)
+ {
+ return FALSE;
+ }
+
+ doc = xmlParseFile (xml_file);
+ g_return_val_if_fail (doc != NULL, FALSE);
+
+ root = xmlDocGetRootElement (doc);
+
+ for (child = root->children; child != NULL; child = child->next)
+ {
+ EphyNode *node;
+
+ node = ephy_node_new_from_xml (db, child);
+ }
+
+ xmlFreeDoc (doc);
+
+ return TRUE;
+}
+
+gboolean
+ephy_node_db_save_to_xml (EphyNodeDb *db, const char *xml_file)
+{
+ xmlDocPtr doc;
+ xmlNodePtr root;
+ GPtrArray *children;
+ int i;
+ char *tmp_file;
+ char *old_file;
+ gboolean old_exist;
+ gboolean retval = TRUE;
+
+ LOG ("Build the xml file %s", xml_file)
+
+ tmp_file = g_strconcat (xml_file, ".tmp", NULL);
+ old_file = g_strconcat (xml_file, ".old", NULL);
+
+ /* save nodes to xml */
+ xmlIndentTreeOutput = TRUE;
+ doc = xmlNewDoc ("1.0");
+
+ root = xmlNewDocNode (doc, NULL, "ephy_node_db", NULL);
+ xmlDocSetRootElement (doc, root);
+
+ xmlSetProp (root, "name", db->priv->name);
+ xmlSetProp (root, "version", db->priv->version);
+
+ g_static_rw_lock_reader_lock (db->priv->id_to_node_lock);
+
+ children = db->priv->id_to_node;
+
+ for (i = RESERVED_IDS; i < children->len; i++)
+ {
+ EphyNode *kid;
+
+ kid = g_ptr_array_index (children, i);
+
+ if (kid)
+ {
+ ephy_node_to_xml (kid, root);
+ }
+ }
+
+ g_static_rw_lock_reader_unlock (db->priv->id_to_node_lock);
+
+ LOG ("Save it")
+
+ if (!xmlSaveFormatFile (tmp_file, doc, 1))
+ {
+ g_warning ("Failed to write XML data to %s", tmp_file);
+ goto failed;
+ }
+
+ old_exist = g_file_test (xml_file, G_FILE_TEST_EXISTS);
+
+ if (old_exist)
+ {
+ if (rename (xml_file, old_file) < 0)
+ {
+ g_warning ("Failed to rename %s to %s", xml_file, old_file);
+ retval = FALSE;
+ goto failed;
+ }
+ }
+
+ if (rename (tmp_file, xml_file) < 0)
+ {
+ g_warning ("Failed to rename %s to %s", tmp_file, xml_file);
+
+ if (rename (old_file, xml_file) < 0)
+ {
+ g_warning ("Failed to restore %s from %s", xml_file, tmp_file);
+ }
+ retval = FALSE;
+ goto failed;
+ }
+
+ if (old_exist)
+ {
+ if (unlink (old_file) < 0)
+ {
+ g_warning ("Failed to delete old file %s", old_file);
+ }
+ }
+
+ failed:
+ g_free (old_file);
+ g_free (tmp_file);
+
+ return retval;
+}
diff --git a/lib/ephy-node-db.h b/lib/ephy-node-db.h
index 2b1f8fd7d..d801c4743 100644
--- a/lib/ephy-node-db.h
+++ b/lib/ephy-node-db.h
@@ -54,13 +54,23 @@ GType ephy_node_db_get_type (void);
EphyNodeDb *ephy_node_db_get_by_name (const char *name);
-EphyNodeDb *ephy_node_db_new (const char *name);
+EphyNodeDb *ephy_node_db_new (const char *name,
+ const char *version);
const char *ephy_node_db_get_name (EphyNodeDb *db);
EphyNode *ephy_node_db_get_node_from_id (EphyNodeDb *db,
long id);
+EphyNode *ephy_node_db_get_root_from_id (EphyNodeDb *db,
+ long id);
+
+gboolean ephy_node_db_load_from_xml (EphyNodeDb *db,
+ const char *xml_file);
+
+gboolean ephy_node_db_save_to_xml (EphyNodeDb *db,
+ const char *xml_file);
+
long _ephy_node_db_new_id (EphyNodeDb *db);
void _ephy_node_db_add_id (EphyNodeDb *db,
diff --git a/lib/ephy-node.c b/lib/ephy-node.c
index d155aae78..b9f0c0695 100644
--- a/lib/ephy-node.c
+++ b/lib/ephy-node.c
@@ -719,8 +719,8 @@ save_parent (gulong id,
}
void
-ephy_node_save_to_xml (EphyNode *node,
- xmlNodePtr parent_xml_node)
+ephy_node_to_xml (EphyNode *node,
+ xmlNodePtr parent_xml_node)
{
xmlNodePtr xml_node;
char *xml;
@@ -736,8 +736,6 @@ ephy_node_save_to_xml (EphyNode *node,
xmlSetProp (xml_node, "id", xml);
g_free (xml);
- xmlSetProp (xml_node, "type", G_OBJECT_TYPE_NAME (node));
-
for (i = 0; i < node->properties->len; i++) {
GValue *value;
xmlNodePtr value_xml_node;
diff --git a/lib/ephy-node.h b/lib/ephy-node.h
index 846d5fa41..591e5118a 100644
--- a/lib/ephy-node.h
+++ b/lib/ephy-node.h
@@ -96,7 +96,7 @@ char *ephy_node_get_property_time (EphyNode *node,
guint property_id);
/* xml storage */
-void ephy_node_save_to_xml (EphyNode *node,
+void ephy_node_to_xml (EphyNode *node,
xmlNodePtr parent_xml_node);
EphyNode *ephy_node_new_from_xml (EphyNodeDb *db,
xmlNodePtr xml_node);
diff --git a/lib/ephy-state.c b/lib/ephy-state.c
index 90d06883c..0703ebddf 100644
--- a/lib/ephy-state.c
+++ b/lib/ephy-state.c
@@ -31,6 +31,7 @@
#define STATES_FILE "states.xml"
#define WINDOW_POSITION_UNSET -1
+#define EPHY_STATES_XML_VERSION "0.1"
enum
{
@@ -48,30 +49,13 @@ static EphyNodeDb *states_db = NULL;
static void
ephy_states_load (void)
{
- xmlDocPtr doc;
- xmlNodePtr root, child;
char *xml_file;
xml_file = g_build_filename (ephy_dot_dir (),
STATES_FILE,
NULL);
- if (g_file_test (xml_file, G_FILE_TEST_EXISTS) == FALSE)
- return;
-
- doc = xmlParseFile (xml_file);
- g_assert (doc != NULL);
-
- root = xmlDocGetRootElement (doc);
-
- for (child = root->children; child != NULL; child = child->next)
- {
- EphyNode *node;
-
- node = ephy_node_new_from_xml (states_db, child);
- }
-
- xmlFreeDoc (doc);
+ ephy_node_db_load_from_xml (states_db, xml_file);
g_free (xml_file);
}
@@ -79,10 +63,6 @@ ephy_states_load (void)
static void
ephy_states_save (void)
{
- xmlDocPtr doc;
- xmlNodePtr root;
- GPtrArray *children;
- int i;
char *xml_file;
if (states == NULL) return;
@@ -91,25 +71,8 @@ ephy_states_save (void)
STATES_FILE,
NULL);
- /* save nodes to xml */
- xmlIndentTreeOutput = TRUE;
- doc = xmlNewDoc ("1.0");
-
- root = xmlNewDocNode (doc, NULL, "ephy_bookmarks", NULL);
- xmlDocSetRootElement (doc, root);
-
- children = ephy_node_get_children (states);
- for (i = 0; i < children->len; i++)
- {
- EphyNode *kid;
-
- kid = g_ptr_array_index (children, i);
-
- ephy_node_save_to_xml (kid, root);
- }
- ephy_node_thaw (states);
+ ephy_node_db_save_to_xml (states_db, xml_file);
- xmlSaveFormatFile (xml_file, doc, 1);
g_free (xml_file);
}
@@ -145,7 +108,7 @@ ensure_states (void)
{
if (states == NULL)
{
- states_db = ephy_node_db_new ("EphyStates");
+ states_db = ephy_node_db_new ("EphyStates", EPHY_STATES_XML_VERSION);
states = ephy_node_new_with_id (states_db, STATES_NODE_ID);
ephy_states_load ();
}
diff --git a/src/bookmarks/ephy-bookmarks.c b/src/bookmarks/ephy-bookmarks.c
index b43b1f981..dd747559e 100644
--- a/src/bookmarks/ephy-bookmarks.c
+++ b/src/bookmarks/ephy-bookmarks.c
@@ -265,82 +265,15 @@ ephy_bookmarks_init_defaults (EphyBookmarks *eb)
static gboolean
ephy_bookmarks_load (EphyBookmarks *eb)
{
- xmlDocPtr doc;
- xmlNodePtr root, child;
- char *tmp;
-
- if (g_file_test (eb->priv->xml_file, G_FILE_TEST_EXISTS) == FALSE)
- return FALSE;
-
- doc = xmlParseFile (eb->priv->xml_file);
- g_return_val_if_fail (doc != NULL, FALSE);
-
- root = xmlDocGetRootElement (doc);
-
- tmp = xmlGetProp (root, "version");
- g_assert (tmp != NULL && strcmp (tmp, EPHY_BOOKMARKS_XML_VERSION) == 0);
- g_free (tmp);
-
- for (child = root->children; child != NULL; child = child->next)
- {
- EphyNode *node;
-
- node = ephy_node_new_from_xml (eb->priv->db, child);
- }
-
- xmlFreeDoc (doc);
-
- return TRUE;
+ return ephy_node_db_load_from_xml (eb->priv->db, eb->priv->xml_file);
}
void
ephy_bookmarks_save (EphyBookmarks *eb)
{
- xmlDocPtr doc;
- xmlNodePtr root;
- GPtrArray *children;
- int i;
char *rdf_file;
- LOG ("Saving bookmarks")
-
- /* save nodes to xml */
- xmlIndentTreeOutput = TRUE;
- doc = xmlNewDoc ("1.0");
-
- root = xmlNewDocNode (doc, NULL, "ephy_bookmarks", NULL);
- xmlSetProp (root, "version", EPHY_BOOKMARKS_XML_VERSION);
- xmlDocSetRootElement (doc, root);
-
- children = ephy_node_get_children (eb->priv->keywords);
- for (i = 0; i < children->len; i++)
- {
- EphyNode *kid;
-
- kid = g_ptr_array_index (children, i);
-
- if (kid != eb->priv->bookmarks &&
- kid != eb->priv->favorites &&
- kid != eb->priv->notcategorized)
- {
- ephy_node_save_to_xml (kid, root);
- }
- }
- ephy_node_thaw (eb->priv->keywords);
-
- children = ephy_node_get_children (eb->priv->bookmarks);
- for (i = 0; i < children->len; i++)
- {
- EphyNode *kid;
-
- kid = g_ptr_array_index (children, i);
-
- ephy_node_save_to_xml (kid, root);
- }
- ephy_node_thaw (eb->priv->bookmarks);
-
- xmlSaveFormatFile (eb->priv->xml_file, doc, 1);
- xmlFreeDoc(doc);
+ ephy_node_db_save_to_xml (eb->priv->db, eb->priv->xml_file);
/* Export bookmarks in rdf */
rdf_file = g_build_filename (ephy_dot_dir (),
@@ -586,7 +519,7 @@ ephy_bookmarks_init (EphyBookmarks *eb)
eb->priv = g_new0 (EphyBookmarksPrivate, 1);
- db = ephy_node_db_new ("EphyBookmarks");
+ db = ephy_node_db_new ("EphyBookmarks", EPHY_BOOKMARKS_XML_VERSION);
eb->priv->db = db;
eb->priv->xml_file = g_build_filename (ephy_dot_dir (),
@@ -691,6 +624,8 @@ ephy_bookmarks_finalize (GObject *object)
ephy_node_unref (eb->priv->favorites);
ephy_node_unref (eb->priv->notcategorized);
+ g_object_unref (eb->priv->db);
+
g_free (eb->priv);
LOG ("Bookmarks finalized")