on e82caeb help sign in
unity8/plugins/Unity/iconutils.cpp
91 lines 3.2 kB history raw
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:
17 * Michal Hruby <michal.hruby@canonical.com>
18 */
20#include <QStringList>
21#include <QUrl>
23#include <gio/gio.h>
24#include "iconutils.h"
26#define BASE_THEME_ICON_URI "image://theme/"
28QString gIconToDeclarativeImageProviderString(QString const &giconString)
30 if (giconString.isEmpty()) return giconString;
32 if (giconString.startsWith('/') || giconString.startsWith(QLatin1String("http")) ||
33 giconString.startsWith(QLatin1String("file:")) || giconString.startsWith(QLatin1String("image:"))) {
34 return giconString;
35 }
37 if (!giconString.startsWith(QLatin1String(". "))) {
38 // must be a themed icon
39 QString themedIcon(BASE_THEME_ICON_URI);
40 themedIcon.append(giconString);
41 return themedIcon;
42 }
44 // special case annotated icon
45 if (giconString.startsWith(QLatin1String(". UnityProtocolAnnotatedIcon "))) {
46 QString annotatedIcon;
47 QString serializedData(QUrl::fromPercentEncoding(giconString.mid(29).toUtf8()));
48 GVariant *variant = g_variant_parse(G_VARIANT_TYPE_VARDICT, serializedData.toUtf8().constData(), NULL, NULL, NULL);
49 gchar *baseUri;
50 if (variant != NULL && g_variant_lookup(variant, "base-icon", "&s", &baseUri)) {
51 annotatedIcon = gIconToDeclarativeImageProviderString(QString(baseUri));
52 // FIXME: enclose in image://annotated/... once unity supports that
53 }
54 if (variant != NULL) g_variant_unref(variant);
56 return annotatedIcon;
57 }
59 // handle real gicon strings
60 QString result;
61 GError *error = NULL;
62 GIcon *icon = g_icon_new_for_string(giconString.toLocal8Bit().constData(), &error);
63 if (error != NULL || icon == NULL) {
64 qWarning("Unable to deserialize icon: %s", giconString.toLocal8Bit().constData());
65 g_clear_error(&error);
66 return result;
67 }
69 if (G_IS_THEMED_ICON(icon)) {
70 QString themedIcon(BASE_THEME_ICON_URI);
71 QStringList list;
72 const char* const *iconNames = g_themed_icon_get_names(G_THEMED_ICON(icon));
73 if (iconNames != NULL) {
74 for (const char * const *iter = iconNames; *iter != NULL; ++iter) {
75 list << QLatin1String(*iter);
76 }
77 }
78 themedIcon.append(list.join(QString(",")));
79 result = themedIcon;
80 } else if (G_IS_FILE_ICON(icon)) {
81 GFile *file = g_file_icon_get_file(G_FILE_ICON(icon));
82 gchar *uri = g_file_get_uri(file);
83 QString iconUri(uri);
84 g_free(uri);
85 result = iconUri;
86 }
88 g_object_unref(icon);
90 return result;