2017-03-02 14:37:32 +00:00
|
|
|
using Gdk;
|
2017-03-11 21:48:35 +00:00
|
|
|
using Gee;
|
2017-03-02 14:37:32 +00:00
|
|
|
using Gtk;
|
2017-03-11 11:13:06 +00:00
|
|
|
using Markup;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
[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] public Button image_button;
|
|
|
|
[GtkChild] public Label jid_label;
|
|
|
|
[GtkChild] public Switch active_switch;
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
[GtkChild] public Stack password_stack;
|
|
|
|
[GtkChild] public Label password_label;
|
|
|
|
[GtkChild] public Button password_button;
|
|
|
|
[GtkChild] public Entry password_entry;
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
[GtkChild] public Stack alias_stack;
|
|
|
|
[GtkChild] public Label alias_label;
|
|
|
|
[GtkChild] public Button alias_button;
|
|
|
|
[GtkChild] public Entry alias_entry;
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-03-11 21:48:35 +00:00
|
|
|
[GtkChild] public Grid settings_list;
|
2017-03-02 14:37:32 +00:00
|
|
|
|
2017-03-11 21:48:35 +00:00
|
|
|
private ArrayList<Plugins.AccountSettingsWidget> plugin_widgets = new ArrayList<Plugins.AccountSettingsWidget>();
|
2017-03-02 14:37:32 +00:00
|
|
|
|
|
|
|
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);
|
2017-03-09 13:27:39 +00:00
|
|
|
password_entry.key_release_event.connect(on_password_key_release_event);
|
|
|
|
alias_entry.key_release_event.connect(on_alias_key_release_event);
|
2017-03-02 14:37:32 +00:00
|
|
|
image_button.clicked.connect(on_image_button_clicked);
|
|
|
|
|
|
|
|
main_stack.set_visible_child_name("no_accounts");
|
2017-03-11 21:48:35 +00:00
|
|
|
|
|
|
|
int row_index = 4;
|
|
|
|
int16 default_top_padding = new Gtk.Button().get_style_context().get_padding(Gtk.StateFlags.NORMAL).top + 1;
|
|
|
|
Application app = GLib.Application.get_default() as Application;
|
|
|
|
foreach (var e in app.plugin_registry.account_settings_entries) {
|
|
|
|
Plugins.AccountSettingsWidget widget = e.get_widget();
|
|
|
|
plugin_widgets.add(widget);
|
|
|
|
widget.visible = true;
|
|
|
|
widget.activated.connect(child_activated);
|
|
|
|
Label label = new Label(e.name);
|
|
|
|
label.get_style_context().add_class("dim-label");
|
|
|
|
label.set_padding(0, e.label_top_padding == -1 ? default_top_padding : e.label_top_padding);
|
|
|
|
label.yalign = 0;
|
|
|
|
label.xalign = 1;
|
|
|
|
label.visible = true;
|
|
|
|
settings_list.attach(label, 0, row_index);
|
|
|
|
settings_list.attach(widget, 1, row_index, 2);
|
|
|
|
row_index++;
|
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(AvatarManager.IDENTITY).received_avatar.connect((pixbuf, jid, account) => {
|
2017-03-02 14:37:32 +00:00
|
|
|
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) => {
|
2017-03-22 16:15:06 +00:00
|
|
|
account.persist(db);
|
2017-03-02 14:37:32 +00:00
|
|
|
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);
|
2017-03-22 16:15:06 +00:00
|
|
|
account_item.account.remove();
|
2017-03-02 14:37:32 +00:00
|
|
|
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_entry.text = account.password;
|
|
|
|
|
|
|
|
alias_stack.set_visible_child_name("label");
|
2017-03-10 15:16:48 +00:00
|
|
|
alias_label.label = account.alias;
|
2017-03-02 14:37:32 +00:00
|
|
|
alias_entry.text = account.alias;
|
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
password_button.clicked.connect(() => { set_active_stack(password_stack); });
|
|
|
|
alias_button.clicked.connect(() => { set_active_stack(alias_stack); });
|
2017-03-02 14:37:32 +00:00
|
|
|
active_switch.state_set.connect(on_active_switch_state_changed);
|
2017-03-11 11:13:06 +00:00
|
|
|
|
2017-03-11 21:48:35 +00:00
|
|
|
foreach(Plugins.AccountSettingsWidget widget in plugin_widgets) {
|
|
|
|
widget.set_account(account);
|
2017-03-11 11:13:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 21:48:35 +00:00
|
|
|
child_activated(null);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 11:13:06 +00:00
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
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;
|
2017-03-19 11:55:36 +00:00
|
|
|
stream_interactor.get_module(AvatarManager.IDENTITY).publish(account, uri);
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-09 13:27:39 +00:00
|
|
|
private bool on_password_key_release_event(EventKey event) {
|
2017-03-02 14:37:32 +00:00
|
|
|
Account account = (account_list.get_selected_row() as AccountRow).account;
|
2017-03-09 13:27:39 +00:00
|
|
|
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;
|
2017-03-02 14:37:32 +00:00
|
|
|
if (event.keyval == Key.Return) {
|
|
|
|
password_stack.set_visible_child_name("label");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-09 13:27:39 +00:00
|
|
|
private bool on_alias_key_release_event(EventKey event) {
|
2017-03-02 14:37:32 +00:00
|
|
|
Account account = (account_list.get_selected_row() as AccountRow).account;
|
2017-03-09 13:27:39 +00:00
|
|
|
account.alias = alias_entry.text;
|
|
|
|
alias_label.label = alias_entry.text;
|
2017-03-02 14:37:32 +00:00
|
|
|
if (event.keyval == Key.Return) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 15:16:48 +00:00
|
|
|
|
2017-03-11 21:48:35 +00:00
|
|
|
private void child_activated(Gtk.Widget? widget) {
|
|
|
|
if (widget != password_stack) password_stack.set_visible_child_name("label");
|
|
|
|
if (widget != alias_stack) alias_stack.set_visible_child_name("label");
|
|
|
|
|
|
|
|
foreach(var w in plugin_widgets) {
|
|
|
|
if (widget != (Gtk.Widget)w) w.deactivate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 15:16:48 +00:00
|
|
|
private void set_active_stack(Stack stack) {
|
|
|
|
stack.set_visible_child_name("entry");
|
2017-03-11 21:48:35 +00:00
|
|
|
child_activated(stack);
|
2017-03-10 15:16:48 +00:00
|
|
|
}
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
2017-03-10 17:07:28 +00:00
|
|
|
|
2017-03-02 14:37:32 +00:00
|
|
|
}
|
|
|
|
|