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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
|
/*
* 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"
#include "converter.h"
#include "actionstateparser.h"
#include "unitymenumodelevents.h"
#include "unitymenuaction.h"
#include "unitymenuactionevents.h"
#include <QIcon>
#include <QQmlComponent>
#include <QCoreApplication>
extern "C" {
#include "gtk/gtkactionmuxer.h"
#include "gtk/gtkmenutracker.h"
#include "gtk/gtksimpleactionobserver.h"
}
G_DEFINE_QUARK (UNITY_MENU_MODEL, unity_menu_model)
G_DEFINE_QUARK (UNITY_SUBMENU_MODEL, unity_submenu_model)
G_DEFINE_QUARK (UNITY_MENU_ITEM_EXTENDED_ATTRIBUTES, unity_menu_item_extended_attributes)
G_DEFINE_QUARK (UNITY_MENU_ACTION, unity_menu_action)
enum MenuRoles {
LabelRole = Qt::DisplayRole + 1,
SensitiveRole,
IsSeparatorRole,
IconRole,
TypeRole,
ExtendedAttributesRole,
ActionRole,
ActionStateRole,
IsCheckRole,
IsRadioRole,
IsToggledRole
};
class UnityMenuModelPrivate
{
public:
UnityMenuModelPrivate(UnityMenuModel *model);
UnityMenuModelPrivate(const UnityMenuModelPrivate& other, UnityMenuModel *model);
~UnityMenuModelPrivate();
void clearItems(bool resetModel=true);
void clearName();
void updateActions();
void updateMenuModel();
QVariant itemState(GtkMenuTrackerItem *item);
UnityMenuModel *model;
GtkActionMuxer *muxer;
GtkMenuTracker *menutracker;
GSequence *items;
GDBusConnection *connection;
QByteArray busName;
QByteArray nameOwner;
guint nameWatchId;
QVariantMap actions;
QByteArray menuObjectPath;
QHash<QByteArray, int> roles;
ActionStateParser* actionStateParser;
QHash<UnityMenuAction*, GtkSimpleActionObserver*> registeredActions;
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);
static void registeredActionAdded(GtkSimpleActionObserver *observer_item,
const gchar *action_name,
gboolean enabled,
GVariant *state);
static void registeredActionEnabledChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, gboolean enabled);
static void registeredActionStateChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, GVariant *state);
static void registeredActionRemoved(GtkSimpleActionObserver *observer_item, const gchar *action_name);
};
void menu_item_free (gpointer data)
{
GtkMenuTrackerItem *item = (GtkMenuTrackerItem *) data;
g_signal_handlers_disconnect_by_func (item, (gpointer) UnityMenuModelPrivate::menuItemChanged, NULL);
g_object_unref (item);
}
UnityMenuModelPrivate::UnityMenuModelPrivate(UnityMenuModel *model)
{
this->model = model;
this->menutracker = NULL;
this->connection = NULL;
this->nameWatchId = 0;
this->actionStateParser = new ActionStateParser(model);
this->muxer = gtk_action_muxer_new ();
this->items = g_sequence_new (menu_item_free);
}
UnityMenuModelPrivate::UnityMenuModelPrivate(const UnityMenuModelPrivate& other, UnityMenuModel *model)
{
this->model = model;
this->menutracker = NULL;
this->connection = NULL;
this->nameWatchId = 0;
this->actionStateParser = new ActionStateParser(model);
this->muxer = GTK_ACTION_MUXER( g_object_ref(other.muxer));
this->items = g_sequence_new (menu_item_free);
}
UnityMenuModelPrivate::~UnityMenuModelPrivate()
{
this->clearItems(false);
g_clear_pointer (&this->menutracker, gtk_menu_tracker_free);
g_clear_object (&this->muxer);
g_clear_object (&this->connection);
QHash<UnityMenuAction*, GtkSimpleActionObserver*>::const_iterator it = this->registeredActions.constBegin();
for (; it != this->registeredActions.constEnd(); ++it) {
g_object_unref(it.value());
}
this->registeredActions.clear();
if (this->nameWatchId)
g_bus_unwatch_name (this->nameWatchId);
}
void UnityMenuModelPrivate::clearItems(bool resetModel)
{
UnityMenuModelClearEvent ummce(resetModel);
QCoreApplication::sendEvent(model, &ummce);
}
void UnityMenuModelPrivate::clearName()
{
this->clearItems();
this->nameOwner = QByteArray();
this->updateActions();
this->updateMenuModel();
}
void UnityMenuModelPrivate::updateActions()
{
Q_FOREACH (QString prefix, this->actions.keys())
gtk_action_muxer_remove (this->muxer, prefix.toUtf8());
if (this->nameOwner.isEmpty())
return;
for (QVariantMap::const_iterator it = this->actions.constBegin(); it != this->actions.constEnd(); ++it) {
GDBusActionGroup *actions;
actions = g_dbus_action_group_get (this->connection, this->nameOwner, it.value().toByteArray());
gtk_action_muxer_insert (this->muxer, it.key().toUtf8(), G_ACTION_GROUP (actions));
g_object_unref (actions);
}
}
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, NULL,
menuItemInserted, menuItemRemoved, this);
g_object_unref (menu);
}
}
QVariant UnityMenuModelPrivate::itemState(GtkMenuTrackerItem *item)
{
QVariant result;
GVariant *state = gtk_menu_tracker_item_get_action_state (item);
if (state != NULL) {
if (actionStateParser != NULL) {
result = actionStateParser->toQVariant(state);
}
g_variant_unref (state);
}
return result;
}
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;
UnityMenuModelAddRowEvent ummare(item, position);
QCoreApplication::sendEvent(priv->model, &ummare);
}
void UnityMenuModelPrivate::menuItemRemoved(gint position, gpointer user_data)
{
UnityMenuModelPrivate *priv = (UnityMenuModelPrivate *)user_data;
UnityMenuModelRemoveRowEvent ummrre(position);
QCoreApplication::sendEvent(priv->model, &ummrre);
}
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 (item), unity_menu_model_quark ());
position = g_sequence_iter_get_position (it);
UnityMenuModelDataChangeEvent ummdce(position);
QCoreApplication::sendEvent(model, &ummdce);
}
UnityMenuModel::UnityMenuModel(QObject *parent):
QAbstractListModel(parent)
{
priv = new UnityMenuModelPrivate(this);
}
UnityMenuModel::UnityMenuModel(const UnityMenuModelPrivate& other, QObject *parent):
QAbstractListModel(parent)
{
priv = new UnityMenuModelPrivate(other, 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);
priv->busName = name;
}
QVariantMap UnityMenuModel::actions() const
{
return priv->actions;
}
void UnityMenuModel::setActions(const QVariantMap &actions)
{
priv->actions = actions;
priv->updateActions();
}
QByteArray UnityMenuModel::menuObjectPath() const
{
return priv->menuObjectPath;
}
void UnityMenuModel::setMenuObjectPath(const QByteArray &path)
{
priv->menuObjectPath = path;
priv->updateMenuModel();
}
ActionStateParser* UnityMenuModel::actionStateParser() const
{
return priv->actionStateParser;
}
void UnityMenuModel::setActionStateParser(ActionStateParser* actionStateParser)
{
if (priv->actionStateParser != actionStateParser) {
if (priv->actionStateParser && priv->actionStateParser->parent() == this) {
delete priv->actionStateParser;
}
priv->actionStateParser = actionStateParser;
Q_EMIT actionStateParserChanged(actionStateParser);
}
}
int UnityMenuModel::rowCount(const QModelIndex &parent) const
{
return !parent.isValid() ? g_sequence_get_length (priv->items) : 0;
}
int UnityMenuModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
static QString iconUri(GIcon *icon)
{
QString uri;
if (G_IS_THEMED_ICON (icon)) {
const gchar* const* iconNames = g_themed_icon_get_names (G_THEMED_ICON (icon));
guint index = 0;
while(iconNames[index] != NULL) {
if (QIcon::hasThemeIcon(iconNames[index])) {
uri = QString("image://theme/") + iconNames[index];
break;
}
index++;
}
}
else if (G_IS_FILE_ICON (icon)) {
GFile *file;
file = g_file_icon_get_file (G_FILE_ICON (icon));
if (g_file_is_native (file)) {
gchar *fileuri;
fileuri = g_file_get_path (file);
uri = QString(fileuri);
g_free (fileuri);
}
}
else if (G_IS_BYTES_ICON (icon)) {
gsize size;
gconstpointer data;
gchar *base64;
data = g_bytes_get_data (g_bytes_icon_get_bytes (G_BYTES_ICON (icon)), &size);
base64 = g_base64_encode ((const guchar *) data, size);
uri = QString("data://");
uri.append (base64);
g_free (base64);
}
return uri;
}
QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
{
GtkMenuTrackerItem *item;
item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (priv->items, index.row()));
switch (role) {
case LabelRole:
return gtk_menu_tracker_item_get_label (item);
case SensitiveRole:
return gtk_menu_tracker_item_get_sensitive (item) == TRUE ? true : false;
case IsSeparatorRole:
return gtk_menu_tracker_item_get_is_separator (item) == TRUE ? true : false;
case IconRole: {
GIcon *icon = gtk_menu_tracker_item_get_icon (item);
if (icon) {
QString uri = iconUri(icon);
g_object_unref (icon);
return uri;
}
else
return QString();
}
case TypeRole: {
gchar *type;
if (gtk_menu_tracker_item_get_attribute (item, "x-canonical-type", "s", &type)) {
QVariant v(type);
g_free (type);
return v;
}
else
return QVariant();
}
case ExtendedAttributesRole: {
QVariantMap *map = (QVariantMap *) g_object_get_qdata (G_OBJECT (item), unity_menu_item_extended_attributes_quark ());
return map ? *map : QVariant();
}
case ActionRole:
return gtk_menu_tracker_item_get_action_name (item);
case ActionStateRole:
return priv->itemState(item);
case IsCheckRole:
return gtk_menu_tracker_item_get_role (item) == GTK_MENU_TRACKER_ITEM_ROLE_CHECK;
case IsRadioRole:
return gtk_menu_tracker_item_get_role (item) == GTK_MENU_TRACKER_ITEM_ROLE_RADIO;
case IsToggledRole:
return gtk_menu_tracker_item_get_toggled (item) == TRUE ? true : false;
default:
return 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();
}
#include <QtDebug>
QHash<int, QByteArray> UnityMenuModel::roleNames() const
{
QHash<int, QByteArray> names;
names[LabelRole] = "label";
names[SensitiveRole] = "sensitive";
names[IsSeparatorRole] = "isSeparator";
names[IconRole] = "icon";
names[TypeRole] = "type";
names[ExtendedAttributesRole] = "ext";
names[ActionRole] = "action";
names[ActionStateRole] = "actionState";
names[IsCheckRole] = "isCheck";
names[IsRadioRole] = "isRadio";
names[IsToggledRole] = "isToggled";
return names;
}
QObject * UnityMenuModel::submenu(int position, QQmlComponent* actionStateParser)
{
GSequenceIter *it;
GtkMenuTrackerItem *item;
UnityMenuModel *model;
it = g_sequence_get_iter_at_pos (priv->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(*priv, this);
if (actionStateParser) {
ActionStateParser* parser = qobject_cast<ActionStateParser*>(actionStateParser->create());
if (parser) {
model->setActionStateParser(parser);
}
}
model->priv->menutracker = gtk_menu_tracker_new_for_item_submenu (item,
UnityMenuModelPrivate::menuItemInserted,
UnityMenuModelPrivate::menuItemRemoved,
model->priv);
g_object_set_qdata (G_OBJECT (item), unity_submenu_model_quark (), model);
}
return model;
}
static void freeExtendedAttrs(gpointer data)
{
QVariantMap *extendedAttrs = (QVariantMap *) data;
delete extendedAttrs;
}
static QVariant attributeToQVariant(GVariant *value, const QString &type)
{
QVariant result;
if (type == "int") {
if (g_variant_is_of_type (value, G_VARIANT_TYPE_INT32))
result = QVariant(g_variant_get_int32(value));
}
if (type == "bool") {
if (g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN))
result = QVariant(g_variant_get_boolean(value));
}
else if (type == "string") {
if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING))
result = QVariant(g_variant_get_string(value, NULL));
}
if (type == "double") {
if (g_variant_is_of_type (value, G_VARIANT_TYPE_DOUBLE))
result = QVariant(g_variant_get_double(value));
}
else if (type == "icon") {
GIcon *icon = g_icon_deserialize (value);
if (icon) {
result = iconUri(icon);
g_object_unref (icon);
}
else {
result = QVariant("");
}
}
return result;
}
/* convert 'some-key' to 'someKey' or 'SomeKey'. (from dconf-qt) */
static QString qtify_name(const char *name)
{
bool next_cap = false;
QString result;
while (*name) {
if (*name == '-') {
next_cap = true;
} else if (next_cap) {
result.append(toupper(*name));
next_cap = false;
} else {
result.append(*name);
}
name++;
}
return result;
}
bool UnityMenuModel::loadExtendedAttributes(int position, const QVariantMap &schema)
{
GtkMenuTrackerItem *item;
QVariantMap *extendedAttrs;
item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (priv->items, position));
extendedAttrs = new QVariantMap;
for (QVariantMap::const_iterator it = schema.constBegin(); it != schema.constEnd(); ++it) {
QString name = it.key();
QString type = it.value().toString();
GVariant *value = gtk_menu_tracker_item_get_attribute_value (item, name.toUtf8(), NULL);
if (value == NULL) {
qWarning("loadExtendedAttributes: menu item does not contain '%s'", it.key().toUtf8().constData());
continue;
}
QVariant qvalue = attributeToQVariant(value, type);
if (qvalue.isValid())
extendedAttrs->insert(qtify_name (name.toUtf8()), qvalue);
else
qWarning("loadExtendedAttributes: key '%s' is of type '%s' (expected '%s')",
name.toUtf8().constData(), g_variant_get_type_string(value), type.constData());
g_variant_unref (value);
}
g_object_set_qdata_full (G_OBJECT (item), unity_menu_item_extended_attributes_quark (),
extendedAttrs, freeExtendedAttrs);
}
QVariant UnityMenuModel::get(int row, const QByteArray &role)
{
if (priv->roles.isEmpty()) {
QHash<int, QByteArray> names = roleNames();
Q_FOREACH (int role, names.keys())
priv->roles.insert(names[role], role);
}
return this->data(this->index(row, 0), priv->roles[role]);
}
void UnityMenuModel::activate(int index, const QVariant& parameter)
{
GtkMenuTrackerItem *item;
item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (priv->items, index));
if (parameter.isValid()) {
gchar *action;
gtk_menu_tracker_item_get_attribute (item, "action", "s", &action);
g_action_group_activate_action (G_ACTION_GROUP (priv->muxer), action, Converter::toGVariant(parameter));
g_free (action);
} else {
gtk_menu_tracker_item_activated (item);
}
}
void UnityMenuModel::changeState(int index, const QVariant& parameter)
{
GtkMenuTrackerItem* item;
item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (priv->items, index));
if (!item) return;
GVariant* data = Converter::toGVariant(parameter);
gtk_menu_tracker_item_change_state (item, data);
g_variant_unref(data);
}
bool UnityMenuModel::event(QEvent* e)
{
if (e->type() == UnityMenuModelClearEvent::eventType) {
UnityMenuModelClearEvent *emmce = static_cast<UnityMenuModelClearEvent*>(e);
GSequenceIter *begin;
GSequenceIter *end;
if (emmce->reset)
beginResetModel();
begin = g_sequence_get_begin_iter (priv->items);
end = g_sequence_get_end_iter (priv->items);
g_sequence_remove_range (begin, end);
if (emmce->reset)
endResetModel();
return true;
} else if (e->type() == UnityMenuModelAddRowEvent::eventType) {
UnityMenuModelAddRowEvent *ummrce = static_cast<UnityMenuModelAddRowEvent*>(e);
GSequenceIter *it;
it = g_sequence_get_iter_at_pos (priv->items, ummrce->position);
if (it) {
beginInsertRows(QModelIndex(), ummrce->position, ummrce->position);
it = g_sequence_insert_before (it, g_object_ref (ummrce->item));
g_object_set_qdata (G_OBJECT (ummrce->item), unity_menu_model_quark (), this);
g_signal_connect (ummrce->item, "notify", G_CALLBACK (UnityMenuModelPrivate::menuItemChanged), it);
endInsertRows();
}
return true;
} else if (e->type() == UnityMenuModelRemoveRowEvent::eventType) {
UnityMenuModelRemoveRowEvent *ummrre = static_cast<UnityMenuModelRemoveRowEvent*>(e);
GSequenceIter *it;
it = g_sequence_get_iter_at_pos (priv->items, ummrre->position);
if (it) {
beginRemoveRows(QModelIndex(), ummrre->position, ummrre->position);
g_sequence_remove (it);
endRemoveRows();
}
return true;
} else if (e->type() == UnityMenuModelDataChangeEvent::eventType) {
UnityMenuModelDataChangeEvent *ummdce = static_cast<UnityMenuModelDataChangeEvent*>(e);
Q_EMIT dataChanged(index(ummdce->position, 0), index(ummdce->position, 0));
return true;
}
return QAbstractListModel::event(e);
}
void UnityMenuModel::registerAction(UnityMenuAction* action)
{
if (!priv->registeredActions.contains(action)) {
GtkSimpleActionObserver* observer_item;
observer_item = gtk_simple_action_observer_new(GTK_ACTION_OBSERVABLE (priv->muxer),
UnityMenuModelPrivate::registeredActionAdded,
UnityMenuModelPrivate::registeredActionEnabledChanged,
UnityMenuModelPrivate::registeredActionStateChanged,
UnityMenuModelPrivate::registeredActionRemoved);
g_object_set_qdata (G_OBJECT (observer_item), unity_menu_action_quark (), action);
priv->registeredActions[action] = observer_item;
connect(action, SIGNAL(nameChanged(const QString&)), SLOT(onRegisteredActionNameChanged(const QString&)));
connect(action, SIGNAL(activate(const QVariant&)), SLOT(onRegisteredActionActivated(const QVariant&)));
connect(action, SIGNAL(changeState(const QVariant&)), SLOT(onRegisteredActionStateChanged(const QVariant&)));
}
}
void UnityMenuModel::unregisterAction(UnityMenuAction* action)
{
if (priv->registeredActions.contains(action)) {
GtkSimpleActionObserver* observer_item;
observer_item = priv->registeredActions[action];
g_object_unref(observer_item);
priv->registeredActions.remove(action);
disconnect(action);
}
}
void UnityMenuModel::onRegisteredActionNameChanged(const QString& name)
{
UnityMenuAction* action = qobject_cast<UnityMenuAction*>(sender());
if (!action || !priv->registeredActions.contains(action))
return;
GtkSimpleActionObserver* observer_item;
observer_item = priv->registeredActions[action];
QByteArray nameArray = name.toUtf8();
const gchar* action_name = nameArray.constData();
gtk_simple_action_observer_register_action (observer_item, action_name);
const GVariantType *parameter_type;
gboolean enabled;
GVariant *state;
if (g_action_group_query_action (G_ACTION_GROUP (priv->muxer), action_name,
&enabled, ¶meter_type, NULL, NULL, &state))
{
UnityMenuActionAddEvent umaae(enabled, Converter::toQVariant(state));
QCoreApplication::sendEvent(action, &umaae);
if (state) {
g_variant_unref (state);
}
}
}
void UnityMenuModel::onRegisteredActionActivated(const QVariant& parameter)
{
UnityMenuAction* action = qobject_cast<UnityMenuAction*>(sender());
if (!action || action->name().isEmpty())
return;
QByteArray nameArray = action->name().toUtf8();
const gchar* action_name = nameArray.constData();
g_action_group_activate_action (G_ACTION_GROUP (priv->muxer), action_name, Converter::toGVariant(parameter));
}
void UnityMenuModel::onRegisteredActionStateChanged(const QVariant& parameter)
{
UnityMenuAction* action = qobject_cast<UnityMenuAction*>(sender());
if (!action || action->name().isEmpty())
return;
QByteArray nameArray = action->name().toUtf8();
const gchar* action_name = nameArray.constData();
g_action_group_change_action_state (G_ACTION_GROUP (priv->muxer), action_name, Converter::toGVariant(parameter));
}
void UnityMenuModelPrivate::registeredActionAdded(GtkSimpleActionObserver *observer_item,
const gchar *action_name,
gboolean enabled,
GVariant *state)
{
UnityMenuAction *action;
action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ());
if (action) {
UnityMenuActionAddEvent umaae(enabled, Converter::toQVariant(state));
QCoreApplication::sendEvent(action, &umaae);
}
}
void UnityMenuModelPrivate::registeredActionEnabledChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, gboolean enabled)
{
UnityMenuAction *action;
action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ());
if (action) {
UnityMenuActionEnabledChangedEvent umaece(enabled);
QCoreApplication::sendEvent(action, &umaece);
}
}
void UnityMenuModelPrivate::registeredActionStateChanged(GtkSimpleActionObserver *observer_item, const gchar *action_name, GVariant *state)
{
UnityMenuAction *action;
action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ());
if (action) {
UnityMenuActionStateChangeEvent umasce(Converter::toQVariant(state));
QCoreApplication::sendEvent(action, &umasce);
}
}
void UnityMenuModelPrivate::registeredActionRemoved(GtkSimpleActionObserver *observer_item, const gchar *action_name)
{
UnityMenuAction *action;
action = (UnityMenuAction *) g_object_get_qdata (G_OBJECT (observer_item), unity_menu_action_quark ());
if (action) {
UnityMenuActionRemoveEvent umare;
QCoreApplication::sendEvent(action, &umare);
}
}
|