summaryrefslogtreecommitdiff
path: root/ui/gtk3/indicator.vala
blob: 013b1c48b5ce201e7e52422ecad8d8c6397734f5 (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
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
/* vim:set et sts=4 sw=4:
 *
 * ibus - The Input Bus
 *
 * Copyright(c) 2015 Takao Fujiwara <takao.fujiwara1@gmail.com>
 * Copyright(c) 2015 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

/* This class extends AppIndicator because
 * AppIndicator misses "Activate" dbus method in the definition
 * for left click on the indicator.
 */

public extern string _notification_item;
public extern string _notification_watcher;

class Indicator : IBus.Service
{
    public string id { get; construct; }
    public string category_s { get; construct; }
    public string status_s { get; set; }
    public string icon_name { get; set; }
    public string icon_desc { get; set; }
    public string attention_icon_name { get; set; }
    public string attention_icon_desc { get; set; }
    public string title { get; set; }
    public string icon_theme_path { get; set; }
    public bool   connected { get; set; }
    public string label_s { get; set; }
    public string label_guide_s { get; set; }
    public uint32 ordering_index { get; set; }

    public enum Category {
        APPLICATION_STATUS,
        COMMUNICATIONS,
        SYSTEM_SERVICES,
        HARDWARE,
        OTHER;

        public string to_nick() {
            switch(this) {
            case APPLICATION_STATUS: return "ApplicationStatus";
            case COMMUNICATIONS: return "Communications";
            case SYSTEM_SERVICES: return "SystemServices";
            case HARDWARE: return "Hardware";
            case OTHER: return "Other";
            default: assert_not_reached();
            }
        }
    }

    public enum Status {
        PASSIVE,
        ACTIVE,
        ATTENTION;

        public string to_nick() {
            switch(this) {
            case PASSIVE: return "Passive";
            case ACTIVE: return "Active";
            case ATTENTION: return "NeedsAttention";
            default: assert_not_reached();
            }
        }
    }

    private const string DEFAULT_ITEM_PATH = "/org/ayatana/NotificationItem";
    private const string NOTIFICATION_ITEM_DBUS_IFACE =
            "org.kde.StatusNotifierItem";
    private const string NOTIFICATION_WATCHER_DBUS_IFACE =
            "org.kde.StatusNotifierWatcher";
    private const string NOTIFICATION_WATCHER_DBUS_ADDR =
            "org.kde.StatusNotifierWatcher";
    private const string NOTIFICATION_WATCHER_DBUS_OBJ =
            "/StatusNotifierWatcher";

    private GLib.DBusNodeInfo m_watcher_node_info;
    private unowned GLib.DBusInterfaceInfo m_watcher_interface_info;
    private GLib.DBusProxy m_proxy;
    private int m_context_menu_x;
    private int m_context_menu_y;
    private int m_activate_menu_x;
    private int m_activate_menu_y;

    public Indicator(string id,
                     GLib.DBusConnection connection,
                     Category category = Category.OTHER) {
        string path = DEFAULT_ITEM_PATH + "/" + id;
        path = path.delimit("-", '_');

        // AppIndicator.set_category() converts enum value to string internally.
        GLib.Object(object_path: path,
                    id: id,
                    connection: connection,
                    category_s: category.to_nick());
        this.status_s = Status.PASSIVE.to_nick();
        this.icon_name = "";
        this.icon_desc = "";
        this.title = "";
        this.icon_theme_path = "";
        this.attention_icon_name = "";
        this.attention_icon_desc = "";
        this.label_s = "";
        this.label_guide_s = "";
        unregister(connection);
        add_interfaces(_notification_item);
        try {
            if (!register(connection))
                return;
        } catch (GLib.Error e) {
            warning("Failed to register the application indicator xml: " +
                    e.message);
            return;
        }

        try {
            m_watcher_node_info =
                    new GLib.DBusNodeInfo.for_xml(_notification_watcher);
        } catch (GLib.Error e) {
            warning("Failed to create dbus node info: " + e.message);
            return;
        }
        m_watcher_interface_info =
                m_watcher_node_info.lookup_interface(
                        NOTIFICATION_WATCHER_DBUS_IFACE);
        check_connect();
    }

    private void check_connect() {
        if (m_proxy == null) {
            GLib.DBusProxy.new.begin(
                    connection,
                    GLib.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES |
                            GLib.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS,
                    m_watcher_interface_info,
                    NOTIFICATION_WATCHER_DBUS_ADDR,
                    NOTIFICATION_WATCHER_DBUS_OBJ,
                    NOTIFICATION_WATCHER_DBUS_IFACE,
                    null,
                    (obj, res) => {
                            bus_watcher_ready(obj, res);
                    });
        } else {
            bus_watcher_ready(null, null);
        }
    }

    private void bus_watcher_ready(GLib.Object? obj, GLib.AsyncResult? res) {
        if (res != null) {
            try {
                m_proxy = GLib.DBusProxy.new.end(res);
            } catch (GLib.IOError e) {
                warning("Failed to call dbus proxy: " + e.message);
                return;
            }

            m_proxy.notify["g-name-owner"].connect((obj, pspec) => {
                    var name = m_proxy.get_name_owner();
                    if (name != null)
                        check_connect();
            });
        }

        var name = m_proxy.get_name_owner();
        // KDE panel does not run yet if name == null
        if (name == null)
            return;

        m_proxy.call.begin("RegisterStatusNotifierItem",
                           new GLib.Variant("(s)", this.object_path),
                           GLib.DBusCallFlags.NONE,
                           -1,
                           null,
                           (p_obj, p_res) => {
                                   try {
                                       m_proxy.call.end(p_res);
                                       registered_status_notifier_item();
                                   } catch (GLib.Error e) {
                        warning("Failed to call " +
                                "RegisterStatusNotifierItem: " +
                                e.message);
                                   }
                           });
    }

    private void _context_menu_cb(GLib.DBusConnection       connection,
                                  GLib.Variant              parameters,
                                  GLib.DBusMethodInvocation invocation) {
        GLib.Variant var_x = parameters.get_child_value(0);
        GLib.Variant var_y = parameters.get_child_value(1);
        m_context_menu_x = var_x.get_int32();
        m_context_menu_y = var_y.get_int32();
        context_menu(2, 0);
    }

    private void _activate_menu_cb(GLib.DBusConnection       connection,
                                   GLib.Variant              parameters,
                                   GLib.DBusMethodInvocation invocation) {
        GLib.Variant var_x = parameters.get_child_value(0);
        GLib.Variant var_y = parameters.get_child_value(1);
        m_activate_menu_x = var_x.get_int32();
        m_activate_menu_y = var_y.get_int32();
        activate();
    }

    private GLib.Variant _get_id(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.id);
    }

    private GLib.Variant _get_category(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.category_s);
    }

    private GLib.Variant _get_status(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.status_s);
    }

    private GLib.Variant _get_icon_name(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.icon_name);
    }

    private GLib.Variant _get_icon_desc(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.icon_desc);
    }

    private GLib.Variant _get_attention_icon_name(GLib.DBusConnection
                                                            connection) {
        return new GLib.Variant.string(this.attention_icon_name);
    }

    private GLib.Variant _get_attention_icon_desc(GLib.DBusConnection
                                                            connection) {
        return new GLib.Variant.string(this.attention_icon_desc);
    }

    private GLib.Variant _get_title(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.title);
    }

    private GLib.Variant _get_icon_theme_path(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.icon_theme_path);
    }

    private GLib.Variant? _get_menu(GLib.DBusConnection connection) {
        return null;
    }

    private GLib.Variant _get_xayatana_label(GLib.DBusConnection connection) {
        return new GLib.Variant.string(this.label_s);
    }

    private GLib.Variant _get_xayatana_label_guide(GLib.DBusConnection
                                                             connection) {
        return new GLib.Variant.string(this.label_guide_s);
    }

    private GLib.Variant _get_xayatana_ordering_index(GLib.DBusConnection
                                                             connection) {
        return new GLib.Variant.uint32(this.ordering_index);
    }

    public override void service_method_call(GLib.DBusConnection
                                                                connection,
                                             string             sender,
                                             string             object_path,
                                             string             interface_name,
                                             string             method_name,
                                             GLib.Variant       parameters,
                                             GLib.DBusMethodInvocation
                                                                invocation) {
        GLib.return_if_fail (object_path == this.object_path);
        GLib.return_if_fail (interface_name == NOTIFICATION_ITEM_DBUS_IFACE);

        if (method_name == "Activate") {
            _activate_menu_cb(connection, parameters, invocation);
            return;
        }
        if (method_name == "ContextMenu") {
            _context_menu_cb(connection, parameters, invocation);
            return;
        }

        warning("service_method_call() does not handle the method: " +
                method_name);
    }

    public override GLib.Variant service_get_property(GLib.DBusConnection
                                                               connection,
                                                      string   sender,
                                                      string   object_path,
                                                      string   interface_name,
                                                      string   property_name) {
        GLib.return_val_if_fail (object_path == this.object_path, null);
        GLib.return_val_if_fail (
                interface_name == NOTIFICATION_ITEM_DBUS_IFACE,
                null);

        if (property_name == "Id")
            return _get_id(connection);
        if (property_name == "Category")
            return _get_category(connection);
        if (property_name == "Status")
            return _get_status(connection);
        if (property_name == "IconName")
            return _get_icon_name(connection);
        if (property_name == "IconAccessibleDesc")
            return _get_icon_desc(connection);
        if (property_name == "AttentionIconName")
            return _get_attention_icon_name(connection);
        if (property_name == "AttentionAccessibleDesc")
            return _get_attention_icon_desc(connection);
        if (property_name == "Title")
            return _get_title(connection);
        if (property_name == "IconThemePath")
            return _get_icon_theme_path(connection);
        if (property_name == "Menu")
            return _get_menu(connection);
        if (property_name == "XAyatanaLabel")
            return _get_xayatana_label(connection);
        if (property_name == "XAyatanaLabelGuide")
            return _get_xayatana_label_guide(connection);
        if (property_name == "XAyatanaOrderingIndex")
            return _get_xayatana_ordering_index(connection);

        warning("service_get_property() does not handle the property: " +
                property_name);

        return null;
    }

    public override bool service_set_property(GLib.DBusConnection
                                                           connection,
                                              string       sender,
                                              string       object_path,
                                              string       interface_name,
                                              string       property_name,
                                              GLib.Variant value) {
        return false;
    }

    // AppIndicator.set_status() converts enum value to string internally.
    public void set_status(Status status) {
        string status_s = status.to_nick();
        if (this.status_s == status_s)
            return;
        this.status_s = status_s;
        if (this.connection == null)
            return;
        try {
            this.connection.emit_signal(null,
                                        this.object_path,
                                        NOTIFICATION_ITEM_DBUS_IFACE,
                                        "NewStatus",
                                        new GLib.Variant("(s)", status_s));
        } catch(GLib.Error e) {
            warning("Unable to send signal for NewIcon: %s", e.message);
        }
    }

    // AppIndicator.set_icon() is deprecated.
    public void set_icon_full(string icon_name, string? icon_desc) {
        bool changed = false;
        if (this.icon_name != icon_name) {
            this.icon_name = icon_name;
            changed = true;
        }
        if (this.icon_desc != icon_desc) {
            this.icon_desc = icon_desc;
            changed = true;
        }
        if (!changed)
            return;
        if (this.connection == null)
            return;
        try {
            this.connection.emit_signal(null,
                                        this.object_path,
                                        NOTIFICATION_ITEM_DBUS_IFACE,
                                        "NewIcon",
                                        null);
        } catch(GLib.Error e) {
            warning("Unable to send signal for NewIcon: %s", e.message);
        }
    }

    public void position_context_menu(Gtk.Menu menu,
                                      out int  x,
                                      out int  y,
                                      out bool push_in) {
        x = m_context_menu_x;
        y = m_context_menu_y;
        push_in = false;
    }

    public void position_activate_menu(Gtk.Menu menu,
                                       out int  x,
                                       out int  y,
                                       out bool push_in) {
        x = m_activate_menu_x;
        y = m_activate_menu_y;
        push_in = false;
    }

    public signal void context_menu(uint button, uint activate_time);
    public signal void activate();
    public signal void registered_status_notifier_item();
}