anotherim-desktop/main/src/ui/add_conversation/select_jid_fragment.vala

112 lines
3.7 KiB
Vala
Raw Normal View History

2017-03-02 14:37:32 +00:00
using Gee;
using Gtk;
using Dino.Entities;
namespace Dino.Ui {
2017-03-02 14:37:32 +00:00
2017-08-17 19:24:01 +00:00
[GtkTemplate (ui = "/im/dino/add_conversation/select_jid_fragment.ui")]
2017-03-02 14:37:32 +00:00
public class SelectJidFragment : Gtk.Box {
public signal void add_jid();
public signal void remove_jid(ListRow row);
public bool done {
2017-08-25 19:20:09 +00:00
get { return filterable_list.get_selected_row() != null; }
private set {}
}
2017-03-02 14:37:32 +00:00
2017-03-10 17:07:28 +00:00
[GtkChild] private Entry entry;
[GtkChild] private Box box;
[GtkChild] private Button add_button;
[GtkChild] private Button remove_button;
2017-03-02 14:37:32 +00:00
2017-06-11 11:59:24 +00:00
private StreamInteractor stream_interactor;
2017-03-02 14:37:32 +00:00
private FilterableList filterable_list;
2017-06-11 11:59:24 +00:00
private Gee.List<Account> accounts;
2017-03-02 14:37:32 +00:00
private ArrayList<AddListRow> added_rows = new ArrayList<AddListRow>();
2017-06-11 11:59:24 +00:00
public SelectJidFragment(StreamInteractor stream_interactor, FilterableList filterable_list, Gee.List<Account> accounts) {
2017-03-02 14:37:32 +00:00
this.stream_interactor = stream_interactor;
this.filterable_list = filterable_list;
2017-06-11 11:59:24 +00:00
this.accounts = accounts;
2017-03-02 14:37:32 +00:00
filterable_list.visible = true;
filterable_list.activate_on_single_click = false;
filterable_list.vexpand = true;
box.add(filterable_list);
filterable_list.set_sort_func(sort);
filterable_list.row_selected.connect(check_buttons_active);
filterable_list.row_selected.connect(() => { done = true; }); // just for notifying
2017-08-25 19:20:09 +00:00
entry.changed.connect(() => { set_filter(entry.text); });
2017-03-02 14:37:32 +00:00
add_button.clicked.connect(() => { add_jid(); });
remove_button.clicked.connect(() => { remove_jid(filterable_list.get_selected_row() as ListRow); });
}
2017-08-25 19:20:09 +00:00
public void set_filter(string str) {
if (entry.text != str) entry.text = str;
foreach (AddListRow row in added_rows) filterable_list.remove(row);
2017-03-02 14:37:32 +00:00
added_rows.clear();
2017-08-25 19:20:09 +00:00
string[] ? values = str == "" ? null : str.split(" ");
2017-03-02 14:37:32 +00:00
filterable_list.set_filter_values(values);
Jid? parsed_jid = Jid.parse(str);
if (parsed_jid != null && parsed_jid.localpart != null) {
2017-06-11 11:59:24 +00:00
foreach (Account account in accounts) {
2017-03-02 14:37:32 +00:00
AddListRow row = new AddListRow(stream_interactor, str, account);
filterable_list.add(row);
added_rows.add(row);
}
}
}
private void check_buttons_active() {
ListBoxRow? row = filterable_list.get_selected_row();
bool active = row != null && !row.get_type().is_a(typeof(AddListRow));
remove_button.sensitive = active;
}
private int sort(ListBoxRow row1, ListBoxRow row2) {
AddListRow al1 = (row1 as AddListRow);
AddListRow al2 = (row2 as AddListRow);
if (al1 != null && al2 == null) {
return -1;
} else if (al2 != null && al1 == null) {
return 1;
}
return filterable_list.sort(row1, row2);
}
private class AddListRow : ListRow {
public AddListRow(StreamInteractor stream_interactor, string jid, Account account) {
this.account = account;
this.jid = new Jid(jid);
name_label.label = jid;
if (stream_interactor.get_accounts().size > 1) {
via_label.label = account.bare_jid.to_string();
} else {
via_label.visible = false;
}
image.set_from_pixbuf((new AvatarGenerator(35, 35)).set_greyscale(true).draw_text("?"));
}
}
}
public abstract class FilterableList : Gtk.ListBox {
public string[]? filter_values;
public void set_filter_values(string[] values) {
if (filter_values == values) return;
filter_values = values;
invalidate_filter();
}
public abstract int sort(ListBoxRow row1, ListBoxRow row2);
}
}