@@ -0,0 +1,243 @@
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+ * Authors: Nick Dedekind <nick.dedekind@canonical.com>
17+ */
18+
19+#include "unitymenumodel.h"
20+#include <QDebug>
21+
22+enum MenuRoles {
23+ LabelRole = Qt::DisplayRole + 1,
24+ SensitiveRole,
25+ IsSeparatorRole,
26+ IconRole,
27+ TypeRole,
28+ ExtendedAttributesRole,
29+ ActionRole,
30+ ActionStateRole,
31+ IsCheckRole,
32+ IsRadioRole,
33+ IsToggledRole
34+};
35+
36+UnityMenuModel::UnityMenuModel(QObject *parent)
37+: QAbstractListModel(parent)
38+{
39+}
40+
41+UnityMenuModel::UnityMenuModel(const QVariant& data, QObject *parent)
42+: QAbstractListModel(parent)
43+{
44+ QVariantList li = data.toList();
45+ m_modelData = li;
46+}
47+
48+QVariant UnityMenuModel::modelData() const
49+{
50+ return m_modelData;
51+}
52+
53+void UnityMenuModel::setModelData(const QVariant& data)
54+{
55+ beginResetModel();
56+
57+ m_modelData.clear();
58+ m_modelData = data.toList();
59+
60+ endResetModel();
61+}
62+
63+
64+void UnityMenuModel::insertRow(int row, const QVariant& data)
65+{
66+ row = qMin(row, rowCount());
67+
68+ beginInsertRows(QModelIndex(), row, row);
69+
70+ m_modelData.insert(row, data);
71+
72+ endInsertRows();
73+}
74+
75+void UnityMenuModel::appendRow(const QVariant& data)
76+{
77+ insertRow(rowCount(), data);
78+}
79+
80+void UnityMenuModel::removeRow(int row)
81+{
82+ if (row < 0 || rowCount() <= row) {
83+ return;
84+ }
85+
86+ beginRemoveRows(QModelIndex(), row, row);
87+
88+ m_modelData.removeAt(row);
89+
90+ endRemoveRows();
91+}
92+
93+UnityMenuModel::~UnityMenuModel()
94+{
95+}
96+
97+QByteArray UnityMenuModel::busName() const
98+{
99+ return m_busName;
100+}
101+
102+void UnityMenuModel::setBusName(const QByteArray &busName)
103+{
104+ this->m_busName = busName;
105+}
106+
107+QVariantMap UnityMenuModel::actions() const
108+{
109+ return m_actions;
110+}
111+
112+void UnityMenuModel::setActions(const QVariantMap &actions)
113+{
114+ this->m_actions = actions;
115+}
116+
117+QByteArray UnityMenuModel::menuObjectPath() const
118+{
119+ return m_menuObjectPath;
120+}
121+
122+void UnityMenuModel::setMenuObjectPath(const QByteArray &path)
123+{
124+ this->m_menuObjectPath = path;
125+}
126+
127+ActionStateParser* UnityMenuModel::actionStateParser() const
128+{
129+ return NULL;
130+}
131+
132+void UnityMenuModel::setActionStateParser(ActionStateParser*)
133+{
134+}
135+
136+int UnityMenuModel::rowCount(const QModelIndex&) const
137+{
138+ return m_modelData.count();
139+}
140+
141+int UnityMenuModel::columnCount(const QModelIndex&) const
142+{
143+ return 1;
144+}
145+
146+QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
147+{
148+ return rowData(index.row())[roleNames()[role]];
149+}
150+
151+QVariantMap UnityMenuModel::rowData(int row) const
152+{
153+ return m_modelData[row].toMap()["rowData"].toMap();
154+}
155+
156+QVariant UnityMenuModel::subMenuData(int row) const
157+{
158+ return m_modelData[row].toMap()["submenu"];
159+}
160+
161+QModelIndex UnityMenuModel::index(int row, int column, const QModelIndex&) const
162+{
163+ return createIndex(row, column);
164+}
165+
166+QModelIndex UnityMenuModel::parent(const QModelIndex &) const
167+{
168+ return QModelIndex();
169+}
170+
171+QHash<int, QByteArray> UnityMenuModel::roleNames() const
172+{
173+ QHash<int, QByteArray> names;
174+
175+ names[LabelRole] = "label";
176+ names[SensitiveRole] = "sensitive";
177+ names[IsSeparatorRole] = "isSeparator";
178+ names[IconRole] = "icon";
179+ names[TypeRole] = "type";
180+ names[ExtendedAttributesRole] = "ext";
181+ names[ActionRole] = "action";
182+ names[ActionStateRole] = "actionState";
183+ names[IsCheckRole] = "isCheck";
184+ names[IsRadioRole] = "isRadio";
185+ names[IsToggledRole] = "isToggled";
186+
187+ return names;
188+}
189+
190+QObject * UnityMenuModel::submenu(int position, QQmlComponent*)
191+{
192+ if (position < 0 || m_modelData.count() < position) {
193+ return NULL;
194+ }
195+
196+ while (submenus.count() <= position) {
197+ submenus.append(NULL);
198+ }
199+
200+ QVariant submenuData = subMenuData(position);
201+ if (submenuData.type() == (int)QMetaType::QVariantList) {
202+ UnityMenuModel*& model = submenus[position];
203+ if (!model) {
204+ model = new UnityMenuModel(submenuData, this);
205+ return model;
206+ }
207+ }
208+
209+ return NULL;
210+}
211+
212+bool UnityMenuModel::loadExtendedAttributes(int, const QVariantMap &)
213+{
214+ return false;
215+}
216+
217+QVariant UnityMenuModel::get(int row, const QByteArray &role)
218+{
219+ static QHash<QByteArray, int> roles;
220+ if (roles.isEmpty()) {
221+ QHash<int, QByteArray> names = roleNames();
222+ Q_FOREACH (int role, names.keys())
223+ roles.insert(names[role], role);
224+ }
225+
226+ return this->data(this->index(row, 0), roles[role]);
227+}
228+
229+void UnityMenuModel::activate(int, const QVariant&)
230+{
231+}
232+
233+void UnityMenuModel::changeState(int, const QVariant&)
234+{
235+}
236+
237+void UnityMenuModel::registerAction(UnityMenuAction*)
238+{
239+}
240+
241+void UnityMenuModel::unregisterAction(UnityMenuAction*)
242+{
243+}