aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/add_conversation/accounts_combo_box.vala
blob: 5fdd18e6fd93ecba77438a6f91b492148fbf56ab (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
using Gee;
using Gtk;

using Dino.Entities;

namespace Dino.Ui {

class AccountComboBox : ComboBox {

    public Account? selected {
        get {
            TreeIter selected;
            if (get_active_iter(out selected)) {
                Value value;
                list_store.get_value(selected, 1, out value);
                return value as Account;
            }
            return null;
        }
        set {
            TreeIter iter;
            if (list_store.get_iter_first(out iter)) {
                int i = 0;
                do {
                    Value val;
                    list_store.get_value(iter, 1, out val);
                    if ((val as Account).equals(value)) {
                        active = i;
                        break;
                    }
                    i++;
                } while (list_store.iter_next(ref iter));
            }
        }
    }

    private StreamInteractor? stream_interactor;
    private Gtk.ListStore list_store = new Gtk.ListStore(2, typeof(string), typeof(Account));

    public void initialize(StreamInteractor stream_interactor) {
        this.stream_interactor = stream_interactor;

        CellRendererText renderer = new Gtk.CellRendererText();
        pack_start(renderer, true);
        add_attribute(renderer, "text", 0);

        TreeIter iter;
        foreach (Account account in stream_interactor.get_accounts()) {
            list_store.append(out iter);
            list_store.set(iter, 0, account.bare_jid.to_string(), 1, account);
        }
        set_model(list_store);
        active = 0;
    }
}

}