Context menus for attachments

This commit is contained in:
bleakgrey 2018-05-21 19:13:49 +03:00
parent c012754ff3
commit b6d265e6a8
2 changed files with 68 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}