diff --git a/src/Utils.vala b/src/Utils.vala index 1b89dda..7c24d77 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -40,5 +40,38 @@ public class Tootle.Utils{ .replace ("'", "'"); clipboard.set_text (normalized, -1); } + + public static void download (string url) { + debug ("Downloading file: %s", url); + + var i = url.last_index_of ("/"); + var name = url.substring (i + 1, url.length - i - 1); + if (name == null) + name = "unknown"; + + var dir_path = "%s/%s".printf (GLib.Environment.get_user_special_dir (UserDirectory.DOWNLOAD), Tootle.app.program_name); + var file_path = "%s/%s".printf (dir_path, name); + + var msg = new Soup.Message("GET", url); + msg.finished.connect(() => { + try { + var dir = File.new_for_path (dir_path); + if (!dir.query_exists ()) + dir.make_directory (); + + var file = File.new_for_path (file_path); + if (!file.query_exists ()) { + var data = msg.response_body.data; + FileOutputStream stream = file.create (FileCreateFlags.PRIVATE); + stream.write (data); + } + Tootle.app.toast ("Media downloaded"); + } catch (Error e) { + Tootle.app.toast (e.message); + warning ("Error: %s\n", e.message); + } + }); + Tootle.network.queue (msg); + } } diff --git a/src/Widgets/AttachmentWidget.vala b/src/Widgets/AttachmentWidget.vala index b952db4..6f81ee2 100644 --- a/src/Widgets/AttachmentWidget.vala +++ b/src/Widgets/AttachmentWidget.vala @@ -1,4 +1,5 @@ using Gtk; +using Gdk; public class Tootle.AttachmentWidget : Gtk.EventBox { @@ -55,11 +56,7 @@ public class Tootle.AttachmentWidget : Gtk.EventBox { break; } show (); - button_press_event.connect(() => { - if (!editable) - Tootle.Utils.open_url (attachment.url); - return true; - }); + button_press_event.connect(on_clicked); } public AttachmentWidget.upload (string uri) { @@ -96,5 +93,38 @@ public class Tootle.AttachmentWidget : Gtk.EventBox { Tootle.app.error (_("File read error"), _("Can't read file %s: %s").printf (uri, e.message)); } } + + private bool on_clicked (EventButton ev){ + if (ev.button == 3) + return open_menu (ev.button, ev.time); + + Tootle.Utils.open_url (attachment.url); + return true; + } + + public virtual bool open_menu (uint button, uint32 time) { + var menu = new Gtk.Menu (); + menu.selection_done.connect (() => { + menu.detach (); + menu.destroy (); + }); + + var item_open_link = new Gtk.MenuItem.with_label (_("Open in Browser")); + item_open_link.activate.connect (() => Utils.open_url (attachment.url)); + var item_copy_link = new Gtk.MenuItem.with_label (_("Copy Link")); + item_copy_link.activate.connect (() => Utils.copy (attachment.url)); + var item_download = new Gtk.MenuItem.with_label (_("Download")); + item_download.activate.connect (() => Utils.download (attachment.url)); + menu.add (item_open_link); + if (attachment.type != "unknown") + menu.add (item_download); + menu.add (new Gtk.SeparatorMenuItem ()); + menu.add (item_copy_link); + + menu.show_all (); + menu.attach_widget = this; + menu.popup (null, null, null, button, time); + return true; + } }