aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/entity/message.vala
blob: 16562561b5dc7bcaf51deeecb6d6cca6bf83dae3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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,
        UNSENT,
        WONTSEND
    }

    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();
    }
}