aboutsummaryrefslogtreecommitdiff
path: root/client/src/ui/manage_accounts/dialog.vala
blob: d3695019b63830752c06c3c47c07fd38fee245cd (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
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
using Gdk;
using Gtk;

using Dino.Entities;

namespace Dino.Ui.ManageAccounts {

[GtkTemplate (ui = "/org/dino-im/manage_accounts/dialog.ui")]
public class Dialog : Gtk.Window {

    public signal void account_enabled(Account account);
    public signal void account_disabled(Account account);

    [GtkChild]
    public Stack main_stack;

    [GtkChild]
    public ListBox account_list;

    [GtkChild]
    public Button no_accounts_add;

    [GtkChild]
    public ToolButton add_button;

    [GtkChild]
    public ToolButton remove_button;

    [GtkChild]
    public Image image;

    [GtkChild] Button image_button;

    [GtkChild]
    public Label jid_label;

    [GtkChild]
    public Switch active_switch;

    [GtkChild]
    public Stack password_stack;

    [GtkChild]
    public Label password_label;

    [GtkChild]
    public Button password_button;

    [GtkChild]
    public Entry password_entry;

    [GtkChild]
    public Stack alias_stack;

    [GtkChild]
    public Label alias_label;

    [GtkChild]
    public Button alias_button;

    [GtkChild]
    public Entry alias_entry;

    private Database db;
    private StreamInteractor stream_interactor;

    construct {
        account_list.row_selected.connect(account_list_row_selected);
        add_button.clicked.connect(add_button_clicked);
        no_accounts_add.clicked.connect(add_button_clicked);
        remove_button.clicked.connect(remove_button_clicked);
        password_entry.key_press_event.connect(on_password_entry_key_press_event);
        alias_entry.key_press_event.connect(on_alias_entry_key_press_event);
        image_button.clicked.connect(on_image_button_clicked);

        main_stack.set_visible_child_name("no_accounts");
    }

    public Dialog(StreamInteractor stream_interactor, Database db) {
        this.db = db;
        this.stream_interactor = stream_interactor;
        foreach (Account account in db.get_accounts()) {
            add_account(account);
        }

        AvatarManager.get_instance(stream_interactor).received_avatar.connect((pixbuf, jid, account) => {
        Idle.add(() => {
            on_received_avatar(pixbuf, jid, account);
            return false;
        });});

        if (account_list.get_row_at_index(0) != null) account_list.select_row(account_list.get_row_at_index(0));
    }

    public AccountRow add_account(Account account) {
        AccountRow account_item = new AccountRow (stream_interactor, account);
        account_list.add(account_item);
        main_stack.set_visible_child_name("accounts_exist");
        return account_item;
    }

    private void add_button_clicked() {
        AddAccountDialog add_account_dialog = new AddAccountDialog(stream_interactor);
        add_account_dialog.set_transient_for(this);
        add_account_dialog.added.connect((account) => {
            db.add_account(account);
            AccountRow account_item = add_account(account);
            account_list.select_row(account_item);
            account_list.queue_draw();
        });
        add_account_dialog.show();
    }

    private void remove_button_clicked() {
        AccountRow account_item = account_list.get_selected_row() as AccountRow;
        if (account_item != null) {
            account_list.remove(account_item);
            account_list.queue_draw();
            if (account_item.account.enabled) account_disabled(account_item.account);
            db.remove_account(account_item.account);
            if (account_list.get_row_at_index(0) != null) {
                account_list.select_row(account_list.get_row_at_index(0));
            } else {
                main_stack.set_visible_child_name("no_accounts");
            }
        }
    }

    private void account_list_row_selected(ListBoxRow? row) {
        AccountRow? account_item = row as AccountRow;
        if (account_item != null) populate_grid_data(account_item.account);
    }

    private void populate_grid_data(Account account) {
        active_switch.state_set.disconnect(on_active_switch_state_changed);

        Util.image_set_from_scaled_pixbuf(image, (new AvatarGenerator(50, 50, image.scale_factor)).draw_account(stream_interactor, account));
        active_switch.set_active(account.enabled);
        jid_label.label = account.bare_jid.to_string();

        string filler = "";
        for (int i = 0; i < account.password.length; i++) filler += password_entry.get_invisible_char().to_string();
        password_label.label = filler;
        password_stack.set_visible_child_name("label");
        password_button.clicked.connect(() => {
            password_stack.set_visible_child_name("entry");
            alias_stack.set_visible_child_name("label");
            set_focus(password_entry);
        });
        password_entry.text = account.password;

        alias_label.label = account.alias;
        alias_stack.set_visible_child_name("label");
        alias_button.clicked.connect(() => {
            alias_stack.set_visible_child_name("entry");
            password_stack.set_visible_child_name("label");
            set_focus(alias_entry);
        });
        alias_entry.text = account.alias;

        active_switch.state_set.connect(on_active_switch_state_changed);
    }

    private void on_image_button_clicked() {
        FileChooserDialog chooser = new FileChooserDialog (
				"Select avatar", this, FileChooserAction.OPEN,
				"Cancel", ResponseType.CANCEL,
				"Select", ResponseType.ACCEPT);
        FileFilter filter = new FileFilter();
        filter.add_mime_type("image/*");
        chooser.set_filter(filter);
        if (chooser.run() == Gtk.ResponseType.ACCEPT) {
            string uri = chooser.get_filename();
            Account account = (account_list.get_selected_row() as AccountRow).account;
            AvatarManager.get_instance(stream_interactor).publish(account, uri);
        }
        chooser.close();
    }

    private bool on_active_switch_state_changed(bool state) {
        Account account = (account_list.get_selected_row() as AccountRow).account;
        account.enabled = state;
        if (state) {
            account_enabled(account);
        } else {
            account_disabled(account);
        }
        return false;
    }

    private bool on_password_entry_key_press_event(EventKey event) {
        Account account = (account_list.get_selected_row() as AccountRow).account;
        if (event.keyval == Key.Return) {
            account.password = password_entry.text;
            string filler = "";
            for (int i = 0; i < account.password.length; i++) filler += password_entry.get_invisible_char().to_string();
            password_label.label = filler;
            password_stack.set_visible_child_name("label");
        }
        return false;
    }

    private bool on_alias_entry_key_press_event(EventKey event) {
        Account account = (account_list.get_selected_row() as AccountRow).account;
        if (event.keyval == Key.Return) {
            account.alias = alias_entry.text;
            alias_label.label = alias_entry.text;
            alias_stack.set_visible_child_name("label");
        }
        return false;
    }

    private void on_received_avatar(Pixbuf pixbuf, Jid jid, Account account) {
        Account curr_account = (account_list.get_selected_row() as AccountRow).account;
        if (curr_account.equals(account) && jid.equals(account.bare_jid)) {
            Util.image_set_from_scaled_pixbuf(image, (new AvatarGenerator(50, 50, image.scale_factor)).draw_account(stream_interactor, account));
        }
    }
}
}