diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 876e4a66..4a799758 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -102,6 +102,7 @@ SOURCES src/ui/conversation_summary/conversation_item_skeleton.vala src/ui/conversation_summary/conversation_view.vala src/ui/conversation_summary/date_separator_populator.vala + src/ui/conversation_summary/default_file_display.vala src/ui/conversation_summary/default_message_display.vala src/ui/conversation_summary/file_populator.vala src/ui/conversation_summary/image_display.vala diff --git a/main/data/conversation_summary/message_item.ui b/main/data/conversation_summary/message_item.ui index a6937d10..dd982ca9 100644 --- a/main/data/conversation_summary/message_item.ui +++ b/main/data/conversation_summary/message_item.ui @@ -11,7 +11,6 @@ 30 2 30 - True 0 diff --git a/main/src/ui/conversation_summary/conversation_item_skeleton.vala b/main/src/ui/conversation_summary/conversation_item_skeleton.vala index aa18c9d8..585cfa7a 100644 --- a/main/src/ui/conversation_summary/conversation_item_skeleton.vala +++ b/main/src/ui/conversation_summary/conversation_item_skeleton.vala @@ -68,6 +68,7 @@ public class ConversationItemSkeleton : Grid { private void setup(Plugins.MetaConversationItem item) { update_time(); if (item.requires_avatar) { + image.visible = true; Util.image_set_from_scaled_pixbuf(image, (new AvatarGenerator(30, 30, image.scale_factor)).set_greyscale(item.dim).draw_jid(stream_interactor, item.jid, conversation.account)); } if (item.requires_header) { diff --git a/main/src/ui/conversation_summary/default_file_display.vala b/main/src/ui/conversation_summary/default_file_display.vala new file mode 100644 index 00000000..413871ca --- /dev/null +++ b/main/src/ui/conversation_summary/default_file_display.vala @@ -0,0 +1,95 @@ +using Gdk; +using Gtk; + +using Dino.Entities; + +namespace Dino.Ui.ConversationSummary { + +public class DefaultFileDisplay : Plugins.MetaConversationItem { + public override Jid? jid { get; set; } + public override DateTime? sort_time { get; set; } + public override DateTime? display_time { get; set; } + public override Encryption? encryption { get; set; } + public override Entities.Message.Marked? mark { get; set; } + + public override bool can_merge { get; set; default=true; } + public override bool requires_avatar { get; set; default=true; } + public override bool requires_header { get; set; default=true; } + + private const int MAX_HEIGHT = 300; + private const int MAX_WIDTH = 600; + + private StreamInteractor stream_interactor; + private FileTransfer file_transfer; + + public DefaultFileDisplay(StreamInteractor stream_interactor, FileTransfer file_transfer) { + this.stream_interactor = stream_interactor; + this.file_transfer = file_transfer; + + this.jid = file_transfer.direction == FileTransfer.DIRECTION_SENT ? new Jid.with_resource(file_transfer.account.bare_jid.to_string(), file_transfer.account.resourcepart) : file_transfer.counterpart; + this.sort_time = file_transfer.time; + this.seccondary_sort_indicator = file_transfer.id + 0.2903; + this.display_time = file_transfer.time; + this.encryption = file_transfer.encryption; + this.mark = file_to_message_state(file_transfer.state); + file_transfer.notify["state"].connect_after(() => { + this.mark = file_to_message_state(file_transfer.state); + }); + } + + public override Object? get_widget(Plugins.WidgetType widget_type) { + Box main_box = new Box(Orientation.HORIZONTAL, 4) { halign=Align.START, visible=true }; + string? content_type = ContentType.from_mime_type(file_transfer.mime_type); + string? icon_name = ContentType.get_generic_icon_name(file_transfer.mime_type); + Image content_type_image = new Image.from_icon_name(icon_name, IconSize.DND) { visible=true }; + main_box.add(content_type_image); + + Box right_box = new Box(Orientation.VERTICAL, 0) { visible=true }; + Label name_label = new Label(file_transfer.file_name) { xalign=0, yalign=0, visible=true}; + right_box.add(name_label); + Label mime_label = new Label("" + _("File") + ": " + file_transfer.mime_type + "") { use_markup=true, xalign=0, yalign=1, visible=true}; + mime_label.get_style_context().add_class("dim-label"); + right_box.add(mime_label); + main_box.add(right_box); + + EventBox event_box = new EventBox() { halign=Align.START, visible=true }; + event_box.add(main_box); + + event_box.enter_notify_event.connect((event) => { + event.get_window().set_cursor(new Cursor.for_display(Gdk.Display.get_default(), CursorType.HAND2)); + return false; + }); + event_box.leave_notify_event.connect((event) => { + event.get_window().set_cursor(new Cursor.for_display(Gdk.Display.get_default(), CursorType.XTERM)); + return false; + }); + event_box.button_release_event.connect((event_button) => { + if (event_button.button == 1) { + try{ + AppInfo.launch_default_for_uri("file://" + file_transfer.get_uri(), null); + } catch (Error err) { + print("Tryed to open " + file_transfer.get_uri()); + } + } + return false; + }); + + return event_box; + } + + private Entities.Message.Marked file_to_message_state(FileTransfer.State state) { + switch (state) { + case FileTransfer.State.IN_PROCESS: + return Entities.Message.Marked.UNSENT; + case FileTransfer.State.COMPLETE: + return Entities.Message.Marked.NONE; + case FileTransfer.State.NOT_STARTED: + return Entities.Message.Marked.UNSENT; + case FileTransfer.State.FAILED: + return Entities.Message.Marked.WONTSEND; + } + assert_not_reached(); + } +} + +} diff --git a/main/src/ui/conversation_summary/file_populator.vala b/main/src/ui/conversation_summary/file_populator.vala index 4dc6aaec..af7bc992 100644 --- a/main/src/ui/conversation_summary/file_populator.vala +++ b/main/src/ui/conversation_summary/file_populator.vala @@ -41,132 +41,13 @@ class FilePopulator : Plugins.ConversationItemPopulator, Object { public void populate_between_widgets(Conversation conversation, DateTime from, DateTime to) { } private void insert_file(FileTransfer transfer) { + Plugins.MetaConversationItem item = null; if (transfer.mime_type != null && transfer.mime_type.has_prefix("image")) { - item_collection.insert_item(new ImageItem(stream_interactor, transfer)); + item = new ImageDisplay(stream_interactor, transfer); + } else { + item = new DefaultFileDisplay(stream_interactor, transfer); } - } -} - -public class ImageItem : Plugins.MetaConversationItem { - public override Jid? jid { get; set; } - public override DateTime? sort_time { get; set; } - public override DateTime? display_time { get; set; } - public override Encryption? encryption { get; set; } - public override Entities.Message.Marked? mark { get; set; } - - public override bool can_merge { get; set; default=true; } - public override bool requires_avatar { get; set; default=true; } - public override bool requires_header { get; set; default=true; } - - private const int MAX_HEIGHT = 300; - private const int MAX_WIDTH = 600; - - private StreamInteractor stream_interactor; - private FileTransfer file_transfer; - - public ImageItem(StreamInteractor stream_interactor, FileTransfer file_transfer) { - this.stream_interactor = stream_interactor; - this.file_transfer = file_transfer; - - this.jid = file_transfer.direction == FileTransfer.DIRECTION_SENT ? new Jid.with_resource(file_transfer.account.bare_jid.to_string(), file_transfer.account.resourcepart) : file_transfer.counterpart; - this.sort_time = file_transfer.time; - this.seccondary_sort_indicator = file_transfer.id + 0.2903; - this.display_time = file_transfer.time; - this.encryption = file_transfer.encryption; - this.mark = file_to_message_state(file_transfer.state); - file_transfer.notify["state"].connect_after(() => { - this.mark = file_to_message_state(file_transfer.state); - }); - } - - public override Object? get_widget(Plugins.WidgetType widget_type) { - Image image = new Image() { halign=Align.START, visible = true }; - Gdk.Pixbuf pixbuf; - try { - pixbuf = new Gdk.Pixbuf.from_file(file_transfer.get_uri()); - } catch (Error error) { - return null; - } - - int max_scaled_height = MAX_HEIGHT * image.scale_factor; - if (pixbuf.height > max_scaled_height) { - pixbuf = pixbuf.scale_simple((int) ((double) max_scaled_height / pixbuf.height * pixbuf.width), max_scaled_height, Gdk.InterpType.BILINEAR); - } - int max_scaled_width = MAX_WIDTH * image.scale_factor; - if (pixbuf.width > max_scaled_width) { - pixbuf = pixbuf.scale_simple(max_scaled_width, (int) ((double) max_scaled_width / pixbuf.width * pixbuf.height), Gdk.InterpType.BILINEAR); - } - pixbuf = AvatarGenerator.crop_corners(pixbuf, 3 * image.get_scale_factor()); - Util.image_set_from_scaled_pixbuf(image, pixbuf); - Util.force_css(image, "* { box-shadow: 0px 0px 2px 0px rgba(0,0,0,0.1); margin: 2px; border-radius: 3px; }"); - - Builder builder = new Builder.from_resource("/im/dino/Dino/conversation_summary/image_toolbar.ui"); - Widget toolbar = builder.get_object("main") as Widget; - Util.force_background(toolbar, "rgba(0, 0, 0, 0.5)"); - Util.force_css(toolbar, "* { padding: 3px; border-radius: 3px; }"); - - Label url_label = builder.get_object("url_label") as Label; - Util.force_color(url_label, "#eee"); - file_transfer.notify["info"].connect_after(() => { update_info(url_label, file_transfer.info); }); - update_info(url_label, file_transfer.info); - - Image copy_image = builder.get_object("copy_image") as Image; - Util.force_css(copy_image, "*:not(:hover) { color: #eee; }"); - Button copy_button = builder.get_object("copy_button") as Button; - Util.force_css(copy_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }"); - copy_button.clicked.connect(() => { - if (file_transfer.info != null) Clipboard.get_default(Gdk.Display.get_default()).set_text(file_transfer.info, file_transfer.info.length); - }); - - Image open_image = builder.get_object("open_image") as Image; - Util.force_css(open_image, "*:not(:hover) { color: #eee; }"); - Button open_button = builder.get_object("open_button") as Button; - Util.force_css(open_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }"); - open_button.clicked.connect(() => { - try{ - AppInfo.launch_default_for_uri(file_transfer.info, null); - } catch (Error err) { - print("Tryed to open " + file_transfer.info); - } - }); - - Revealer toolbar_revealer = new Revealer() { transition_type=RevealerTransitionType.CROSSFADE, transition_duration=400, visible=true }; - toolbar_revealer.add(toolbar); - - Grid grid = new Grid() { visible=true }; - grid.attach(toolbar_revealer, 0, 0, 1, 1); - grid.attach(image, 0, 0, 1, 1); - - EventBox event_box = new EventBox() { halign=Align.START, visible=true }; - event_box.add(grid); - event_box.enter_notify_event.connect(() => { toolbar_revealer.reveal_child = true; return false; }); - event_box.leave_notify_event.connect(() => { toolbar_revealer.reveal_child = false; return false; }); - - return event_box; - } - - private void update_info(Label url_label, string? info) { - string url = info ?? ""; - if (url.has_prefix("https://")) url = url.substring(8); - if (url.has_prefix("http://")) url = url.substring(7); - if (url.has_prefix("www.")) url = url.substring(4); - string[] slash_split = url.split("/"); - if (slash_split.length > 2) url = slash_split[0] + "/…/" + slash_split[slash_split.length - 1]; - url_label.label = url; - } - - private Entities.Message.Marked file_to_message_state(FileTransfer.State state) { - switch (state) { - case FileTransfer.State.IN_PROCESS: - return Entities.Message.Marked.UNSENT; - case FileTransfer.State.COMPLETE: - return Entities.Message.Marked.NONE; - case FileTransfer.State.NOT_STARTED: - return Entities.Message.Marked.UNSENT; - case FileTransfer.State.FAILED: - return Entities.Message.Marked.WONTSEND; - } - assert_not_reached(); + item_collection.insert_item(item); } } diff --git a/main/src/ui/conversation_summary/image_display.vala b/main/src/ui/conversation_summary/image_display.vala index e69de29b..3f1b0bd6 100644 --- a/main/src/ui/conversation_summary/image_display.vala +++ b/main/src/ui/conversation_summary/image_display.vala @@ -0,0 +1,130 @@ +using Gtk; + +using Dino.Entities; + +namespace Dino.Ui.ConversationSummary { + +public class ImageDisplay : Plugins.MetaConversationItem { + public override Jid? jid { get; set; } + public override DateTime? sort_time { get; set; } + public override DateTime? display_time { get; set; } + public override Encryption? encryption { get; set; } + public override Entities.Message.Marked? mark { get; set; } + + public override bool can_merge { get; set; default=true; } + public override bool requires_avatar { get; set; default=true; } + public override bool requires_header { get; set; default=true; } + + private const int MAX_HEIGHT = 300; + private const int MAX_WIDTH = 600; + + private StreamInteractor stream_interactor; + private FileTransfer file_transfer; + + public ImageDisplay(StreamInteractor stream_interactor, FileTransfer file_transfer) { + this.stream_interactor = stream_interactor; + this.file_transfer = file_transfer; + + this.jid = file_transfer.direction == FileTransfer.DIRECTION_SENT ? new Jid.with_resource(file_transfer.account.bare_jid.to_string(), file_transfer.account.resourcepart) : file_transfer.counterpart; + this.sort_time = file_transfer.time; + this.seccondary_sort_indicator = file_transfer.id + 0.2903; + this.display_time = file_transfer.time; + this.encryption = file_transfer.encryption; + this.mark = file_to_message_state(file_transfer.state); + file_transfer.notify["state"].connect_after(() => { + this.mark = file_to_message_state(file_transfer.state); + }); + } + + public override Object? get_widget(Plugins.WidgetType widget_type) { + Image image = new Image() { halign=Align.START, visible = true }; + Gdk.Pixbuf pixbuf; + try { + pixbuf = new Gdk.Pixbuf.from_file(file_transfer.get_uri()); + } catch (Error error) { + return null; + } + + int max_scaled_height = MAX_HEIGHT * image.scale_factor; + if (pixbuf.height > max_scaled_height) { + pixbuf = pixbuf.scale_simple((int) ((double) max_scaled_height / pixbuf.height * pixbuf.width), max_scaled_height, Gdk.InterpType.BILINEAR); + } + int max_scaled_width = MAX_WIDTH * image.scale_factor; + if (pixbuf.width > max_scaled_width) { + pixbuf = pixbuf.scale_simple(max_scaled_width, (int) ((double) max_scaled_width / pixbuf.width * pixbuf.height), Gdk.InterpType.BILINEAR); + } + pixbuf = AvatarGenerator.crop_corners(pixbuf, 3 * image.get_scale_factor()); + Util.image_set_from_scaled_pixbuf(image, pixbuf); + Util.force_css(image, "* { box-shadow: 0px 0px 2px 0px rgba(0,0,0,0.1); margin: 2px; border-radius: 3px; }"); + + Builder builder = new Builder.from_resource("/im/dino/Dino/conversation_summary/image_toolbar.ui"); + Widget toolbar = builder.get_object("main") as Widget; + Util.force_background(toolbar, "rgba(0, 0, 0, 0.5)"); + Util.force_css(toolbar, "* { padding: 3px; border-radius: 3px; }"); + + Label url_label = builder.get_object("url_label") as Label; + Util.force_color(url_label, "#eee"); + file_transfer.notify["info"].connect_after(() => { update_info(url_label, file_transfer.info); }); + update_info(url_label, file_transfer.info); + + Image copy_image = builder.get_object("copy_image") as Image; + Util.force_css(copy_image, "*:not(:hover) { color: #eee; }"); + Button copy_button = builder.get_object("copy_button") as Button; + Util.force_css(copy_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }"); + copy_button.clicked.connect(() => { + if (file_transfer.info != null) Clipboard.get_default(Gdk.Display.get_default()).set_text(file_transfer.info, file_transfer.info.length); + }); + + Image open_image = builder.get_object("open_image") as Image; + Util.force_css(open_image, "*:not(:hover) { color: #eee; }"); + Button open_button = builder.get_object("open_button") as Button; + Util.force_css(open_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }"); + open_button.clicked.connect(() => { + try{ + AppInfo.launch_default_for_uri(file_transfer.info, null); + } catch (Error err) { + print("Tryed to open " + file_transfer.info); + } + }); + + Revealer toolbar_revealer = new Revealer() { transition_type=RevealerTransitionType.CROSSFADE, transition_duration=400, visible=true }; + toolbar_revealer.add(toolbar); + + Grid grid = new Grid() { visible=true }; + grid.attach(toolbar_revealer, 0, 0, 1, 1); + grid.attach(image, 0, 0, 1, 1); + + EventBox event_box = new EventBox() { halign=Align.START, visible=true }; + event_box.add(grid); + event_box.enter_notify_event.connect(() => { toolbar_revealer.reveal_child = true; return false; }); + event_box.leave_notify_event.connect(() => { toolbar_revealer.reveal_child = false; return false; }); + + return event_box; + } + + private void update_info(Label url_label, string? info) { + string url = info ?? ""; + if (url.has_prefix("https://")) url = url.substring(8); + if (url.has_prefix("http://")) url = url.substring(7); + if (url.has_prefix("www.")) url = url.substring(4); + string[] slash_split = url.split("/"); + if (slash_split.length > 2) url = slash_split[0] + "/…/" + slash_split[slash_split.length - 1]; + url_label.label = url; + } + + private Entities.Message.Marked file_to_message_state(FileTransfer.State state) { + switch (state) { + case FileTransfer.State.IN_PROCESS: + return Entities.Message.Marked.UNSENT; + case FileTransfer.State.COMPLETE: + return Entities.Message.Marked.NONE; + case FileTransfer.State.NOT_STARTED: + return Entities.Message.Marked.UNSENT; + case FileTransfer.State.FAILED: + return Entities.Message.Marked.WONTSEND; + } + assert_not_reached(); + } +} + +} diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index aa8a6357..21d833eb 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -17,25 +17,38 @@ public class FileProvider : Dino.FileProvider, Object { public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) { this.stream_interactor = stream_interactor; this.url_regex = new Regex("""^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))$"""); - this.file_ext_regex = new Regex("""\.(png|jpg|jpeg|svg|gif|pgp)"""); + this.file_ext_regex = new Regex("""\.(png|jpg|jpeg|svg|gif|pgp)$"""); - stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(check_message); - stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_message); + stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(check_in_message); + stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_out_message); stream_interactor.get_module(Manager.IDENTITY).uploaded.connect((file_transfer, url) => { file_transfer.info = url; ignore_once.add(url); }); } - public void check_message(Message message, Conversation conversation) { - if (ignore_once.remove(message.body)) return; + private void check_in_message(Message message, Conversation conversation) { + if (!url_regex.match(message.body)) return; Jid relevant_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(message.from, conversation.account) ?? conversation.counterpart; bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, relevant_jid) != null; if (message.direction == Message.DIRECTION_RECEIVED && !in_roster) return; + + string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(message.stanza); + if ((oob_url != null && oob_url == message.body) || file_ext_regex.match(message.body)) { + download_url(message, conversation); + } + } + + public void check_out_message(Message message, Conversation conversation) { + if (ignore_once.remove(message.body)) return; if (message.body.length < 5) return; if (!url_regex.match(message.body)) return; if (!file_ext_regex.match(message.body)) return; + download_url(message, conversation); + } + + private void download_url(Message message, Conversation conversation) { var session = new Soup.Session(); var head_message = new Soup.Message("HEAD", message.body); if (head_message != null) { @@ -47,7 +60,7 @@ public class FileProvider : Dino.FileProvider, Object { if (name == "Content-Type") content_type = val; if (name == "Content-Length") content_length = val; }); - if (/*content_type != null && content_type.has_prefix("image") &&*/ content_length != null && int.parse(content_length) < 5000000) { + if (content_length != null && int.parse(content_length) < 5000000) { FileTransfer file_transfer = new FileTransfer(); Soup.Request request = session.request(message.body); request.send_async.begin(null, (obj, res) => { diff --git a/xmpp-vala/src/module/xep/0066_out_of_band_data.vala b/xmpp-vala/src/module/xep/0066_out_of_band_data.vala index b482252b..804d406a 100644 --- a/xmpp-vala/src/module/xep/0066_out_of_band_data.vala +++ b/xmpp-vala/src/module/xep/0066_out_of_band_data.vala @@ -8,4 +8,8 @@ public static void add_url_to_message(Message.Stanza message, string url, string message.stanza.put_node(new StanzaNode.build("x", NS_URI).add_self_xmlns().put_node(new StanzaNode.build("url", NS_URI).put_node(new StanzaNode.text(url)))); } +public static string? get_url_from_message(Message.Stanza message) { + return message.stanza.get_deep_string_content(NS_URI + ":x", NS_URI + ":url"); +} + }