diff options
author | Xan Lopez <xan@igalia.com> | 2012-06-14 03:47:07 +0800 |
---|---|---|
committer | Xan Lopez <xan@igalia.com> | 2012-06-14 13:40:12 +0800 |
commit | 600192397f1a70f56f8c592353e40fe213aa3157 (patch) | |
tree | 15ef61e5263ba3b5e878f3fa79dbb08a71c50fa4 /src | |
parent | 6c219a58f4126138eb7ff31614b5101272fc6a30 (diff) | |
download | gsoc2013-epiphany-600192397f1a70f56f8c592353e40fe213aa3157.tar.gz gsoc2013-epiphany-600192397f1a70f56f8c592353e40fe213aa3157.tar.zst gsoc2013-epiphany-600192397f1a70f56f8c592353e40fe213aa3157.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/ephy-session.c | 92 | ||||
-rw-r--r-- | 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 7a4ebbce0..642ba686d 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 3bf1ffd9a..59741a272 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); |