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

« back to all changes in this revision

Viewing changes to src/preview.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
// local
 
21
#include "preview.h"
 
22
#include "previewaction.h"
 
23
#include "previewinfohint.h"
 
24
#include "genericpreview.h"
 
25
#include "applicationpreview.h"
 
26
#include "moviepreview.h"
 
27
#include "musicpreview.h"
 
28
#include "socialpreview.h"
 
29
#include "iconutils.h"
 
30
#include "variantutils.h"
 
31
 
 
32
// Qt
 
33
#include <QDebug>
 
34
#include <QtAlgorithms>
 
35
 
 
36
#include <UnityCore/GenericPreview.h>
 
37
#include <UnityCore/ApplicationPreview.h>
 
38
#include <UnityCore/MoviePreview.h>
 
39
#include <UnityCore/MusicPreview.h>
 
40
#include <UnityCore/SocialPreview.h>
 
41
 
 
42
#include <UnityCore/GLibWrapper.h>
 
43
 
 
44
Preview::Preview(QObject *parent):
 
45
    QObject(parent),
 
46
    m_unityPreview(nullptr),
 
47
    m_result(new Result(this))
 
48
{
 
49
}
 
50
 
 
51
QString Preview::rendererName() const
 
52
{
 
53
    if (m_unityPreview) {
 
54
        return QString::fromStdString(m_unityPreview->renderer_name());
 
55
    } else {
 
56
        qWarning() << "Preview not set";
 
57
    }
 
58
    return QString();
 
59
}
 
60
 
 
61
QString Preview::title() const
 
62
{
 
63
    if (m_unityPreview) {
 
64
        return QString::fromStdString(m_unityPreview->title());
 
65
    } else {
 
66
        qWarning() << "Preview not set";
 
67
    }
 
68
    return QString();
 
69
}
 
70
 
 
71
QString Preview::subtitle () const
 
72
{
 
73
    if (m_unityPreview) {
 
74
        return QString::fromStdString(m_unityPreview->subtitle());
 
75
    } else {
 
76
        qWarning() << "Preview not set";
 
77
    }
 
78
    return QString();
 
79
}
 
80
 
 
81
QString Preview::description() const
 
82
{
 
83
    if (m_unityPreview) {
 
84
        return QString::fromStdString(m_unityPreview->description());
 
85
    } else {
 
86
        qWarning() << "Preview not set";
 
87
    }
 
88
    return QString();
 
89
}
 
90
 
 
91
QVariant Preview::actions()
 
92
{
 
93
    return QVariant::fromValue(m_actions);
 
94
}
 
95
 
 
96
QVariant Preview::infoHints()
 
97
{
 
98
    return QVariant::fromValue(m_infoHints);
 
99
}
 
100
 
 
101
QVariantMap Preview::infoHintsHash() const
 
102
{
 
103
    return m_infoHintsHash;
 
104
}
 
105
 
 
106
QString Preview::image() const
 
107
{
 
108
    if (m_unityPreview) {
 
109
        if (m_unityPreview->image() != nullptr) {
 
110
            auto giconString = g_icon_to_string(m_unityPreview->image());
 
111
            QString result(gIconToDeclarativeImageProviderString(QString::fromUtf8(giconString)));
 
112
            g_free(giconString);
 
113
            return result;
 
114
        } else {
 
115
            QString sourceMedia(QString::fromStdString(m_unityPreview->image_source_uri()));
 
116
            QString thumbnailUri(uriToThumbnailerProviderString(sourceMedia, m_result->mimeType(), m_result->metadata().toHash()));
 
117
            if (!thumbnailUri.isNull()) return thumbnailUri;
 
118
        }
 
119
    } else {
 
120
        qWarning() << "Preview not set";
 
121
    }
 
122
    return QString::null;
 
123
}
 
124
 
 
125
QVariant Preview::result() const
 
126
{
 
127
    return QVariant::fromValue(m_result);
 
128
}
 
129
 
 
130
Preview* Preview::newFromUnityPreview(unity::dash::Preview::Ptr unityPreview)
 
131
{
 
132
    Preview* preview = nullptr;
 
133
 
 
134
    if (dynamic_cast<unity::dash::GenericPreview *>(unityPreview.get()) != nullptr) {
 
135
        preview = new GenericPreview();
 
136
    } else if (dynamic_cast<unity::dash::MusicPreview *>(unityPreview.get()) != nullptr) {
 
137
        preview = new MusicPreview();
 
138
    } else if (dynamic_cast<unity::dash::MoviePreview *>(unityPreview.get()) != nullptr) {
 
139
        preview = new MoviePreview();
 
140
    } else if (dynamic_cast<unity::dash::ApplicationPreview *>(unityPreview.get()) != nullptr) {
 
141
        preview = new ApplicationPreview();
 
142
    } else if (dynamic_cast<unity::dash::SocialPreview *>(unityPreview.get()) != nullptr) {
 
143
        preview = new SocialPreview();
 
144
    } else {
 
145
        qWarning() << "Unknown preview type: " << typeid(*unityPreview).name();
 
146
        preview = new GenericPreview();
 
147
    }
 
148
 
 
149
    preview->setUnityPreviewBase(unityPreview);
 
150
    preview->setUnityPreview(unityPreview);
 
151
 
 
152
    return preview;
 
153
}
 
154
 
 
155
void Preview::setUnityPreviewBase(unity::dash::Preview::Ptr unityPreview)
 
156
{
 
157
    m_unityPreview = unityPreview;
 
158
    m_result->setPreview(unityPreview);
 
159
 
 
160
    qDeleteAll(m_infoHints);
 
161
    m_infoHints.clear();
 
162
    m_infoHintsHash.clear();
 
163
    qDeleteAll(m_actions);
 
164
    m_actions.clear();
 
165
 
 
166
    for (auto unityInfoHint: m_unityPreview->GetInfoHints()) {
 
167
        auto hint = new PreviewInfoHint(this);
 
168
        hint->setUnityInfoHint(unityInfoHint);
 
169
        m_infoHints.append(hint);
 
170
        m_infoHintsHash[hint->id()] = QVariant::fromValue(hint);
 
171
    }
 
172
 
 
173
    for (auto unityAction: m_unityPreview->GetActions()) {
 
174
        auto action = new PreviewAction(this);
 
175
        action->setUnityAction(unityAction);
 
176
        m_actions.append(action);
 
177
    }
 
178
}
 
179
 
 
180
void Preview::setUnityPreview(unity::dash::Preview::Ptr /* unityPreview */)
 
181
{
 
182
    // default implementation does nothing
 
183
}
 
184
 
 
185
void Preview::execute(const QString& actionId, const QHash<QString, QVariant>& hints)
 
186
{
 
187
    if (m_unityPreview) {
 
188
        auto unityHints = convertToHintsMap(hints);
 
189
        m_actionCancellable.Renew();
 
190
        m_unityPreview->PerformAction(actionId.toStdString(), unityHints, nullptr, m_actionCancellable);
 
191
    } else {
 
192
        qWarning() << "Preview not set";
 
193
    }
 
194
}
 
195
 
 
196
void Preview::cancelAction()
 
197
{
 
198
    m_actionCancellable.Cancel();
 
199
}