=== modified file 'diodon/main.vala' --- diodon/main.vala 2015-10-12 17:28:58 +0000 +++ diodon/main.vala 2016-02-13 05:11:15 +0000 @@ -34,28 +34,36 @@ private static bool show_version = false; /** - * checksums to be pasted. should only be one though - */ - private static string[] checksums; - - /** * main clipboard controller */ private Controller? controller = null; - + + /** + * determine whether help information should be printed + */ + private static bool show_help = false; + + /** + * store unmached options and possible actions + */ + private static string[] remaining_options; + /** * list of available command line options */ private static const OptionEntry[] options = { - { OPTION_REMAINING, '\0', 0, OptionArg.STRING_ARRAY, ref checksums, null, "[CHECKSUM]" }, + { OPTION_REMAINING, '\0', 0, OptionArg.STRING_ARRAY, ref remaining_options, null, "[CHECKSUM]" }, + { "help", 'h', 0, OptionArg.NONE, ref show_help, "Show help options", null }, { "version", 'v', 0, OptionArg.NONE, ref show_version, "Print version information", null }, { null } }; public DiodonApplication() { - Object(application_id: Config.BUSNAME, flags: ApplicationFlags.FLAGS_NONE); + Object(application_id: Config.BUSNAME, flags: ApplicationFlags.HANDLES_COMMAND_LINE); + command_line.connect (handle_command_line); + // add supported actions SimpleAction paste_action = new SimpleAction("paste-action", VariantType.STRING); paste_action.activate.connect(activate_paste_action); @@ -64,13 +72,23 @@ public void activate_paste_action(GLib.Variant? parameter) { + + if(parameter == null || controller == null) + return; + + string checksum = parameter.get_strv ()[0]; + if (checksum == null) + return; + hold(); - - if(parameter != null && controller != null) { - string checksum = parameter.get_string(); - debug("Execute paste-action with checksum %s", checksum); - controller.select_item_by_checksum.begin(checksum); - } + + // it might be an uri so we have to remove uri first before + // TODO: + // see ZeitgeistClipboardStorage.CLIPBOARD_URI why clipboard: + // is used staticly here + checksum = checksum.replace("clipboard:", ""); + debug("Execute paste with checksum %s", checksum); + controller.select_item_by_checksum.begin(checksum); release(); } @@ -91,6 +109,59 @@ } } + /** + * Process command line arguments + */ + private int handle_command_line (ApplicationCommandLine command_line) + { + var args = command_line.get_arguments (); + show_version = false; + show_help = false; + remaining_options = new string[args.length]; + + try + { + OptionContext opt_context = new OptionContext("- GTK+ Clipboard Manager"); + opt_context.set_help_enabled(false); + opt_context.add_main_entries(options, null); + opt_context.add_group(Gtk.get_option_group(true)); + opt_context.parse_strv (ref args); + + if(show_help) { + command_line.print(opt_context.get_help (true, null)); + return 0; + } + } catch(OptionError e) { + stdout.printf("Option parsing failed: %s\n", e.message); + } + + if(show_version) { + command_line.print("Diodon %s\n", Config.VERSION); + return 0; + } + + if (remaining_options.length > 0 && remaining_options[0] != null) + { + if (has_action (remaining_options[0])) + { + var i = 1; + while (remaining_options[i] != null) + i++; + + activate_action(remaining_options[0], new Variant.strv(remaining_options[1:i])); + return 0; + } else + { + activate_action("paste", new Variant.strv(remaining_options[0:1])); + return 0; + } + } + + activate (); + + return 0; + } + public static int main(string[] args) { try { @@ -103,45 +174,8 @@ // diodon should only show up in gnome DesktopAppInfo.set_desktop_env("GNOME"); - // init vars - checksums = new string[1]; // can only process one checksum max - - // init option context - OptionContext opt_context = new OptionContext("- GTK+ Clipboard Manager"); - opt_context.set_help_enabled(true); - opt_context.add_main_entries(options, null); - opt_context.add_group(Gtk.get_option_group(true)); - opt_context.parse(ref args); - - if(show_version) { - stdout.printf("Diodon %s\n", Config.VERSION); - return 0; // bail out - } - - // check whether there is a checksum of clipboard content to paste - string checksum = null; - if(checksums.length > 0 && checksums[0] != null) { - checksum = checksums[0]; - - // it might be an uri so we have to remove uri first before - // TODO: - // see ZeitgeistClipboardStorage.CLIPBOARD_URI why clipboard: - // is used staticly here - checksum = checksum.replace("clipboard:", ""); - } - DiodonApplication app = new DiodonApplication(); - - if(checksum != null) { - debug("activate paste-action with checksum %s", checksum); - app.register(); - app.activate_action("paste-action", new Variant.string(checksum)); - return 0; - } - return app.run(args); - } catch(OptionError e) { - stdout.printf("Option parsing failed: %s\n", e.message); } catch(Error e) { stdout.printf("Unexpected error occured: %s\n", e.message); } === modified file 'libdiodon/controller.vala' --- libdiodon/controller.vala 2015-09-08 12:36:18 +0000 +++ libdiodon/controller.vala 2016-02-13 05:07:21 +0000 @@ -64,6 +64,8 @@ * Called after recent menu has been rebuilt */ public signal void on_recent_menu_changed(Gtk.Menu recent_menu); + + public delegate void Callback(string[] args); public Controller() { @@ -214,6 +216,16 @@ enable_clipboard_manager(ClipboardType.PRIMARY, configuration.use_primary); } + + /** + * Add an action to the application + */ + public void add_command_line_action(string name, Callback callback) + { + var action = new SimpleAction (name, VariantType.STRING_ARRAY); + action.activate.connect ((parameter) => callback (parameter.dup_strv ())); + Application.get_default ().add_action (action); + } /** * Select a clipboard item identified by its checksum