2 * Copyright 2013 Canonical Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; version 3.
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 Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Nick Dedekind <nick.dedekind@canonical.com>
20#include "rootactionstate.h"
21#include "indicators.h"
23#include <unitymenumodel.h>
32RootActionState::RootActionState(QObject *parent)
33 : ActionStateParser(parent),
38RootActionState::~RootActionState()
41 m_menu->disconnect(this);
42 m_menu->setActionStateParser(NULL);
46UnityMenuModel* RootActionState::menu() const
51void RootActionState::setMenu(UnityMenuModel* menu)
55 m_menu->disconnect(this);
56 m_menu->setActionStateParser(NULL);
61 connect(m_menu, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SLOT(onModelRowsAdded(const QModelIndex&, int, int)));
62 connect(m_menu, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SLOT(onModelRowsRemoved(const QModelIndex&, int, int)));
63 connect(m_menu, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)), SLOT(onModelDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int>&)));
65 m_menu->setActionStateParser(this);
72void RootActionState::onModelRowsAdded(const QModelIndex& parent, int start, int end)
75 if (start == 0 && end >= 0) {
80void RootActionState::onModelRowsRemoved(const QModelIndex& parent, int start, int end)
83 if (start == 0 && end >= 0) {
88void RootActionState::onModelDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles)
91 if (!topLeft.isValid() || !bottomRight.isValid()) {
95 if (topLeft.row() <= 0 && bottomRight.row() >= 0) {
100void RootActionState::updateActionState()
102 if (m_menu && m_menu->rowCount() > 0) {
103 m_cachedState = m_menu->get(0, "actionState").toMap();
105 m_cachedState.clear();
110bool RootActionState::isValid() const
112 return m_menu && m_menu->rowCount() > 0;
115QString RootActionState::title() const
117 if (!isValid()) return QString();
119 return m_cachedState.value("title", QVariant::fromValue(QString())).toString();
122QString RootActionState::leftLabel() const
124 if (!isValid()) return QString();
126 return m_cachedState.value("pre-label", QVariant::fromValue(QString())).toString();
129QString RootActionState::rightLabel() const
131 if (!isValid()) return QString();
133 return m_cachedState.value("label", QVariant::fromValue(QString())).toString();
136QStringList RootActionState::icons() const
138 if (!isValid()) return QStringList();
140 return m_cachedState.value("icons", QVariant::fromValue(QStringList())).toStringList();
143QString RootActionState::accessibleName() const
145 if (!isValid()) return QString();
147 return m_cachedState.value("accessible-desc", QVariant::fromValue(QString())).toString();
150bool RootActionState::isVisible() const
152 if (!isValid()) return false;
154 return m_cachedState.value("visible", QVariant::fromValue(true)).toBool();
157static QString iconUri(GIcon *icon)
161 if (G_IS_THEMED_ICON (icon)) {
162 const gchar* const* iconNames = g_themed_icon_get_names (G_THEMED_ICON (icon));
164 QStringList iconNameList;
165 for (uint index = 0; iconNames[index] != NULL; index++) {
166 iconNameList << iconNames[index];
169 if (!iconNameList.empty()) {
170 uri = QString("image://theme/%1").arg(iconNameList.join(","));
173 else if (G_IS_FILE_ICON (icon)) {
176 file = g_file_icon_get_file (G_FILE_ICON (icon));
177 if (g_file_is_native (file)) {
180 fileuri = g_file_get_path (file);
181 uri = QString(fileuri);
186 else if (G_IS_BYTES_ICON (icon)) {
191 data = g_bytes_get_data (g_bytes_icon_get_bytes (G_BYTES_ICON (icon)), &size);
192 base64 = g_base64_encode ((const guchar *) data, size);
194 uri = QString("data://");
203QVariant RootActionState::toQVariant(GVariant* state) const
209 if (g_variant_is_of_type(state, G_VARIANT_TYPE_VARDICT)) {
215 g_variant_iter_init (&iter, state);
216 while (g_variant_iter_loop (&iter, "{sv}", &key, &vvalue))
218 QString str = QString::fromUtf8(key);
219 if (str == "icon" && !qmap.contains("icons")) {
222 // FIXME - should be sending a url.
223 GIcon *gicon = g_icon_deserialize (vvalue);
225 icons << iconUri(gicon);
226 g_object_unref (gicon);
228 qmap.insert("icons", icons);
230 } else if (str == "icons") {
234 if (g_variant_type_is_array(g_variant_get_type(vvalue))) {
237 for (int i = 0, iMax = g_variant_n_children(vvalue); i < iMax; i++) {
238 GVariant *child = g_variant_get_child_value(vvalue, i);
240 // FIXME - should be sending a url.
241 GIcon *gicon = g_icon_deserialize (child);
243 icons << iconUri(gicon);
244 g_object_unref (gicon);
246 g_variant_unref(child);
249 // will overwrite icon.
250 qmap.insert("icons", icons);
253 qmap.insert(str, ActionStateParser::toQVariant(vvalue));
257 return QVariant::fromValue(qmap);
259 } else if (g_variant_is_of_type (state, G_VARIANT_TYPE ("(sssb)"))) {
264 char* accessible_name;
268 g_variant_get(state, "(sssb)", &label,
273 qmap["label"] = label ? QString::fromUtf8(label) : "";
274 qmap["accessible-desc"] = accessible_name ? QString::fromUtf8(accessible_name) : "";
275 qmap["visible"] = visible;
277 gicon = g_icon_new_for_string (icon, NULL);
279 qmap["icons"] = QStringList() << iconUri(gicon);
280 g_object_unref (gicon);
283 if (label) g_free(label);
284 if (icon) g_free(icon);
285 if (accessible_name) g_free(accessible_name);
287 return QVariant::fromValue(qmap);
289 return ActionStateParser::toQVariant(state);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list