aboutsummaryrefslogtreecommitdiff
path: root/client/src/entity
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/entity')
-rw-r--r--client/src/entity/account.vala40
-rw-r--r--client/src/entity/conversation.vala48
-rw-r--r--client/src/entity/jid.vala91
-rw-r--r--client/src/entity/message.vala89
4 files changed, 268 insertions, 0 deletions
diff --git a/client/src/entity/account.vala b/client/src/entity/account.vala
new file mode 100644
index 00000000..48be527a
--- /dev/null
+++ b/client/src/entity/account.vala
@@ -0,0 +1,40 @@
+using Gee;
+
+namespace Dino.Entities {
+public class Account : Object {
+
+ public int id { get; set; }
+ public string localpart { get { return bare_jid.localpart; } }
+ public string domainpart { get { return bare_jid.domainpart; } }
+ public string resourcepart { get; set; }
+ 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();
+ }
+ }
+ }
+ public string? alias { get; set; }
+ public bool enabled { get; set; }
+
+ public Account.from_bare_jid(string bare_jid) {
+ this.bare_jid = new Jid(bare_jid);
+ }
+
+ public bool equals(Account acc) {
+ return equals_func(this, acc);
+ }
+
+ public static bool equals_func(Account acc1, Account acc2) {
+ return acc1.bare_jid.to_string() == acc2.bare_jid.to_string();
+ }
+
+ public static uint hash_func(Account acc) {
+ return acc.bare_jid.to_string().hash();
+ }
+}
+} \ No newline at end of file
diff --git a/client/src/entity/conversation.vala b/client/src/entity/conversation.vala
new file mode 100644
index 00000000..d5c861d9
--- /dev/null
+++ b/client/src/entity/conversation.vala
@@ -0,0 +1,48 @@
+namespace Dino.Entities {
+public class Conversation : Object {
+
+ public signal void object_updated(Conversation conversation);
+
+ public const int ENCRYPTION_UNENCRYPTED = 0;
+ public const int ENCRYPTION_PGP = 1;
+
+ public const int TYPE_CHAT = 0;
+ public const int TYPE_GROUPCHAT = 1;
+
+ 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 int encryption { get; set; }
+ public int? type_ { get; set; }
+ public Message read_up_to { get; set; }
+
+ public Conversation(Jid jid, Account account) {
+ this.counterpart = jid;
+ this.account = account;
+ this.active = false;
+ this.last_active = new DateTime.from_unix_utc(0);
+ this.encryption = ENCRYPTION_UNENCRYPTED;
+ }
+
+ public Conversation.with_id(Jid jid, Account account, int id) {
+ this.counterpart = jid;
+ this.account = account;
+ this.id = id;
+ }
+
+ public bool equals(Conversation? conversation) {
+ if (conversation == null) return false;
+ return equals_func(this, conversation);
+ }
+
+ public static bool equals_func(Conversation conversation1, Conversation conversation2) {
+ return conversation1.counterpart.equals(conversation2.counterpart) && conversation1.account.equals(conversation2.account);
+ }
+
+ public static uint hash_func(Conversation conversation) {
+ return conversation.counterpart.to_string().hash() ^ conversation.account.bare_jid.to_string().hash();
+ }
+}
+} \ No newline at end of file
diff --git a/client/src/entity/jid.vala b/client/src/entity/jid.vala
new file mode 100644
index 00000000..aab31b98
--- /dev/null
+++ b/client/src/entity/jid.vala
@@ -0,0 +1,91 @@
+public class Dino.Entities.Jid : Object {
+ public string? localpart { get; set; }
+ public string domainpart { get; set; }
+ public string? resourcepart { get; set; }
+
+ public Jid? bare_jid {
+ owned get { return new Jid(@"$localpart@$domainpart"); }
+ }
+
+ private string jid { get; private set; }
+
+ public Jid(string jid) {
+ Jid? parsed = Jid.parse(jid);
+ string? localpart = parsed != null ? parsed.localpart : null;
+ string domainpart = parsed != null ? parsed.domainpart : jid;
+ string? resourcepart = parsed != null ? parsed.resourcepart : null;
+ Jid.components(localpart, domainpart, resourcepart);
+ }
+
+ public Jid.with_resource(string bare_jid, string resource) {
+ Jid? parsed = Jid.parse(bare_jid);
+ print(parsed.localpart + "\n");
+ print(parsed.domainpart + "\n");
+ Jid.components(parsed.localpart, parsed.domainpart, resourcepart);
+ }
+
+ public Jid.components(string? localpart, string domainpart, string? resourcepart) {
+ string jid = domainpart;
+ if (localpart != null) {
+ jid = @"$localpart@$jid";
+ }
+ if (resourcepart != null) {
+ jid = @"$jid/$resourcepart";
+ }
+ this.jid = jid;
+ this.localpart = localpart;
+ this.domainpart = domainpart;
+ this.resourcepart = resourcepart;
+ }
+
+ public static Jid? parse(string jid) {
+ int slash_index = jid.index_of("/");
+ string resourcepart = slash_index == -1 ? null : jid.slice(slash_index + 1, jid.length);
+ string bare_jid = slash_index == -1 ? jid : jid.slice(0, slash_index);
+ int at_index = bare_jid.index_of("@");
+ string localpart = at_index == -1 ? null : bare_jid.slice(0, at_index);
+ string domainpart = at_index == -1 ? bare_jid : bare_jid.slice(at_index + 1, bare_jid.length);
+
+ if (domainpart == "") return null;
+ if (slash_index != -1 && resourcepart == "") return null;
+ if (at_index != -1 && localpart == "") return null;
+
+ return new Jid.components(localpart, domainpart, resourcepart);
+ }
+
+ public bool is_bare() {
+ return localpart != null && resourcepart == null;
+ }
+
+ public bool is_full() {
+ return localpart != null && resourcepart != null;
+ }
+
+ public string to_string() {
+ return jid;
+ }
+
+ public bool equals_bare(Jid jid) {
+ return equals_bare_func(this, jid);
+ }
+
+ public bool equals(Jid jid) {
+ return equals_func(this, jid);
+ }
+
+ public static new bool equals_bare_func(Jid jid1, Jid jid2) {
+ return jid1.bare_jid.to_string() == jid2.bare_jid.to_string();
+ }
+
+ public static bool equals_func(Jid jid1, Jid jid2) {
+ return jid1.to_string() == jid2.to_string();
+ }
+
+ public static new uint hash_bare_func(Jid jid) {
+ return jid.bare_jid.to_string().hash();
+ }
+
+ public static new uint hash_func(Jid jid) {
+ return jid.to_string().hash();
+ }
+}
diff --git a/client/src/entity/message.vala b/client/src/entity/message.vala
new file mode 100644
index 00000000..042166b0
--- /dev/null
+++ b/client/src/entity/message.vala
@@ -0,0 +1,89 @@
+using Gee;
+
+using Xmpp;
+
+public class Dino.Entities.Message : Object {
+
+ public const bool DIRECTION_SENT = true;
+ public const bool DIRECTION_RECEIVED = false;
+
+ public enum Marked {
+ NONE,
+ RECEIVED,
+ READ,
+ ACKNOWLEDGED
+ }
+
+ public enum Encryption {
+ NONE,
+ PGP
+ }
+
+ public enum Type {
+ ERROR,
+ CHAT,
+ GROUPCHAT,
+ HEADLINE,
+ NORMAL
+ }
+
+ public int? id { get; set; }
+ public Account account { get; set; }
+ public Jid? counterpart { get; set; }
+ public Jid? ourpart { get; set; }
+ public Jid? from {
+ get { return direction == DIRECTION_SENT ? account.bare_jid : counterpart; }
+ }
+ public Jid? to {
+ get { return direction == DIRECTION_SENT ? counterpart : account.bare_jid; }
+ }
+ public bool direction { get; set; }
+ public string? real_jid { get; set; }
+ public Type type_ { get; set; }
+ public string? body { get; set; }
+ public string? stanza_id { get; set; }
+ public DateTime? time { get; set; }
+ public DateTime? local_time { get; set; }
+ public Encryption encryption { get; set; default = Encryption.NONE; }
+ public Marked marked { get; set; default = Marked.NONE; }
+ public Xmpp.Message.Stanza stanza { get; set; }
+
+ public void set_type_string(string type) {
+ switch (type) {
+ case Xmpp.Message.Stanza.TYPE_CHAT:
+ type_ = Type.CHAT; break;
+ case Xmpp.Message.Stanza.TYPE_GROUPCHAT:
+ type_ = Type.GROUPCHAT; break;
+ default:
+ type_ = Type.NORMAL; break;
+ }
+ }
+
+ public new string get_type_string() {
+ switch (type_) {
+ case Type.CHAT:
+ return Xmpp.Message.Stanza.TYPE_CHAT;
+ case Type.GROUPCHAT:
+ return Xmpp.Message.Stanza.TYPE_GROUPCHAT;
+ default:
+ return Xmpp.Message.Stanza.TYPE_NORMAL;
+ }
+ }
+
+ public bool equals(Message? m) {
+ if (m == null) return false;
+ return equals_func(this, m);
+ }
+
+ public static bool equals_func(Message m1, Message m2) {
+ if (m1.stanza_id == m2.stanza_id &&
+ m1.body == m2.body) {
+ return true;
+ }
+ return false;
+ }
+
+ public static uint hash_func(Message message) {
+ return message.body.hash();
+ }
+}