From 81f6076c5ce33ef0b92074413503f9177e4ed5d1 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Fri, 9 Mar 2018 22:22:09 +0100 Subject: [PATCH] Desktop Action Menu --- data/com.github.artemanufrij.webpin.desktop | 6 ++ src/Application.vala | 81 ++++++++++++++++++--- src/MainWindow.vala | 3 +- src/Objects/DesktopFile.vala | 36 +++++---- 4 files changed, 100 insertions(+), 26 deletions(-) diff --git a/data/com.github.artemanufrij.webpin.desktop b/data/com.github.artemanufrij.webpin.desktop index 3bde0e9..ded90a6 100644 --- a/data/com.github.artemanufrij.webpin.desktop +++ b/data/com.github.artemanufrij.webpin.desktop @@ -10,3 +10,9 @@ Terminal=false Type=Application Categories=Network;GNOME;GTK; X-GNOME-Gettext-Domain=webpin +Actions=New; + +[Desktop Action New] +Name=New Webapp +Exec=com.github.artemanufrij.webpin --new +Icon=document-new-symbolic \ No newline at end of file diff --git a/src/Application.vala b/src/Application.vala index 29b7b96..813519a 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -1,6 +1,6 @@ /*- * Copyright (c) 2015 Erasmo MarĂ­n - * Copyright (c) 2017-2017 Artem Anufrij + * Copyright (c) 2017-2018 Artem Anufrij * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by @@ -40,16 +40,12 @@ namespace Webpin { } } - construct { - flags |= GLib.ApplicationFlags.HANDLES_OPEN; + [CCode (array_length = false, array_null_terminated = true)] + string[] ? arg_files = null; - var open_web_app = new SimpleAction ("open-web-app", GLib.VariantType.STRING); - add_action (open_web_app); - open_web_app.activate.connect ((parameter) => { - if (parameter != null) { - start_webapp (parameter.get_string ()); - } - }); + construct { + this.flags |= GLib.ApplicationFlags.HANDLES_OPEN; + this.flags |= ApplicationFlags.HANDLES_COMMAND_LINE; create_cache_folders (); } @@ -91,13 +87,76 @@ namespace Webpin { mainwindow = new Windows.WebApp (desktop_file); mainwindow.set_application (this); } + + public override int command_line (ApplicationCommandLine cmd) { + command_line_interpreter (cmd); + return 0; + } + + private void command_line_interpreter (ApplicationCommandLine cmd) { + string[] args_cmd = cmd.get_arguments (); + unowned string[] args = args_cmd; + + bool new_app = false; + bool remove_app = false; + + GLib.OptionEntry [] options = new OptionEntry [4]; + options [0] = { "new", 0, 0, OptionArg.NONE, ref new_app, "Create new Webapp", null }; + options [1] = { "remove", 0, 0, OptionArg.NONE, ref remove_app, "Remove Webapp", null }; + options [2] = { "", 0, 0, OptionArg.STRING_ARRAY, ref arg_files, null, "[URI...]" }; + options [3] = { null }; + + var opt_context = new OptionContext ("actions"); + opt_context.add_main_entries (options, null); + try { + opt_context.parse (ref args); + } catch (Error err) { + warning (err.message); + return; + } + + if (new_app) { + if (new_app) { + activate (); + (mainwindow as MainWindow).show_assistant (); + } + return; + } + + File[] files = null; + foreach (string arg_file in arg_files) { + files += (File.new_for_uri (arg_file)); + } + + if (files != null && files.length > 0) { + if (remove_app) { + var app_info = Services.DesktopFilesManager.get_app_by_url (files [0].get_uri ()); + var desktop_file = new Webpin.DesktopFile.from_desktopappinfo (app_info); + desktop_file.delete_file (); + } else { + open (files, ""); + } + return; + } + + activate (); + } } } static int main (string[] args) { Gtk.init (ref args); var app = Webpin.WebpinApp.instance; - if (args.length > 1) { + + bool has_new_arg = false; + + foreach (var arg in args) { + if (arg == "--new") { + has_new_arg = true; + } + } + + if (args.length > 1 && !has_new_arg) { var checksum = new GLib.Checksum (GLib.ChecksumType.MD5); checksum.update (args[1].data, args[1].length); var id = "a" + checksum.get_string ().substring (0, 5) + "a.artemanufrij.webpin"; diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 2f36c7a..9e88531 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -63,6 +63,7 @@ namespace Webpin { headerbar = new Gtk.HeaderBar (); headerbar.show_close_button = true; headerbar.title = "Webpin"; + headerbar.get_style_context ().add_class ("default-decoration"); set_titlebar (headerbar); back_button = new Gtk.Button.with_label (_("Applications")); @@ -171,7 +172,7 @@ namespace Webpin { this.present (); } - private void show_assistant (DesktopFile? desktop_file = null) { + public void show_assistant (DesktopFile? desktop_file = null) { stack.set_transition_type (Gtk.StackTransitionType.SLIDE_LEFT); stack.set_visible_child_name("editor"); back_button.show_all (); diff --git a/src/Objects/DesktopFile.vala b/src/Objects/DesktopFile.vala index 6003c45..0cf52e8 100644 --- a/src/Objects/DesktopFile.vala +++ b/src/Objects/DesktopFile.vala @@ -30,20 +30,27 @@ namespace Webpin { public class DesktopFile : GLib.Object { string template = """ - [Desktop Entry] - Name=Webpin - GenericName=Web app - Comment=Webpin web app - Exec=com.github.artemanufrij.webpin - Keywords=webpin;webapp;internet; - Icon=application-default-icon - Terminal=false - Type=Application - Categories=Network; - X-GNOME-Gettext-Domain=com.github.artemanufrij.webpin - X-GNOME-UsesNotifications=true - StartupWMClass=Webpin - X-Webpin-PrimaryColor=rgba (222,222,222,1)"""; + [Desktop Entry] + Name=Webpin + GenericName=Web app + Comment=Webpin web app + Exec=com.github.artemanufrij.webpin + Keywords=webpin;webapp;internet; + Icon=application-default-icon + Terminal=false + Type=Application + Categories=Network; + X-GNOME-Gettext-Domain=com.github.artemanufrij.webpin + X-GNOME-UsesNotifications=true + StartupWMClass=Webpin + X-Webpin-PrimaryColor=rgba (222,222,222,1) + Actions=Remove; + + [Desktop Action Remove] + Name=Remove Webapp + Exec=com.github.artemanufrij.webpin --remove + Icon=edit-delete-symbolic + """; GLib.KeyFile file; @@ -112,6 +119,7 @@ namespace Webpin { file.set_string ("Desktop Entry", "Icon", icon); file.set_string ("Desktop Entry", "StartupWMClass", url); file.set_string ("Desktop Entry", "X-Webpin-StayOpen", stay_open.to_string ()); + file.set_string ("Desktop Action Remove", "Exec", "com.github.artemanufrij.webpin --remove " + url); } public DesktopFile.from_desktopappinfo (GLib.DesktopAppInfo info) {