From 4ce4ae46413bc574375818a37e233b52e374af56 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sun, 20 Mar 2016 12:36:57 +0000 Subject: [PATCH] Add save_state() and load_state() to Client --- src/matrix-client.vala | 19 +++++++++ src/matrix-http-api.vala | 4 +- src/matrix-http-client.vala | 81 +++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/src/matrix-client.vala b/src/matrix-client.vala index 4fc4ecb..ee6de07 100644 --- a/src/matrix-client.vala +++ b/src/matrix-client.vala @@ -247,4 +247,23 @@ public interface Matrix.Client : GLib.Object { SendCallback? cb, out ulong txn_id) throws Matrix.Error; + + /* + * Save the state of the client. Implementors can choose what they + * actually save, and in what format. + * + * @param filename the name of the file to save state to + */ + public abstract void + save_state(string filename) + throws Matrix.Error, GLib.Error; + + /** + * Load the state of the client, as saved by save_state(). + * + * @param filename the name of the file to load state from + */ + public abstract void + load_state(string filename) + throws Matrix.Error, GLib.Error; } diff --git a/src/matrix-http-api.vala b/src/matrix-http-api.vala index b26142a..0f1c54d 100644 --- a/src/matrix-http-api.vala +++ b/src/matrix-http-api.vala @@ -87,7 +87,7 @@ public class Matrix.HTTPAPI : GLib.Object, Matrix.API { _soup_session.ssl_strict = value; } } - private string? _user_id; + protected string? _user_id; public string? user_id { get { return _user_id; @@ -97,7 +97,7 @@ public class Matrix.HTTPAPI : GLib.Object, Matrix.API { } public string? token { get; set; default = null; } public string? refresh_token { get; set; default = null; } - private string? _homeserver; + protected string? _homeserver; public string? homeserver { get { return _homeserver; diff --git a/src/matrix-http-client.vala b/src/matrix-http-client.vala index 674a470..6258512 100644 --- a/src/matrix-http-client.vala +++ b/src/matrix-http-client.vala @@ -564,4 +564,85 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client { evt_root.get_member("content")); } } + + public void + save_state(string filename) + throws Matrix.Error, GLib.Error + { + var root = new Json.Object(); + + root.set_string_member("base_url", base_url); + + root.set_boolean_member("validate_certificate", validate_certificate); + + if (user_id != null) { + root.set_string_member("user_id", user_id); + } + + if (homeserver != null) { + root.set_string_member("homeserver_name", homeserver); + } + + if (token != null) { + root.set_string_member("access_token", token); + } + + if (refresh_token != null) { + root.set_string_member("refresh_token", refresh_token); + } + + var node = new Json.Node(Json.NodeType.OBJECT); + node.set_object(root); + + var generator = new Json.Generator(); + generator.set_root(node); + generator.to_file(filename); + } + + public void + load_state(string filename) + throws Matrix.Error, GLib.Error + { + var parser = new Json.Parser(); + + parser.load_from_file(filename); + Json.Node? node = parser.get_root(); + + if (node.get_node_type() != Json.NodeType.OBJECT) { + throw new Matrix.Error.INVALID_FORMAT( + "Save data must be a JSON object."); + } + + var root = node.get_object(); + + if ((node = root.get_member("base_url")) == null) { + throw new Matrix.Error.INVALID_FORMAT( + "Save data has no base_url key"); + } + + base_url = node.get_string(); + + if ((node = root.get_member("validate_certificate")) == null) { + throw new Matrix.Error.INVALID_FORMAT( + "Save data has no validate_certificate key"); + } + + validate_certificate = node.get_boolean(); + + if ((node = root.get_member("user_id")) != null) { + _user_id = node.get_string(); + } + + if ((node = root.get_member("homeserver_name")) != null) { + _homeserver = node.get_string(); + } + + if ((node = root.get_member("access_token")) != null) { + token = node.get_string(); + } + + if ((node = root.get_member("refresh_token")) != null) { + refresh_token = node.get_string(); + } + } }