summaryrefslogtreecommitdiff
path: root/src/actions/actionRegistry.vala
blob: 579fc93ae8cef1436db38150f225389411ccef81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2016 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;
    }
}

}