~kay20/switchboard/plug_search

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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/***
BEGIN LICENSE
Copyright (C) 2011-2012 Avi Romanoff <aviromanoff@gmail.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 2.1, as published
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
END LICENSE
***/

namespace Switchboard {

    public class CategoryView : Gtk.Grid {

        public enum Columns {
            ICON,
            TEXT,
            DESCRIPTION,
            VISIBLE,
            PLUG,
            N_COLUMNS
        }

        const int ITEM_WIDTH = 96;

        public signal void plug_selected (Switchboard.Plug plug);

        Gtk.IconTheme theme = Gtk.IconTheme.get_default ();
        Gtk.Grid personal_grid;
        Gtk.Grid hardware_grid;
        Gtk.Grid network_grid;
        Gtk.Grid system_grid;

        public Gtk.IconView personal_iconview;
        public Gtk.IconView hardware_iconview;
        public Gtk.IconView network_iconview;
        public Gtk.IconView system_iconview;

        private string? plug_to_open = null;

        public CategoryView (string? plug_to_open = null) {
            this.plug_to_open = plug_to_open;

            setup_category (Switchboard.Plug.Category.PERSONAL, 0);
            setup_category (Switchboard.Plug.Category.HARDWARE, 1);
            setup_category (Switchboard.Plug.Category.NETWORK, 2);
            setup_category (Switchboard.Plug.Category.SYSTEM, 3);
        }

        private void setup_category (Switchboard.Plug.Category category, int i) {
            var category_label = new Gtk.Label (get_category_name (category));
            category_label.get_style_context ().add_class ("category-label");

            category_label.margin_left = 12;
            category_label.margin_right = 8;
            ((Gtk.Misc) category_label).xalign = 0.02f;
            category_label.use_markup = true;

            var category_plugs = setup_icon_view ();
            category_plugs.get_style_context ().remove_class (Gtk.STYLE_CLASS_VIEW);

            var grid = new Gtk.Grid ();

            // Always add a Seperator
            var h_separator = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
            h_separator.set_hexpand (true);
            h_separator.margin_right = 12;
            grid.attach (category_label, 0, 0, 1, 1);
            grid.attach (h_separator, 1, 0, 1, 1);

            grid.attach (category_plugs, 0, 1, 2, 1);
            switch (category) {
                case Switchboard.Plug.Category.PERSONAL:
                    personal_iconview = category_plugs;
                    personal_grid = grid;
                    break;
                case Switchboard.Plug.Category.HARDWARE:
                    hardware_iconview = category_plugs;
                    hardware_grid = grid;
                    break;
                case Switchboard.Plug.Category.NETWORK:
                    network_iconview = category_plugs;
                    network_grid = grid;
                    break;
                case Switchboard.Plug.Category.SYSTEM:
                    system_iconview = category_plugs;
                    system_grid = grid;
                    break;
            }

            category_plugs.focus_out_event.connect ((e) => {
                category_plugs.unselect_all ();

                return false;
            });

            category_plugs.focus_in_event.connect ((e) => {
                Gtk.TreePath path;

                if (!category_plugs.get_cursor (out path, null)) {
                    path = new Gtk.TreePath.from_indices (0, -1);
                }
                category_plugs.select_path (path);

                return false;
            });

            category_plugs.keynav_failed.connect ((direction) => {
                Gtk.IconView new_view = null;
                Gtk.TreePath path = null;
                Gtk.TreePath selected_path = null;
                int smallest_distance = 1000;
                Gtk.TreeIter iter;
                int distance;

                if (direction == Gtk.DirectionType.UP) {
                    new_view = get_prev_icon_view (category);
                    if (new_view != null && category_plugs.get_cursor (out path, null)) {
                        var current_column = category_plugs.get_item_column (path);
                        var model = new_view.get_model ();

                        model.get_iter_first (out iter);
                        do {
                            path = model.get_path (iter);
                            var next_column = new_view.get_item_column (path);
                            distance = (next_column - current_column).abs ();
                            if (distance <= smallest_distance) {
                                selected_path = path;
                                smallest_distance = distance;
                            }
                        } while (model.iter_next (ref iter));
                    }
                } else if (direction == Gtk.DirectionType.DOWN) {
                    new_view = get_next_icon_view (category);
                    if (new_view != null && category_plugs.get_cursor (out path, null)) {
                        var current_column = category_plugs.get_item_column (path);
                        var model = new_view.get_model ();

                        model.get_iter_first (out iter);
                        do {
                            path = model.get_path (iter);
                            var next_column = new_view.get_item_column (path);
                            distance = (next_column - current_column).abs ();
                            if (distance < smallest_distance) {
                                selected_path = path;
                                smallest_distance = distance;
                            }
                        } while (model.iter_next (ref iter));
                    }
                }

                if (new_view != null) {
                    new_view.set_cursor (selected_path, null, false);
                    new_view.grab_focus ();
                    return true;
                }

                return false;
            });

            attach (grid, 0, i, 1, 1);
        }

