diff options
author | fiaxh <git@mx.ax.lt> | 2017-03-22 17:15:06 +0100 |
---|---|---|
committer | fiaxh <git@mx.ax.lt> | 2017-03-23 14:36:53 +0100 |
commit | fa78573b052693b29350bdd0f7eaf74dc6571e4a (patch) | |
tree | 7efcb3eb96e5c3d17c6a1c0604eff533e1244a20 /libdino/src/entity/account.vala | |
parent | ec06d5f9ebd7142f4be422e73b46d6702671927d (diff) | |
download | dino-fa78573b052693b29350bdd0f7eaf74dc6571e4a.tar.gz dino-fa78573b052693b29350bdd0f7eaf74dc6571e4a.zip |
Move some database interaction into entities
fixes #2
Diffstat (limited to 'libdino/src/entity/account.vala')
-rw-r--r-- | libdino/src/entity/account.vala | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/libdino/src/entity/account.vala b/libdino/src/entity/account.vala index 48be527a..3f1bfdb2 100644 --- a/libdino/src/entity/account.vala +++ b/libdino/src/entity/account.vala @@ -1,6 +1,7 @@ using Gee; namespace Dino.Entities { + public class Account : Object { public int id { get; set; } @@ -10,19 +11,51 @@ public class Account : Object { public Jid bare_jid { get; private set; } public string? password { get; set; } public string display_name { - owned get { - if (alias != null) { - return alias; - } else { - return bare_jid.to_string(); - } - } + owned get { return alias ?? bare_jid.to_string(); } } public string? alias { get; set; } - public bool enabled { get; set; } + public bool enabled { get; set; default = false; } + + private Database? db; - public Account.from_bare_jid(string bare_jid) { - this.bare_jid = new Jid(bare_jid); + public Account(Jid bare_jid, string? resourcepart, string? password, string? alias) { + this.id = -1; + this.resourcepart = resourcepart ?? "dino." + Random.next_int().to_string("%x"); + this.bare_jid = bare_jid; + this.password = password; + this.alias = alias; + } + + public Account.from_row(Database db, Qlite.Row row) { + this.db = db; + id = row[db.account.id]; + resourcepart = row[db.account.resourcepart]; + bare_jid = new Jid(row[db.account.bare_jid]); + password = row[db.account.password]; + alias = row[db.account.alias]; + enabled = row[db.account.enabled]; + + notify.connect(on_update); + } + + public void persist(Database db) { + this.db = db; + id = (int) db.account.insert() + .value(db.account.bare_jid, bare_jid.to_string()) + .value(db.account.resourcepart, resourcepart) + .value(db.account.password, password) + .value(db.account.alias, alias) + .value(db.account.enabled, enabled) + .perform(); + + notify.connect(on_update); + } + + public void remove() { + db.account.delete().with(db.account.bare_jid, "=", bare_jid.to_string()).perform(); + notify.disconnect(on_update); + id = -1; + db = null; } public bool equals(Account acc) { @@ -36,5 +69,16 @@ public class Account : Object { public static uint hash_func(Account acc) { return acc.bare_jid.to_string().hash(); } + + private void on_update(Object o, ParamSpec sp) { + db.account.update().with(db.account.id, "=", id) + .set(db.account.bare_jid, bare_jid.to_string()) + .set(db.account.resourcepart, resourcepart) + .set(db.account.password, password) + .set(db.account.alias, alias) + .set(db.account.enabled, enabled) + .perform(); + } } + }
\ No newline at end of file |