~stolowski/unity-scopes-shell/single-preview

« back to all changes in this revision

Viewing changes to src/variantutils.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
 * Authors:
 
5
 *  Pawel Stolowski <pawel.stolowski@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; version 3.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
#include <QUrl>
 
21
#include <QUrlQuery>
 
22
#include <QStringList>
 
23
 
 
24
#include <deelistmodel.h>
 
25
#include "variantutils.h"
 
26
 
 
27
unity::glib::HintsMap convertToHintsMap(const QHash<QString, QVariant> &val)
 
28
{
 
29
    unity::glib::HintsMap hintsMap;
 
30
    QHash<QString, QVariant>::const_iterator it = val.constBegin();
 
31
    while (it != val.constEnd()) {
 
32
        hintsMap[it.key().toStdString()] = DeeListModel::DataFromVariant(it.value());
 
33
        ++it;
 
34
    }
 
35
    return hintsMap;
 
36
}
 
37
 
 
38
unity::glib::HintsMap convertToHintsMap(const QVariant &var)
 
39
{
 
40
    if (var.type() == QVariant::Hash) {
 
41
        unity::glib::HintsMap hintsMap;
 
42
        const auto hash = var.toHash();
 
43
        return convertToHintsMap(hash);
 
44
    }
 
45
    return unity::glib::HintsMap();
 
46
}
 
47
 
 
48
QVariantHash convertToQVariantHash(const unity::glib::HintsMap& var)
 
49
{
 
50
    QVariantHash hash;
 
51
    for (auto hint: var) {
 
52
        hash.insert(QString::fromStdString(hint.first), DeeListModel::VariantForData(hint.second));
 
53
    }
 
54
    return hash;
 
55
}
 
56
 
 
57
/* This will translate uris in the form of:
 
58
 *   "subscope:master.scope/sub.scope?foo=bar"
 
59
 * into a QVariantHash that can be passed as a metadata field to scope.
 
60
 */
 
61
QVariantHash subscopeUriToMetadataHash(const QString &metadata_string)
 
62
{
 
63
    QUrl metadata_url(metadata_string);
 
64
    if (metadata_url.scheme() == QLatin1String("subscope")) {
 
65
        QString subscope_uri(metadata_url.toString(QUrl::RemoveScheme | QUrl::RemoveQuery));
 
66
        QVariantHash new_metadata;
 
67
        QUrlQuery query(metadata_url.query());
 
68
        QList<QPair<QString,QString>> queryItems(query.queryItems());
 
69
        for (auto it = queryItems.begin(); it != queryItems.end(); ++it) {
 
70
            new_metadata[it->first] = QVariant::fromValue(it->second);
 
71
        }
 
72
        QStringList parts(subscope_uri.split(QChar('/')));
 
73
        for (int i = parts.size() - 1; i >= 0; i--) {
 
74
            QVariantHash inner;
 
75
            inner["scope-id"] = QVariant::fromValue(parts[i]);
 
76
            inner["content"] = QVariant::fromValue(new_metadata);
 
77
            new_metadata = inner;
 
78
        }
 
79
        return new_metadata;
 
80
    }
 
81
    return QVariantHash();
 
82
}