        public async void load_default_plugs () {
            var plugsmanager = Switchboard.PlugsManager.get_default ();
            plugsmanager.plug_added.connect ((plug) => {
                plug.visibility_changed.connect (() => plug_visibility_changed (plug));
                add_plug (plug);
            });

            Idle.add (() => {
                foreach (var plug in plugsmanager.get_plugs ()) {
                    plug.visibility_changed.connect (() => plug_visibility_changed (plug));
                    if (plug.can_show == true) {
                        add_plug (plug);
                    }
                }

                return false;
            });
        }

        private Gtk.IconView setup_icon_view () {
            var store = new Gtk.ListStore (Columns.N_COLUMNS, typeof (Gdk.Pixbuf), typeof (string),
                typeof(string), typeof(bool), typeof(Switchboard.Plug));
            store.set_sort_column_id (1, Gtk.SortType.ASCENDING);
            store.set_sort_column_id (1, Gtk.SortType.ASCENDING);

            var filtered = new Gtk.TreeModelFilter (store, null);
            filtered.set_visible_column (3);
            filtered.refilter ();

            var category_plugs = new Gtk.IconView.with_model (filtered);
            category_plugs.set_item_width (ITEM_WIDTH);
            category_plugs.set_text_column (Columns.TEXT);
            category_plugs.set_pixbuf_column (Columns.ICON);
            category_plugs.set_tooltip_column (Columns.DESCRIPTION);
            category_plugs.set_hexpand (true);
            category_plugs.set_selection_mode (Gtk.SelectionMode.SINGLE);
            category_plugs.set_activate_on_single_click (true);

            category_plugs.item_activated.connect (on_item_activated);
            var cellrenderer = (Gtk.CellRendererText)category_plugs.get_cells ().nth_data (0);
            cellrenderer.wrap_mode = Pango.WrapMode.WORD;
            cellrenderer.ellipsize_set = true;

            return category_plugs;
        }

        private void plug_visibility_changed (Switchboard.Plug plug) {
            if (plug.can_show == true) {
                add_plug (plug);
            }
        }

        public void add_plug (Switchboard.Plug plug) {
            if (plug.can_show == false)
                return;
            Gtk.TreeIter root;
            Gtk.TreeModelFilter model_filter;

            switch (plug.category) {
                case Switchboard.Plug.Category.PERSONAL:
                    model_filter = (Gtk.TreeModelFilter)personal_iconview.get_model ();
                    personal_grid.show_all ();
                    break;
                case Switchboard.Plug.Category.HARDWARE:
                    model_filter = (Gtk.TreeModelFilter)hardware_iconview.get_model ();
                    hardware_grid.show_all ();
                    break;
                case Switchboard.Plug.Category.NETWORK:
                    model_filter = (Gtk.TreeModelFilter)network_iconview.get_model ();
                    network_grid.show_all ();
                    break;
                case Switchboard.Plug.Category.SYSTEM:
                    model_filter = (Gtk.TreeModelFilter)system_iconview.get_model ();
                    system_grid.show_all ();
                    break;
                default:
                    return;
            }

            var store = model_filter.child_model as Gtk.ListStore;
            Gdk.Pixbuf icon_pixbuf = null;
            try {
                // FIXME: if we get no icon, we probably dont want that oneā€¦
                icon_pixbuf = theme.load_icon (plug.icon, 32, Gtk.IconLookupFlags.GENERIC_FALLBACK);
            } catch {
                critical ("Unable to load plug %s's icon: %s", plug.display_name, plug.icon);
                return;
            }

            store.append (out root);
            store.set (root, Columns.ICON, icon_pixbuf, Columns.TEXT, plug.display_name,
                Columns.DESCRIPTION, plug.description, Columns.VISIBLE, true, Columns.PLUG, plug);
            unowned SwitchboardApp app = (SwitchboardApp) GLib.Application.get_default ();
            app.search_box.sensitive = true;
            filter_plugs (app.search_box.get_text ());
#if HAVE_UNITY
            app.update_libunity_quicklist ();
#endif
            if (plug_to_open != null && plug_to_open != "") {
                if (plug_to_open.has_suffix (plug.code_name)) {
                    app.load_plug (plug);
                    plug_to_open = "";
                }
            }
        }


        // focus to first visible item
        public void grab_focus_first_icon_view () {
                Gtk.TreePath first_path = null;

                if (get_first_visible_path (personal_iconview, out first_path)) {
                    personal_iconview.grab_focus ();
                } else if (get_first_visible_path (hardware_iconview, out first_path)) {
                    hardware_iconview.grab_focus ();
                } else if (get_first_visible_path (network_iconview, out first_path)) {
                    network_iconview.grab_focus ();
                } else if (get_first_visible_path (system_iconview, out first_path)) {
                    system_iconview.grab_focus ();
                }
        }

        // activate first visible item
        public void activate_first_item () {
                Gtk.TreePath first_path = null;

                if (get_first_visible_path (personal_iconview, out first_path)) {
                    personal_iconview.item_activated (first_path);
                } else if (get_first_visible_path (hardware_iconview, out first_path)){
                    hardware_iconview.item_activated (first_path);
                } else if (get_first_visible_path (network_iconview, out first_path)){
                    network_iconview.item_activated (first_path);
                } else if (get_first_visible_path (system_iconview, out first_path)){
                    system_iconview.item_activated (first_path);
                }
        }

