summaryrefslogtreecommitdiffstats
path: root/src/gui/Roster.vala
blob: ade3161a978d1bb9bc253fed90e8aa06aa06ee4a (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
public class Roster {
  private Gtk.TreeView rosterView;
  private ContactListModel contactList;
  private RosterModel model;
  private Gee.Map<string, Gee.Map<string, Contact>> groups = new Gee.HashMap<string, Gee.Map<string, Contact>>();

  public signal void start_conversation(string jid);

  public Roster(Gtk.TreeView rosterView0) {
    rosterView = rosterView0;
    contactList = new ContactListModel(this);
    model = new RosterModel(contactList);
    
    rosterView.set_model(new RosterFilter(this, new RosterSorter(model)));

    rosterView.query_tooltip.connect((x, y, keyboard_tip, tooltip) => {
        Gtk.TreeModel model;
        Gtk.TreeIter iter;

        if(!rosterView.get_tooltip_context(out x, out y, keyboard_tip, out model, null, out iter))
          return false;

        Value value;
        model.get_value(iter, 0, out value);

        Contact? c = value.get_object() as Contact;
        if(c == null)
          return false;

        Gee.Map.Entry<string, Contact.Resource>? r = c.get_resource_with_highest_priority();

        if(r == null)
          return false;

        tooltip.set_text("Resource: " + r.key + " (" + r.value.priority.to_string() + ")");

        return true;
      });
    rosterView.has_tooltip = true;

    rosterView.row_activated.connect((view, path, column) => {
        Gtk.TreeIter iter;
        view.model.get_iter(out iter, path);

        Value value;
        view.model.get_value(iter, 0, out value);

        Contact contact = value.get_object() as Contact;
        if(contact == null)
          return;

        Gee.Map.Entry<string, Contact.Resource> res = contact.get_resource_with_highest_priority();
        if(res == null)
          return; //FIXME

        start_conversation(contact.jid + "/" + res.key);
      });
    
    rosterView.model.row_has_child_toggled.connect((path, iter) => {
        rosterView.expand_row(path, true);
      });
    
    Gtk.TreeViewColumn contactColumn = new Gtk.TreeViewColumn.with_attributes(null, new CellRendererContact(), "data", 0, null);
    rosterView.append_column(contactColumn);
  }
  
  public void update_contact(Contact c) {
    contactList.update_contact(c);
  }

  public Contact get_contact(string jid) {
    return contactList.get_contact(jid);
  }

  private void update_groups() {
    foreach(Gee.Map.Entry<string, Gee.Map<string, Contact>> group in groups) {
      if(group.value.is_empty) {
        model.remove_group(group.key);
        groups.unset(group.key);
      }
      else {
        model.add_group(group.key);
      }
    }
  }
  
  public Gee.Map<string, Contact> get_group(string group) {
    return groups[group];
  }
  
  private class RosterModel : Object, Gtk.TreeModel {
    private int stamp = 0;
    private Gee.Map<String, Gtk.TreeModel> groupModels =  new Gee.TreeMap<String, Gtk.TreeModel>();
    private Gee.ArrayList<Gtk.TreeIter?> childIters = new Gee.ArrayList<Gtk.TreeIter?>();
    private ContactListModel contactList;

    
    private void incrementStamp() {
      stamp++;
      childIters.clear();
    }
    
    public RosterModel(ContactListModel contactList0) {
      contactList = contactList0;
    }
    
    public void add_group(string name) {
      int index = 0;
      foreach(String group in groupModels.keys) {
        if(group.data == name) {
          Gtk.TreePath path = new Gtk.TreePath.from_indices(index, -1);
          Gtk.TreeIter iter;
          get_iter(out iter, path);
          row_changed(path, iter);
          
          return;
        }
        
        index++;
      }
      
      Gtk.TreeModel model = new GroupFilter(contactList, name);

      model.row_changed.connect((subpath, subiter) => {
          Gtk.TreePath path = child_path_to_path(name, subpath);
          Gtk.TreeIter iter;
          get_iter(out iter, path);
          row_changed(path, iter);
        });
      
      model.row_deleted.connect((path) => {
          path.prepend_index(0);
          row_deleted(path);

          Gtk.TreePath parent = path.copy();
          parent.up();
          if(parent.get_depth() == 1) {
            Gtk.TreeIter iter;
            get_iter(out iter, parent);
            if(!iter_has_child(iter)) {
              row_has_child_toggled(parent, iter);
            }
          }
        });
      
      model.row_has_child_toggled.connect((subpath, subiter) => {
          Gtk.TreePath path = child_path_to_path(name, subpath);
          Gtk.TreeIter iter;
          get_iter(out iter, path);
          row_has_child_toggled(path, iter);
        });
      
      model.row_inserted.connect((subpath, subiter) => {
          Gtk.TreePath path = child_path_to_path(name, subpath);
          
          incrementStamp();
          Gtk.TreeIter iter;
          get_iter(out iter, path);
          row_inserted(path, iter);
          
          Gtk.TreePath parent = path.copy();
          parent.up();
          if(parent.get_depth() == 1) {
            get_iter(out iter, parent);
            Value value;
            get_value(iter, 0, out value);
            if(iter_n_children(iter) == 0) {
              row_has_child_toggled(parent, iter);
            }
          }
        });
      
      model.rows_reordered.connect((subpath, subiter, new_order) => {
          Gtk.TreePath path = child_path_to_path(name, subpath);
          Gtk.TreeIter iter;
          get_iter(out iter, path);
          rows_reordered(path, iter, new_order);
        });
      
      groupModels[new String(name)] = model;
      
      index = 0;
      foreach(String group in groupModels.keys) {
        if(group.data == name)
          break;
        
        index++;
      }
      
      Gtk.TreePath path = new Gtk.TreePath.from_indices(index, -1);
      Gtk.TreeIter iter;
      get_iter(out iter, path);
      row_inserted(path, iter);
      
      model.foreach((childModel, childPath, childIter) => {
          Gtk.TreePath subpath = child_path_to_path(name, childPath);
          Gtk.TreeIter subiter;
          get_iter(out subiter, subpath);
          row_inserted(subpath, subiter);
          
          return false;
        });
    }
    
    public void remove_group(string name) {
      int index = 0;
      foreach(String group in groupModels.keys) {
        if(group.data == name) {
          Gee.LinkedList<Gtk.TreePath> children = new Gee.LinkedList<Gtk.TreePath>();
          
          groupModels[group].foreach((childModel, childPath, childIter) => {
              children.offer_head(childPath);
              
              return false;
            });
          
          while(!children.is_empty) {
            row_deleted(child_path_to_path(name, children.poll_head()));
          }
          
          Gtk.TreePath path = new Gtk.TreePath.from_indices(index, -1);
          row_deleted(path);
          
          groupModels.unset(group);
          
          return;
        }
        
        index++;
      }
    }
    
    private Gtk.TreePath child_path_to_path(string groupName, Gtk.TreePath childPath) {
      int index = 0;
      foreach(String group in groupModels.keys) {
        if(group.data == groupName) {
          Gtk.TreePath path = childPath.copy();
          path.prepend_index(index);
          
          return path;
        }
        
        index++;
      }
      
      assert_not_reached();
    }
    
    private long child_iter_index(Gtk.TreeIter iter) {
      for(int index = 0; index < childIters.size; ++index) {
        if(childIters[index].stamp != iter.stamp)
          continue;
        if(childIters[index].user_data != iter.user_data)
          continue;
        if(childIters[index].user_data2 != iter.user_data2)
          continue;
        if(childIters[index].user_data3 != iter.user_data3)
          continue;
        
        return index;
      }
      
      childIters.add(iter);
      return childIters.size-1;
    }
    
    private Gtk.TreeIter get_nth_child_iter(long n) {
      return childIters[(int)n];
    }
    
    private Gtk.TreeIter? get_child_iter(Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);
      if((long)iter.user_data3 != -1)
        return get_nth_child_iter((long)iter.user_data3);
      else
        return null;
    }
    
    private Gtk.TreeModel get_child_model(Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);
      return groupModels[iter.user_data2 as String];
    }
    
    public Type get_column_type(int index) {
      switch(index) {
      case 0:
        return typeof(Object);
      }

      assert_not_reached();
    }

    public Gtk.TreeModelFlags get_flags() {
      return 0;
    }

    public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path) {
      if(path.get_depth() == 0)
        return false;
      
      unowned int[] indices = path.get_indices();
      int index = indices[0];
      if(index < 0 || index >= groupModels.size)
        return false;

      Gee.MapIterator<String, Gtk.TreeModel> it = groupModels.map_iterator();
      it.first();
      for(int i = 0; i < index; ++i)
        it.next();

      iter.stamp = stamp;
      iter.user_data = this;
      iter.user_data2 = it.get_key();
      iter.user_data3 = (void*)(-1);

      if(path.get_depth() > 1) {
        Gtk.TreePath subpath = new Gtk.TreePath();
        for(int i = 1; i < path.get_depth(); ++i) {
          subpath.append_index(indices[i]);
        }

        Gtk.TreeIter subiter;

        if(!it.get_value().get_iter(out subiter, subpath))
          return false;

        iter.user_data3 = (void*)child_iter_index(subiter);
      }

      return true;
    }

    public int get_n_columns() {
      return 1;
    }

    public Gtk.TreePath get_path(Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);

      int index = 0;

      foreach(String group in groupModels.keys) {
        if(group == iter.user_data2)
          break;

        ++index;
      }

      Gtk.TreePath subpath = new Gtk.TreePath();

      if((long)iter.user_data3 != -1) {
        Gtk.TreeIter subiter = get_child_iter(iter);
        subpath = get_child_model(iter).get_path(subiter);
      }
      subpath.prepend_index(index);
      return subpath;
    }

    public void get_value(Gtk.TreeIter iter, int column, out Value value) {
      assert (column == 0 && iter.stamp == stamp && iter.user_data == this);
      
      if((long)iter.user_data3 == -1) {
        value = Value(typeof(String));
        value.take_object(iter.user_data2 as String);
      }
      else {
        get_child_model(iter).get_value(get_child_iter(iter), 0, out value);
      }
    }

    public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent) {
      if (parent == null) {
        return get_iter(out iter, new Gtk.TreePath.from_indices(0, -1));
      }

      assert(parent.stamp == stamp && parent.user_data == this);
      
      Gtk.TreeIter childIter;
      if(!get_child_model(parent).iter_children(out childIter, get_child_iter(parent)))
        return false;
      
      iter.stamp = stamp;
      iter.user_data = this;
      iter.user_data2 = parent.user_data2;
      iter.user_data3 = (void*)child_iter_index(childIter);
      
      return true;
    }

    public bool iter_has_child(Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);

      return (iter_n_children(iter) > 0);
    }

    public int iter_n_children(Gtk.TreeIter? iter) {
      if (iter == null)
        return groupModels.size;

      assert(iter.stamp == stamp && iter.user_data == this);

      return get_child_model(iter).iter_n_children(get_child_iter(iter));
    }

    public bool iter_next(ref Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);

      if((long)iter.user_data3 == -1) {
        bool next = false;
        foreach(String group in groupModels.keys) {
          if(next) {
            iter.user_data2 = group;
            return true;
          }

          if(group == iter.user_data2)
            next = true;
        }

        iter.stamp = -1;
        return false;
      }

      else {
        Gtk.TreeModel m = get_child_model(iter);
        Gtk.TreeIter subiter = get_child_iter(iter);
        if(!m.iter_next(ref subiter)) {
          iter.stamp = -1;
          return false;
        }

        iter.user_data3 = (void*)child_iter_index(subiter);
        return true;
      }
    }

    public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n) {
      if (parent == null) {
        return get_iter(out iter, new Gtk.TreePath.from_indices(n, -1));
      }
      else {
        Gtk.TreePath path = get_path(parent);
        path.append_index(n);
        return get_iter(out iter, path);
      }
    }

    public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child) {
      assert(child.stamp == stamp && child.user_data == this);
      
      if((long)child.user_data3 == -1) {
          iter.stamp = -1;
          return false;
      }
      
      iter.stamp = stamp;
      iter.user_data = this;
      iter.user_data2 = child.user_data2;

      Gtk.TreeIter childParent;
      if(get_child_model(child).iter_parent(out childParent, get_child_iter(child))) {
        iter.user_data3 = (void*)child_iter_index(childParent);
      }
      else {
        iter.user_data3 = (void*)(-1);
      }
      
      return true;
    }

    public void ref_node(Gtk.TreeIter iter) {}
    public void unref_node(Gtk.TreeIter iter) {}
  }

  private class ContactListModel : Object, Gtk.TreeModel {
    private unowned Roster roster;
    private int stamp = 0;
    private Gee.TreeMap<string, Contact> entries = new Gee.TreeMap<string, Contact>();

    
    public ContactListModel(Roster roster0) {
      roster = roster0;
    }
    
    public void update_contact(Contact c) {
      ++stamp;
      bool added = true;
      if(c.jid in entries.keys) {
        added = false;

        foreach(string group in entries[c.jid].get_groups()) {
          roster.groups[group].unset(c.jid);
        }
      }

      entries[c.jid] = c;

      Gtk.TreeIter iter = Gtk.TreeIter();
      iter.stamp = stamp;
      iter.user_data = this;
      iter.user_data2 = c;
      iter.user_data3 = null;

      Gtk.TreePath path = get_path(iter);

      if(added)
        row_inserted(path, iter);
      else
        row_changed(path, iter);

      foreach(string group in c.get_groups()) {
        if(!(group in roster.groups.keys)) {
          roster.groups[group] = new Gee.HashMap<string, Contact>();
        }
        
        roster.groups[group][c.jid] = c;
      }

      roster.update_groups();
    }

    public Contact get_contact(string jid) {
      string bareJID = jid.split("/", 2)[0];

      return entries[bareJID];
    }

    public Type get_column_type(int index) {
      switch(index) {
      case 0:
        return typeof(Contact);
      }

      assert_not_reached();
    }

    public Gtk.TreeModelFlags get_flags() {
      return Gtk.TreeModelFlags.LIST_ONLY;
    }

    public bool get_iter(out Gtk.TreeIter iter, Gtk.TreePath path) {
      if(path.get_depth() != 1)
        return false;

      int index = path.get_indices()[0];
      if(index < 0 || index >= entries.size)
        return false;

      Gee.MapIterator<string, Contact> it = entries.map_iterator();
      it.first();
      for(int i = 0; i < index; ++i)
        it.next();

      iter.stamp = stamp;
      iter.user_data = this;
      iter.user_data2 = it.get_value();
      iter.user_data3 = null;

      return true;
    }

    public int get_n_columns() {
      return 1;
    }

    public Gtk.TreePath get_path(Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);

      int index = 0;

      foreach(Contact c in entries.values) {
        if(c == iter.user_data2)
          break;

        ++index;
      }

      return new Gtk.TreePath.from_indices(index, -1);
    }

    public void get_value(Gtk.TreeIter iter, int column, out Value value) {
      assert (column == 0 && iter.stamp == stamp && iter.user_data == this);

      Contact c = iter.user_data2 as Contact;
      value = Value(typeof(Contact));
      value.take_object(c);
    }

    public bool iter_children(out Gtk.TreeIter iter, Gtk.TreeIter? parent) {
      if (parent != null) {
        iter.stamp = -1;
        return false;
      }

      return get_iter(out iter, new Gtk.TreePath.from_indices(0, -1));
    }

    public bool iter_has_child(Gtk.TreeIter iter) {
      return false;
    }

    public int iter_n_children(Gtk.TreeIter? iter) {
      if (iter == null)
        return entries.size;
      else
        return 0;
    }

    public bool iter_next(ref Gtk.TreeIter iter) {
      assert(iter.stamp == stamp && iter.user_data == this);

      bool next = false;
      foreach(Contact c in entries.values) {
        if(next) {
          iter.user_data2 = c;
          return true;
        }

        if(c == iter.user_data2)
          next = true;
      }

      iter.stamp = -1;
      return false;
    }

    public bool iter_nth_child(out Gtk.TreeIter iter, Gtk.TreeIter? parent, int n) {
      if (parent != null) {
        iter.stamp = -1;
        return false;
      }

      return get_iter(out iter, new Gtk.TreePath.from_indices(n, -1));
    }

    public bool iter_parent(out Gtk.TreeIter iter, Gtk.TreeIter child) {
      iter.stamp = -1;
      return false;
    }

    public void ref_node(Gtk.TreeIter iter) {}
    public void unref_node(Gtk.TreeIter iter) {}
  }

  private class GroupFilter : Gtk.TreeModelFilter {
    private string group;

    public GroupFilter(Gtk.TreeModel childModel, string group0) {
      Object(child_model: childModel);

      group = group0;

      set_visible_func((model, iter) => {
          Value value;
          model.get_value(iter, 0, out value);

          Contact c = value.get_object() as Contact;

          if(c == null)
            return true;
          
          return (group in c.get_groups());
        });
    }
  }
  
  private class RosterFilter : Gtk.TreeModelFilter {
    private unowned Roster roster;
    
    private bool show_contact(Contact c) {
      return (c.get_resource_with_highest_priority() != null);
    }
    
    public RosterFilter(Roster roster0, Gtk.TreeModel childModel) {
      Object(child_model: childModel);
      roster = roster0;

      set_visible_func((model, iter) => {
          Value value;
          model.get_value(iter, 0, out value);

          Contact c = value.get_object() as Contact;

          if(c != null) {
            return show_contact(c);
          }
          else {
            assert(value.get_object() is String);
            string group = (value.get_object() as String).data;
            
            foreach(Gee.Map.Entry<string, Contact> contact in roster.get_group(group)) {
              if(show_contact(contact.value))
                return true;
            }
            
            return false;
          }
        });
    }
  }
  
  private class RosterSorter : Gtk.TreeModelSort {
    public RosterSorter(Gtk.TreeModel childModel) {
      Object(model: childModel);
      
      set_sort_column_id(0, Gtk.SortType.ASCENDING);
      set_sort_func(0, (model, itera, iterb) => {
          Value value;
          model.get_value(itera, 0, out value);
          Object a = value.get_object();
          model.get_value(iterb, 0, out value);
          Object b = value.get_object();
          
          if(a is String && b is String) {
            return (a as String).data.collate((b as String).data);
          }
          else if(a is Contact && b is Contact) {
            return (a as Contact).display_string.collate((b as Contact).display_string);
          }
          
          assert_not_reached();
        });
    }
  }
}