aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/unitymenumodel.cpp
blob: ba1fac47e6e38242b747cc40da5b374c4df0f973 (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
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program 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; version 3.
 *
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Lars Uebernickel <lars.uebernickel@canonical.com>
 */

#include "unitymenumodel.h"

extern "C" {
  #include "gtk/gtkactionmuxer.h"
  #include "gtk/gtkmenutracker.h"
}

G_DEFINE_QUARK (UNITY_MENU_MODEL, unity_menu_model)
G_DEFINE_QUARK (UNITY_SUBMENU_MODEL, unity_submenu_model)

class UnityMenuModelPrivate
{
public:
    UnityMenuModelPrivate(UnityMenuModel *model);

    static UnityMenuModelPrivate * forSubMenu(UnityMenuModel *model, GtkMenuTrackerItem *item);

    ~UnityMenuModelPrivate();
    int nrItems();
    QVariant data(int position, int role);
    void activate(int position);
    UnityMenuModel * submenu(int position);

    void clearItems(bool resetModel=true);
    void clearName();
    void updateActions();
    void updateMenuModel();

    UnityMenuModel *model;
    GtkActionMuxer *muxer;
    GtkMenuTracker *menutracker;
    GSequence *items;
    GDBusConnection *connection;
    QByteArray busName;
    QByteArray nameOwner;
    guint nameWatchId;
    QByteArray actionObjectPath;
    QByteArray menuObjectPath;

    static void freeMenuItem(gpointer data, gpointer user_data);
    static void nameAppeared(GDBusConnection *connection, const gchar *name, const gchar *owner, gpointer user_data);
    static void nameVanished(GDBusConnection *connection, const gchar *name, gpointer user_data);
    static void menuItemInserted(GtkMenuTrackerItem *item, gint position, gpointer user_data);
    static void menuItemRemoved(gint position, gpointer user_data);
    static void menuItemChanged(GObject *object, GParamSpec *pspec, gpointer user_data);
};

/*
 * Same as g_sequence_foreach_range, but calls func with the GSequenceIter
 * instead of the item.
 */
static void
g_sequence_foreach_iter_range (GSequenceIter *begin,
                               GSequenceIter *end,
                               GFunc          func,
                               gpointer       user_data)
{
    GSequenceIter *it;

    for (it = begin; it != end; it = g_sequence_iter_next (it))
        func (it, user_data);
}

UnityMenuModelPrivate::UnityMenuModelPrivate(UnityMenuModel *model)
{
    this->model = model;
    this->menutracker = NULL;
    this->nameWatchId = 0;

    this->muxer = gtk_action_muxer_new ();
    g_object_set_qdata (G_OBJECT (this->muxer), unity_menu_model_quark (), model);

    this->items = g_sequence_new (NULL);
}

UnityMenuModelPrivate * UnityMenuModelPrivate::forSubMenu(UnityMenuModel *model, GtkMenuTrackerItem *item)
{
    UnityMenuModelPrivate *priv = new UnityMenuModelPrivate(model);

    priv->menutracker = gtk_menu_tracker_new_for_item_submenu (item, menuItemInserted, menuItemRemoved, priv);

    return priv;
}

UnityMenuModelPrivate::~UnityMenuModelPrivate()
{
    this->clearItems(false);

    if (this->menutracker)
        gtk_menu_tracker_free (this->menutracker);

    if (this->muxer)
        g_object_unref (this->muxer);

    g_clear_object (&this->connection);

    if (this->nameWatchId)
        g_bus_unwatch_name (this->nameWatchId);
}

int UnityMenuModelPrivate::nrItems()
{
    return g_sequence_get_length (this->items);
}

QVariant UnityMenuModelPrivate::data(int position, int role)
{
    GtkMenuTrackerItem *item;

    item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (this->items, position));

    switch (role) {
        case UnityMenuModel::LabelRole:
            return gtk_menu_tracker_item_get_label (item);

        case UnityMenuModel::SensitiveRole:
            return gtk_menu_tracker_item_get_sensitive (item);

        case UnityMenuModel::IsSeparatorRole:
            return gtk_menu_tracker_item_get_is_separator (item);

        default:
            return QVariant();
    }
}

