[epiphany] ephy-session: add a method to load a session from a string



commit 600192397f1a70f56f8c592353e40fe213aa3157
Author: Xan Lopez <xan igalia com>
Date:   Wed Jun 13 21:47:07 2012 +0200

    ephy-session: add a method to load a session from a string
    
    And re-write the ephy_session_load method on top of it. We'll use this
    in our unit tests.

 src/ephy-session.c |   92 ++++++++++++++++++++++++++++++++++++++++-----------
 src/ephy-session.h |    5 +++
 2 files changed, 77 insertions(+), 20 deletions(-)
---
diff --git a/src/ephy-session.c b/src/ephy-session.c
index 7a4ebbc..642ba68 100644
--- a/src/ephy-session.c
+++ b/src/ephy-session.c
@@ -1052,37 +1052,41 @@ restore_geometry (GtkWindow *window,
 }
 
 /**
- * ephy_session_load:
- * @session: a #EphySession
- * @filename: the path of the source file
- * @user_time: a user_time, or 0
- *
- * Load a session from disk, restoring the windows and their state
- *
- * Return value: TRUE if at least a window has been opened
+ * ephy_session_load_from_string:
+ * @session: an #EphySession
+ * @session_data: a string with the session data to load
+ * @length: the length of @session_data, or -1 to assume %NULL terminated
+ * @user_time: a user time, or 0
+ * 
+ * Loads the session described in @session_data, restoring windows and
+ * their state.
+ * 
+ * Returns: TRUE if at least a window has been opened
  **/
 gboolean
-ephy_session_load (EphySession *session,
-		   const char *filename,
-		   guint32 user_time)
+ephy_session_load_from_string (EphySession *session,
+			       const char *session_data,
+			       gssize length,
+			       guint32 user_time)
 {
-	EphySessionPrivate *priv = session->priv;
+	EphySessionPrivate *priv;
 	xmlDocPtr doc;
 	xmlNodePtr child;
 	EphyWindow *window;
 	GtkWidget *widget = NULL;
-	GFile *save_to_file;
-	char *save_to_path;
 	gboolean first_window_created = FALSE;
+	
+	g_return_val_if_fail (EPHY_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (session_data, FALSE);
 
-	LOG ("ephy_sesion_load %s", filename);
+	priv = session->priv;
 
-	save_to_file = get_session_file (filename);
-	save_to_path = g_file_get_path (save_to_file);
-	g_object_unref (save_to_file);
+	/* If length is -1 assume the data is a NULL-terminated, UTF-8
+	 * encoded string. */
+	if (length == -1)
+		length = g_utf8_strlen (session_data, -1);
 
-	doc = xmlParseFile (save_to_path);
-	g_free (save_to_path);
+	doc = xmlParseMemory (session_data, (int)length);
 
 	if (doc == NULL)
 	{
@@ -1190,6 +1194,54 @@ ephy_session_load (EphySession *session,
 }
 
 /**
+ * ephy_session_load:
+ * @session: a #EphySession
+ * @filename: the path of the source file
+ * @user_time: a user_time, or 0
+ *
+ * Load a session from disk, restoring the windows and their state
+ *
+ * Return value: TRUE if at least a window has been opened
+ **/
+gboolean
+ephy_session_load (EphySession *session,
+		   const char *filename,
+		   guint32 user_time)
+{
+	GFile *save_to_file;
+	char *save_to_path;
+	gboolean ret_value;
+	char *contents;
+	gsize length;
+	GError *error = NULL;
+
+	g_return_val_if_fail (EPHY_IS_SESSION (session), FALSE);
+	g_return_val_if_fail (filename, FALSE);
+
+	LOG ("ephy_sesion_load %s", filename);
+
+	save_to_file = get_session_file (filename);
+	save_to_path = g_file_get_path (save_to_file);
+	g_object_unref (save_to_file);
+
+	if (!g_file_get_contents (save_to_path, &contents, &length, &error))
+	{
+		LOG ("Could not load session, error reading session file: %s", error->message);
+		g_error_free (error);
+		g_free (save_to_path);
+
+		return FALSE;
+	}
+
+	ret_value = ephy_session_load_from_string (session, contents, length, user_time);
+
+	g_free (contents);
+	g_free (save_to_path);
+
+	return ret_value;
+}
+
+/**
  * ephy_session_get_windows:
  * @session: the #EphySession
  *
diff --git a/src/ephy-session.h b/src/ephy-session.h
index 3bf1ffd..59741a2 100644
--- a/src/ephy-session.h
+++ b/src/ephy-session.h
@@ -79,6 +79,11 @@ gboolean	 ephy_session_load		(EphySession *session,
 						 const char *filename,
 						 guint32 user_time);
 
+gboolean	 ephy_session_load_from_string	(EphySession *session,
+						 const char *session_data,
+						 gssize length,
+						 guint32 user_time);
+
 void		 ephy_session_close		(EphySession *session);
 
 GList		*ephy_session_get_windows	(EphySession *session);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]