diff options
author | Marvin W <git@larma.de> | 2017-03-11 23:52:12 +0100 |
---|---|---|
committer | Marvin W <git@larma.de> | 2017-03-11 23:58:35 +0100 |
commit | e27d63269d0b41fa8d5b5f0f2e4a9dc7de4b9ab9 (patch) | |
tree | 80e547d0e02e770e8bcd912e42f8afa753aeb0f6 /main/src/ui/conversation_selector | |
parent | cd6b904e970291a63551d0f1d80bbd550e778ec8 (diff) | |
download | dino-e27d63269d0b41fa8d5b5f0f2e4a9dc7de4b9ab9.tar.gz dino-e27d63269d0b41fa8d5b5f0f2e4a9dc7de4b9ab9.zip |
Move UI code into main dir
Diffstat (limited to 'main/src/ui/conversation_selector')
-rw-r--r-- | main/src/ui/conversation_selector/chat_row.vala | 90 | ||||
-rw-r--r-- | main/src/ui/conversation_selector/conversation_row.vala | 160 | ||||
-rw-r--r-- | main/src/ui/conversation_selector/groupchat_row.vala | 35 | ||||
-rw-r--r-- | main/src/ui/conversation_selector/list.vala | 175 | ||||
-rw-r--r-- | main/src/ui/conversation_selector/view.vala | 52 |
5 files changed, 512 insertions, 0 deletions
diff --git a/main/src/ui/conversation_selector/chat_row.vala b/main/src/ui/conversation_selector/chat_row.vala new file mode 100644 index 00000000..8b36b333 --- /dev/null +++ b/main/src/ui/conversation_selector/chat_row.vala @@ -0,0 +1,90 @@ +using Gdk; +using Gee; +using Gtk; + +using Xmpp; +using Dino.Entities; + +namespace Dino.Ui.ConversationSelector { + +public class ChatRow : ConversationRow { + + public ChatRow(StreamInteractor stream_interactor, Conversation conversation) { + base(stream_interactor, conversation); + has_tooltip = true; + query_tooltip.connect ((x, y, keyboard_tooltip, tooltip) => { + tooltip.set_custom(generate_tooltip()); + return true; + }); + update_avatar(); + } + + public override void on_show_received(Show show) { + update_avatar(); + } + + public override void network_connection(bool connected) { + if (!connected) { + set_avatar((new AvatarGenerator(AVATAR_SIZE, AVATAR_SIZE, image.scale_factor)).set_greyscale(true).draw_conversation(stream_interactor, conversation), image.scale_factor); + } else { + update_avatar(); + } + } + + public void on_updated_roster_item(Roster.Item roster_item) { + if (roster_item.name != null) { + display_name = roster_item.name; + update_name(); + } + update_avatar(); + } + + public void update_avatar() { + ArrayList<Jid> full_jids = PresenceManager.get_instance(stream_interactor).get_full_jids(conversation.counterpart, conversation.account); + set_avatar((new AvatarGenerator(AVATAR_SIZE, AVATAR_SIZE, image.scale_factor)) + .set_greyscale(full_jids == null) + .draw_conversation(stream_interactor, conversation), image.scale_factor); + } + + private Widget generate_tooltip() { + Builder builder = new Builder.from_resource("/org/dino-im/conversation_selector/chat_row_tooltip.ui"); + Box main_box = builder.get_object("main_box") as Box; + Box inner_box = builder.get_object("inner_box") as Box; + Label jid_label = builder.get_object("jid_label") as Label; + + jid_label.label = conversation.counterpart.to_string(); + + ArrayList<Jid>? full_jids = PresenceManager.get_instance(stream_interactor).get_full_jids(conversation.counterpart, conversation.account); + if (full_jids != null) { + for (int i = 0; i < full_jids.size; i++) { + Box box = new Box(Orientation.HORIZONTAL, 5); + + Show show = PresenceManager.get_instance(stream_interactor).get_last_show(full_jids[i], conversation.account); + Image image = new Image(); + Pixbuf pixbuf; + int icon_size = 13 * image.scale_factor; + if (show.as == Show.AWAY) { + pixbuf = new Pixbuf.from_resource_at_scale("/org/dino-im/img/status_away.svg", icon_size, icon_size, true); + } else if (show.as == Show.XA || show.as == Show.DND) { + pixbuf = new Pixbuf.from_resource_at_scale("/org/dino-im/img/status_dnd.svg", icon_size, icon_size, true); + } else if (show.as == Show.CHAT) { + pixbuf = new Pixbuf.from_resource_at_scale("/org/dino-im/img/status_chat.svg", icon_size, icon_size, true); + } else { + pixbuf = new Pixbuf.from_resource_at_scale("/org/dino-im/img/status_online.svg", icon_size, icon_size, true); + } + Util.image_set_from_scaled_pixbuf(image, pixbuf); + box.add(image); + + Label resource = new Label(full_jids[i].resourcepart); + resource.xalign = 0; + box.add(resource); + box.show_all(); + + inner_box.add(box); + } + } + return main_box; + } +} + +}
\ No newline at end of file diff --git a/main/src/ui/conversation_selector/conversation_row.vala b/main/src/ui/conversation_selector/conversation_row.vala new file mode 100644 index 00000000..0a6b7e70 --- /dev/null +++ b/main/src/ui/conversation_selector/conversation_row.vala @@ -0,0 +1,160 @@ +using Gee; +using Gdk; +using Gtk; +using Pango; + +using Xmpp; +using Dino.Entities; + +namespace Dino.Ui.ConversationSelector { + +[GtkTemplate (ui = "/org/dino-im/conversation_selector/conversation_row.ui")] +public abstract class ConversationRow : ListBoxRow { + + [GtkChild] protected Image image; + [GtkChild] private Label name_label; + [GtkChild] private Label time_label; + [GtkChild] private Label message_label; + [GtkChild] protected Button x_button; + [GtkChild] private Revealer time_revealer; + [GtkChild] private Revealer xbutton_revealer; + [GtkChild] public Revealer main_revealer; + + public Conversation conversation { get; private set; } + + protected const int AVATAR_SIZE = 40; + + protected string display_name; + protected string message; + protected DateTime time; + protected bool read = true; + + + protected StreamInteractor stream_interactor; + + construct { + name_label.attributes = new AttrList(); + } + + public ConversationRow(StreamInteractor stream_interactor, Conversation conversation) { + this.conversation = conversation; + this.stream_interactor = stream_interactor; + + x_button.clicked.connect(on_x_button_clicked); + + update_name(Util.get_conversation_display_name(stream_interactor, conversation)); + Entities.Message message = MessageManager.get_instance(stream_interactor).get_last_message(conversation); + if (message != null) { + message_received(message); + } + } + + public void update() { + update_time(); + } + + public void message_received(Entities.Message message) { + update_message(message.body.replace("\n", " ")); + update_time(message.time.to_local()); + } + + public void set_avatar(Pixbuf pixbuf, int scale_factor = 1) { + Util.image_set_from_scaled_pixbuf(image, pixbuf, scale_factor); + image.queue_draw(); + } + + public void mark_read() { + update_read(true); + } + + public void mark_unread() { + update_read(false); + } + + public abstract void on_show_received(Show presence); + public abstract void network_connection(bool connected); + + protected void update_name(string? new_name = null) { + if (new_name != null) { + display_name = new_name; + } + name_label.label = display_name; + } + + protected void update_time(DateTime? new_time = null) { + time_label.visible = true; + if (new_time != null) { + time = new_time; + } + if (time != null) { + time_label.label = get_relative_time(time); + } + } + + protected void update_message(string? new_message = null) { + if (new_message != null) { + message = new_message; + } + if (message != null) { + message_label.visible = true; + message_label.label = message; + } + } + + protected void update_read(bool read) { + this.read = read; + if (read) { + name_label.attributes.filter((attr) => attr.equal(attr_weight_new(Weight.BOLD))); + time_label.attributes.filter((attr) => attr.equal(attr_weight_new(Weight.BOLD))); + message_label.attributes.filter((attr) => attr.equal(attr_weight_new(Weight.BOLD))); + } else { + name_label.attributes.insert(attr_weight_new(Weight.BOLD)); + time_label.attributes.insert(attr_weight_new(Weight.BOLD)); + message_label.attributes.insert(attr_weight_new(Weight.BOLD)); + } + name_label.label = name_label.label; // TODO initializes redrawing, which would otherwise not happen. nicer? + time_label.label = time_label.label; + message_label.label = message_label.label; + } + + private void on_x_button_clicked() { + main_revealer.set_transition_type(RevealerTransitionType.SLIDE_UP); + main_revealer.set_reveal_child(false); + main_revealer.notify["child-revealed"].connect(() => { + conversation.active = false; + }); + } + + public override void state_flags_changed(StateFlags flags) { + StateFlags curr_flags = get_state_flags(); + if ((curr_flags & StateFlags.PRELIGHT) != 0) { + time_revealer.set_reveal_child(false); + xbutton_revealer.set_reveal_child(true); + } else { + time_revealer.set_reveal_child(true); + xbutton_revealer.set_reveal_child(false); + } + } + + private static string get_relative_time(DateTime datetime) { + DateTime now = new DateTime.now_local(); + TimeSpan timespan = now.difference(datetime); + if (timespan > 365 * TimeSpan.DAY) { + return datetime.get_year().to_string(); + } else if (timespan > 7 * TimeSpan.DAY) { + return datetime.format("%d.%m"); + } else if (timespan > 2 * TimeSpan.DAY) { + return datetime.format("%a"); + } else if (timespan > 1 * TimeSpan.DAY) { + return "Yesterday"; + } else if (timespan > 9 * TimeSpan.MINUTE) { + return datetime.format("%H:%M"); + } else if (timespan > 1 * TimeSpan.MINUTE) { + return (timespan / TimeSpan.MINUTE).to_string() + " min ago"; + } else { + return "Just now"; + } + } +} + +} diff --git a/main/src/ui/conversation_selector/groupchat_row.vala b/main/src/ui/conversation_selector/groupchat_row.vala new file mode 100644 index 00000000..7fe52d89 --- /dev/null +++ b/main/src/ui/conversation_selector/groupchat_row.vala @@ -0,0 +1,35 @@ +using Dino.Entities; + +namespace Dino.Ui.ConversationSelector { + +public class GroupchatRow : ConversationRow { + + public GroupchatRow(StreamInteractor stream_interactor, Conversation conversation) { + base(stream_interactor, conversation); + has_tooltip = true; + set_tooltip_text(conversation.counterpart.bare_jid.to_string()); + set_avatar((new AvatarGenerator(AVATAR_SIZE, AVATAR_SIZE, image.scale_factor)) + .set_greyscale(true) + .draw_conversation(stream_interactor, conversation), image.scale_factor); + x_button.clicked.connect(on_x_button_clicked); + } + + + public override void on_show_received(Show show) { + set_avatar((new AvatarGenerator(AVATAR_SIZE, AVATAR_SIZE, image.scale_factor)) + .draw_conversation(stream_interactor, conversation), image.scale_factor); + } + + public override void network_connection(bool connected) { + set_avatar((new AvatarGenerator(AVATAR_SIZE, AVATAR_SIZE, image.scale_factor)) + .set_greyscale(!connected || + MucManager.get_instance(stream_interactor).get_nick(conversation.counterpart, conversation.account) == null) // TODO better currently joined + .draw_conversation(stream_interactor, conversation), image.scale_factor); + } + + private void on_x_button_clicked() { + MucManager.get_instance(stream_interactor).part(conversation.account, conversation.counterpart); + } +} + +}
\ No newline at end of file diff --git a/main/src/ui/conversation_selector/list.vala b/main/src/ui/conversation_selector/list.vala new file mode 100644 index 00000000..ea2f9622 --- /dev/null +++ b/main/src/ui/conversation_selector/list.vala @@ -0,0 +1,175 @@ +using Gee; +using Gtk; + +using Xmpp; +using Dino.Entities; + +namespace Dino.Ui.ConversationSelector { + +public class List : ListBox { + + public signal void conversation_selected(Conversation conversation); + + private StreamInteractor stream_interactor; + private string[]? filter_values; + private HashMap<Conversation, ConversationRow> rows = new HashMap<Conversation, ConversationRow>(Conversation.hash_func, Conversation.equals_func); + + public List(StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + + get_style_context().add_class("sidebar"); + set_filter_func(filter); + set_header_func(header); + set_sort_func(sort); + + ChatInteraction.get_instance(stream_interactor).conversation_read.connect((conversation) => { + Idle.add(() => {rows[conversation].mark_read(); return false;}); + }); + ChatInteraction.get_instance(stream_interactor).conversation_unread.connect((conversation) => { + Idle.add(() => {rows[conversation].mark_unread(); return false;}); + }); + ConversationManager.get_instance(stream_interactor).conversation_activated.connect((conversation) => { + Idle.add(() => {add_conversation(conversation); return false;}); + }); + MessageManager.get_instance(stream_interactor).message_received.connect((message, conversation) => { + Idle.add(() => {message_received(message, conversation); return false;}); + }); + MessageManager.get_instance(stream_interactor).message_sent.connect((message, conversation) => { + Idle.add(() => {message_received(message, conversation); return false;}); + }); + PresenceManager.get_instance(stream_interactor).show_received.connect((show, jid, account) => { + Idle.add(() => { + Conversation? conversation = ConversationManager.get_instance(stream_interactor).get_conversation(jid, account); + if (conversation != null && rows.has_key(conversation)) rows[conversation].on_show_received(show); + return false; + }); + }); + RosterManager.get_instance(stream_interactor).updated_roster_item.connect((account, jid, roster_item) => { + Idle.add(() => { + Conversation? conversation = ConversationManager.get_instance(stream_interactor).get_conversation(jid, account); + if (conversation != null && rows.has_key(conversation)) { + ChatRow row = rows[conversation] as ChatRow; + if (row != null) row.on_updated_roster_item(roster_item); + } + return false; + }); + }); + AvatarManager.get_instance(stream_interactor).received_avatar.connect((avatar, jid, account) => { + Idle.add(() => { + Conversation? conversation = ConversationManager.get_instance(stream_interactor).get_conversation(jid, account); + if (conversation != null && rows.has_key(conversation)) { + ChatRow row = rows[conversation] as ChatRow; + if (row != null) row.update_avatar(); + } + return false; + }); + }); + stream_interactor.connection_manager.connection_state_changed.connect((account, state) => { + Idle.add(() => { + foreach (ConversationRow row in rows.values) { + if (row.conversation.account.equals(account)) row.network_connection(state == ConnectionManager.ConnectionState.CONNECTED); + } + return false; + }); + }); + Timeout.add_seconds(60, () => { + foreach (ConversationRow row in rows.values) row.update(); + return true; + }); + } + + public override void row_activated(ListBoxRow r) { + if (r.get_type().is_a(typeof(ConversationRow))) { + ConversationRow row = r as ConversationRow; + conversation_selected(row.conversation); + } + } + + public void set_filter_values(string[]? values) { + if (filter_values == values) { + return; + } + filter_values = values; + invalidate_filter(); + } + + public void add_conversation(Conversation conversation) { + ConversationRow row; + if (!rows.has_key(conversation)) { + if (conversation.type_ == Conversation.Type.GROUPCHAT) { + row = new GroupchatRow(stream_interactor, conversation); + } else { + row = new ChatRow(stream_interactor, conversation); + } + rows[conversation] = row; + add(row); + row.main_revealer.set_reveal_child(true); + conversation.notify["active"].connect((s, p) => { + if (rows.has_key(conversation) && !conversation.active) { + remove_conversation(conversation); + } + }); + } + invalidate_sort(); + queue_draw(); + } + + public void remove_conversation(Conversation conversation) { + remove(rows[conversation]); + rows.unset(conversation); + } + + public void on_conversation_selected(Conversation conversation) { + if (!rows.has_key(conversation)) { + add_conversation(conversation); + } + this.select_row(rows[conversation]); + } + + private void message_received(Entities.Message message, Conversation conversation) { + if (rows.has_key(conversation)) { + rows[conversation].message_received(message); + invalidate_sort(); + } + } + + private void header(ListBoxRow row, ListBoxRow? before_row) { + if (row.get_header() == null && before_row != null) { + row.set_header(new Separator(Orientation.HORIZONTAL)); + } + } + + private bool filter(ListBoxRow r) { + if (r.get_type().is_a(typeof(ConversationRow))) { + ConversationRow row = r as ConversationRow; + if (filter_values != null && filter_values.length != 0) { + foreach (string filter in filter_values) { + if (!(Util.get_conversation_display_name(stream_interactor, row.conversation).down().contains(filter.down()) || + row.conversation.counterpart.to_string().down().contains(filter.down()))) { + return false; + } + } + } + } + return true; + } + + private int sort(ListBoxRow row1, ListBoxRow row2) { + ConversationRow cr1 = row1 as ConversationRow; + ConversationRow cr2 = row2 as ConversationRow; + if (cr1 != null && cr2 != null) { + Conversation c1 = cr1.conversation; + Conversation c2 = cr2.conversation; + int comp = c2.last_active.compare(c1.last_active); + if (comp == 0) { + return Util.get_conversation_display_name(stream_interactor, c1) + .collate(Util.get_conversation_display_name(stream_interactor, c2)); + } else { + return comp; + } + } + return 0; + } +} + +}
\ No newline at end of file diff --git a/main/src/ui/conversation_selector/view.vala b/main/src/ui/conversation_selector/view.vala new file mode 100644 index 00000000..ae641664 --- /dev/null +++ b/main/src/ui/conversation_selector/view.vala @@ -0,0 +1,52 @@ +using Gee; +using Gtk; +using Gdk; + +using Dino.Entities; + +namespace Dino.Ui.ConversationSelector { + +[GtkTemplate (ui = "/org/dino-im/conversation_selector/view.ui")] +public class View : Grid { + public List conversation_list; + + [GtkChild] public SearchEntry search_entry; + [GtkChild] public SearchBar search_bar; + [GtkChild] private ScrolledWindow scrolled; + + public View(StreamInteractor stream_interactor) { + conversation_list = new List(stream_interactor); + scrolled.add(conversation_list); + search_entry.key_release_event.connect(search_key_release_event); + search_entry.search_changed.connect(search_changed); + } + + public void conversation_selected(Conversation? conversation) { + search_entry.set_text(""); + } + + private void refilter() { + string[]? values = null; + string str = search_entry.get_text (); + if (str != "") values = str.split(" "); + conversation_list.set_filter_values(values); + } + + private void search_changed(Editable editable) { + refilter(); + } + + private bool search_key_release_event(EventKey event) { + conversation_list.select_row(conversation_list.get_row_at_y(0)); + if (event.keyval == Key.Down) { + ConversationRow? row = (ConversationRow) conversation_list.get_row_at_index(0); + if (row != null) { + conversation_list.select_row(row); + row.grab_focus(); + } + } + return false; + } +} + +}
\ No newline at end of file |