void UnityMenuModelPrivate::activate(int position)
{
    GtkMenuTrackerItem *item;

    item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (this->items, position));
    gtk_menu_tracker_item_activated (item);
}

UnityMenuModel * UnityMenuModelPrivate::submenu(int position)
{
    GSequenceIter *it;
    GtkMenuTrackerItem *item;
    UnityMenuModel *model;

    it = g_sequence_get_iter_at_pos (this->items, position);
    if (g_sequence_iter_is_end (it))
        return NULL;

    item = (GtkMenuTrackerItem *) g_sequence_get (it);
    if (!gtk_menu_tracker_item_get_has_submenu (item))
        return NULL;

    model = (UnityMenuModel *) g_object_get_qdata (G_OBJECT (item), unity_submenu_model_quark ());
    if (model == NULL) {
        model = new UnityMenuModel(this->model);
        model->priv = UnityMenuModelPrivate::forSubMenu(model, item);
        g_object_set_qdata (G_OBJECT (item), unity_submenu_model_quark (), model);
    }

    return model;
}

void UnityMenuModelPrivate::freeMenuItem (gpointer data, gpointer user_data)
{
    GSequenceIter *it = (GSequenceIter *) data;
    GtkMenuTrackerItem *item;

    item = (GtkMenuTrackerItem *) g_sequence_get (it);
    g_signal_handlers_disconnect_by_func (item, (gpointer) menuItemChanged, it);
    g_object_unref (item);
}

void UnityMenuModelPrivate::clearItems(bool resetModel)
{
    GSequenceIter *begin;
    GSequenceIter *end;

    if (resetModel)
        model->beginResetModel();

    begin = g_sequence_get_begin_iter (this->items);
    end = g_sequence_get_end_iter (this->items);
    g_sequence_foreach_iter_range (begin, end, freeMenuItem, NULL);
    g_sequence_remove_range (begin, end);

    if (resetModel)
        model->endResetModel();
}

void UnityMenuModelPrivate::clearName()
{
    this->clearItems();

    this->nameOwner = QByteArray();

    this->updateActions();
    this->updateMenuModel();
}

void UnityMenuModelPrivate::updateActions()
{
    if (!this->nameOwner.isEmpty()) {
        GDBusActionGroup *actions;

        actions = g_dbus_action_group_get (this->connection, this->nameOwner, this->actionObjectPath.constData());
        gtk_action_muxer_insert (this->muxer, "indicator", G_ACTION_GROUP (actions));

        g_object_unref (actions);
    }
    else {
        gtk_action_muxer_remove (this->muxer, "indicator");
    }
}

void UnityMenuModelPrivate::updateMenuModel()
{
    this->clearItems();
    g_clear_pointer (&this->menutracker, gtk_menu_tracker_free);

    if (!this->nameOwner.isEmpty()) {
        GDBusMenuModel *menu;

        menu = g_dbus_menu_model_get (this->connection, this->nameOwner, this->menuObjectPath.constData());
        this->menutracker = gtk_menu_tracker_new (GTK_ACTION_OBSERVABLE (this->muxer),
                                                  G_MENU_MODEL (menu), TRUE, "indicator",
                                                  menuItemInserted, menuItemRemoved, this);

        g_object_unref (menu);
    }
}

void UnityMenuModelPrivate::nameAppeared(GDBusConnection *connection, const gchar *name, const gchar *owner, gpointer user_data)
{
    UnityMenuModelPrivate *priv = (UnityMenuModelPrivate *)user_data;

    priv->connection = (GDBusConnection *) g_object_ref (connection);
    priv->nameOwner = owner;

    priv->updateActions();
    priv->updateMenuModel();
}

void UnityMenuModelPrivate::nameVanished(GDBusConnection *connection, const gchar *name, gpointer user_data)
{
    UnityMenuModelPrivate *priv = (UnityMenuModelPrivate *)user_data;

    priv->clearName();
}

