From fa78573b052693b29350bdd0f7eaf74dc6571e4a Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 22 Mar 2017 17:15:06 +0100 Subject: Move some database interaction into entities fixes #2 --- libdino/src/entity/conversation.vala | 68 +++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 12 deletions(-) (limited to 'libdino/src/entity/conversation.vala') diff --git a/libdino/src/entity/conversation.vala b/libdino/src/entity/conversation.vala index 5a83e02b..fd226b3e 100644 --- a/libdino/src/entity/conversation.vala +++ b/libdino/src/entity/conversation.vala @@ -1,4 +1,5 @@ namespace Dino.Entities { + public class Conversation : Object { public signal void object_updated(Conversation conversation); @@ -11,24 +12,51 @@ public class Conversation : Object { public int id { get; set; } public Account account { get; private set; } public Jid counterpart { get; private set; } - public bool active { get; set; } - public DateTime last_active { get; set; } - public Encryption encryption { get; set; } - public Type? type_ { get; set; } + public bool active { get; set; default = false; } + public DateTime? last_active { get; set; } + public Encryption encryption { get; set; default = Encryption.NONE; } + public Type type_ { get; set; } public Message read_up_to { get; set; } - public Conversation(Jid jid, Account account) { + private Database? db; + + public Conversation(Jid jid, Account account, Type type) { this.counterpart = jid; this.account = account; - this.active = false; - this.last_active = new DateTime.from_unix_utc(0); - this.encryption = Encryption.NONE; + this.type_ = type; } - public Conversation.with_id(Jid jid, Account account, int id) { - this.counterpart = jid; - this.account = account; - this.id = id; + public Conversation.from_row(Database db, Qlite.Row row) { + this.db = db; + + id = row[db.conversation.id]; + counterpart = new Jid(db.get_jid_by_id(row[db.conversation.jid_id])); + account = db.get_account_by_id(row[db.conversation.account_id]); + active = row[db.conversation.active]; + int64? last_active = row[db.conversation.last_active]; + if (last_active != null) this.last_active = new DateTime.from_unix_utc(last_active); + type_ = (Conversation.Type) row[db.conversation.type_]; + encryption = (Encryption) row[db.conversation.encryption]; + int? read_up_to = row[db.conversation.read_up_to]; + if (read_up_to != null) this.read_up_to = db.get_message_by_id(read_up_to); + + notify.connect(on_update); + } + + public void persist(Database db) { + this.db = db; + var insert = db.conversation.insert() + .value(db.conversation.jid_id, db.get_jid_id(counterpart)) + .value(db.conversation.account_id, account.id) + .value(db.conversation.type_, type_) + .value(db.conversation.encryption, encryption) + //.value(conversation.read_up_to, new_conversation.read_up_to) + .value(db.conversation.active, active); + if (last_active != null) { + insert.value(db.conversation.last_active, (long) last_active.to_unix()); + } + id = (int) insert.perform(); + notify.connect(on_update); } public bool equals(Conversation? conversation) { @@ -43,5 +71,21 @@ public class Conversation : Object { public static uint hash_func(Conversation conversation) { return conversation.counterpart.to_string().hash() ^ conversation.account.bare_jid.to_string().hash(); } + + private void on_update(Object o, ParamSpec sp) { + var update = db.conversation.update().with(db.conversation.jid_id, "=", db.get_jid_id(counterpart)) + .with(db.conversation.account_id, "=", account.id) + .set(db.conversation.type_, type_) + .set(db.conversation.encryption, encryption) + //.set(conversation.read_up_to, changed_conversation.read_up_to) + .set(db.conversation.active, active); + if (last_active != null) { + update.set(db.conversation.last_active, (long) last_active.to_unix()); + } else { + update.set_null(db.conversation.last_active); + } + update.perform(); + } } + } \ No newline at end of file -- cgit v1.2.3-54-g00ecf