Skip to content
This repository has been archived by the owner on Apr 2, 2022. It is now read-only.

Open from clipboard #277

Merged
merged 4 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion data/launcher.desktop.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 52 additions & 1 deletion src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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);
Expand Down