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

« back to all changes in this revision

Viewing changes to src/categoryresults.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
 *  Michal Hruby <michal.hruby@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
// self
 
21
#include "categoryresults.h"
 
22
#include "iconutils.h"
 
23
 
 
24
// TODO: use something from libunity once it's public
 
25
enum ResultsColumn {
 
26
    URI,
 
27
    ICON_HINT,
 
28
    CATEGORY,
 
29
    RESULT_TYPE,
 
30
    MIMETYPE,
 
31
    TITLE,
 
32
    COMMENT,
 
33
    DND_URI,
 
34
    METADATA
 
35
};
 
36
 
 
37
CategoryResults::CategoryResults(QObject* parent)
 
38
    : DeeListModel(parent)
 
39
    , m_categoryIndex(-1)
 
40
{
 
41
    m_roles[CategoryResults::RoleUri] = "uri";
 
42
    m_roles[CategoryResults::RoleIconHint] = "icon";
 
43
    m_roles[CategoryResults::RoleCategory] = "category";
 
44
    m_roles[CategoryResults::RoleMimetype] = "mimetype";
 
45
    m_roles[CategoryResults::RoleTitle] = "title";
 
46
    m_roles[CategoryResults::RoleComment] = "comment";
 
47
    m_roles[CategoryResults::RoleDndUri] = "dndUri";
 
48
    m_roles[CategoryResults::RoleMetadata] = "metadata";
 
49
    m_roles[CategoryResults::RoleRendererHints] = "rendererHints";
 
50
}
 
51
 
 
52
int CategoryResults::categoryIndex() const
 
53
{
 
54
    return m_categoryIndex;
 
55
}
 
56
 
 
57
void CategoryResults::setCategoryIndex(int index)
 
58
{
 
59
    if (m_categoryIndex != index) {
 
60
        m_categoryIndex = index;
 
61
        Q_EMIT categoryIndexChanged(m_categoryIndex);
 
62
    }
 
63
}
 
64
 
 
65
QHash<int, QByteArray>
 
66
CategoryResults::roleNames() const
 
67
{
 
68
    return m_roles;
 
69
}
 
70
 
 
71
QVariant
 
72
CategoryResults::data(const QModelIndex& index, int role) const
 
73
{
 
74
    if (!index.isValid()) {
 
75
        return QVariant();
 
76
    }
 
77
 
 
78
    switch (role) {
 
79
        case RoleUri:
 
80
            return DeeListModel::data(index, ResultsColumn::URI);
 
81
        case RoleIconHint: {
 
82
            QString giconString(DeeListModel::data(index, ResultsColumn::ICON_HINT).toString());
 
83
            if (giconString.isEmpty()) {
 
84
                QString mimetype(DeeListModel::data(index, ResultsColumn::MIMETYPE).toString());
 
85
                QString uri(DeeListModel::data(index, ResultsColumn::URI).toString());
 
86
                QString thumbnailerUri(uriToThumbnailerProviderString(uri, mimetype, DeeListModel::data(index, ResultsColumn::METADATA).toHash()));
 
87
                if (!thumbnailerUri.isNull()) {
 
88
                    return QVariant::fromValue(thumbnailerUri);
 
89
                }
 
90
            }
 
91
            return QVariant::fromValue(gIconToDeclarativeImageProviderString(giconString));
 
92
        }
 
93
        case RoleCategory:
 
94
            return DeeListModel::data(index, ResultsColumn::CATEGORY);
 
95
        case RoleMimetype:
 
96
            return DeeListModel::data(index, ResultsColumn::MIMETYPE);
 
97
        case RoleTitle:
 
98
            return DeeListModel::data(index, ResultsColumn::TITLE);
 
99
        case RoleComment:
 
100
            return DeeListModel::data(index, ResultsColumn::COMMENT);
 
101
        case RoleDndUri:
 
102
            return DeeListModel::data(index, ResultsColumn::DND_URI);
 
103
        case RoleMetadata:
 
104
            return DeeListModel::data(index, ResultsColumn::METADATA);
 
105
        case RoleRendererHints:
 
106
        {
 
107
            QVariantHash hash(DeeListModel::data(index, ResultsColumn::METADATA).toHash());
 
108
            if (hash.contains("content")) {
 
109
                QVariantMap hints;
 
110
                QVariantHash innerHash(hash["content"].toHash());
 
111
                if (innerHash.contains("scope_disabled")) {
 
112
                    hints["scope_disabled"] = innerHash["scope_disabled"];
 
113
                }
 
114
                return hints.empty() ? QVariant() : QVariant::fromValue(hints);
 
115
            }
 
116
            return QVariant();
 
117
        }
 
118
        default:
 
119
            return QVariant();
 
120
    }
 
121
}