From b82c9758ac6e55b329ac6652f1f9b75a6bedcf99 Mon Sep 17 00:00:00 2001 From: Marius Meisenzahl Date: Mon, 15 Mar 2021 18:39:19 +0100 Subject: [PATCH] Open from clipboard (#277) * Open URI from clipboard * Add Desktop Action to open URI from clipboard * Always try to open URI * Apply suggestions from code review Co-authored-by: Cassidy James Blaede Co-authored-by: Cassidy James Blaede --- data/launcher.desktop.in | 6 ++++- src/Application.vala | 53 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/data/launcher.desktop.in b/data/launcher.desktop.in index 49f7149d..7e92cc82 100644 --- a/data/launcher.desktop.in +++ b/data/launcher.desktop.in @@ -10,8 +10,12 @@ Type=Application X-GNOME-Gettext-Domain=com.github.cassidyjames.ephemeral Keywords=WWW;web;browser;internet;private;incognito;focus;temporary;cookies; MimeType=x-scheme-handler/http;x-scheme-handler/https;text/html;application/xhtml+xml;application/x-extension-htm;application/x-extension-html;application/x-extension-shtml;application/x-extension-xht;application/x-extension-mhtml; -Actions=New; +Actions=New;Clipboard; [Desktop Action New] Name=New Window Exec=com.github.cassidyjames.ephemeral + +[Desktop Action Clipboard] +Name=Open from Clipboard +Exec=com.github.cassidyjames.ephemeral --clipboard diff --git a/src/Application.vala b/src/Application.vala index 8fc8f09f..4e0ef8ca 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -48,10 +48,18 @@ public class Ephemeral.Application : Gtk.Application { private bool opening_link = false; + private static bool open_from_clipboard = false; + + private const OptionEntry[] OPTIONS = { + { "clipboard", 'c', 0, OptionArg.NONE, ref open_from_clipboard, + "Open links from clipboard" }, + { null } + }; + public Application () { Object ( application_id: "com.github.cassidyjames.ephemeral", - flags: ApplicationFlags.HANDLES_OPEN + flags: ApplicationFlags.HANDLES_OPEN | ApplicationFlags.HANDLES_COMMAND_LINE ); } @@ -156,6 +164,49 @@ public class Ephemeral.Application : Gtk.Application { } } + public override int command_line (ApplicationCommandLine command_line) { + this.hold (); + + int res = handle_command_line (command_line); + + this.release (); + return res; + } + + private int handle_command_line (ApplicationCommandLine command_line) { + string[] args = command_line.get_arguments (); + + if (args.length == 1) { + args = { args[0], "." }; + } + + unowned string[] tmp = args; + + try { + var option_context = new OptionContext (); + option_context.set_help_enabled (true); + option_context.add_main_entries (OPTIONS, null); + + option_context.parse (ref tmp); + } catch (OptionError e) { + command_line.print (_("Error: %s") + "\n", e.message); + command_line.print (_("Run '%s --help' to see a full list of available options.") + "\n", args[0]); + return 1; + } + + if (open_from_clipboard) { + var display = Gdk.Display.get_default (); + var clipboard = Gtk.Clipboard.get_default (display); + + var uri = clipboard.wait_for_text (); + open ({File.new_for_uri (uri)}, ""); + } else { + activate (); + } + + return 0; + } + public static int main (string[] args) { var app = new Application (); return app.run (args);