~ps-jenkins/unity-scopes-shell/ubuntu-rtm-14.09-proposed

« back to all changes in this revision

Viewing changes to src/iconutils.cpp

  • Committer: Michal Hruby
  • Date: 2013-11-07 17:48:16 UTC
  • Revision ID: michal.mhr@gmail.com-20131107174816-w1vqq6juarrawx23
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
19
 
 
20
#include <QStringList>
 
21
#include <QUrl>
 
22
 
 
23
#include <gio/gio.h>
 
24
#include "iconutils.h"
 
25
 
 
26
#define BASE_THEME_ICON_URI "image://theme/"
 
27
#define BASE_THUMBNAILER_URI "image://thumbnailer/"
 
28
#define BASE_ALBUMART_URI "image://albumart/"
 
29
 
 
30
QString gIconToDeclarativeImageProviderString(QString const &giconString)
 
31
{
 
32
    if (giconString.isEmpty()) return giconString;
 
33
 
 
34
    if (giconString.startsWith('/') || giconString.startsWith(QLatin1String("http")) ||
 
35
            giconString.startsWith(QLatin1String("file:")) || giconString.startsWith(QLatin1String("image:"))) {
 
36
        return giconString;
 
37
    }
 
38
 
 
39
    if (!giconString.startsWith(QLatin1String(". "))) {
 
40
        // must be a themed icon
 
41
        QString themedIcon(BASE_THEME_ICON_URI);
 
42
        themedIcon.append(giconString);
 
43
        return themedIcon;
 
44
    }
 
45
 
 
46
    // special case annotated icon
 
47
    if (giconString.startsWith(QLatin1String(". UnityProtocolAnnotatedIcon "))) {
 
48
        QString annotatedIcon;
 
49
        QString serializedData(QUrl::fromPercentEncoding(giconString.mid(29).toUtf8()));
 
50
        GVariant *variant = g_variant_parse(G_VARIANT_TYPE_VARDICT, serializedData.toUtf8().constData(), NULL, NULL, NULL);
 
51
        gchar *baseUri;
 
52
        if (variant != NULL && g_variant_lookup(variant, "base-icon", "&s", &baseUri)) {
 
53
            annotatedIcon = gIconToDeclarativeImageProviderString(QString(baseUri));
 
54
            // FIXME: enclose in image://annotated/... once unity supports that
 
55
        }
 
56
        if (variant != NULL) g_variant_unref(variant);
 
57
 
 
58
        return annotatedIcon;
 
59
    }
 
60
 
 
61
    // handle real gicon strings
 
62
    QString result;
 
63
    GError *error = NULL;
 
64
    GIcon *icon = g_icon_new_for_string(giconString.toLocal8Bit().constData(), &error);
 
65
    if (error != NULL || icon == NULL) {
 
66
        qWarning("Unable to deserialize icon: %s", giconString.toLocal8Bit().constData());
 
67
        g_clear_error(&error);
 
68
        return result;
 
69
    }
 
70
 
 
71
    if (G_IS_THEMED_ICON(icon)) {
 
72
        QString themedIcon(BASE_THEME_ICON_URI);
 
73
        QStringList list;
 
74
        const char* const *iconNames = g_themed_icon_get_names(G_THEMED_ICON(icon));
 
75
        if (iconNames != NULL) {
 
76
            for (const char * const *iter = iconNames; *iter != NULL; ++iter) {
 
77
                list << QLatin1String(*iter);
 
78
            }
 
79
        }
 
80
        themedIcon.append(list.join(QString(",")));
 
81
        result = themedIcon;
 
82
    } else if (G_IS_FILE_ICON(icon)) {
 
83
        GFile *file = g_file_icon_get_file(G_FILE_ICON(icon));
 
84
        gchar *uri = g_file_get_uri(file);
 
85
        QString iconUri(uri);
 
86
        g_free(uri);
 
87
        result = iconUri;
 
88
    }
 
89
 
 
90
    g_object_unref(icon);
 
91
 
 
92
    return result;
 
93
}
 
94
 
 
95
QString uriToThumbnailerProviderString(QString const &uri, QString const &mimetype, QVariantHash const &metadata)
 
96
{
 
97
    if (uri.startsWith(QLatin1String("file:///"))) {
 
98
        bool isAudio = mimetype.startsWith(QLatin1String("audio/"));
 
99
        QString thumbnailerUri;
 
100
        if (isAudio) {
 
101
            thumbnailerUri = BASE_ALBUMART_URI;
 
102
            if (metadata.contains("content")) {
 
103
                QVariantHash contentHash = metadata["content"].toHash();
 
104
                if (contentHash.contains("content")) { // nested content in Home?
 
105
                    contentHash = contentHash["content"].toHash();
 
106
                }
 
107
                if (contentHash.contains("album") &&
 
108
                    contentHash.contains("artist")) {
 
109
                    const QString album = contentHash["album"].toString();
 
110
                    const QString artist = contentHash["artist"].toString();
 
111
                    thumbnailerUri.append(QUrl::toPercentEncoding(artist));
 
112
                    thumbnailerUri.append("/");
 
113
                    thumbnailerUri.append(QUrl::toPercentEncoding(album));
 
114
                }
 
115
            }
 
116
        } else {
 
117
            thumbnailerUri = BASE_THUMBNAILER_URI;
 
118
            thumbnailerUri.append(uri.midRef(7));
 
119
        }
 
120
        return thumbnailerUri;
 
121
    }
 
122
 
 
123
    return QString::null;
 
124
}