dino/main/src/ui/add_conversation/conference_list.vala

120 lines
4.3 KiB
Vala
Raw Normal View History

2017-03-02 14:37:32 +00:00
using Gee;
using Gtk;
using Xmpp;
using Xmpp.Xep.Bookmarks;
2017-03-02 14:37:32 +00:00
using Dino.Entities;
namespace Dino.Ui {
2017-03-10 17:07:28 +00:00
2022-02-14 13:55:59 +00:00
protected class ConferenceList {
2017-03-02 14:37:32 +00:00
public signal void conversation_selected(Conversation? conversation);
private StreamInteractor stream_interactor;
2022-02-14 13:55:59 +00:00
private ListBox list_box = new ListBox();
private HashMap<Account, Set<Conference>> lists = new HashMap<Account, Set<Conference>>(Account.hash_func, Account.equals_func);
private HashMap<Account, HashMap<Jid, ListBoxRow>> widgets = new HashMap<Account, HashMap<Jid, ListBoxRow>>(Account.hash_func, Account.equals_func);
ulong bookmarks_updated_handler_id = -1;
2017-03-02 14:37:32 +00:00
public ConferenceList(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor;
bookmarks_updated_handler_id = stream_interactor.get_module(MucManager.IDENTITY).bookmarks_updated.connect((account, conferences) => {
2017-11-11 20:29:13 +00:00
lists[account] = conferences;
refresh_conferences();
2017-03-02 14:37:32 +00:00
});
foreach (Account account in stream_interactor.get_accounts()) {
stream_interactor.get_module(MucManager.IDENTITY).get_bookmarks.begin(account, (_, res) => {
Set<Conference>? conferences = stream_interactor.get_module(MucManager.IDENTITY).get_bookmarks.end(res);
set_bookmarks(account, conferences);
});
}
stream_interactor.get_module(MucManager.IDENTITY).conference_added.connect(add_conference);
stream_interactor.get_module(MucManager.IDENTITY).conference_removed.connect(remove_conference);
}
~ConferenceList() {
stream_interactor.get_module(MucManager.IDENTITY).disconnect(bookmarks_updated_handler_id);
stream_interactor.get_module(MucManager.IDENTITY).conference_added.disconnect(add_conference);
stream_interactor.get_module(MucManager.IDENTITY).conference_removed.disconnect(remove_conference);
}
private void add_conference(Account account, Conference conference) {
if (!widgets.has_key(account)) {
widgets[account] = new HashMap<Jid, ListBoxRow>(Jid.hash_func, Jid.equals_func);
}
var widget = new ConferenceListRow(stream_interactor, conference, account);
var list_box_row = new ListBoxRow();
list_box_row.set_child(widget);
widgets[account][conference.jid] = list_box_row;
list_box.append(list_box_row);
}
private void remove_conference(Account account, Jid jid) {
if (widgets.has_key(account) && widgets[account].has_key(jid)) {
2022-02-14 13:55:59 +00:00
list_box.remove(widgets[account][jid]);
widgets[account].unset(jid);
2017-03-02 14:37:32 +00:00
}
}
public void refresh_conferences() {
2022-05-14 12:45:59 +00:00
foreach (Account account in widgets.keys) {
var account_widgets_cpy = new HashMap<Jid, ListBoxRow>();
account_widgets_cpy.set_all(widgets[account]);
foreach (Jid jid in account_widgets_cpy.keys) {
2022-07-08 14:33:40 +00:00
list_box.remove(widgets[account][jid]);
2022-05-14 12:45:59 +00:00
}
}
2017-03-02 14:37:32 +00:00
foreach (Account account in lists.keys) {
foreach (Conference conference in lists[account]) {
add_conference(account, conference);
2017-03-02 14:37:32 +00:00
}
}
}
private void set_bookmarks(Account account, Set<Conference>? conferences) {
if (conferences == null) {
lists.unset(account);
} else {
lists[account] = conferences;
}
2017-11-11 20:29:13 +00:00
refresh_conferences();
2017-03-02 14:37:32 +00:00
}
2022-02-14 13:55:59 +00:00
public ListBox get_list_box() {
return list_box;
2017-03-02 14:37:32 +00:00
}
}
internal class ConferenceListRow : ListRow {
public Conference bookmark;
2017-03-02 14:37:32 +00:00
public ConferenceListRow(StreamInteractor stream_interactor, Conference bookmark, Account account) {
this.jid = bookmark.jid;
2017-03-02 14:37:32 +00:00
this.account = account;
this.bookmark = bookmark;
name_label.label = bookmark.name != null && bookmark.name != "" ? bookmark.name : bookmark.jid.to_string();
if (stream_interactor.get_accounts().size > 1) {
via_label.label = "via " + account.bare_jid.to_string();
} else if (bookmark.name != null && bookmark.name != bookmark.jid.to_string()) {
via_label.label = bookmark.jid.to_string();
2017-03-02 14:37:32 +00:00
} else {
via_label.visible = false;
}
image.set_conversation(stream_interactor, new Conversation(jid, account, Conversation.Type.GROUPCHAT));
2017-03-02 14:37:32 +00:00
}
}
2017-03-10 17:07:28 +00:00
}