summaryrefslogtreecommitdiff
path: root/src/pies/pieManager.vala
blob: d9bf5b72841fd7eb2e461b45f354a61864ef676d (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/////////////////////////////////////////////////////////////////////////
// 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 static class which stores all Pies. It can be used to add, delete
/// and open Pies.
/////////////////////////////////////////////////////////////////////////

public class PieManager : GLib.Object {

    /////////////////////////////////////////////////////////////////////
    /// A map of all Pies. It contains both, dynamic and persistent Pies.
    /// They are associated to their ID's.
    /////////////////////////////////////////////////////////////////////

    public static Gee.HashMap<string, Pie?> all_pies { get; private set; }

    /////////////////////////////////////////////////////////////////////
    /// Stores all PieWindows which are currently opened. Should be
    /// rarely more than two...
    /////////////////////////////////////////////////////////////////////

    public static Gee.HashSet<PieWindow?> opened_windows { get; private set; }

    /////////////////////////////////////////////////////////////////////
    /// Stores all global hotkeys.
    /////////////////////////////////////////////////////////////////////

    public static BindingManager bindings;

    /////////////////////////////////////////////////////////////////////
    /// True, if any pie has the current focus. If it is closing this
    /// will be false already.
    /////////////////////////////////////////////////////////////////////

    private static bool a_pie_is_active = false;

    /////////////////////////////////////////////////////////////////////
    /// Storing the position of the last Pie. Used for subpies, which are
    /// opened at their parents location.
    /////////////////////////////////////////////////////////////////////

    private static int last_x = 0;
    private static int last_y = 0;

    /////////////////////////////////////////////////////////////////////
    /// Initializes all Pies. They are loaded from the pies.conf file.
    /////////////////////////////////////////////////////////////////////

    public static void init() {
        all_pies = new Gee.HashMap<string, Pie?>();
        opened_windows = new Gee.HashSet<PieWindow?>();
        bindings = new BindingManager();

        // load all Pies from th pies.conf file
        Pies.load();

        // open the according pie if it's hotkey is pressed
        bindings.on_press.connect((id) => {
            open_pie(id);
        });
    }

    /////////////////////////////////////////////////////////////////////
    /// Opens the Pie with the given ID, if it exists.
    /////////////////////////////////////////////////////////////////////

    public static void open_pie(string id) {
        if (!a_pie_is_active) {
            Pie? pie = all_pies[id];

            if (pie != null) {

                a_pie_is_active = true;

                //change WM_CLASS so launchers can track windows properly
                Gdk.set_program_class("gnome-pie-" + id);

                var window = new PieWindow();
                window.load_pie(pie);

                window.on_closed.connect(() => {
                    opened_windows.remove(window);
                    if (opened_windows.size == 0) {
                        Icon.clear_cache();
                    }
                });

                window.on_closing.connect(() => {
                    window.get_center_pos(out last_x, out last_y);
                    a_pie_is_active = false;
                });

                opened_windows.add(window);

                window.open();

                //restore default WM_CLASS after window open
                Gdk.set_program_class("Gnome-pie");

            } else {
                warning("Failed to open pie with ID \"" + id + "\": ID does not exist!");
            }
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Prints the names of all pies with their IDs.
    /////////////////////////////////////////////////////////////////////

    public static void print_ids() {
        foreach(var pie in all_pies.entries) {
            if (pie.value.id.length == 3) {
                message(pie.value.id + " " + pie.value.name);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the hotkey which the Pie with the given ID is bound to.
    /////////////////////////////////////////////////////////////////////

    public static string get_accelerator_of(string id) {
        return bindings.get_accelerator_of(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns a human-readable version of the hotkey which the Pie
    /// with the given ID is bound to.
    /////////////////////////////////////////////////////////////////////

    public static string get_accelerator_label_of(string id) {
        return bindings.get_accelerator_label_of(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Bind the Pie with the given ID to the given trigger.
    /////////////////////////////////////////////////////////////////////

    public static void bind_trigger(Trigger trigger, string id) {
        bindings.unbind(id);
        bindings.bind(trigger, id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns true if the pie with the given id is in turbo mode.
    /////////////////////////////////////////////////////////////////////

    public static bool get_is_turbo(string id) {
        return bindings.get_is_turbo(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns true if the pie with the given id opens in the middle of
    /// the screen.
    /////////////////////////////////////////////////////////////////////

    public static bool get_is_centered(string id) {
        return bindings.get_is_centered(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns true if the mouse pointer will be warped to the center of
    /// the pie.
    /////////////////////////////////////////////////////////////////////

    public static bool get_is_warp(string id) {
        return bindings.get_is_warp(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns true if the pie with the given id is auto shaped
    /////////////////////////////////////////////////////////////////////

    public static bool get_is_auto_shape(string id) {
        return bindings.get_is_auto_shape(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the prefered pie shape number
    /////////////////////////////////////////////////////////////////////

    public static int get_shape_number(string id) {
        return bindings.get_shape_number(id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the name of the Pie with the given ID.
    /////////////////////////////////////////////////////////////////////

    public static string get_name_of(string id) {
        Pie? pie = all_pies[id];
        if (pie == null) return "";
        else             return pie.name;
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the name ID of the Pie bound to the given Trigger.
    /// Returns "" if there is nothing bound to this trigger.
    /////////////////////////////////////////////////////////////////////

    public static string get_assigned_id(Trigger trigger) {
        return bindings.get_assigned_id(trigger);
    }

    /////////////////////////////////////////////////////////////////////
    /// Creates a new Pie which is displayed in the configuration dialog
    /// and gets saved.
    /////////////////////////////////////////////////////////////////////

    public static Pie create_persistent_pie(string name, string icon_name, Trigger? hotkey, string? desired_id = null) {
        Pie pie = create_pie(name, icon_name, 100, 999, desired_id);

        if (hotkey != null) bindings.bind(hotkey, pie.id);

        create_launcher(pie.id);

        return pie;
    }

    /////////////////////////////////////////////////////////////////////
    /// Creates a new Pie which is not displayed in the configuration
    /// dialog and is not saved.
    /////////////////////////////////////////////////////////////////////

    public static Pie create_dynamic_pie(string name, string icon_name, string? desired_id = null) {
        return create_pie(name, icon_name, 1000, 9999, desired_id);
    }

    /////////////////////////////////////////////////////////////////////
    /// Adds a new Pie. Can't be accesd from outer scope. Use
    /// create_persistent_pie or create_dynamic_pie instead.
    /////////////////////////////////////////////////////////////////////

    private static Pie create_pie(string name, string icon_name, int min_id, int max_id, string? desired_id = null) {
         var random = new GLib.Rand();

        string final_id;

        if (desired_id == null)
            final_id = random.int_range(min_id, max_id).to_string();
        else {
            final_id = desired_id;
            final_id.canon("0123456789", '_');
            final_id = final_id.replace("_", "");

            int id = int.parse(final_id);

            if (id < min_id || id > max_id) {
                final_id = random.int_range(min_id, max_id).to_string();
                warning("The ID for pie \"" + name + "\" should be in range %u - %u! Using \"" + final_id + "\" instead of \"" + desired_id + "\"...", min_id, max_id);
            }
        }

        if (all_pies.has_key(final_id)) {
            var tmp = final_id;
            var id_number = int.parse(final_id) + 1;
            if (id_number == max_id+1) id_number = min_id;
            final_id = id_number.to_string();
            warning("Trying to add pie \"" + name + "\": ID \"" + tmp + "\" already exists! Using \"" + final_id + "\" instead...");
            return create_pie(name, icon_name, min_id, max_id, final_id);
        }

        Pie pie = new Pie(final_id, name, icon_name);
        all_pies.set(final_id, pie);

        return pie;
    }

    /////////////////////////////////////////////////////////////////////
    /// Removes the Pie with the given ID if it exists. Additionally it
    /// unbinds it's global hotkey.
    /////////////////////////////////////////////////////////////////////

    public static void remove_pie(string id) {
        if (all_pies.has_key(id)) {
            all_pies[id].on_remove();
            all_pies.unset(id);
            bindings.unbind(id);

            if (id.length == 3)
                remove_launcher(id);
        }
        else {
            warning("Failed to remove pie with ID \"" + id + "\": ID does not exist!");
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Creates a desktop file for which opens the Pie with given ID.
    /////////////////////////////////////////////////////////////////////

    public static void create_launcher(string id) {
        if (all_pies.has_key(id)) {
            Pie? pie = all_pies[id];

            string launcher_entry =
                "#!/usr/bin/env xdg-open\n" +
                "[Desktop Entry]\n" +
                "Name=%s\n".printf(pie.name) +
                "Exec=%s -o %s\n".printf(Paths.executable, pie.id) +
                "Encoding=UTF-8\n" +
                "Type=Application\n" +
                "Icon=%s\n".printf(pie.icon) +
                "StartupWMClass=gnome-pie-%s\n".printf(pie.id);

            // create the launcher file
            string launcher = Paths.launchers + "/%s.desktop".printf(pie.id);

            try {
                FileUtils.set_contents(launcher, launcher_entry);
                FileUtils.chmod(launcher, 0755);
            } catch (Error e) {
                warning(e.message);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Deletes the desktop file for the Pie with the given ID.
    /////////////////////////////////////////////////////////////////////

    private static void remove_launcher(string id) {
        string launcher = Paths.launchers + "/%s.desktop".printf(id);
        if (FileUtils.test(launcher, FileTest.EXISTS)) {
            FileUtils.remove(launcher);
        }
    }
}

}