@@ -0,0 +1,186 @@
1+/*
2+ * Copyright (C) 2013 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ *
16+ * Author: Nick Dedekind <nick.dedekind@canonical.com>
17+ */
18+
19+
20+#include "unitymenumodelpaths.h"
21+#include <QDBusArgument>
22+#include <QDebug>
23+
24+static QVariant parseVariantData(const QVariant& var);
25+
26+UnityMenuModelPaths::UnityMenuModelPaths(QObject *parent)
27+ : QObject(parent)
28+{
29+}
30+
31+QVariant UnityMenuModelPaths::source() const
32+{
33+ return m_sourceData;
34+}
35+
36+void UnityMenuModelPaths::setSource(const QVariant& data)
37+{
38+ if (m_sourceData != data) {
39+ m_sourceData = data;
40+ Q_EMIT sourceChanged();
41+
42+ updateData();
43+ }
44+}
45+
46+const QDBusArgument &operator>>(const QDBusArgument &arg, QVariantMap &map)
47+{
48+ arg.beginMap();
49+ map.clear();
50+ while (!arg.atEnd()) {
51+ QString key;
52+ QVariant value;
53+ arg.beginMapEntry();
54+
55+ arg >> key >> value;
56+ map.insertMulti(key, parseVariantData(value)); // re-parse for qdbusargument
57+
58+ arg.endMapEntry();
59+ }
60+ arg.endMap();
61+ return arg;
62+}
63+
64+static QVariant parseVariantData(const QVariant& var) {
65+ if ((int)var.type() == QMetaType::User) {
66+ if (var.userType() == qMetaTypeId<QDBusArgument>()) {
67+
68+ QDBusArgument arg(var.value<QDBusArgument>());
69+ if (arg.currentType() == QDBusArgument::MapType) {
70+ QVariantMap map;
71+ arg >> map;
72+ return map;
73+ }
74+ return arg.asVariant();
75+ }
76+ }
77+ return var;
78+}
79+
80+void UnityMenuModelPaths::updateData()
81+{
82+ QVariantMap dataMap = parseVariantData(m_sourceData).toMap();
83+
84+ if (!m_busNameHint.isEmpty() && dataMap.contains(m_busNameHint)) {
85+ setBusName(dataMap[m_busNameHint].toByteArray());
86+ } else {
87+ setBusName("");
88+ }
89+
90+ if (!m_menuObjectPathHint.isEmpty() && dataMap.contains(m_menuObjectPathHint)) {
91+ setMenuObjectPath(dataMap[m_menuObjectPathHint].toByteArray());
92+ } else {
93+ setMenuObjectPath("");
94+ }
95+
96+ if (!m_actionsHint.isEmpty() && dataMap.contains(m_actionsHint)) {
97+ setActions(dataMap[m_actionsHint].toMap());
98+ } else {
99+ setActions(QVariantMap());
100+ }
101+}
102+
103+QByteArray UnityMenuModelPaths::busName() const
104+{
105+ return m_busName;
106+}
107+
108+void UnityMenuModelPaths::setBusName(const QByteArray &name)
109+{
110+ if (m_busName != name) {
111+ m_busName = name;
112+ Q_EMIT busNameChanged();
113+ }
114+}
115+
116+QVariantMap UnityMenuModelPaths::actions() const
117+{
118+ return m_actions;
119+}
120+
121+void UnityMenuModelPaths::setActions(const QVariantMap &actions)
122+{
123+ if (m_actions != actions) {
124+ qDebug() << "UnityMenuModelPaths setActions " << actions;
125+ m_actions = actions;
126+ Q_EMIT actionsChanged();
127+ }
128+}
129+
130+QByteArray UnityMenuModelPaths::menuObjectPath() const
131+{
132+ return m_menuObjectPath;
133+}
134+
135+void UnityMenuModelPaths::setMenuObjectPath(const QByteArray &path)
136+{
137+ if (m_menuObjectPath != path) {
138+ m_menuObjectPath = path;
139+ Q_EMIT menuObjectPathChanged();
140+ }
141+}
142+
143+QByteArray UnityMenuModelPaths::busNameHint() const
144+{
145+ return m_busNameHint;
146+}
147+
148+void UnityMenuModelPaths::setBusNameHint(const QByteArray &nameHint)
149+{
150+ if (m_busNameHint != nameHint) {
151+ m_busNameHint = nameHint;
152+ Q_EMIT busNameHintChanged();
153+
154+ updateData();
155+ }
156+}
157+
158+QByteArray UnityMenuModelPaths::actionsHint() const
159+{
160+ return m_actionsHint;
161+}
162+
163+void UnityMenuModelPaths::setActionsHint(const QByteArray &actionsHint)
164+{
165+ if (m_actionsHint != actionsHint) {
166+ m_actionsHint = actionsHint;
167+ Q_EMIT actionsHintChanged();
168+
169+ updateData();
170+ }
171+}
172+
173+QByteArray UnityMenuModelPaths::menuObjectPathHint() const
174+{
175+ return m_menuObjectPathHint;
176+}
177+
178+void UnityMenuModelPaths::setMenuObjectPathHint(const QByteArray &pathHint)
179+{
180+ if (m_menuObjectPathHint != pathHint) {
181+ m_menuObjectPathHint = pathHint;
182+ Q_EMIT menuObjectPathHintChanged();
183+
184+ updateData();
185+ }
186+}