Add save_state() and load_state() to Client
This commit is contained in:
parent
734a8349d0
commit
4ce4ae4641
@ -247,4 +247,23 @@ public interface Matrix.Client : GLib.Object {
|
|||||||
SendCallback? cb,
|
SendCallback? cb,
|
||||||
out ulong txn_id)
|
out ulong txn_id)
|
||||||
throws Matrix.Error;
|
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;
|
||||||
}
|
}
|
||||||
|
@ -87,7 +87,7 @@ public class Matrix.HTTPAPI : GLib.Object, Matrix.API {
|
|||||||
_soup_session.ssl_strict = value;
|
_soup_session.ssl_strict = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private string? _user_id;
|
protected string? _user_id;
|
||||||
public string? user_id {
|
public string? user_id {
|
||||||
get {
|
get {
|
||||||
return _user_id;
|
return _user_id;
|
||||||
@ -97,7 +97,7 @@ public class Matrix.HTTPAPI : GLib.Object, Matrix.API {
|
|||||||
}
|
}
|
||||||
public string? token { get; set; default = null; }
|
public string? token { get; set; default = null; }
|
||||||
public string? refresh_token { get; set; default = null; }
|
public string? refresh_token { get; set; default = null; }
|
||||||
private string? _homeserver;
|
protected string? _homeserver;
|
||||||
public string? homeserver {
|
public string? homeserver {
|
||||||
get {
|
get {
|
||||||
return _homeserver;
|
return _homeserver;
|
||||||
|
@ -564,4 +564,85 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client {
|
|||||||
evt_root.get_member("content"));
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user