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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
|
/*
A library to communicate a menu object set accross DBus and
track updates and maintain consistency.
Copyright 2009 Canonical Ltd.
Authors:
Ted Gould <ted@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of either or both of the following licenses:
1) the GNU Lesser General Public License version 3, as published by the
Free Software Foundation; and/or
2) 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 applicable version of the GNU Lesser General Public
License for more details.
You should have received a copy of both the GNU Lesser General Public
License version 3 and version 2.1 along with this program. If not, see
<http://www.gnu.org/licenses/>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gio/gio.h>
#include "client.h"
#include "client-private.h"
#include "menuitem.h"
#include "menuitem-private.h"
#include "client-menuitem.h"
#include "server-marshal.h"
#include "client-marshal.h"
#include "dbus-menu-clean.xml.h"
#include "enum-types.h"
/* How many property requests should we queue before
sending the message on dbus */
#define MAX_PROPERTIES_TO_QUEUE 100
/* Properties */
enum {
PROP_0,
PROP_DBUSOBJECT,
PROP_DBUSNAME,
PROP_STATUS,
PROP_TEXT_DIRECTION
};
/* Signals */
enum {
LAYOUT_UPDATED,
ROOT_CHANGED,
NEW_MENUITEM,
ITEM_ACTIVATE,
EVENT_RESULT,
ICON_THEME_DIRS,
LAST_SIGNAL
};
typedef void (*properties_func) (GVariant * properties, GError * error, gpointer user_data);
static guint signals[LAST_SIGNAL] = { 0 };
struct _DbusmenuClientPrivate
{
DbusmenuMenuitem * root;
gchar * dbus_object;
gchar * dbus_name;
GDBusConnection * session_bus;
GCancellable * session_bus_cancel;
GDBusProxy * menuproxy;
GCancellable * menuproxy_cancel;
GCancellable * layoutcall;
GVariant * layout_props;
gint current_revision;
gint my_revision;
guint dbusproxy;
GHashTable * type_handlers;
GArray * delayed_property_list;
GArray * delayed_property_listeners;
gint delayed_idle;
DbusmenuTextDirection text_direction;
DbusmenuStatus status;
GStrv icon_dirs;
};
typedef struct _newItemPropData newItemPropData;
struct _newItemPropData
{
DbusmenuClient * client;
DbusmenuMenuitem * item;
DbusmenuMenuitem * parent;
};
typedef struct _properties_listener_t properties_listener_t;
struct _properties_listener_t {
gint id;
properties_func callback;
gpointer user_data;
gboolean replied;
};
typedef struct _event_data_t event_data_t;
struct _event_data_t {
DbusmenuClient * client;
DbusmenuMenuitem * menuitem;
gchar * event;
GVariant * variant;
guint timestamp;
};
typedef struct _type_handler_t type_handler_t;
struct _type_handler_t {
DbusmenuClient * client;
DbusmenuClientTypeHandler cb;
GDestroyNotify destroy_cb;
gpointer user_data;
gchar * type;
};
typedef struct _properties_callback_t properties_callback_t;
struct _properties_callback_t {
DbusmenuClient * client;
GArray * listeners;
};
#define DBUSMENU_CLIENT_GET_PRIVATE(o) (DBUSMENU_CLIENT(o)->priv)
#define DBUSMENU_INTERFACE "com.canonical.dbusmenu"
/* GObject Stuff */
static void dbusmenu_client_class_init (DbusmenuClientClass *klass);
static void dbusmenu_client_init (DbusmenuClient *self);
static void dbusmenu_client_dispose (GObject *object);
static void dbusmenu_client_finalize (GObject *object);
static void set_property (GObject * obj, guint id, const GValue * value, GParamSpec * pspec);
static void get_property (GObject * obj, guint id, GValue * value, GParamSpec * pspec);
/* Private Funcs */
static void layout_update (GDBusProxy * proxy, guint revision, gint parent, DbusmenuClient * client);
static void id_prop_update (GDBusProxy * proxy, gint id, gchar * property, GVariant * value, DbusmenuClient * client);
static void id_update (GDBusProxy * proxy, gint id, DbusmenuClient * client);
static void build_proxies (DbusmenuClient * client);
static DbusmenuMenuitem * parse_layout_xml(DbusmenuClient * client, GVariant * layout, DbusmenuMenuitem * item, DbusmenuMenuitem * parent, GDBusProxy * proxy);
static gint parse_layout (DbusmenuClient * client, GVariant * layout);
static void update_layout_cb (GObject * proxy, GAsyncResult * res, gpointer data);
static void update_layout (DbusmenuClient * client);
static void menuitem_get_properties_cb (GVariant * properties, GError * error, gpointer data);
static void get_properties_globber (DbusmenuClient * client, gint id, const gchar ** properties, properties_func callback, gpointer user_data);
static GQuark error_domain (void);
static void item_activated (GDBusProxy * proxy, gint id, guint timestamp, DbusmenuClient * client);
static void menuproxy_build_cb (GObject * object, GAsyncResult * res, gpointer user_data);
static void menuproxy_prop_changed_cb (GDBusProxy * proxy, GVariant * properties, GStrv invalidated, gpointer user_data);
static void menuproxy_name_changed_cb (GObject * object, GParamSpec * pspec, gpointer user_data);
static void menuproxy_signal_cb (GDBusProxy * proxy, gchar * sender, gchar * signal, GVariant * params, gpointer user_data);
static void type_handler_destroy (gpointer user_data);
/* Globals */
static GDBusNodeInfo * dbusmenu_node_info = NULL;
static GDBusInterfaceInfo * dbusmenu_interface_info = NULL;
/* Build a type */
G_DEFINE_TYPE (DbusmenuClient, dbusmenu_client, G_TYPE_OBJECT);
static void
dbusmenu_client_class_init (DbusmenuClientClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (klass, sizeof (DbusmenuClientPrivate));
object_class->dispose = dbusmenu_client_dispose;
object_class->finalize = dbusmenu_client_finalize;
object_class->set_property = set_property;
object_class->get_property = get_property;
/**
DbusmenuClient::layout-update:
@arg0: The #DbusmenuClient object
Tells that the layout has been updated and parsed by
this object and is ready for grabbing by the calling
application.
*/
signals[LAYOUT_UPDATED] = g_signal_new(DBUSMENU_CLIENT_SIGNAL_LAYOUT_UPDATED,
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (DbusmenuClientClass, layout_updated),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0, G_TYPE_NONE);
/**
DbusmenuClient::root-changed:
@arg0: The #DbusmenuClient object
@arg1: The new root #DbusmenuMenuitem
The layout has changed in a way that can not be
represented by the individual items changing as the
root of this client has changed.
*/
signals[ROOT_CHANGED] = g_signal_new(DBUSMENU_CLIENT_SIGNAL_ROOT_CHANGED,
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (DbusmenuClientClass, root_changed),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, G_TYPE_OBJECT);
/**
DbusmenuClient::new-menuitem:
@arg0: The #DbusmenuClient object
@arg1: The new #DbusmenuMenuitem created
Signaled when the client creates a new menuitem. This
doesn't mean that it's placed anywhere. The parent that
it's applied to will signal #DbusmenuMenuitem::child-added
when it gets parented.
*/
signals[NEW_MENUITEM] = g_signal_new(DBUSMENU_CLIENT_SIGNAL_NEW_MENUITEM,
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (DbusmenuClientClass, new_menuitem),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, G_TYPE_OBJECT);
/**
DbusmenuClient::item-activate:
@arg0: The #DbusmenuClient object
@arg1: The #DbusmenuMenuitem activated
@arg2: A timestamp that the event happened at
Signaled when the server wants to activate an item in
order to display the menu.
*/
signals[ITEM_ACTIVATE] = g_signal_new(DBUSMENU_CLIENT_SIGNAL_ITEM_ACTIVATE,
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (DbusmenuClientClass, item_activate),
NULL, NULL,
_dbusmenu_client_marshal_VOID__OBJECT_UINT,
G_TYPE_NONE, 2, G_TYPE_OBJECT, G_TYPE_UINT);
/**
DbusmenuClient::event-error:
@arg0: The #DbusmenuClient object
@arg1: The #DbusmenuMenuitem sent an event
@arg2: The ID of the event sent
@arg3: The data sent along with the event
@arg4: A timestamp that the event happened at
@arg5: Possibly the error in sending the event (or NULL)
Signal sent to show that there was an error in sending the event
to the server.
*/
signals[EVENT_RESULT] = g_signal_new(DBUSMENU_CLIENT_SIGNAL_EVENT_RESULT,
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (DbusmenuClientClass, event_result),
NULL, NULL,
_dbusmenu_client_marshal_VOID__OBJECT_STRING_VARIANT_UINT_POINTER,
G_TYPE_NONE, 5, G_TYPE_OBJECT, G_TYPE_STRING, G_TYPE_VARIANT, G_TYPE_UINT, G_TYPE_POINTER);
/**
DbusmenuClient::icon-theme-dirs-changed:
@arg0: The #DbusmenuClient object
@arg1: A #GStrv of theme directories
Signaled when the theme directories are changed by the server.
*/
signals[ICON_THEME_DIRS] = g_signal_new(DBUSMENU_CLIENT_SIGNAL_ICON_THEME_DIRS_CHANGED,
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (DbusmenuClientClass, icon_theme_dirs),
NULL, NULL,
_dbusmenu_client_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
g_object_class_install_property (object_class, PROP_DBUSOBJECT,
g_param_spec_string(DBUSMENU_CLIENT_PROP_DBUS_OBJECT, "DBus Object we represent",
"The Object on the client that we're getting our data from.",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_DBUSNAME,
g_param_spec_string(DBUSMENU_CLIENT_PROP_DBUS_NAME, "DBus Client we connect to",
"Name of the DBus client we're connecting to.",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_STATUS,
g_param_spec_enum(DBUSMENU_CLIENT_PROP_STATUS, "Status of viewing the menus",
"Whether the menus should be given special visuals",
DBUSMENU_TYPE_STATUS, DBUSMENU_STATUS_NORMAL,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class, PROP_TEXT_DIRECTION,
g_param_spec_enum(DBUSMENU_CLIENT_PROP_TEXT_DIRECTION, "Direction text values have",
"Signals which direction the default text direction is for the menus",
DBUSMENU_TYPE_TEXT_DIRECTION, DBUSMENU_TEXT_DIRECTION_NONE,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
if (dbusmenu_node_info == NULL) {
GError * error = NULL;
dbusmenu_node_info = g_dbus_node_info_new_for_xml(dbus_menu_clean_xml, &error);
if (error != NULL) {
g_error("Unable to parse DBusmenu Interface description: %s", error->message);
g_error_free(error);
}
}
if (dbusmenu_interface_info == NULL) {
dbusmenu_interface_info = g_dbus_node_info_lookup_interface(dbusmenu_node_info, DBUSMENU_INTERFACE);
if (dbusmenu_interface_info == NULL) {
g_error("Unable to find interface '" DBUSMENU_INTERFACE "'");
}
}
return;
}
static void
dbusmenu_client_init (DbusmenuClient *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), DBUSMENU_TYPE_CLIENT, DbusmenuClientPrivate);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(self);
priv->root = NULL;
priv->dbus_object = NULL;
priv->dbus_name = NULL;
priv->session_bus = NULL;
priv->session_bus_cancel = NULL;
priv->menuproxy = NULL;
priv->menuproxy_cancel = NULL;
priv->layoutcall = NULL;
gchar * layout_props[5];
layout_props[0] = DBUSMENU_MENUITEM_PROP_TYPE;
layout_props[1] = DBUSMENU_MENUITEM_PROP_LABEL;
layout_props[2] = DBUSMENU_MENUITEM_PROP_VISIBLE;
layout_props[3] = DBUSMENU_MENUITEM_PROP_ENABLED;
layout_props[4] = NULL;
priv->layout_props = g_variant_new_strv((const gchar * const *)layout_props, 4);
g_variant_ref_sink(priv->layout_props);
priv->current_revision = 0;
priv->my_revision = 0;
priv->dbusproxy = 0;
priv->type_handlers = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, type_handler_destroy);
priv->delayed_idle = 0;
priv->delayed_property_list = g_array_new(TRUE, FALSE, sizeof(gchar *));
priv->delayed_property_listeners = g_array_new(FALSE, FALSE, sizeof(properties_listener_t));
priv->text_direction = DBUSMENU_TEXT_DIRECTION_NONE;
priv->status = DBUSMENU_STATUS_NORMAL;
priv->icon_dirs = NULL;
return;
}
static void
dbusmenu_client_dispose (GObject *object)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(object);
if (priv->delayed_idle != 0) {
g_source_remove(priv->delayed_idle);
priv->delayed_idle = 0;
}
/* Only used for queueing up a new command, so we can
just drop this array. */
if (priv->delayed_property_list == NULL) {
gchar ** dataregion = (gchar **)g_array_free(priv->delayed_property_list, FALSE);
if (dataregion != NULL) {
g_strfreev(dataregion);
}
priv->delayed_property_list = NULL;
}
if (priv->delayed_property_listeners == NULL) {
gint i;
GError * localerror = NULL;
/* Making sure all the callbacks get called so that if they had
memory in their user_data that needs to be free'd that happens. */
for (i = 0; i < priv->delayed_property_listeners->len; i++) {
properties_listener_t * listener = &g_array_index(priv->delayed_property_listeners, properties_listener_t, i);
if (!listener->replied) {
if (localerror == NULL) {
g_set_error_literal(&localerror, error_domain(), 0, "DbusmenuClient Shutdown");
}
listener->callback(NULL, localerror, listener->user_data);
}
}
if (localerror != NULL) {
g_error_free(localerror);
}
g_array_free(priv->delayed_property_listeners, TRUE);
priv->delayed_property_listeners = NULL;
}
if (priv->layoutcall != NULL) {
g_cancellable_cancel(priv->layoutcall);
g_object_unref(priv->layoutcall);
priv->layoutcall = NULL;
}
if (priv->layout_props != NULL) {
g_variant_unref(priv->layout_props);
priv->layout_props = NULL;
}
/* Bring down the menu proxy, ensure we're not
looking for one at the same time. */
if (priv->menuproxy_cancel != NULL) {
g_cancellable_cancel(priv->menuproxy_cancel);
g_object_unref(priv->menuproxy_cancel);
priv->menuproxy_cancel = NULL;
}
if (priv->menuproxy != NULL) {
g_object_unref(G_OBJECT(priv->menuproxy));
priv->menuproxy = NULL;
}
if (priv->dbusproxy != 0) {
g_bus_unwatch_name(priv->dbusproxy);
priv->dbusproxy = 0;
}
/* Bring down the session bus, ensure we're not
looking for one at the same time. */
if (priv->session_bus_cancel != NULL) {
g_cancellable_cancel(priv->session_bus_cancel);
g_object_unref(priv->session_bus_cancel);
priv->session_bus_cancel = NULL;
}
if (priv->session_bus != NULL) {
g_object_unref(priv->session_bus);
priv->session_bus = NULL;
}
if (priv->root != NULL) {
g_object_unref(G_OBJECT(priv->root));
priv->root = NULL;
}
G_OBJECT_CLASS (dbusmenu_client_parent_class)->dispose (object);
return;
}
static void
dbusmenu_client_finalize (GObject *object)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(object);
g_free(priv->dbus_name);
g_free(priv->dbus_object);
if (priv->type_handlers != NULL) {
g_hash_table_destroy(priv->type_handlers);
}
if (priv->icon_dirs != NULL) {
g_strfreev(priv->icon_dirs);
priv->icon_dirs = NULL;
}
G_OBJECT_CLASS (dbusmenu_client_parent_class)->finalize (object);
return;
}
static void
set_property (GObject * obj, guint id, const GValue * value, GParamSpec * pspec)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(obj);
switch (id) {
case PROP_DBUSNAME:
priv->dbus_name = g_value_dup_string(value);
if (priv->dbus_name != NULL && priv->dbus_object != NULL) {
build_proxies(DBUSMENU_CLIENT(obj));
}
break;
case PROP_DBUSOBJECT:
priv->dbus_object = g_value_dup_string(value);
if (priv->dbus_name != NULL && priv->dbus_object != NULL) {
build_proxies(DBUSMENU_CLIENT(obj));
}
break;
default:
g_warning("Unknown property %d.", id);
return;
}
return;
}
static void
get_property (GObject * obj, guint id, GValue * value, GParamSpec * pspec)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(obj);
switch (id) {
case PROP_DBUSNAME:
g_value_set_string(value, priv->dbus_name);
break;
case PROP_DBUSOBJECT:
g_value_set_string(value, priv->dbus_object);
break;
case PROP_STATUS:
g_value_set_enum(value, priv->status);
break;
case PROP_TEXT_DIRECTION:
g_value_set_enum(value, priv->text_direction);
break;
default:
g_warning("Unknown property %d.", id);
return;
}
return;
}
/* Internal funcs */
static GQuark
error_domain (void)
{
static GQuark error = 0;
if (error == 0) {
error = g_quark_from_static_string(G_LOG_DOMAIN "-CLIENT");
}
return error;
}
/* Quick little function to search through the listeners and find
one that matches an ID */
static properties_listener_t *
find_listener (GArray * listeners, guint index, gint id)
{
if (index >= listeners->len) {
return NULL;
}
properties_listener_t * retval = &g_array_index(listeners, properties_listener_t, index);
if (retval->id == id) {
return retval;
}
return find_listener(listeners, index + 1, id);
}
/* Call back from getting the group properties, now we need
to unwind and call the various functions. */
static void
get_properties_callback (GObject *obj, GAsyncResult * res, gpointer user_data)
{
properties_callback_t * cbdata = (properties_callback_t *)user_data;
GArray * listeners = cbdata->listeners;
int i;
GError * error = NULL;
GVariant * params = NULL;
params = g_dbus_proxy_call_finish(G_DBUS_PROXY(obj), res, &error);
if (error != NULL) {
/* If we get an error, all our callbacks need to hear about it. */
g_warning("Group Properties error: %s", error->message);
for (i = 0; i < listeners->len; i++) {
properties_listener_t * listener = &g_array_index(listeners, properties_listener_t, i);
listener->callback(NULL, error, listener->user_data);
}
g_error_free(error);
goto out;
}
/* Callback all the folks we can find */
GVariant * child = g_variant_get_child_value(params, 0);
GVariantIter * iter = g_variant_iter_new(child);
g_variant_unref(child);
while ((child = g_variant_iter_next_value(iter)) != NULL) {
if (g_strcmp0(g_variant_get_type_string(child), "(ia{sv})") != 0) {
g_warning("Properties return signature is not '(ia{sv})' it is '%s'", g_variant_get_type_string(child));
g_variant_unref(child);
continue;
}
GVariant * idv = g_variant_get_child_value(child, 0);
gint id = g_variant_get_int32(idv);
g_variant_unref(idv);
GVariant * properties = g_variant_get_child_value(child, 1);
properties_listener_t * listener = find_listener(listeners, 0, id);
if (listener == NULL) {
g_warning("Unable to find listener for ID %d", id);
g_variant_unref(properties);
g_variant_unref(child);
continue;
}
if (!listener->replied) {
listener->callback(properties, NULL, listener->user_data);
listener->replied = TRUE;
} else {
g_warning("Odd, we've already replied to the listener on ID %d", id);
}
g_variant_unref(properties);
g_variant_unref(child);
}
g_variant_iter_free(iter);
g_variant_unref(params);
/* Provide errors for those who we can't */
GError * localerror = NULL;
for (i = 0; i < listeners->len; i++) {
properties_listener_t * listener = &g_array_index(listeners, properties_listener_t, i);
if (!listener->replied) {
g_warning("Generating properties error for: %d", listener->id);
if (localerror == NULL) {
g_set_error_literal(&localerror, error_domain(), 0, "Error getting properties for ID");
}
listener->callback(NULL, localerror, listener->user_data);
}
}
if (localerror != NULL) {
g_error_free(localerror);
}
out:
/* Clean up */
g_array_free(listeners, TRUE);
g_object_unref(cbdata->client);
g_free(user_data);
return;
}
/* Idle handler to send out all of our property requests as one big
lovely property request. */
static gboolean
get_properties_idle (gpointer user_data)
{
properties_callback_t * cbdata = NULL;
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(user_data);
g_return_val_if_fail(priv->menuproxy != NULL, TRUE);
if (priv->delayed_property_listeners->len == 0) {
g_warning("Odd, idle func got no listeners.");
return FALSE;
}
/* Build up an ID list to pass */
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
gint i;
for (i = 0; i < priv->delayed_property_listeners->len; i++) {
g_variant_builder_add(&builder, "i", g_array_index(priv->delayed_property_listeners, properties_listener_t, i).id);
}
GVariant * variant_ids = g_variant_builder_end(&builder);
/* Build up a prop list to pass */
GVariantType * type = g_variant_type_new("as");
g_variant_builder_init(&builder, type);
g_variant_type_free(type);
/* TODO: need to use delayed property list here */
GVariant * variant_props = g_variant_builder_end(&builder);
/* Combine them into a value for the parameter */
g_variant_builder_init(&builder, G_VARIANT_TYPE_TUPLE);
g_variant_builder_add_value(&builder, variant_ids);
g_variant_builder_add_value(&builder, variant_props);
GVariant * variant_params = g_variant_builder_end(&builder);
cbdata = g_new(properties_callback_t, 1);
cbdata->listeners = priv->delayed_property_listeners;
cbdata->client = DBUSMENU_CLIENT(user_data);
g_object_ref(G_OBJECT(user_data));
g_dbus_proxy_call(priv->menuproxy,
"GetGroupProperties",
variant_params,
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
NULL, /* cancellable */
get_properties_callback,
cbdata);
/* Free properties */
gchar ** dataregion = (gchar **)g_array_free(priv->delayed_property_list, FALSE);
if (dataregion != NULL) {
g_strfreev(dataregion);
}
priv->delayed_property_list = g_array_new(TRUE, FALSE, sizeof(gchar *));
/* Rebuild the listeners */
priv->delayed_property_listeners = g_array_new(FALSE, FALSE, sizeof(properties_listener_t));
/* Make sure we set for a new idle */
priv->delayed_idle = 0;
return FALSE;
}
/* Forces a call out to start getting properties with the menu items
that we have queued up already. */
static void
get_properties_flush (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (priv->delayed_idle == 0) {
return;
}
g_source_remove(priv->delayed_idle);
priv->delayed_idle = 0;
get_properties_idle(client);
return;
}
/* A function to group all the get_properties commands to make them
more efficient over dbus. */
static void
get_properties_globber (DbusmenuClient * client, gint id, const gchar ** properties, properties_func callback, gpointer user_data)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (find_listener(priv->delayed_property_listeners, 0, id) != NULL) {
g_warning("Asking for properties from same ID twice: %d", id);
GError * localerror = NULL;
g_set_error_literal(&localerror, error_domain(), 0, "ID already queued");
callback(NULL, localerror, user_data);
g_error_free(localerror);
return;
}
if (properties == NULL || properties[0] == NULL) {
/* get all case */
if (priv->delayed_property_list->len != 0) {
/* If there are entries in the list, then we'll need to
remove them all, and start over */
gchar ** dataregion = (gchar **)g_array_free(priv->delayed_property_list, FALSE);
if (dataregion != NULL) {
g_strfreev(dataregion);
}
priv->delayed_property_list = g_array_new(TRUE, FALSE, sizeof(gchar *));
}
} else {
/* there could be a list we care about */
/* TODO: No one uses this today */
/* TODO: Copy them into the list */
}
properties_listener_t listener = {0};
listener.id = id;
listener.callback = callback;
listener.user_data = user_data;
listener.replied = FALSE;
g_array_append_val(priv->delayed_property_listeners, listener);
if (priv->delayed_idle == 0) {
priv->delayed_idle = g_idle_add(get_properties_idle, client);
}
/* Look at how many proprites we have queued up and
make it so that we don't leave too many in one
request. */
if (priv->delayed_property_listeners->len == MAX_PROPERTIES_TO_QUEUE) {
get_properties_flush(client);
}
return;
}
/* Called when a server item wants to activate the menu */
static void
item_activated (GDBusProxy * proxy, gint id, guint timestamp, DbusmenuClient * client)
{
g_return_if_fail(DBUSMENU_IS_CLIENT(client));
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (priv->root == NULL) {
g_warning("Asked to activate item %d when we don't have a menu structure.", id);
return;
}
DbusmenuMenuitem * menuitem = dbusmenu_menuitem_find_id(priv->root, id);
if (menuitem == NULL) {
g_warning("Unable to find menu item %d to activate.", id);
return;
}
g_signal_emit(G_OBJECT(client), signals[ITEM_ACTIVATE], 0, menuitem, timestamp, TRUE);
return;
}
/* Annoying little wrapper to make the right function update */
static void
layout_update (GDBusProxy * proxy, guint revision, gint parent, DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
priv->current_revision = revision;
if (priv->current_revision > priv->my_revision) {
update_layout(client);
}
return;
}
/* Signal from the server that a property has changed
on one of our menuitems */
static void
id_prop_update (GDBusProxy * proxy, gint id, gchar * property, GVariant * value, DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
g_return_if_fail(priv->root != NULL);
DbusmenuMenuitem * menuitem = dbusmenu_menuitem_find_id(priv->root, id);
if (menuitem == NULL) {
#ifdef MASSIVEDEBUGGING
g_debug("Property update '%s' on id %d which couldn't be found", property, id);
#endif
return;
}
dbusmenu_menuitem_property_set_variant(menuitem, property, value);
return;
}
/* Oh, lots of updates now. That silly server, they want
to change all kinds of stuff! */
static void
id_update (GDBusProxy * proxy, gint id, DbusmenuClient * client)
{
#ifdef MASSIVEDEBUGGING
g_debug("Client side ID update: %d", id);
#endif
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
g_return_if_fail(priv->root != NULL);
DbusmenuMenuitem * menuitem = dbusmenu_menuitem_find_id(priv->root, id);
g_return_if_fail(menuitem != NULL);
g_debug("Getting properties");
g_object_ref(menuitem);
get_properties_globber(client, id, NULL, menuitem_get_properties_cb, menuitem);
return;
}
/* Watches to see if our DBus savior comes onto the bus */
static void
dbus_owner_change (GDBusConnection * connection, const gchar * name, const gchar * owner, gpointer user_data)
{
g_return_if_fail(DBUSMENU_IS_CLIENT(user_data));
DbusmenuClient * client = DBUSMENU_CLIENT(user_data);
/* Woot! A service for us to love and to hold for ever
and ever and ever! */
return build_proxies(client);
}
/* This function builds the DBus proxy which will look out for
the service coming up. */
static void
build_dbus_proxy (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (priv->dbusproxy != 0) {
return;
}
priv->dbusproxy = g_bus_watch_name_on_connection(priv->session_bus,
priv->dbus_name,
G_BUS_NAME_WATCHER_FLAGS_NONE,
dbus_owner_change,
NULL,
client,
NULL);
/* Now let's check to make sure we're not in some race
condition case. */
/* TODO: Not sure how to check for names in GDBus */
return;
}
/* A signal handler that gets called when a proxy is destoryed a
so it needs to clean up a little. Make sure we don't think we
have a layout and setup the dbus watcher. */
static void
proxy_destroyed (GObject * gobj_proxy, gpointer userdata)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(userdata);
if (priv->root != NULL) {
g_object_unref(G_OBJECT(priv->root));
priv->root = NULL;
#ifdef MASSIVEDEBUGGING
g_debug("Proxies destroyed, signaling a root change and a layout update.");
#endif
g_signal_emit(G_OBJECT(userdata), signals[ROOT_CHANGED], 0, NULL, TRUE);
g_signal_emit(G_OBJECT(userdata), signals[LAYOUT_UPDATED], 0, TRUE);
}
if ((gpointer)priv->menuproxy == (gpointer)gobj_proxy) {
if (priv->layoutcall != NULL) {
g_cancellable_cancel(priv->layoutcall);
g_object_unref(priv->layoutcall);
priv->layoutcall = NULL;
}
}
priv->current_revision = 0;
priv->my_revision = 0;
build_dbus_proxy(DBUSMENU_CLIENT(userdata));
return;
}
/* Respond to us getting the session bus (hopefully) or handle
the error if not */
void
session_bus_cb (GObject * object, GAsyncResult * res, gpointer user_data)
{
GError * error = NULL;
/* NOTE: We're not using any other variables before checking
the result because they could be destroyed and thus invalid */
GDBusConnection * bus = g_bus_get_finish(res, &error);
if (error != NULL) {
g_warning("Unable to get session bus: %s", error->message);
g_error_free(error);
return;
}
/* If this wasn't cancelled, we should be good */
DbusmenuClient * client = DBUSMENU_CLIENT(user_data);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
priv->session_bus = bus;
if (priv->session_bus_cancel != NULL) {
g_object_unref(priv->session_bus_cancel);
priv->session_bus_cancel = NULL;
}
/* Retry to build the proxies now that we have a bus */
build_proxies(DBUSMENU_CLIENT(user_data));
return;
}
/* When we have a name and an object, build the two proxies and get the
first version of the layout */
static void
build_proxies (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
g_return_if_fail(priv->dbus_object != NULL);
g_return_if_fail(priv->dbus_name != NULL);
if (priv->session_bus == NULL) {
/* We don't have the session bus yet, that's okay, but
we need to handle that. */
/* If we're already running we don't need to look again. */
if (priv->session_bus_cancel == NULL) {
priv->session_bus_cancel = g_cancellable_new();
/* Async get the session bus */
g_bus_get(G_BUS_TYPE_SESSION, priv->session_bus_cancel, session_bus_cb, client);
}
/* This function exists, it'll be called again when we get
the session bus so this condition will be ignored */
return;
}
/* Build us a menu proxy */
if (priv->menuproxy == NULL) {
/* Check to see if we're already building one */
if (priv->menuproxy_cancel == NULL) {
priv->menuproxy_cancel = g_cancellable_new();
g_dbus_proxy_new(priv->session_bus,
G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
dbusmenu_interface_info,
priv->dbus_name,
priv->dbus_object,
DBUSMENU_INTERFACE,
priv->menuproxy_cancel,
menuproxy_build_cb,
client);
}
}
return;
}
/* Callback when we know if the menu proxy can be created or
not and do something with it! */
static void
menuproxy_build_cb (GObject * object, GAsyncResult * res, gpointer user_data)
{
GError * error = NULL;
/* NOTE: We're not using any other variables before checking
the result because they could be destroyed and thus invalid */
GDBusProxy * proxy = g_dbus_proxy_new_finish(res, &error);
if (error != NULL) {
g_warning("Unable to get menu proxy: %s", error->message);
g_error_free(error);
return;
}
/* If this wasn't cancelled, we should be good */
DbusmenuClient * client = DBUSMENU_CLIENT(user_data);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
priv->menuproxy = proxy;
if (priv->menuproxy_cancel != NULL) {
g_object_unref(priv->menuproxy_cancel);
priv->menuproxy_cancel = NULL;
}
/* Check the text direction if available */
GVariant * textdir = g_dbus_proxy_get_cached_property(priv->menuproxy, "TextDirection");
if (textdir != NULL) {
if (g_variant_is_of_type(textdir, G_VARIANT_TYPE_VARIANT)) {
GVariant * tmp = g_variant_get_variant(textdir);
g_variant_unref(textdir);
textdir = tmp;
}
priv->text_direction = dbusmenu_text_direction_get_value_from_nick(g_variant_get_string(textdir, NULL));
g_object_notify(G_OBJECT(user_data), DBUSMENU_CLIENT_PROP_TEXT_DIRECTION);
g_variant_unref(textdir);
}
/* Check the status if available */
GVariant * status = g_dbus_proxy_get_cached_property(priv->menuproxy, "Status");
if (status != NULL) {
if (g_variant_is_of_type(status, G_VARIANT_TYPE_VARIANT)) {
GVariant * tmp = g_variant_get_variant(status);
g_variant_unref(status);
status = tmp;
}
priv->status = dbusmenu_status_get_value_from_nick(g_variant_get_string(status, NULL));
g_object_notify(G_OBJECT(user_data), DBUSMENU_CLIENT_PROP_STATUS);
g_variant_unref(status);
}
/* Get the icon theme directories if available */
GVariant * icon_dirs = g_dbus_proxy_get_cached_property(priv->menuproxy, "IconThemePath");
if (icon_dirs != NULL) {
if (priv->icon_dirs != NULL) {
g_strfreev(priv->icon_dirs);
priv->icon_dirs = NULL;
}
priv->icon_dirs = g_variant_dup_strv(icon_dirs, NULL);
g_signal_emit(G_OBJECT(client), signals[ICON_THEME_DIRS], 0, priv->icon_dirs, TRUE);
g_variant_unref(icon_dirs);
}
/* If we get here, we don't need the DBus proxy */
if (priv->dbusproxy != 0) {
g_bus_unwatch_name(priv->dbusproxy);
priv->dbusproxy = 0;
}
g_signal_connect(priv->menuproxy, "g-signal", G_CALLBACK(menuproxy_signal_cb), client);
g_signal_connect(priv->menuproxy, "notify::g-name-owner", G_CALLBACK(menuproxy_name_changed_cb), client);
g_signal_connect(priv->menuproxy, "g-properties-changed", G_CALLBACK(menuproxy_prop_changed_cb), client);
gchar * name_owner = g_dbus_proxy_get_name_owner(priv->menuproxy);
if (name_owner != NULL) {
update_layout(client);
g_free(name_owner);
}
return;
}
/* Handle the properites changing */
static void
menuproxy_prop_changed_cb (GDBusProxy * proxy, GVariant * properties, GStrv invalidated, gpointer user_data)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(user_data);
DbusmenuTextDirection olddir = priv->text_direction;
DbusmenuStatus oldstatus = priv->status;
gboolean dirs_changed = FALSE;
/* Invalidate first */
gchar * invalid;
gint i = 0;
for (invalid = invalidated[i]; invalid != NULL; invalid = invalidated[++i]) {
if (g_strcmp0(invalid, "TextDirection") == 0) {
priv->text_direction = DBUSMENU_TEXT_DIRECTION_NONE;
}
if (g_strcmp0(invalid, "Status") == 0) {
priv->status = DBUSMENU_STATUS_NORMAL;
}
if (g_strcmp0(invalid, "IconThemePath") == 0) {
if (priv->icon_dirs != NULL) {
dirs_changed = TRUE;
g_strfreev(priv->icon_dirs);
priv->icon_dirs = NULL;
}
}
}
/* Check updates */
GVariantIter iters;
gchar * key; GVariant * value;
g_variant_iter_init(&iters, properties);
while (g_variant_iter_next(&iters, "{sv}", &key, &value)) {
if (g_strcmp0(key, "TextDirection") == 0) {
if (g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT)) {
GVariant * tmp = g_variant_get_variant(value);
g_variant_unref(value);
value = tmp;
}
priv->text_direction = dbusmenu_text_direction_get_value_from_nick(g_variant_get_string(value, NULL));
}
if (g_strcmp0(key, "Status") == 0) {
if (g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT)) {
GVariant * tmp = g_variant_get_variant(value);
g_variant_unref(value);
value = tmp;
}
priv->status = dbusmenu_status_get_value_from_nick(g_variant_get_string(value, NULL));
}
if (g_strcmp0(key, "IconThemePath") == 0) {
if (priv->icon_dirs != NULL) {
g_strfreev(priv->icon_dirs);
priv->icon_dirs = NULL;
}
priv->icon_dirs = g_variant_dup_strv(value, NULL);
dirs_changed = TRUE;
}
g_variant_unref(value);
g_free(key);
}
if (olddir != priv->text_direction) {
g_object_notify(G_OBJECT(user_data), DBUSMENU_CLIENT_PROP_TEXT_DIRECTION);
}
if (oldstatus != priv->status) {
g_object_notify(G_OBJECT(user_data), DBUSMENU_CLIENT_PROP_STATUS);
}
if (dirs_changed) {
g_signal_emit(G_OBJECT(user_data), signals[ICON_THEME_DIRS], 0, priv->icon_dirs, TRUE);
}
return;
}
/* Handle the case where we change owners */
static void
menuproxy_name_changed_cb (GObject * object, GParamSpec * pspec, gpointer user_data)
{
GDBusProxy * proxy = G_DBUS_PROXY(object);
gchar * owner = g_dbus_proxy_get_name_owner(proxy);
if (owner == NULL) {
/* Oh, no! We lost our owner! */
proxy_destroyed(G_OBJECT(proxy), user_data);
} else {
g_free(owner);
update_layout(DBUSMENU_CLIENT(user_data));
}
return;
}
/* Handle the signals out of the proxy */
static void
menuproxy_signal_cb (GDBusProxy * proxy, gchar * sender, gchar * signal, GVariant * params, gpointer user_data)
{
g_return_if_fail(DBUSMENU_IS_CLIENT(user_data));
DbusmenuClient * client = DBUSMENU_CLIENT(user_data);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
if (g_strcmp0(signal, "LayoutUpdated") == 0) {
guint revision; gint parent;
g_variant_get(params, "(ui)", &revision, &parent);
layout_update(proxy, revision, parent, client);
} else if (priv->root == NULL) {
/* Drop out here, all the rest of these really need to have a root
node so we can just ignore them if there isn't one. */
} else if (g_strcmp0(signal, "ItemPropertiesUpdated") == 0) {
/* Remove before adding just incase there is a duplicate, against the
rules, but we can handle it so let's do it. */
GVariantIter ritems;
GVariant * ritemsv = g_variant_get_child_value(params, 1);
g_variant_iter_init(&ritems, ritemsv);
GVariant * ritem;
while ((ritem = g_variant_iter_next_value(&ritems)) != NULL) {
GVariant * idv = g_variant_get_child_value(ritem, 0);
gint id = g_variant_get_int32(idv);
g_variant_unref(idv);
DbusmenuMenuitem * menuitem = dbusmenu_menuitem_find_id(priv->root, id);
if (menuitem == NULL) {
continue;
}
GVariantIter properties;
GVariant * propv = g_variant_get_child_value(ritem, 1);
g_variant_iter_init(&properties, propv);
gchar * property;
while (g_variant_iter_next(&properties, "s", &property)) {
/* g_debug("Removing property '%s' on %d", property, id); */
dbusmenu_menuitem_property_remove(menuitem, property);
g_free(property);
}
g_variant_unref(ritem);
g_variant_unref(propv);
}
g_variant_unref(ritemsv);
GVariantIter items;
GVariant * itemsv = g_variant_get_child_value(params, 0);
g_variant_iter_init(&items, itemsv);
GVariant * item;
while ((item = g_variant_iter_next_value(&items)) != NULL) {
GVariant * idv = g_variant_get_child_value(item, 0);
gint id = g_variant_get_int32(idv);
g_variant_unref(idv);
GVariantIter properties;
GVariant * propv = g_variant_get_child_value(item, 1);
g_variant_iter_init(&properties, propv);
gchar * property;
GVariant * value;
while (g_variant_iter_next(&properties, "{sv}", &property, &value)) {
GVariant * internalvalue = value;
if (G_LIKELY(g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT))) {
/* Unboxing if needed */
internalvalue = g_variant_get_variant(value);
g_variant_unref(value);
}
id_prop_update(proxy, id, property, internalvalue, client);
g_variant_unref(internalvalue);
}
g_variant_unref(propv);
g_variant_unref(item);
}
g_variant_unref(itemsv);
} else if (g_strcmp0(signal, "ItemPropertyUpdated") == 0) {
gint id; gchar * property; GVariant * value;
g_variant_get(params, "(isv)", &id, &property, &value);
id_prop_update(proxy, id, property, value, client);
g_free(property);
g_variant_unref(value);
} else if (g_strcmp0(signal, "ItemUpdated") == 0) {
gint id;
g_variant_get(params, "(i)", &id);
id_update(proxy, id, client);
} else if (g_strcmp0(signal, "ItemActivationRequested") == 0) {
gint id; guint timestamp;
g_variant_get(params, "(iu)", &id, ×tamp);
item_activated(proxy, id, timestamp, client);
} else {
g_warning("Received signal '%s' from menu proxy that is unknown", signal);
}
return;
}
/* This is the callback for the properties on a menu item. There
should be all of them in the Hash, and they we use foreach to
copy them into the menuitem.
This isn't the most efficient way. We can optimize this by
somehow removing the foreach. But that is for later. */
static void
menuitem_get_properties_cb (GVariant * properties, GError * error, gpointer data)
{
g_return_if_fail(DBUSMENU_IS_MENUITEM(data));
DbusmenuMenuitem * item = DBUSMENU_MENUITEM(data);
if (error != NULL) {
g_warning("Error getting properties on a menuitem: %s", error->message);
g_object_unref(data);
return;
}
GVariantIter * iter = g_variant_iter_new(properties);
gchar * key;
GVariant * value;
while (g_variant_iter_next(iter, "{sv}", &key, &value)) {
dbusmenu_menuitem_property_set_variant(item, key, value);
g_variant_unref(value);
g_free(key);
}
g_variant_iter_free(iter);
g_object_unref(data);
return;
}
/* This function is called to refresh the properites on an item that
is getting recycled with the update, but we think might have prop
changes. */
static void
menuitem_get_properties_replace_cb (GVariant * properties, GError * error, gpointer data)
{
g_return_if_fail(DBUSMENU_IS_MENUITEM(data));
gboolean have_error = FALSE;
if (error != NULL) {
g_warning("Unable to replace properties on %d: %s", dbusmenu_menuitem_get_id(DBUSMENU_MENUITEM(data)), error->message);
have_error = TRUE;
}
GList * current_props = dbusmenu_menuitem_properties_list(DBUSMENU_MENUITEM(data));
GList * tmp = NULL;
for (tmp = current_props; tmp != NULL && have_error == FALSE; tmp = g_list_next(tmp)) {
dbusmenu_menuitem_property_remove(DBUSMENU_MENUITEM(data), (const gchar *)tmp->data);
}
g_list_free(current_props);
if (!have_error) {
menuitem_get_properties_cb(properties, error, data);
} else {
g_object_unref(data);
}
return;
}
/* This is a different get properites call back that also sends
new signals. It basically is a small wrapper around the original. */
static void
menuitem_get_properties_new_cb (GVariant * properties, GError * error, gpointer data)
{
g_return_if_fail(data != NULL);
newItemPropData * propdata = (newItemPropData *)data;
if (error != NULL) {
g_warning("Error getting properties on a new menuitem: %s", error->message);
g_object_unref(propdata->item);
return;
}
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(propdata->client);
/* Extra ref as get_properties will unref once itself */
g_object_ref(propdata->item);
menuitem_get_properties_cb (properties, error, propdata->item);
gboolean handled = FALSE;
const gchar * type;
type_handler_t * th = NULL;
type = dbusmenu_menuitem_property_get(propdata->item, DBUSMENU_MENUITEM_PROP_TYPE);
if (type != NULL) {
th = (type_handler_t *)g_hash_table_lookup(priv->type_handlers, type);
} else {
th = (type_handler_t *)g_hash_table_lookup(priv->type_handlers, DBUSMENU_CLIENT_TYPES_DEFAULT);
}
if (th != NULL && th->cb != NULL) {
handled = th->cb(propdata->item, propdata->parent, propdata->client, th->user_data);
}
#ifdef MASSIVEDEBUGGING
g_debug("Client has realized a menuitem: %d", dbusmenu_menuitem_get_id(propdata->item));
#endif
dbusmenu_menuitem_set_realized(propdata->item);
if (!handled) {
g_signal_emit(G_OBJECT(propdata->client), signals[NEW_MENUITEM], 0, propdata->item, TRUE);
}
g_object_unref(propdata->item);
g_free(propdata);
return;
}
/* Respond to the call function to make sure that the other side
got it, or print a warning. */
static void
menuitem_call_cb (GObject * proxy, GAsyncResult * res, gpointer userdata)
{
GError * error = NULL;
event_data_t * edata = (event_data_t *)userdata;
GVariant * params;
params = g_dbus_proxy_call_finish(G_DBUS_PROXY(proxy), res, &error);
if (error != NULL) {
g_warning("Unable to call event '%s' on menu item %d: %s", edata->event, dbusmenu_menuitem_get_id(edata->menuitem), error->message);
}
g_signal_emit(edata->client, signals[EVENT_RESULT], 0, edata->menuitem, edata->event, edata->variant, edata->timestamp, error, TRUE);
g_variant_unref(edata->variant);
g_free(edata->event);
g_object_unref(edata->menuitem);
g_free(edata);
if (G_UNLIKELY(error != NULL)) {
g_error_free(error);
}
if (G_LIKELY(params != NULL)) {
g_variant_unref(params);
}
return;
}
/* Sends the event over DBus to the server on the other side
of the bus. */
void
dbusmenu_client_send_event (DbusmenuClient * client, gint id, const gchar * name, GVariant * variant, guint timestamp)
{
g_return_if_fail(DBUSMENU_IS_CLIENT(client));
g_return_if_fail(id >= 0);
g_return_if_fail(name != NULL);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
DbusmenuMenuitem * mi = dbusmenu_menuitem_find_id(priv->root, id);
if (mi == NULL) {
g_warning("Asked to activate a menuitem %d that we don't know about", id);
return;
}
if (variant == NULL) {
variant = g_variant_new_int32(0);
}
event_data_t * edata = g_new0(event_data_t, 1);
edata->client = client;
edata->menuitem = mi;
g_object_ref(edata->menuitem);
edata->event = g_strdup(name);
edata->timestamp = timestamp;
edata->variant = variant;
g_variant_ref_sink(variant);
g_dbus_proxy_call(priv->menuproxy,
"Event",
g_variant_new("(isvu)", id, name, variant, timestamp),
G_DBUS_CALL_FLAGS_NONE,
1000, /* timeout */
NULL, /* cancellable */
menuitem_call_cb,
edata);
return;
}
typedef struct _about_to_show_t about_to_show_t;
struct _about_to_show_t {
DbusmenuClient * client;
void (*cb) (gpointer data);
gpointer cb_data;
};
/* Reports errors and responds to update request that were a result
of sending the about to show signal. */
static void
about_to_show_cb (GObject * proxy, GAsyncResult * res, gpointer userdata)
{
gboolean need_update = FALSE;
GError * error = NULL;
about_to_show_t * data = (about_to_show_t *)userdata;
GVariant * params = NULL;
params = g_dbus_proxy_call_finish(G_DBUS_PROXY(proxy), res, &error);
if (error != NULL) {
g_warning("Unable to send about_to_show: %s", error->message);
/* Note: we're just ensuring only the callback gets called */
need_update = FALSE;
g_error_free(error);
error = NULL;
} else {
g_variant_get(params, "(b)", &need_update);
g_variant_unref(params);
}
/* If we need to update, do that first. */
if (need_update) {
update_layout(data->client);
}
if (data->cb != NULL) {
data->cb(data->cb_data);
}
g_object_unref(data->client);
g_free(data);
return;
}
/* Sends the about to show signal for a given id to the
server on the other side of DBus */
void
dbusmenu_client_send_about_to_show(DbusmenuClient * client, gint id, void (*cb)(gpointer data), gpointer cb_data)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
about_to_show_t * data = g_new0(about_to_show_t, 1);
data->client = client;
data->cb = cb;
data->cb_data = cb_data;
g_object_ref(client);
g_dbus_proxy_call(priv->menuproxy,
"AboutToShow",
g_variant_new("(i)", id),
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
NULL, /* cancellable */
about_to_show_cb,
data);
return;
}
/* Builds a new child with property requests and everything
else to clean up the code a bit */
static DbusmenuMenuitem *
parse_layout_new_child (gint id, DbusmenuClient * client, DbusmenuMenuitem * parent)
{
DbusmenuMenuitem * item = NULL;
/* Build a new item */
item = DBUSMENU_MENUITEM(dbusmenu_client_menuitem_new(id, client));
if (parent == NULL) {
dbusmenu_menuitem_set_root(item, TRUE);
}
/* Get the properties queued up for this item */
/* Not happy allocating about this, but I need these :( */
newItemPropData * propdata = g_new0(newItemPropData, 1);
if (propdata != NULL) {
propdata->client = client;
propdata->item = item;
propdata->parent = parent;
g_object_ref(item);
get_properties_globber(client, id, NULL, menuitem_get_properties_new_cb, propdata);
} else {
g_warning("Unable to allocate memory to get properties for menuitem. This menuitem will never be realized.");
}
return item;
}
/* Refresh the properties on this item */
static void
parse_layout_update (DbusmenuMenuitem * item, DbusmenuClient * client)
{
g_object_ref(item);
get_properties_globber(client, dbusmenu_menuitem_get_id(item), NULL, menuitem_get_properties_replace_cb, item);
return;
}
/* Parse recursively through the XML and make it into
objects as need be */
static DbusmenuMenuitem *
parse_layout_xml(DbusmenuClient * client, GVariant * layout, DbusmenuMenuitem * item, DbusmenuMenuitem * parent, GDBusProxy * proxy)
{
if (layout == NULL) {
return NULL;
}
/* First verify and figure out what we've got */
GVariant * idv = g_variant_get_child_value(layout, 0);
gint id = g_variant_get_int32(idv);
g_variant_unref(idv);
if (id < 0) {
return NULL;
}
#ifdef MASSIVEDEBUGGING
g_debug("Client looking at node with id: %d", id);
#endif
g_return_val_if_fail(item != NULL, NULL);
g_return_val_if_fail(id == dbusmenu_menuitem_get_id(item), NULL);
/* Some variables */
GVariantIter children;
GVariant * childrenv;
childrenv = g_variant_get_child_value(layout, 2);
g_variant_iter_init(&children, childrenv);
guint position = 0;
GList * oldchildren = g_list_copy(dbusmenu_menuitem_get_children(item));
/* g_debug("Starting old children: %d", g_list_length(oldchildren)); */
/* Go through all the XML Nodes and make sure that we have menuitems
to cover those XML nodes. */
GVariant * child;
while ((child = g_variant_iter_next_value(&children)) != NULL) {
/* g_debug("Looking at child: %d", position); */
if (g_variant_is_of_type(child, G_VARIANT_TYPE_VARIANT)) {
GVariant * tmp = g_variant_get_variant(child);
g_variant_unref(child);
child = tmp;
}
GVariant * childidv = g_variant_get_child_value(child, 0);
gint childid = g_variant_get_int32(childidv);
g_variant_unref(childidv);
if (childid < 0) {
/* Don't increment the position when there isn't a valid
node in the XML tree. It's probably a comment. */
g_variant_unref(child);
continue;
}
DbusmenuMenuitem * childmi = NULL;
/* First see if we can recycle a node that we've already built
on this menu item */
GList * childsearch = NULL;
for (childsearch = oldchildren; childsearch != NULL; childsearch = g_list_next(childsearch)) {
DbusmenuMenuitem * cs_mi = DBUSMENU_MENUITEM(childsearch->data);
if (childid == dbusmenu_menuitem_get_id(cs_mi)) {
oldchildren = g_list_remove(oldchildren, cs_mi);
childmi = cs_mi;
break;
}
}
if (childmi == NULL) {
#ifdef MASSIVEDEBUGGING
g_debug("Building new menu item %d at position %d", childid, position);
#endif
/* If we can't recycle, then we build a new one */
childmi = parse_layout_new_child(childid, client, item);
dbusmenu_menuitem_child_add_position(item, childmi, position);
g_object_unref(childmi);
} else {
#ifdef MASSIVEDEBUGGING
g_debug("Recycling menu item %d at position %d", childid, position);
#endif
/* If we can recycle, make sure it's in the right place */
dbusmenu_menuitem_child_reorder(item, childmi, position);
parse_layout_update(childmi, client);
}
/* Apply known properties sent in the structure to the
menu item. Sometimes they may just be copies */
if (childmi != NULL) {
GVariantIter iter;
gchar * prop;
GVariant * value;
GVariant * child_props;
/* Set the type first as it can manage the behavior of
all other properties. */
child_props = g_variant_get_child_value(child, 1);
g_variant_iter_init(&iter, child_props);
while (g_variant_iter_next(&iter, "{sv}", &prop, &value)) {
if (g_strcmp0(prop, DBUSMENU_MENUITEM_PROP_TYPE) == 0) {
dbusmenu_menuitem_property_set_variant(childmi, prop, value);
}
g_free(prop);
g_variant_unref(value);
}
/* Now go through and do all the properties. */
g_variant_iter_init(&iter, child_props);
while (g_variant_iter_next(&iter, "{sv}", &prop, &value)) {
dbusmenu_menuitem_property_set_variant(childmi, prop, value);
g_free(prop);
g_variant_unref(value);
}
g_variant_unref(child_props);
}
position++;
g_variant_unref(child);
}
/* Remove any children that are no longer used by this version of
the layout. */
GList * oldchildleft = NULL;
for (oldchildleft = oldchildren; oldchildleft != NULL; oldchildleft = g_list_next(oldchildleft)) {
DbusmenuMenuitem * oldmi = DBUSMENU_MENUITEM(oldchildleft->data);
#ifdef MASSIVEDEBUGGING
g_debug("Unref'ing menu item with layout update. ID: %d", dbusmenu_menuitem_get_id(oldmi));
#endif
dbusmenu_menuitem_child_delete(item, oldmi);
}
g_list_free(oldchildren);
/* We've got everything built up at this node and reconcilled */
/* Flush the properties requests if this is the first level */
if (parent != NULL && dbusmenu_menuitem_get_id(parent) == 0) {
get_properties_flush(client);
}
/* now it's time to recurse down the tree. */
g_variant_iter_init(&children, childrenv);
child = g_variant_iter_next_value(&children);
GList * childmis = dbusmenu_menuitem_get_children(item);
while (child != NULL && childmis != NULL) {
if (g_variant_is_of_type(child, G_VARIANT_TYPE_VARIANT)) {
GVariant * tmp = g_variant_get_variant(child);
g_variant_unref(child);
child = tmp;
}
GVariant * xmlidv = g_variant_get_child_value(child, 0);
gint xmlid = g_variant_get_int32(xmlidv);
g_variant_unref(xmlidv);
/* If this isn't a valid menu item we need to move on
until we have one. This avoids things like comments. */
if (xmlid < 0) {
g_variant_unref(child);
child = g_variant_iter_next_value(&children);
continue;
}
#ifdef MASSIVEDEBUGGING
gint miid = dbusmenu_menuitem_get_id(DBUSMENU_MENUITEM(childmis->data));
g_debug("Recursing parse_layout_xml. XML ID: %d MI ID: %d", xmlid, miid);
#endif
parse_layout_xml(client, child, DBUSMENU_MENUITEM(childmis->data), item, proxy);
g_variant_unref(child);
child = g_variant_iter_next_value(&children);
childmis = g_list_next(childmis);
}
g_variant_unref(childrenv);
if (child != NULL) {
g_warning("Sync failed, now we've got extra layout nodes.");
}
if (childmis != NULL) {
g_warning("Sync failed, now we've got extra menu items.");
}
return item;
}
/* Take the layout passed to us over DBus and turn it into
a set of beautiful objects */
static gint
parse_layout (DbusmenuClient * client, GVariant * layout)
{
#ifdef MASSIVEDEBUGGING
g_debug("Client Parsing a new layout");
#endif
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
DbusmenuMenuitem * oldroot = priv->root;
if (priv->root == NULL) {
priv->root = parse_layout_new_child(0, client, NULL);
} else {
parse_layout_update(priv->root, client);
}
priv->root = parse_layout_xml(client, layout, priv->root, NULL, priv->menuproxy);
if (priv->root == NULL) {
g_warning("Unable to parse layout on client %s object %s: %s", priv->dbus_name, priv->dbus_object, g_variant_print(layout, TRUE));
}
if (priv->root != oldroot) {
#ifdef MASSIVEDEBUGGING
g_debug("Client signaling root changed.");
#endif
/* If they are different, and there was an old root we must
clean up that old root */
if (oldroot != NULL) {
dbusmenu_menuitem_set_root(oldroot, FALSE);
g_object_unref(oldroot);
oldroot = NULL;
}
/* If the root changed we can signal that */
g_signal_emit(G_OBJECT(client), signals[ROOT_CHANGED], 0, priv->root, TRUE);
}
return 1;
}
/* When the layout property returns, here's where we take care of that. */
static void
update_layout_cb (GObject * proxy, GAsyncResult * res, gpointer data)
{
DbusmenuClient * client = DBUSMENU_CLIENT(data);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
GError * error = NULL;
GVariant * params = NULL;
GVariant * layout = NULL;
params = g_dbus_proxy_call_finish(G_DBUS_PROXY(proxy), res, &error);
if (error != NULL) {
g_warning("Getting layout failed: %s", error->message);
g_error_free(error);
goto out;
}
GVariant * revv = g_variant_get_child_value(params, 0);
guint rev = g_variant_get_uint32(revv);
g_variant_unref(revv);
layout = g_variant_get_child_value(params, 1);
guint parseable = parse_layout(client, layout);
if (parseable == 0) {
g_warning("Unable to parse layout!");
goto out;
}
priv->my_revision = rev;
/* g_debug("Root is now: 0x%X", (unsigned int)priv->root); */
#ifdef MASSIVEDEBUGGING
g_debug("Client signaling layout has changed.");
#endif
g_signal_emit(G_OBJECT(client), signals[LAYOUT_UPDATED], 0, TRUE);
/* Check to see if we got another update in the time this
one was issued. */
if (priv->my_revision < priv->current_revision) {
update_layout(client);
}
out:
if (priv->layoutcall != NULL) {
g_object_unref(priv->layoutcall);
priv->layoutcall = NULL;
}
if (layout != NULL) {
g_variant_unref(layout);
}
if (params != NULL) {
g_variant_unref(params);
}
g_object_unref(G_OBJECT(client));
return;
}
/* Call the property on the server we're connected to and set it up to
be async back to _update_layout_cb */
static void
update_layout (DbusmenuClient * client)
{
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
g_return_if_fail(priv->layout_props != NULL);
if (priv->menuproxy == NULL) {
return;
}
gchar * name_owner = g_dbus_proxy_get_name_owner(priv->menuproxy);
if (name_owner == NULL) {
return;
}
g_free(name_owner);
if (priv->layoutcall != NULL) {
return;
}
priv->layoutcall = g_cancellable_new();
GVariantBuilder tupleb;
g_variant_builder_init(&tupleb, G_VARIANT_TYPE_TUPLE);
g_variant_builder_add_value(&tupleb, g_variant_new_int32(0)); // root
g_variant_builder_add_value(&tupleb, g_variant_new_int32(-1)); // recurse
g_variant_builder_add_value(&tupleb, priv->layout_props); // props
GVariant * args = g_variant_builder_end(&tupleb);
// g_debug("Args (type: %s): %s", g_variant_get_type_string(args), g_variant_print(args, TRUE));
g_object_ref(G_OBJECT(client));
g_dbus_proxy_call(priv->menuproxy,
"GetLayout",
args,
G_DBUS_CALL_FLAGS_NONE,
-1, /* timeout */
priv->layoutcall, /* cancellable */
update_layout_cb,
client);
return;
}
/* Public API */
/**
* dbusmenu_client_new:
* @name: The DBus name for the server to connect to
* @object: The object on the server to monitor
*
* This function creates a new client that connects to a specific
* server on DBus. That server is at a specific location sharing
* a known object. The interface is assumed by the code to be
* the DBus menu interface. The newly created client will start
* sending out events as it syncs up with the server.
*
* Return value: A brand new #DbusmenuClient
*/
DbusmenuClient *
dbusmenu_client_new (const gchar * name, const gchar * object)
{
DbusmenuClient * self = g_object_new(DBUSMENU_TYPE_CLIENT,
DBUSMENU_CLIENT_PROP_DBUS_NAME, name,
DBUSMENU_CLIENT_PROP_DBUS_OBJECT, object,
NULL);
return self;
}
/**
* dbusmenu_client_get_root:
* @client: The #DbusmenuClient to get the root node from
*
* Grabs the root node for the specified client @client. This
* function may block. It will block if there is currently a
* call to update the layout, it will block on that layout
* updated and then return the newly updated layout. Chances
* are that this update is in the queue for the mainloop as
* it would have been requested some time ago, but in theory
* it could block longer.
*
* Return value: (transfer none): A #DbusmenuMenuitem representing the root of
* menu on the server. If there is no server or there is
* an error receiving its layout it'll return #NULL.
*/
DbusmenuMenuitem *
dbusmenu_client_get_root (DbusmenuClient * client)
{
g_return_val_if_fail(DBUSMENU_IS_CLIENT(client), NULL);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
#ifdef MASSIVEDEBUGGING
g_debug("Client get root: %X", (guint)priv->root);
#endif
return priv->root;
}
/* Remove the type handler when we're all done with it */
static void
type_handler_destroy (gpointer user_data)
{
type_handler_t * th = (type_handler_t *)user_data;
if (th->destroy_cb != NULL) {
th->destroy_cb(th->user_data);
}
g_free(th->type);
g_free(th);
return;
}
/**
* dbusmenu_client_add_type_handler:
* @client: Client where we're getting types coming in
* @type: A text string that will be matched with the 'type'
* property on incoming menu items
* @newfunc: (scope notified): The function that will be executed with those new
* items when they come in.
*
* This function connects into the type handling of the #DbusmenuClient.
* Every new menuitem that comes in immediately gets asked for it's
* properties. When we get those properties we check the 'type'
* property and look to see if it matches a handler that is known
* by the client. If so, the @newfunc function is executed on that
* #DbusmenuMenuitem. If not, then the DbusmenuClient::new-menuitem
* signal is sent.
*
* In the future the known types will be sent to the server so that it
* can make choices about the menu item types availble.
*
* Return value: If registering the new type was successful.
*/
gboolean
dbusmenu_client_add_type_handler (DbusmenuClient * client, const gchar * type, DbusmenuClientTypeHandler newfunc)
{
return dbusmenu_client_add_type_handler_full(client, type, newfunc, NULL, NULL);
}
/**
* dbusmenu_client_add_type_handler_full:
* @client: Client where we're getting types coming in
* @type: A text string that will be matched with the 'type'
* property on incoming menu items
* @newfunc: (scope notified): The function that will be executed with those new
* items when they come in.
* @user_data: Data passed to @newfunc when it is called
* @destroy_func: A function that is called when the type handler is
* removed (usually on client destruction) which will free
* the resources in @user_data.
*
* This function connects into the type handling of the #DbusmenuClient.
* Every new menuitem that comes in immediately gets asked for it's
* properties. When we get those properties we check the 'type'
* property and look to see if it matches a handler that is known
* by the client. If so, the @newfunc function is executed on that
* #DbusmenuMenuitem. If not, then the DbusmenuClient::new-menuitem
* signal is sent.
*
* In the future the known types will be sent to the server so that it
* can make choices about the menu item types availble.
*
* Return value: If registering the new type was successful.
*/
gboolean
dbusmenu_client_add_type_handler_full (DbusmenuClient * client, const gchar * type, DbusmenuClientTypeHandler newfunc, gpointer user_data, GDestroyNotify destroy_func)
{
g_return_val_if_fail(DBUSMENU_IS_CLIENT(client), FALSE);
g_return_val_if_fail(type != NULL, FALSE);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
#ifdef MASSIVEDEBUGGING
g_debug("Adding a type handler for '%s'", type);
#endif
if (priv->type_handlers == NULL) {
g_warning("Type handlers hashtable not built");
return FALSE;
}
gpointer value = g_hash_table_lookup(priv->type_handlers, type);
if (value != NULL) {
g_warning("Type '%s' already had a registered handler.", type);
return FALSE;
}
type_handler_t * th = g_new0(type_handler_t, 1);
th->client = client;
th->cb = newfunc;
th->destroy_cb = destroy_func;
th->user_data = user_data;
th->type = g_strdup(type);
g_hash_table_insert(priv->type_handlers, g_strdup(type), th);
return TRUE;
}
/**
dbusmenu_client_get_text_direction:
@client: #DbusmenuClient to check the text direction on
Gets the text direction that the server is exporting. If
the server is not exporting a direction then the value
#DBUSMENU_TEXT_DIRECTION_NONE will be returned.
Return value: Text direction being exported.
*/
DbusmenuTextDirection
dbusmenu_client_get_text_direction (DbusmenuClient * client)
{
g_return_val_if_fail(DBUSMENU_IS_CLIENT(client), DBUSMENU_TEXT_DIRECTION_NONE);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
return priv->text_direction;
}
/**
dbusmenu_client_get_status:
@client: #DbusmenuClient to check the status on
Gets the recommended current status that the server
is exporting for the menus. In situtations where the
value is #DBUSMENU_STATUS_NOTICE it is recommended that
the client show the menus to the user an a more noticible
way.
Return value: Status being exported.
*/
DbusmenuStatus
dbusmenu_client_get_status (DbusmenuClient * client)
{
g_return_val_if_fail(DBUSMENU_IS_CLIENT(client), DBUSMENU_STATUS_NORMAL);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
return priv->status;
}
/**
* dbusmenu_client_get_icon_paths:
* @client: The #DbusmenuClient to get the icon paths from
*
* Gets the stored and exported icon paths from the client.
*
* Return value: (transfer none): A NULL-terminated list of icon paths with
* memory managed by the client. Duplicate if you want
* to keep them.
*/
const GStrv
dbusmenu_client_get_icon_paths (DbusmenuClient * client)
{
g_return_val_if_fail(DBUSMENU_IS_CLIENT(client), NULL);
DbusmenuClientPrivate * priv = DBUSMENU_CLIENT_GET_PRIVATE(client);
return priv->icon_dirs;
}
|