diff options
Diffstat (limited to 'libdino/src/service/database.vala')
-rw-r--r-- | libdino/src/service/database.vala | 192 |
1 files changed, 9 insertions, 183 deletions
diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 29c6f3ee..f631ff1f 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -137,122 +137,23 @@ public class Database : Qlite.Database { // new table columns are added, outdated columns are still present } - public void add_account(Account new_account) { - new_account.id = (int) account.insert() - .value(account.bare_jid, new_account.bare_jid.to_string()) - .value(account.resourcepart, new_account.resourcepart) - .value(account.password, new_account.password) - .value(account.alias, new_account.alias) - .value(account.enabled, new_account.enabled) - .perform(); - new_account.notify.connect(on_account_update); - } - - private void on_account_update(Object o, ParamSpec sp) { - Account changed_account = (Account) o; - account.update().with(account.id, "=", changed_account.id) - .set(account.bare_jid, changed_account.bare_jid.to_string()) - .set(account.resourcepart, changed_account.resourcepart) - .set(account.password, changed_account.password) - .set(account.alias, changed_account.alias) - .set(account.enabled, changed_account.enabled) - .perform(); - } - - public void remove_account(Account to_delete) { - account.delete().with(account.bare_jid, "=", to_delete.bare_jid.to_string()).perform(); - } - public ArrayList<Account> get_accounts() { ArrayList<Account> ret = new ArrayList<Account>(); foreach(Row row in account.select()) { - Account account = get_account_from_row(row); - account.notify.connect(on_account_update); + Account account = new Account.from_row(this, row); ret.add(account); } return ret; } - private Account? get_account_by_id(int id) { + public Account? get_account_by_id(int id) { Row? row = account.row_with(account.id, id).inner; if (row != null) { - return get_account_from_row(row); + return new Account.from_row(this, row); } return null; } - private Account get_account_from_row(Row row) { - Account new_account = new Account.from_bare_jid(row[account.bare_jid]); - - new_account.id = row[account.id]; - new_account.resourcepart = row[account.resourcepart]; - new_account.password = row[account.password]; - new_account.alias = row[account.alias]; - new_account.enabled = row[account.enabled]; - return new_account; - } - - public void add_message(Message new_message, Account account) { - InsertBuilder builder = message.insert() - .value(message.account_id, new_message.account.id) - .value(message.counterpart_id, get_jid_id(new_message.counterpart)) - .value(message.counterpart_resource, new_message.counterpart.resourcepart) - .value(message.our_resource, new_message.ourpart.resourcepart) - .value(message.direction, new_message.direction) - .value(message.type_, new_message.type_) - .value(message.time, (long) new_message.time.to_unix()) - .value(message.local_time, (long) new_message.local_time.to_unix()) - .value(message.body, new_message.body) - .value(message.encryption, new_message.encryption) - .value(message.marked, new_message.marked); - if (new_message.stanza_id != null) builder.value(message.stanza_id, new_message.stanza_id); - new_message.id = (int) builder.perform(); - - if (new_message.real_jid != null) { - real_jid.insert() - .value(real_jid.message_id, new_message.id) - .value(real_jid.real_jid, new_message.real_jid) - .perform(); - } - new_message.notify.connect(on_message_update); - } - - private void on_message_update(Object o, ParamSpec sp) { - Message changed_message = (Message) o; - UpdateBuilder update_builder = message.update().with(message.id, "=", changed_message.id); - switch (sp.get_name()) { - case "stanza_id": - update_builder.set(message.stanza_id, changed_message.stanza_id); break; - case "counterpart": - update_builder.set(message.counterpart_id, get_jid_id(changed_message.counterpart)); - update_builder.set(message.counterpart_resource, changed_message.counterpart.resourcepart); break; - case "ourpart": - update_builder.set(message.our_resource, changed_message.ourpart.resourcepart); break; - case "direction": - update_builder.set(message.direction, changed_message.direction); break; - case "type_": - update_builder.set(message.type_, changed_message.type_); break; - case "time": - update_builder.set(message.time, (long) changed_message.time.to_unix()); break; - case "local_time": - update_builder.set(message.local_time, (long) changed_message.local_time.to_unix()); break; - case "body": - update_builder.set(message.body, changed_message.body); break; - case "encryption": - update_builder.set(message.encryption, changed_message.encryption); break; - case "marked": - update_builder.set(message.marked, changed_message.marked); break; - } - update_builder.perform(); - - if (sp.get_name() == "real_jid") { - real_jid.insert() - .value(real_jid.message_id, changed_message.id) - .value(real_jid.real_jid, changed_message.real_jid) - .perform(); - } - } - public Gee.List<Message> get_messages(Jid jid, Account account, int count, Message? before) { string jid_id = get_jid_id(jid).to_string(); @@ -267,7 +168,7 @@ public class Database : Qlite.Database { LinkedList<Message> ret = new LinkedList<Message>(); foreach (Row row in select) { - ret.insert(0, get_message_from_row(row)); + ret.insert(0, new Message.from_row(this, row)); } return ret; } @@ -275,7 +176,7 @@ public class Database : Qlite.Database { public Gee.List<Message> get_unsend_messages(Account account) { Gee.List<Message> ret = new ArrayList<Message>(); foreach (Row row in message.select().with(message.marked, "=", (int) Message.Marked.UNSENT)) { - ret.add(get_message_from_row(row)); + ret.add(new Message.from_row(this, row)); } return ret; } @@ -307,94 +208,19 @@ public class Database : Qlite.Database { public Message? get_message_by_id(int id) { Row? row = message.row_with(message.id, id).inner; if (row != null) { - return get_message_from_row(row); + return new Message.from_row(this, row); } return null; } - public Message get_message_from_row(Row row) { - Message new_message = new Message(); - - new_message.id = row[message.id]; - new_message.stanza_id = row[message.stanza_id]; - string from = get_jid_by_id(row[message.counterpart_id]); - string from_resource = row[message.counterpart_resource]; - if (from_resource != null) { - new_message.counterpart = new Jid(from + "/" + from_resource); - } else { - new_message.counterpart = new Jid(from); - } - new_message.direction = row[message.direction]; - new_message.type_ = (Message.Type) row[message.type_]; - new_message.time = new DateTime.from_unix_utc(row[message.time]); - new_message.body = row[message.body]; - new_message.account = get_account_by_id(row[message.account_id]); // TODO dont have to generate acc new - new_message.marked = (Message.Marked) row[message.marked]; - new_message.encryption = (Encryption) row[message.encryption]; - new_message.real_jid = get_real_jid_for_message(new_message); - - new_message.notify.connect(on_message_update); - return new_message; - } - - public string? get_real_jid_for_message(Message message) { - return real_jid.select({real_jid.real_jid}).with(real_jid.message_id, "=", message.id)[real_jid.real_jid]; - } - - public void add_conversation(Conversation new_conversation) { - var insert = conversation.insert() - .value(conversation.jid_id, get_jid_id(new_conversation.counterpart)) - .value(conversation.account_id, new_conversation.account.id) - .value(conversation.type_, new_conversation.type_) - .value(conversation.encryption, new_conversation.encryption) - //.value(conversation.read_up_to, new_conversation.read_up_to) - .value(conversation.active, new_conversation.active); - if (new_conversation.last_active != null) { - insert.value(conversation.last_active, (long) new_conversation.last_active.to_unix()); - } - new_conversation.id = (int) insert.perform(); - new_conversation.notify.connect(on_conversation_update); - } - public ArrayList<Conversation> get_conversations(Account account) { ArrayList<Conversation> ret = new ArrayList<Conversation>(); foreach (Row row in conversation.select().with(conversation.account_id, "=", account.id)) { - ret.add(get_conversation_from_row(row)); + ret.add(new Conversation.from_row(this, row)); } return ret; } - private void on_conversation_update(Object o, ParamSpec sp) { - Conversation changed_conversation = (Conversation) o; - var update = conversation.update().with(conversation.jid_id, "=", get_jid_id(changed_conversation.counterpart)).with(conversation.account_id, "=", changed_conversation.account.id) - .set(conversation.type_, changed_conversation.type_) - .set(conversation.encryption, changed_conversation.encryption) - //.set(conversation.read_up_to, changed_conversation.read_up_to) - .set(conversation.active, changed_conversation.active); - if (changed_conversation.last_active != null) { - update.set(conversation.last_active, (long) changed_conversation.last_active.to_unix()); - } else { - update.set_null(conversation.last_active); - } - update.perform(); - } - - private Conversation get_conversation_from_row(Row row) { - Conversation new_conversation = new Conversation(new Jid(get_jid_by_id(row[conversation.jid_id])), get_account_by_id(row[conversation.account_id])); - - new_conversation.id = row[conversation.id]; - new_conversation.active = row[conversation.active]; - int64? last_active = row[conversation.last_active]; - if (last_active != null) new_conversation.last_active = new DateTime.from_unix_utc(last_active); - new_conversation.type_ = (Conversation.Type) row[conversation.type_]; - new_conversation.encryption = (Encryption) row[conversation.encryption]; - int? read_up_to = row[conversation.read_up_to]; - if (read_up_to != null) new_conversation.read_up_to = get_message_by_id(read_up_to); - - new_conversation.notify.connect(on_conversation_update); - return new_conversation; - } - public void set_avatar_hash(Jid jid, string hash, int type) { avatar.insert().or("REPLACE") .value(avatar.jid, jid.to_string()) @@ -429,12 +255,12 @@ public class Database : Qlite.Database { } - private int get_jid_id(Jid jid_obj) { + public int get_jid_id(Jid jid_obj) { Row? row = jid.row_with(jid.bare_jid, jid_obj.bare_jid.to_string()).inner; return row != null ? row[jid.id] : add_jid(jid_obj); } - private string? get_jid_by_id(int id) { + public string? get_jid_by_id(int id) { return jid.select({jid.bare_jid}).with(jid.id, "=", id)[jid.bare_jid]; } |