aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/manage_accounts/add_account_dialog.vala
blob: f9ab794e8e65832a114340b730bfa9564cb9074f (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
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
using Gee;
using Gtk;

using Dino.Entities;
using Xmpp;

namespace Dino.Ui.ManageAccounts {

[GtkTemplate (ui = "/im/dino/Dino/manage_accounts/add_account_dialog.ui")]
public class AddAccountDialog : Gtk.Dialog {

    public signal void added(Account account);

    [GtkChild] private Stack stack;

    [GtkChild] private Revealer notification_revealer;
    [GtkChild] private Label notification_label;

    // Sign in
    [GtkChild] private Box sign_in_box;
    [GtkChild] private Entry jid_entry;
    [GtkChild] private Entry alias_entry;
    [GtkChild] private Entry password_entry;
    [GtkChild] private Button sign_in_continue;
    [GtkChild] private Button serverlist_button;

    // Select Server
    [GtkChild] private Box create_account_box;
    [GtkChild] private Button login_button;
    [GtkChild] private Stack select_server_continue_stack;
    [GtkChild] private Button select_server_continue;
    [GtkChild] private Label register_form_continue_label;
    [GtkChild] private ListBox server_list_box;
    [GtkChild] private Entry server_entry;

    // Register Form
    [GtkChild] private Box register_box;
    [GtkChild] private Label register_title;
    [GtkChild] private Box form_box;
    [GtkChild] private Button register_form_back;
    [GtkChild] private Stack register_form_continue_stack;
    [GtkChild] private Button register_form_continue;

    private static string[] server_list = new string[]{
        "5222.de",
        "jabber.fr",
        "movim.eu",
        "yax.im"
    };
    private HashMap<ListBoxRow, string> list_box_jids = new HashMap<ListBoxRow, string>();
    private Jid? server_jid = null;
    private Xep.InBandRegistration.Form? form = null;

    public AddAccountDialog(StreamInteractor stream_interactor) {
        this.title = _("Add Account");

        // Sign in
        jid_entry.changed.connect(on_jid_entry_changed);
        jid_entry.focus_out_event.connect(on_jid_entry_focus_out_event);
        sign_in_continue.clicked.connect(on_sign_in_continue_clicked);
        serverlist_button.clicked.connect(show_select_server);

        // Select Server
        server_entry.changed.connect(() => {
            Jid? jid = Jid.parse(server_entry.text);
            select_server_continue.sensitive = jid != null && jid.localpart == null && jid.resourcepart == null;
        });
        select_server_continue.clicked.connect(on_select_server_continue);
        login_button.clicked.connect(show_sign_in);

        foreach (string server in server_list) {
            ListBoxRow list_box_row = new ListBoxRow() { visible=true };
            list_box_row.add(new Label(server) { xalign=0, margin=3, margin_start=7, margin_end=7, visible=true });
            list_box_jids[list_box_row] = server;
            server_list_box.add(list_box_row);
        }

        // Register Form
        register_form_continue.clicked.connect(on_register_form_continue_clicked);
        register_form_back.clicked.connect(show_select_server);

        show_sign_in();
    }

    private void show_sign_in() {
        sign_in_box.visible = true;
        stack.visible_child_name = "login";
        create_account_box.visible = false;
        register_box.visible = false;
        set_default(sign_in_continue);
        animate_window_resize(sign_in_box);
    }

    private void show_select_server() {
        server_entry.text = "";
        server_entry.grab_focus();
        set_default(select_server_continue);

        server_list_box.row_selected.disconnect(on_server_list_row_selected);
        server_list_box.unselect_all();
        server_list_box.row_selected.connect(on_server_list_row_selected);

        create_account_box.visible = true;
        stack.visible_child_name = "server";
        sign_in_box.visible = false;
        register_box.visible = false;

        animate_window_resize(create_account_box);
    }

    private void show_register_form() {
        register_box.visible = true;
        stack.visible_child_name = "form";
        sign_in_box.visible = false;
        create_account_box.visible = false;

        set_default(register_form_continue);
        animate_window_resize(register_box);
    }

    private void on_jid_entry_changed() {
        Jid? jid = Jid.parse(jid_entry.text);
        if (jid != null && jid.localpart != null && jid.resourcepart == null) {
            sign_in_continue.set_sensitive(true);
            jid_entry.secondary_icon_name = null;
        } else {
            sign_in_continue.set_sensitive(false);
        }
    }

    private bool on_jid_entry_focus_out_event() {
        Jid? jid = Jid.parse(jid_entry.text);
        if (jid == null || jid.localpart == null || jid.resourcepart != null) {
            jid_entry.secondary_icon_name = "dialog-warning-symbolic";
            jid_entry.set_icon_tooltip_text(EntryIconPosition.SECONDARY, _("JID should be of the form “user@example.com”"));
        } else {
            jid_entry.secondary_icon_name = null;
        }
        return false;
    }

    private void on_sign_in_continue_clicked() {
        Jid jid = new Jid(jid_entry.get_text());
        string password = password_entry.get_text();
        string alias = alias_entry.get_text();
        store_account(jid, password, alias);
        close();
    }

    private void on_select_server_continue() {
        server_jid = new Jid(server_entry.text);
        request_show_register_form.begin();
    }

    private void on_server_list_row_selected(ListBox box, ListBoxRow? row) {
        server_jid = new Jid(list_box_jids[row]);
        request_show_register_form.begin();
    }

    private async void request_show_register_form() {
        select_server_continue_stack.visible_child_name = "spinner";
        form = yield Register.get_registration_form(server_jid);
        if (select_server_continue_stack == null) {
            return;
        }
        select_server_continue_stack.visible_child_name = "label";
        if (form != null) {
            set_register_form(server_jid, form);
            show_register_form();
        } else {
            display_notification(_("No response from server"));
        }
    }

    private void set_register_form(Jid server, Xep.InBandRegistration.Form form) {
        form_box.foreach((widget) => { widget.destroy(); });
        register_title.label = _("Register on %s").printf(server.to_string());

        if (form.oob != null) {
            form_box.add(new Label(_("The server requires to sign up through a website")){ use_markup=true, visible=true } );
            form_box.add(new Label(@"<a href=\"$(form.oob)\">$(form.oob)</a>") { use_markup=true, visible=true });
            register_form_continue_label.label = _("Open Registration");
            register_form_continue.visible = true;
            register_form_continue.grab_focus();
        } else if (form.fields.size > 0) {
            int i = 0;
            foreach (Xep.DataForms.DataForm.Field field in form.fields) {
                if (field.label != null && field.label != "") {
                    form_box.add(new Label(field.label) { xalign=0, margin_top=7, visible=true });
                }
                Widget field_widget = Util.get_data_form_fild_widget(field);
                if (field_widget != null) {
                    form_box.add(field_widget);
                }
                i++;
            }
            register_form_continue.visible = true;
            register_form_continue_label.label = _("Register");
        } else {
            form_box.add(new Label(_("Check %s for information on how to sign up").printf(@"<a href=\"http://$(server)\">$(server)</a>")) { use_markup=true, visible=true });
            register_form_continue.visible = false;
        }
    }

    private async void on_register_form_continue_clicked() {
        notification_revealer.set_reveal_child(false);
        // Button is opening a registration website
        if (form.oob != null) {
            try {
                AppInfo.launch_default_for_uri(form.oob, null);
            } catch (Error e) { }
            show_sign_in();
            return;
        }

        register_form_continue_stack.visible_child_name = "spinner";
        string? error = yield Register.submit_form(server_jid, form);
        if (register_form_continue_stack == null) {
            return;
        }
        register_form_continue_stack.visible_child_name = "label";
        if (error == null) {
            string? username = null, password = null;
            foreach (Xep.DataForms.DataForm.Field field in form.fields) {
                switch (field.var) {
                    case "username": username = field.get_value_string(); break;
                    case "password": password = field.get_value_string(); break;
                }
            }
            store_account(new Jid(username + "@" + server_jid.domainpart), password, "");
            close();
        } else {
            display_notification(error);
        }
    }

    private void store_account(Jid jid, string password, string? alias) {
        Account account = new Account(jid, null, password, alias);
        added(account);
    }

    private void display_notification(string text) {
        notification_label.label = text;
        notification_revealer.set_reveal_child(true);
        Timeout.add_seconds(5, () => {
            notification_revealer.set_reveal_child(false);
            return false;
        });
    }

    private void animate_window_resize(Widget widget) { // TODO code duplication
        int def_height, curr_width, curr_height;
        get_size(out curr_width, out curr_height);
        widget.get_preferred_height(null, out def_height);
        def_height += 5;
        int difference = def_height - curr_height;
        Timer timer = new Timer();
        Timeout.add((int) (stack.transition_duration / 30),
            () => {
                ulong microsec;
                timer.elapsed(out microsec);
                ulong millisec = microsec / 1000;
                double partial = double.min(1, (double) millisec / stack.transition_duration);
                resize(curr_width, (int) (curr_height + difference * partial));
                return millisec < stack.transition_duration;
            });
    }
}

}