blob: bc349b6c55e0e63361483579f411e4bccd0b7dd2 (
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
|
using Gee;
using Gtk;
using Dino.Entities;
using Xmpp;
namespace Dino.Ui {
[GtkTemplate (ui = "/im/dino/Dino/add_conversation/select_jid_fragment.ui")]
public class SelectJidFragment : Gtk.Box {
public signal void add_jid();
public signal void remove_jid(ListRow row);
public bool done {
get { return filterable_list.get_selected_row() != null; }
private set {}
}
[GtkChild] private unowned Entry entry;
[GtkChild] private unowned Box box;
[GtkChild] private unowned Button add_button;
[GtkChild] private unowned Button remove_button;
private StreamInteractor stream_interactor;
private FilterableList filterable_list;
private Gee.List<Account> accounts;
private ArrayList<AddListRow> added_rows = new ArrayList<AddListRow>();
public SelectJidFragment(StreamInteractor stream_interactor, FilterableList filterable_list, Gee.List<Account> accounts) {
this.stream_interactor = stream_interactor;
this.filterable_list = filterable_list;
this.accounts = accounts;
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
entry.changed.connect(() => { set_filter(entry.text); });
add_button.clicked.connect(() => { add_jid(); });
remove_button.clicked.connect(() => { remove_jid(filterable_list.get_selected_row() as ListRow); });
}
public void set_filter(string str) {
if (entry.text != str) entry.text = str;
foreach (AddListRow row in added_rows) filterable_list.remove(row);
added_rows.clear();
string[] ? values = str == "" ? null : str.split(" ");
filterable_list.set_filter_values(values);
try {
Jid parsed_jid = new Jid(str);
if (parsed_jid != null && parsed_jid.localpart != null) {
foreach (Account account in accounts) {
AddListRow row = new AddListRow(stream_interactor, parsed_jid, account);
filterable_list.add(row);
added_rows.add(row);
}
}
} catch (InvalidJidError ignored) {
// Ignore
}
}
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, Jid jid, Account account) {
this.account = account;
this.jid = jid;
name_label.label = jid.to_string();
if (stream_interactor.get_accounts().size > 1) {
via_label.label = account.bare_jid.to_string();
} else {
via_label.visible = false;
}
image.set_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);
}
}
|