        private bool get_first_visible_path (Gtk.IconView iv, out Gtk.TreePath path) {
            Gtk.TreePath end;

            return (iv.get_visible_range (out path, out end));
        }

        public void filter_plugs (string filter) {

            var any_found = false;

            if (search_by_category (filter, Switchboard.Plug.Category.PERSONAL))
                any_found = true;
            if (search_by_category (filter, Switchboard.Plug.Category.HARDWARE))
                any_found = true;
            if (search_by_category (filter, Switchboard.Plug.Category.NETWORK))
                any_found = true;
            if (search_by_category (filter, Switchboard.Plug.Category.SYSTEM))
                any_found = true;

            unowned SwitchboardApp app = (SwitchboardApp) GLib.Application.get_default ();
            if (!any_found) {
                app.show_alert (_("No settings found"), _("Try changing your search terms"), Gtk.MessageType.INFO);
            } else {
                app.hide_alert ();
            }
        }

        private bool search_by_category (string filter, Plug.Category category) {

            Gtk.TreeModelFilter model_filter;
            Gtk.Widget grid;

            switch (category) {
                case Switchboard.Plug.Category.PERSONAL:
                    model_filter = (Gtk.TreeModelFilter)personal_iconview.get_model ();
                    grid = personal_grid;
                    break;
                case Switchboard.Plug.Category.HARDWARE:
                    model_filter = (Gtk.TreeModelFilter)hardware_iconview.get_model ();
                    grid = hardware_grid;
                    break;
                case Switchboard.Plug.Category.NETWORK:
                    model_filter = (Gtk.TreeModelFilter)network_iconview.get_model ();
                    grid = network_grid;
                    break;
                case Switchboard.Plug.Category.SYSTEM:
                    model_filter = (Gtk.TreeModelFilter)system_iconview.get_model ();
                    grid = system_grid;
                    break;
                default:
                    return false;
            }

            var store = model_filter.child_model as Gtk.ListStore;
            int shown = 0;
            store.foreach ((model, path, iter) => {
                string title;

                store.get (iter, Columns.TEXT, out title);

                if (filter.down () in title.down ()) {
                    store.set_value (iter, Columns.VISIBLE, true);
                    shown++;
                } else {
                    store.set_value (iter, Columns.VISIBLE, false);
                }

                return false;
            });

            if (shown == 0) {
                grid.hide ();
                return false;
            } else {
                grid.show_all ();
                return true;
            }
        }

        public void recalculate_columns () {
            int columns = personal_iconview.get_columns ();
            columns = int.max (columns, hardware_iconview.get_columns ());
            columns = int.max (columns, network_iconview.get_columns ());
            columns = int.max (columns, system_iconview.get_columns ());
            personal_iconview.set_columns (columns);
            hardware_iconview.set_columns (columns);
            network_iconview.set_columns (columns);
            system_iconview.set_columns (columns);
        }

        private void on_item_activated (Gtk.IconView view, Gtk.TreePath path) {
            GLib.Value plug;
            Gtk.TreeIter selected_plug;
            var store = view.get_model ();

            store.get_iter (out selected_plug, path);
            store.get_value (selected_plug, Columns.PLUG, out plug);

            plug_selected ((Switchboard.Plug) plug.get_object ());

            view.unselect_path (path);
        }

        public static string? get_category_name (Switchboard.Plug.Category category) {
            switch (category) {
                case Plug.Category.PERSONAL:
                    return _("Personal");
                case Plug.Category.HARDWARE:
                    return _("Hardware");
                case Plug.Category.NETWORK:
                    return _("Network and Wireless");
                case Plug.Category.SYSTEM:
                    return _("Administration");
            }

            return null;
        }

        private Gtk.IconView? get_next_icon_view (Switchboard.Plug.Category category) {
            if (category == Plug.Category.PERSONAL) {
                if (hardware_iconview.is_visible ())
                    return hardware_iconview;
                else
                    category = Plug.Category.HARDWARE;
            }

            if (category == Plug.Category.HARDWARE) {
                if (network_iconview.is_visible ())
                    return network_iconview;
                else
                    category = Plug.Category.NETWORK;
            }

            if (category == Plug.Category.NETWORK) {
                if (system_iconview.is_visible ())
                    return system_iconview;
                else
                    category = Plug.Category.SYSTEM;
            }

            return null;
        }

        private Gtk.IconView? get_prev_icon_view (Switchboard.Plug.Category category) {
            if (category == Plug.Category.SYSTEM) {
                if (network_iconview.is_visible ())
                    return network_iconview;
                else
                    category = Plug.Category.NETWORK;
            }

            if (category == Plug.Category.NETWORK) {
                if (hardware_iconview.is_visible ())
                    return hardware_iconview;
                else
                    category = Plug.Category.HARDWARE;
            }

            if (category == Plug.Category.HARDWARE) {
                if (personal_iconview.is_visible ())
                    return personal_iconview;
                else
                    category = Plug.Category.SYSTEM;
            }

            return null;
        }
    }
}