From 39e45e4ba460da3780b748564bef7093c631544b Mon Sep 17 00:00:00 2001 From: Marius Meisenzahl Date: Wed, 10 Mar 2021 20:10:54 +0100 Subject: [PATCH 1/4] Open URI from clipboard --- src/Application.vala | 55 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/Application.vala b/src/Application.vala index 8fc8f09f..c20d16ee 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 = { + { "open-from-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,51 @@ 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 (); + if (uri != null) { + open ({File.new_for_uri (uri)}, ""); + } + } else { + activate (); + } + + return 0; + } + public static int main (string[] args) { var app = new Application (); return app.run (args); From 3f690e8a9f368bc439937c6e2b870ccf0c4c6afb Mon Sep 17 00:00:00 2001 From: Marius Meisenzahl Date: Wed, 10 Mar 2021 20:11:12 +0100 Subject: [PATCH 2/4] Add Desktop Action to open URI from clipboard --- data/launcher.desktop.in | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/data/launcher.desktop.in b/data/launcher.desktop.in index 49f7149d..b18b223c 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;Open From Clipboard; [Desktop Action New] Name=New Window Exec=com.github.cassidyjames.ephemeral + +[Desktop Action Open From Clipboard] +Name=Open From Clipboard +Exec=com.github.cassidyjames.ephemeral --open-from-clipboard From 8764e23bcdba5f224e2f7c89cae5b6bae76947f9 Mon Sep 17 00:00:00 2001 From: Marius Meisenzahl Date: Wed, 10 Mar 2021 20:27:19 +0100 Subject: [PATCH 3/4] Always try to open URI --- src/Application.vala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Application.vala b/src/Application.vala index c20d16ee..675d325b 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -199,9 +199,7 @@ public class Ephemeral.Application : Gtk.Application { var clipboard = Gtk.Clipboard.get_default (display); var uri = clipboard.wait_for_text (); - if (uri != null) { - open ({File.new_for_uri (uri)}, ""); - } + open ({File.new_for_uri (uri)}, ""); } else { activate (); } From d090d40e39b4ab7052a75e46c7927554550917f7 Mon Sep 17 00:00:00 2001 From: Marius Meisenzahl Date: Sat, 13 Mar 2021 07:17:20 +0100 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Cassidy James Blaede --- data/launcher.desktop.in | 8 ++++---- src/Application.vala | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/data/launcher.desktop.in b/data/launcher.desktop.in index b18b223c..7e92cc82 100644 --- a/data/launcher.desktop.in +++ b/data/launcher.desktop.in @@ -10,12 +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;Open From Clipboard; +Actions=New;Clipboard; [Desktop Action New] Name=New Window Exec=com.github.cassidyjames.ephemeral -[Desktop Action Open From Clipboard] -Name=Open From Clipboard -Exec=com.github.cassidyjames.ephemeral --open-from-clipboard +[Desktop Action Clipboard] +Name=Open from Clipboard +Exec=com.github.cassidyjames.ephemeral --clipboard diff --git a/src/Application.vala b/src/Application.vala index 675d325b..4e0ef8ca 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -51,7 +51,7 @@ public class Ephemeral.Application : Gtk.Application { private static bool open_from_clipboard = false; private const OptionEntry[] OPTIONS = { - { "open-from-clipboard", 'c', 0, OptionArg.NONE, ref open_from_clipboard, + { "clipboard", 'c', 0, OptionArg.NONE, ref open_from_clipboard, "Open links from clipboard" }, { null } };