summaryrefslogtreecommitdiff
path: root/src/actionGroups/windowListGroup.vala
blob: ba5ea2bfb32373dbe79b7ed5e2886949d3703dc6 (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
/////////////////////////////////////////////////////////////////////////
// Copyright 2011-2018 Simon Schneegans
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////

namespace GnomePie {

/////////////////////////////////////////////////////////////////////
/// This group displays a list of all running application windows.
/////////////////////////////////////////////////////////////////////

public class WindowListGroup : ActionGroup {

    /////////////////////////////////////////////////////////////////////
    /// Used to register this type of ActionGroup. It sets the display
    /// name for this ActionGroup, it's icon name and the string used in
    /// the pies.conf file for this kind of ActionGroups.
    /////////////////////////////////////////////////////////////////////

    public static GroupRegistry.TypeDescription? register() {
        if (GLib.Environment.get_variable("XDG_SESSION_TYPE") == "wayland") {
            warning("The WindowList slice group is not supported on Wayland.");
            return null;
        }

        var description = new GroupRegistry.TypeDescription();
        description.name = _("Group: Window List");
        description.icon = "preferences-system-windows";
        description.description = _("Shows a Slice for each of your opened Windows. Almost like Alt-Tab.");
        description.id = "window_list";
        return description;
    }

    public bool current_workspace_only { get; set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// Cached icon names loaded from .desktop files.
    /////////////////////////////////////////////////////////////////////

    private static Gee.HashMap<string, string> cached_icon_name { private get; private set; }

    /////////////////////////////////////////////////////////////////////
    /// Wnck's Screen object, to control the list of opened windows.
    /////////////////////////////////////////////////////////////////////

    private Wnck.Screen screen;

    /////////////////////////////////////////////////////////////////////
    /// C'tor, initializes all members.
    /////////////////////////////////////////////////////////////////////

    public WindowListGroup(string parent_id) {
        GLib.Object(parent_id : parent_id);

        screen = Wnck.Screen.get_default();
        WindowListGroup.cached_icon_name = new Gee.HashMap<string, string>();

        Gtk.IconTheme.get_default().changed.connect(() => {
            WindowListGroup.cached_icon_name = new Gee.HashMap<string, string>();
            create_actions_for_all_windows();
        });

        screen.active_workspace_changed.connect(create_actions_for_all_windows);
        screen.window_opened.connect(create_action);
        screen.window_closed.connect(remove_action);
    }

    /////////////////////////////////////////////////////////////////////
    /// This one is called when the ActionGroup is saved.
    /////////////////////////////////////////////////////////////////////

    public override void on_save(Xml.TextWriter writer) {
        base.on_save(writer);
        writer.write_attribute("current_workspace_only", this.current_workspace_only.to_string());
    }

    /////////////////////////////////////////////////////////////////////
    /// This one is called when the ActionGroup is loaded.
    /////////////////////////////////////////////////////////////////////

    public override void on_load(Xml.Node* data) {
        for (Xml.Attr* attribute = data->properties; attribute != null; attribute = attribute->next) {
            string attr_name = attribute->name.down();
            string attr_content = attribute->children->content;

            if (attr_name == "current_workspace_only") {
                current_workspace_only = bool.parse(attr_content);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Remove a Action for a given window
    /////////////////////////////////////////////////////////////////////

    private void remove_action(Wnck.Window window) {
        if (!window.is_skip_pager() && !window.is_skip_tasklist())
            foreach (Action action in actions)
                if (window.get_xid() == uint64.parse(action.real_command)) {
                    actions.remove(action);
                    break;
                }
    }

    /////////////////////////////////////////////////////////////////////
    /// Create Action's for all currently opened windows.
    /////////////////////////////////////////////////////////////////////

    private void create_actions_for_all_windows() {
        delete_all();

        foreach (var window in screen.get_windows())
            create_action(window);
    }

    /////////////////////////////////////////////////////////////////////
    /// Create a Action for a given opened window
    /////////////////////////////////////////////////////////////////////

    private void create_action(Wnck.Window window) {
        if (!window.is_skip_pager() && !window.is_skip_tasklist()
            && (!current_workspace_only || (window.get_workspace() != null
            && window.get_workspace() == screen.get_active_workspace()))) {

            var name = window.get_name();
            var icon_name = get_icon_name(window);
            var xid = "%lu".printf(window.get_xid());

            if (name.length > 30) {
                name = name.substring(0, 30) + "...";
            }

            var action = new SigAction(name, icon_name, xid);
            this.add_action(action);

            window.name_changed.connect(() => {
                action.name = window.get_name();
            });

            action.activated.connect((time_stamp) => {
                if (window.get_workspace() != null) {
                    //select the workspace
                    if (window.get_workspace() != window.get_screen().get_active_workspace()) {
                        window.get_workspace().activate(time_stamp);
                    }
                }

                if (window.is_minimized()) {
                    window.unminimize(time_stamp);
                }

                window.activate_transient(time_stamp);
            });
        }
    }

    private string get_icon_name(Wnck.Window window) {
        string icon_name = "";

        #if HAVE_BAMF
            var xid = (uint32) window.get_xid();
            Bamf.Matcher bamf_matcher = Bamf.Matcher.get_default();
            Bamf.Application app = bamf_matcher.get_application_for_xid(xid);
            string desktop_file = null;

            if (app != null)
                desktop_file = app.get_desktop_file();

            if (desktop_file != null) {
                if (WindowListGroup.cached_icon_name.has_key(desktop_file))
                    icon_name = WindowListGroup.cached_icon_name.get(desktop_file);
                else {
                    try {
                        var file = new KeyFile();
                        file.load_from_file(desktop_file, 0);

                        if (file.has_key(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_ICON)) {
                            icon_name = file.get_locale_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_ICON);
                            WindowListGroup.cached_icon_name.set(desktop_file, icon_name);
                        }
                    } catch (GLib.KeyFileError e) {
                        error("%s", e.message);
                    } catch (GLib.FileError e) {
                        error("%s", e.message);
                    }
                }
            } else {
                var application = window.get_application();
                icon_name = application.get_icon_name().down();
            }
        #else
            var application = window.get_application();
            icon_name = application.get_icon_name().down();
        #endif

        return icon_name;
    }
}

}