diff options
Diffstat (limited to 'src/actions')
-rw-r--r-- | src/actions/action.vala | 77 | ||||
-rw-r--r-- | src/actions/actionRegistry.vala | 219 | ||||
-rw-r--r-- | src/actions/appAction.vala | 76 | ||||
-rw-r--r-- | src/actions/keyAction.vala | 81 | ||||
-rw-r--r-- | src/actions/pieAction.vala | 103 | ||||
-rw-r--r-- | src/actions/sigAction.vala | 63 | ||||
-rw-r--r-- | src/actions/uriAction.vala | 77 |
7 files changed, 696 insertions, 0 deletions
diff --git a/src/actions/action.vala b/src/actions/action.vala new file mode 100644 index 0000000..1e6437e --- /dev/null +++ b/src/actions/action.vala @@ -0,0 +1,77 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// A base class for actions, which are executed when the user +/// activates a pie's slice. +///////////////////////////////////////////////////////////////////////// + +public abstract class Action : GLib.Object { + + ///////////////////////////////////////////////////////////////////// + /// The command which gets executed when user activates the Slice. + /// It may be anything but has to be representable with a string. + ///////////////////////////////////////////////////////////////////// + + public abstract string real_command { get; construct set; } + + ///////////////////////////////////////////////////////////////////// + /// The command displayed to the user. It should be a bit more + /// beautiful than the real_command. + ///////////////////////////////////////////////////////////////////// + + public abstract string display_command { get; } + + ///////////////////////////////////////////////////////////////////// + /// The name of the Action. + ///////////////////////////////////////////////////////////////////// + + public virtual string name { get; set; } + + ///////////////////////////////////////////////////////////////////// + /// The name of the icon of this Action. It should be in the users + /// current icon theme. + ///////////////////////////////////////////////////////////////////// + + public virtual string icon { get; set; } + + ///////////////////////////////////////////////////////////////////// + /// True, if this Action is the quickAction of the associated Pie. + /// The quickAction of a Pie gets executed when the users clicks on + /// the center of a Pie. + ///////////////////////////////////////////////////////////////////// + + public virtual bool is_quickaction { get; set; } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, initializes all members. + ///////////////////////////////////////////////////////////////////// + + public Action(string name, string icon, bool is_quickaction) { + GLib.Object(name : name, icon : icon, is_quickaction : is_quickaction); + } + + ///////////////////////////////////////////////////////////////////// + /// This one is called, when the user activates the Slice. + ///////////////////////////////////////////////////////////////////// + + public abstract void activate(uint32 time_stamp); +} + +} diff --git a/src/actions/actionRegistry.vala b/src/actions/actionRegistry.vala new file mode 100644 index 0000000..9a22cc7 --- /dev/null +++ b/src/actions/actionRegistry.vala @@ -0,0 +1,219 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// A which has knowledge on all possible acion types. +///////////////////////////////////////////////////////////////////////// + +public class ActionRegistry : GLib.Object { + + ///////////////////////////////////////////////////////////////////// + /// A list containing all available Action types. + ///////////////////////////////////////////////////////////////////// + + public static Gee.ArrayList<string> types { get; private set; } + + ///////////////////////////////////////////////////////////////////// + /// A map associating a displayable name for each Action, + /// whether it has a custom icon and a name for the pies.conf + /// file with it's type. + ///////////////////////////////////////////////////////////////////// + + public static Gee.HashMap<string, TypeDescription?> descriptions { get; private set; } + + ///////////////////////////////////////////////////////////////////// + /// A helper class storing information on a Action type. + ///////////////////////////////////////////////////////////////////// + + public class TypeDescription { + public string name { get; set; default=""; } + public string icon { get; set; default=""; } + public string description { get; set; default=""; } + public string id { get; set; default=""; } + public bool icon_name_editable { get; set; default=false; } + } + + ///////////////////////////////////////////////////////////////////// + /// Registers all Action types. + ///////////////////////////////////////////////////////////////////// + + public static void init() { + types = new Gee.ArrayList<string>(); + descriptions = new Gee.HashMap<string, TypeDescription?>(); + + TypeDescription type_description; + + types.add(typeof(AppAction).name()); + type_description = AppAction.register(); + descriptions.set(typeof(AppAction).name(), type_description); + + types.add(typeof(KeyAction).name()); + type_description = KeyAction.register(); + descriptions.set(typeof(KeyAction).name(), type_description); + + types.add(typeof(PieAction).name()); + type_description = PieAction.register(); + descriptions.set(typeof(PieAction).name(), type_description); + + types.add(typeof(UriAction).name()); + type_description = UriAction.register(); + descriptions.set(typeof(UriAction).name(), type_description); + } + + ///////////////////////////////////////////////////////////////////// + /// Creates a new Action from the given type name. + ///////////////////////////////////////////////////////////////////// + + public static Action? create_action(string type_id, string name, string icon, string command, bool quickaction) { + switch (type_id) { + case "app": return new AppAction(name, icon, command, quickaction); + case "key": return new KeyAction(name, icon, command, quickaction); + case "uri": return new UriAction(name, icon, command, quickaction); + case "pie": return new PieAction(command, quickaction); + } + + return null; + } + + ///////////////////////////////////////////////////////////////////// + /// A helper method which creates an Action, appropriate for the + /// given URI. This can result in an UriAction or in an AppAction, + /// depending on the Type of the URI. + ///////////////////////////////////////////////////////////////////// + + public static Action? new_for_uri(string uri, string? name = null) { + var file = GLib.File.new_for_uri(uri); + var scheme = file.get_uri_scheme(); + + string final_icon = ""; + string final_name = file.get_basename(); + + switch (scheme) { + case "application": + var file_name = uri.split("//")[1]; + + var desktop_file = GLib.File.new_for_path("/usr/share/applications/" + file_name); + if (desktop_file.query_exists()) + return new_for_desktop_file(desktop_file.get_path()); + + break; + + case "trash": + final_icon = "user-trash"; + final_name = _("Trash"); + break; + + case "http": case "https": + final_icon = "www"; + final_name = get_domain_name(uri); + break; + + case "ftp": case "sftp": + final_icon = "folder-remote"; + final_name = get_domain_name(uri); + break; + + default: + try { + var info = file.query_info("*", GLib.FileQueryInfoFlags.NONE); + + if (info.get_content_type() == "application/x-desktop") + return new_for_desktop_file(file.get_parse_name()); + + // search for an appropriate icon + var icon = info.get_icon(); + final_icon = Icon.get_icon_name(icon); + + } catch (GLib.Error e) { + warning(e.message); + } + + break; + } + + if (!Gtk.IconTheme.get_default().has_icon(final_icon)) + final_icon = "stock_unknown"; + + if (name != null) + final_name = name; + + return new UriAction(final_name, final_icon, uri); + } + + ///////////////////////////////////////////////////////////////////// + /// A helper method which creates an AppAction for given AppInfo. + ///////////////////////////////////////////////////////////////////// + + public static Action? new_for_app_info(GLib.AppInfo info) { + // get icon + var icon = info.get_icon(); + + return new AppAction(info.get_display_name(), Icon.get_icon_name(icon), info.get_commandline()); + } + + ///////////////////////////////////////////////////////////////////// + /// A helper method which creates an AppAction for given *.desktop + /// file. + ///////////////////////////////////////////////////////////////////// + + public static Action? new_for_desktop_file(string file_name) { + // check whether its a desktop file to open one of Gnome-Pie's pies + if (file_name.has_prefix(Paths.launchers)) { + string id = file_name.substring((long)file_name.length - 11, 3); + return new PieAction(id); + } + + var info = new DesktopAppInfo.from_filename(file_name); + return new_for_app_info(info); + } + + ///////////////////////////////////////////////////////////////////// + /// A helper method which creates an AppAction for given mime type. + ///////////////////////////////////////////////////////////////////// + + public static Action? default_for_mime_type(string type) { + var info = AppInfo.get_default_for_type(type, false); + return new_for_app_info(info); + } + + ///////////////////////////////////////////////////////////////////// + /// A helper method which creates an AppAction for given uri scheme. + ///////////////////////////////////////////////////////////////////// + + public static Action? default_for_uri(string uri) { + var info = AppInfo.get_default_for_uri_scheme(uri); + return new_for_app_info(info); + } + + ///////////////////////////////////////////////////////////////////// + /// Returns for example www.google.com when http://www.google.de/?q=h + /// is given. + ///////////////////////////////////////////////////////////////////// + + private static string get_domain_name(string url) { + int domain_end = url.index_of_char('/', 7); + int domain_begin = url.index_of_char('/', 0) + 2; + + if (domain_begin < domain_end) return url.substring(domain_begin, domain_end-domain_begin); + + return url; + } +} + +} diff --git a/src/actions/appAction.vala b/src/actions/appAction.vala new file mode 100644 index 0000000..e1ca3a2 --- /dev/null +++ b/src/actions/appAction.vala @@ -0,0 +1,76 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// This type of Action launches an application or a custom command. +///////////////////////////////////////////////////////////////////////// + +public class AppAction : Action { + + ///////////////////////////////////////////////////////////////////// + /// Used to register this type of Action. It sets the display name + /// for this Action, whether it has a custom Icon/Name and the string + /// used in the pies.conf file for this kind of Actions. + ///////////////////////////////////////////////////////////////////// + + public static ActionRegistry.TypeDescription register() { + var description = new ActionRegistry.TypeDescription(); + description.name = _("Launch application"); + description.icon = "application-x-executable"; + description.description = _("Executes the given command."); + description.icon_name_editable = true; + description.id = "app"; + return description; + } + + ///////////////////////////////////////////////////////////////////// + /// Stores the command line. + ///////////////////////////////////////////////////////////////////// + + public override string real_command { get; construct set; } + + ///////////////////////////////////////////////////////////////////// + /// Simply returns the real_command. No beautification. + ///////////////////////////////////////////////////////////////////// + + public override string display_command { get {return real_command;} } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, initializes all members. + ///////////////////////////////////////////////////////////////////// + + public AppAction(string name, string icon, string command, bool is_quickaction = false) { + GLib.Object(name : name, icon : icon, real_command : command, is_quickaction : is_quickaction); + } + + ///////////////////////////////////////////////////////////////////// + /// Launches the desired command. + ///////////////////////////////////////////////////////////////////// + + public override void activate(uint32 time_stamp) { + try{ + var item = GLib.AppInfo.create_from_commandline(this.real_command, null, GLib.AppInfoCreateFlags.NONE); + item.launch(null, null); + } catch (Error e) { + warning(e.message); + } + } +} + +} diff --git a/src/actions/keyAction.vala b/src/actions/keyAction.vala new file mode 100644 index 0000000..cbe8b6e --- /dev/null +++ b/src/actions/keyAction.vala @@ -0,0 +1,81 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// This type of Action "presses" a key stroke. +///////////////////////////////////////////////////////////////////////// + +public class KeyAction : Action { + + ///////////////////////////////////////////////////////////////////// + /// Used to register this type of Action. It sets the display name + /// for this Action, whether it has a custom Icon/Name and the string + /// used in the pies.conf file for this kind of Actions. + ///////////////////////////////////////////////////////////////////// + + public static ActionRegistry.TypeDescription register() { + var description = new ActionRegistry.TypeDescription(); + description.name = _("Press hotkey"); + description.icon = "preferences-desktop-keyboard-shortcuts"; + description.description = _("Simulates the activation of a hotkey."); + description.icon_name_editable = true; + description.id = "key"; + return description; + } + + ///////////////////////////////////////////////////////////////////// + /// Stores the accelerator of this action. + ///////////////////////////////////////////////////////////////////// + + public override string real_command { get; construct set; } + + ///////////////////////////////////////////////////////////////////// + /// Returns a human readable form of the accelerator. + ///////////////////////////////////////////////////////////////////// + + public override string display_command { get {return key.label;} } + + ///////////////////////////////////////////////////////////////////// + /// The simulated key which gets 'pressed' on execution. + ///////////////////////////////////////////////////////////////////// + + public Key key { get; set; } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, initializes all members. + ///////////////////////////////////////////////////////////////////// + + public KeyAction(string name, string icon, string command, bool is_quickaction = false) { + GLib.Object(name : name, icon : icon, real_command : command, is_quickaction : is_quickaction); + } + + construct { + this.key = new Key.from_string(real_command); + } + + ///////////////////////////////////////////////////////////////////// + /// Presses the desired key. + ///////////////////////////////////////////////////////////////////// + + public override void activate(uint32 time_stamp) { + key.press(); + } +} + +} diff --git a/src/actions/pieAction.vala b/src/actions/pieAction.vala new file mode 100644 index 0000000..931c9d3 --- /dev/null +++ b/src/actions/pieAction.vala @@ -0,0 +1,103 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// This Action opens another pie. +///////////////////////////////////////////////////////////////////////// + +public class PieAction : Action { + + ///////////////////////////////////////////////////////////////////// + /// Used to register this type of Action. It sets the display name + /// for this Action, whether it has a custom Icon/Name and the string + /// used in the pies.conf file for this kind of Actions. + ///////////////////////////////////////////////////////////////////// + + public static ActionRegistry.TypeDescription register() { + var description = new ActionRegistry.TypeDescription(); + description.name = _("Open Pie"); + description.icon = "gnome-pie"; + description.description = _("Opens another Pie of Gnome-Pie. You may create sub menus this way."); + description.icon_name_editable = false; + description.id = "pie"; + return description; + } + + ///////////////////////////////////////////////////////////////////// + /// Stores the ID of the referenced Pie. + ///////////////////////////////////////////////////////////////////// + + public override string real_command { get; construct set; } + + ///////////////////////////////////////////////////////////////////// + /// Returns the name of the referenced Pie. + ///////////////////////////////////////////////////////////////////// + + public override string display_command { get {return name;} } + + ///////////////////////////////////////////////////////////////////// + /// Returns the name of the referenced Pie. + ///////////////////////////////////////////////////////////////////// + + public override string name { + get { + var referee = PieManager.all_pies[real_command]; + if (referee != null) { + owned_name = "↪" + referee.name; + return owned_name; + } + return ""; + } + protected set {} + } + + private string owned_name; + + ///////////////////////////////////////////////////////////////////// + /// Returns the icon of the referenced Pie. + ///////////////////////////////////////////////////////////////////// + + public override string icon { + get { + var referee = PieManager.all_pies[real_command]; + if (referee != null) + return referee.icon; + return ""; + } + protected set {} + } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, initializes all members. + ///////////////////////////////////////////////////////////////////// + + public PieAction(string id, bool is_quickaction = false) { + GLib.Object(name : "", icon : "", real_command : id, is_quickaction : is_quickaction); + } + + ///////////////////////////////////////////////////////////////////// + /// Opens the desired Pie. + ///////////////////////////////////////////////////////////////////// + + public override void activate(uint32 time_stamp) { + PieManager.open_pie(real_command); + } +} + +} diff --git a/src/actions/sigAction.vala b/src/actions/sigAction.vala new file mode 100644 index 0000000..bf9374d --- /dev/null +++ b/src/actions/sigAction.vala @@ -0,0 +1,63 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// This type of Action can't be selected by the user, therefore there is +/// no register() method for this class. But it may be useful for +/// ActionGroups: It emits a signal on activation. +///////////////////////////////////////////////////////////////////////// + +public class SigAction : Action { + + ///////////////////////////////////////////////////////////////////// + /// This signal is emitted on activation. + ///////////////////////////////////////////////////////////////////// + + public signal void activated(uint32 time_stamp); + + ///////////////////////////////////////////////////////////////////// + /// This may store something useful. + ///////////////////////////////////////////////////////////////////// + + public override string real_command { get; construct set; } + + ///////////////////////////////////////////////////////////////////// + /// Only for inheritance... Greetings to Liskov. + ///////////////////////////////////////////////////////////////////// + + public override string display_command { get {return real_command;} } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, initializes all members. + ///////////////////////////////////////////////////////////////////// + + public SigAction(string name, string icon, string command, bool is_quickaction = false) { + GLib.Object(name : name, icon : icon, real_command : command, is_quickaction : is_quickaction); + } + + ///////////////////////////////////////////////////////////////////// + /// Emits the signal on activation. + ///////////////////////////////////////////////////////////////////// + + public override void activate(uint32 time_stamp) { + this.activated(time_stamp); + } +} + +} diff --git a/src/actions/uriAction.vala b/src/actions/uriAction.vala new file mode 100644 index 0000000..d0a41b8 --- /dev/null +++ b/src/actions/uriAction.vala @@ -0,0 +1,77 @@ +///////////////////////////////////////////////////////////////////////// +// Copyright (c) 2011-2015 by Simon Schneegans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or (at +// your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +///////////////////////////////////////////////////////////////////////// + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// This type of Action opens the default application for an URI. +///////////////////////////////////////////////////////////////////////// + +public class UriAction : Action { + + ///////////////////////////////////////////////////////////////////// + /// Used to register this type of Action. It sets the display name + /// for this Action, whether it has a custom Icon/Name and the string + /// used in the pies.conf file for this kind of Actions. + ///////////////////////////////////////////////////////////////////// + + public static ActionRegistry.TypeDescription register() { + var description = new ActionRegistry.TypeDescription(); + description.name = _("Open URI"); + description.icon = "web-browser"; + description.description = _("Opens a given location. You may use URL's or files paths."); + description.icon_name_editable = true; + description.id = "uri"; + return description; + } + + ///////////////////////////////////////////////////////////////////// + /// The URI of this Action. + ///////////////////////////////////////////////////////////////////// + + public override string real_command { get; construct set; } + + ///////////////////////////////////////////////////////////////////// + /// Returns only the real URI. An URI can't be beautified. + ///////////////////////////////////////////////////////////////////// + + public override string display_command { get {return real_command;} } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, initializes all members. + ///////////////////////////////////////////////////////////////////// + + public UriAction(string name, string icon, string command, bool is_quickaction = false) { + GLib.Object(name : name, icon : icon, + real_command : command.has_prefix("www") ? "http://" + command : command, + is_quickaction : is_quickaction); + } + + ///////////////////////////////////////////////////////////////////// + /// Opens the default application for the URI. + ///////////////////////////////////////////////////////////////////// + + public override void activate(uint32 time_stamp) { + try{ + GLib.AppInfo.launch_default_for_uri(real_command, null); + } catch (Error e) { + warning(e.message); + } + } +} + +} |