summaryrefslogtreecommitdiffstats
path: root/src/gui/Contact.vala
blob: 512c33f0df05ffb18618074a446251015bde75e2 (plain)
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
87
88
89
90
91
92
93
public class Contact : Object {
  private static Gee.TreeSet<string> defaultGroups = new Gee.TreeSet<string>();
  private Gee.TreeSet<string> groups = new Gee.TreeSet<string>();
  private Gee.HashMap<string, Resource> resources = new Gee.HashMap<string, Resource>();
  
  public string jid {get; construct;}
  public string? name {get; construct;}
  public Subscription subscription {get; set;}
  public Gdk.Pixbuf? avatar {get; set; default = null;}
  public string display_string {get; private set;}
  
  static construct {
    defaultGroups.add(get_default_group());
  }
  
  private static string get_default_group() {
    return "Allgemein";
  }
  
  public Contact(string jid0, string? name0) {
    Object(jid: jid0, name: name0);
    
    subscription = Subscription.NONE;
    
    update_display_string();
  }
  
  public Gee.Set<string> get_groups() {
    if(groups.is_empty) {
      return defaultGroups.read_only_view;
    }
    else {
      return groups.read_only_view;
    }
  }
  
  public void add_group(string group) {
    groups.add(group);
  }
  
  private void update_display_string() {
    if (name != null)
      display_string = name;
    else
      display_string = jid.split("@", 2)[0];
  }
  
  public Gee.Map.Entry<string, Resource>? get_resource_with_highest_priority() {
    int max_prio = int.MIN;
    Gee.Map.Entry<string, Resource> ret = null;
    
    foreach(Gee.Map.Entry<string, Resource> res in resources) {
      if(res.value.priority > max_prio) {
        max_prio = res.value.priority;
        ret = res;
      }
    }
    
    return ret;
  }
  
  public void update_resource(string resource_name, Resource resource) {
    resources[resource_name] = resource;
    update_display_string();
  }

  
  public enum Show {
    ONLINE, AWAY, CHAT, DND, XA, UNDEFINED
  }
  
  public enum Subscription {
    NONE, PENDING_OUT, PENDING_IN, PENDING_BOTH, TO, TO_PENDING_IN, FROM, FROM_PENDING_OUT, BOTH;
    
    public bool is_to() {
      return (this == TO || this == TO_PENDING_IN || this == BOTH);
    }
    
    public bool is_from() {
      return (this == FROM || this == FROM_PENDING_OUT || this == BOTH);
    }
  }
  
  public class Resource : Object {
    public Resource(int prio0, Show show0, string? status0) {
      Object(priority: prio0, show: show0, status: status0);
    }
    
    public int priority {get; construct;}
    public Show show {get; construct;}
    public string? status {get; construct;}
  }
}