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/message.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/message.vala')
-rw-r--r-- | libdino/src/entity/message.vala | 99 |
1 files changed, 95 insertions, 4 deletions
diff --git a/libdino/src/entity/message.vala b/libdino/src/entity/message.vala index 16562561..602c74b6 100644 --- a/libdino/src/entity/message.vala +++ b/libdino/src/entity/message.vala @@ -1,8 +1,8 @@ using Gee; -using Xmpp; +namespace Dino.Entities { -public class Dino.Entities.Message : Object { +public class Message : Object { public const bool DIRECTION_SENT = true; public const bool DIRECTION_RECEIVED = false; @@ -45,8 +45,62 @@ public class Dino.Entities.Message : Object { public Marked marked { get; set; default = Marked.NONE; } public Xmpp.Message.Stanza stanza { get; set; } + private Database? db; + + public Message(string? body, Type type) { + this.id = -1; + this.body = body; + this.type_ = type; + } + + public Message.from_row(Database db, Qlite.Row row) { + this.db = db; + + id = row[db.message.id]; + stanza_id = row[db.message.stanza_id]; + string from = db.get_jid_by_id(row[db.message.counterpart_id]); + string from_resource = row[db.message.counterpart_resource]; + counterpart = from_resource != null ? new Jid(from + "/" + from_resource) : new Jid(from); + direction = row[db.message.direction]; + type_ = (Message.Type) row[db.message.type_]; + time = new DateTime.from_unix_utc(row[db.message.time]); + body = row[db.message.body]; + account = db.get_account_by_id(row[db.message.account_id]); // TODO dont have to generate acc new + marked = (Message.Marked) row[db.message.marked]; + encryption = (Encryption) row[db.message.encryption]; + real_jid = db.real_jid.select({db.real_jid.real_jid}).with(db.real_jid.message_id, "=", id)[db.real_jid.real_jid]; + + notify.connect(on_update); + } + + public void persist(Database db) { + this.db = db; + Qlite.InsertBuilder builder = db.message.insert() + .value(db.message.account_id, account.id) + .value(db.message.counterpart_id, db.get_jid_id(counterpart)) + .value(db.message.counterpart_resource, counterpart.resourcepart) + .value(db.message.our_resource, ourpart.resourcepart) + .value(db.message.direction, direction) + .value(db.message.type_, type_) + .value(db.message.time, (long) time.to_unix()) + .value(db.message.local_time, (long) local_time.to_unix()) + .value(db.message.body, body) + .value(db.message.encryption, encryption) + .value(db.message.marked, marked); + if (stanza_id != null) builder.value(db.message.stanza_id, stanza_id); + id = (int) builder.perform(); + + if (real_jid != null) { + db.real_jid.insert() + .value(db.real_jid.message_id, id) + .value(db.real_jid.real_jid, real_jid) + .perform(); + } + notify.connect(on_update); + } + public void set_type_string(string type) { - switch (type) { + switch (type) { case Xmpp.Message.Stanza.TYPE_CHAT: type_ = Type.CHAT; break; case Xmpp.Message.Stanza.TYPE_GROUPCHAT: @@ -57,7 +111,7 @@ public class Dino.Entities.Message : Object { } public new string get_type_string() { - switch (type_) { + switch (type_) { case Type.CHAT: return Xmpp.Message.Stanza.TYPE_CHAT; case Type.GROUPCHAT: @@ -83,4 +137,41 @@ public class Dino.Entities.Message : Object { public static uint hash_func(Message message) { return message.body.hash(); } + + private void on_update(Object o, ParamSpec sp) { + Qlite.UpdateBuilder update_builder = db.message.update().with(db.message.id, "=", id); + switch (sp.get_name()) { + case "stanza_id": + update_builder.set(db.message.stanza_id, stanza_id); break; + case "counterpart": + update_builder.set(db.message.counterpart_id, db.get_jid_id(counterpart)); + update_builder.set(db.message.counterpart_resource, counterpart.resourcepart); break; + case "ourpart": + update_builder.set(db.message.our_resource, ourpart.resourcepart); break; + case "direction": + update_builder.set(db.message.direction, direction); break; + case "type_": + update_builder.set(db.message.type_, type_); break; + case "time": + update_builder.set(db.message.time, (long) time.to_unix()); break; + case "local_time": + update_builder.set(db.message.local_time, (long) local_time.to_unix()); break; + case "body": + update_builder.set(db.message.body, body); break; + case "encryption": + update_builder.set(db.message.encryption, encryption); break; + case "marked": + update_builder.set(db.message.marked, marked); break; + } + update_builder.perform(); + + if (sp.get_name() == "real_jid") { + db.real_jid.insert() + .value(db.real_jid.message_id, id) + .value(db.real_jid.real_jid, real_jid) + .perform(); + } + } } + +}
\ No newline at end of file |