void UnityMenuModelPrivate::menuItemInserted(GtkMenuTrackerItem *item, gint position, gpointer user_data)
{
    UnityMenuModelPrivate *priv = (UnityMenuModelPrivate *)user_data;
    GSequenceIter *it;

    priv->model->beginInsertRows(QModelIndex(), position, position);

    it = g_sequence_get_iter_at_pos (priv->items, position);
    g_signal_connect (item, "notify", G_CALLBACK (menuItemChanged), it);
    g_sequence_insert_before (it, g_object_ref (item));

    priv->model->endInsertRows();
}

void UnityMenuModelPrivate::menuItemRemoved(gint position, gpointer user_data)
{
    UnityMenuModelPrivate *priv = (UnityMenuModelPrivate *)user_data;
    GSequenceIter *it;

    priv->model->beginRemoveRows(QModelIndex(), position, position);

    it = g_sequence_get_iter_at_pos (priv->items, position);
    freeMenuItem ((gpointer) it, NULL);
    g_sequence_remove (it);

    priv->model->endRemoveRows();
}

void UnityMenuModelPrivate::menuItemChanged(GObject *object, GParamSpec *pspec, gpointer user_data)
{
    GSequenceIter *it = (GSequenceIter *) user_data;
    GtkMenuTrackerItem *item;
    GtkActionObservable *muxer;
    UnityMenuModel *model;
    gint position;

    item = (GtkMenuTrackerItem *) g_sequence_get (it);
    muxer = _gtk_menu_tracker_item_get_observable (item);
    model = (UnityMenuModel *) g_object_get_qdata (G_OBJECT (muxer), unity_menu_model_quark ());
    position = g_sequence_iter_get_position (it);

    Q_EMIT model->dataChanged(model->index(position, 0), model->index(position, 0));
}

UnityMenuModel::UnityMenuModel(QObject *parent):
    QAbstractListModel(parent)
{
    priv = new UnityMenuModelPrivate(this);
}

UnityMenuModel::~UnityMenuModel()
{
    delete priv;
}

QByteArray UnityMenuModel::busName() const
{
    return priv->busName;
}

void UnityMenuModel::setBusName(const QByteArray &name)
{
    priv->clearName();

    if (priv->nameWatchId)
        g_bus_unwatch_name (priv->nameWatchId);

    priv->nameWatchId = g_bus_watch_name (G_BUS_TYPE_SESSION, name.constData(), G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
                                          UnityMenuModelPrivate::nameAppeared, UnityMenuModelPrivate::nameVanished,
                                          priv, NULL);
}

QByteArray UnityMenuModel::actionObjectPath() const
{
    return priv->actionObjectPath;
}

void UnityMenuModel::setActionObjectPath(const QByteArray &path)
{
    priv->actionObjectPath = path;
    priv->updateActions();
}

QByteArray UnityMenuModel::menuObjectPath() const
{
    return priv->menuObjectPath;
}

void UnityMenuModel::setMenuObjectPath(const QByteArray &path)
{
    priv->menuObjectPath = path;
    priv->updateMenuModel();
}

int UnityMenuModel::rowCount(const QModelIndex &parent) const
{
    return priv && !parent.isValid() ? priv->nrItems() : 0;
}

int UnityMenuModel::columnCount(const QModelIndex &parent) const
{
    return 1;
}

QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
{
    return priv ? priv->data(index.row(), role) : QVariant();
}

QModelIndex UnityMenuModel::index(int row, int column, const QModelIndex &parent) const
{
    return createIndex(row, column);
}

QModelIndex UnityMenuModel::parent(const QModelIndex &index) const
{
    return QModelIndex();
}

QHash<int, QByteArray> UnityMenuModel::roleNames() const
{
    QHash<int, QByteArray> names;

    names[LabelRole] = "label";
    names[ActionRole] = "action";
    names[SensitiveRole] = "sensitive";
    names[IsSeparatorRole] = "isSeparator";

    return names;
}

QObject * UnityMenuModel::submenu(int position)
{
    return priv ? priv->submenu(position) : NULL;
}

void UnityMenuModel::activate(int index)
{
    if (priv)
        priv->activate(index);
}