on e82caeb help sign in
unity8/plugins/Unity/preview.cpp
203 lines 5.5 kB history raw
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 */
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"
32// Qt
33#include <QDebug>
34#include <QtAlgorithms>
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>
42#include <UnityCore/GLibWrapper.h>
44Preview::Preview(QObject *parent):
45 QObject(parent),
46 m_unityPreview(nullptr),
47 m_result(new Result(this))
51QString Preview::rendererName() const
53 if (m_unityPreview) {
54 return QString::fromStdString(m_unityPreview->renderer_name());
55 } else {
56 qWarning() << "Preview not set";
57 }
58 return QString();
61QString Preview::title() const
63 if (m_unityPreview) {
64 return QString::fromStdString(m_unityPreview->title());
65 } else {
66 qWarning() << "Preview not set";
67 }
68 return QString();
71QString Preview::subtitle () const
73 if (m_unityPreview) {
74 return QString::fromStdString(m_unityPreview->subtitle());
75 } else {
76 qWarning() << "Preview not set";
77 }
78 return QString();
81QString Preview::description() const
83 if (m_unityPreview) {
84 return QString::fromStdString(m_unityPreview->description());
85 } else {
86 qWarning() << "Preview not set";
87 }
88 return QString();
91QVariant Preview::actions()
93 return QVariant::fromValue(m_actions);
96QVariant Preview::infoHints()
98 return QVariant::fromValue(m_infoHints);
101QVariantMap Preview::infoHintsHash() const
103 return m_infoHintsHash;
106QString Preview::image() const
108 if (m_unityPreview) {
109 auto giconString = g_icon_to_string(m_unityPreview->image());
110 QString result(gIconToDeclarativeImageProviderString(QString::fromUtf8(giconString)));
111 g_free(giconString);
112 return result;
113 } else {
114 qWarning() << "Preview not set";
115 }
116 return QString::null;
119QString Preview::imageSourceUri() const
121 if (m_unityPreview) {
122 return QString::fromStdString(m_unityPreview->image_source_uri());
123 } else {
124 qWarning() << "Preview not set";
125 }
126 return QString::null;
129QVariant Preview::result() const
131 return QVariant::fromValue(m_result);
134Preview* Preview::newFromUnityPreview(unity::dash::Preview::Ptr unityPreview)
136 Preview* preview = nullptr;
138 if (dynamic_cast<unity::dash::GenericPreview *>(unityPreview.get()) != nullptr) {
139 preview = new GenericPreview();
140 } else if (dynamic_cast<unity::dash::MusicPreview *>(unityPreview.get()) != nullptr) {
141 preview = new MusicPreview();
142 } else if (dynamic_cast<unity::dash::MoviePreview *>(unityPreview.get()) != nullptr) {
143 preview = new MoviePreview();
144 } else if (dynamic_cast<unity::dash::ApplicationPreview *>(unityPreview.get()) != nullptr) {
145 preview = new ApplicationPreview();
146 } else if (dynamic_cast<unity::dash::SocialPreview *>(unityPreview.get()) != nullptr) {
147 preview = new SocialPreview();
148 } else {
149 qWarning() << "Unknown preview type: " << typeid(*unityPreview).name();
150 preview = new GenericPreview();
151 }
153 preview->setUnityPreviewBase(unityPreview);
154 preview->setUnityPreview(unityPreview);
156 return preview;
159void Preview::setUnityPreviewBase(unity::dash::Preview::Ptr unityPreview)
161 m_unityPreview = unityPreview;
162 m_result->setPreview(unityPreview);
164 qDeleteAll(m_infoHints);
165 m_infoHints.clear();
166 m_infoHintsHash.clear();
167 qDeleteAll(m_actions);
168 m_actions.clear();
170 for (auto unityInfoHint: m_unityPreview->GetInfoHints()) {
171 auto hint = new PreviewInfoHint(this);
172 hint->setUnityInfoHint(unityInfoHint);
173 m_infoHints.append(hint);
174 m_infoHintsHash[hint->id()] = QVariant::fromValue(hint);
175 }
177 for (auto unityAction: m_unityPreview->GetActions()) {
178 auto action = new PreviewAction(this);
179 action->setUnityAction(unityAction);
180 m_actions.append(action);
181 }
184void Preview::setUnityPreview(unity::dash::Preview::Ptr /* unityPreview */)
186 // default implementation does nothing
189void Preview::execute(const QString& actionId, const QHash<QString, QVariant>& hints)
191 if (m_unityPreview) {
192 auto unityHints = convertToHintsMap(hints);
193 m_actionCancellable.Renew();
194 m_unityPreview->PerformAction(actionId.toStdString(), unityHints, nullptr, m_actionCancellable);
195 } else {
196 qWarning() << "Preview not set";
197 }
200void Preview::cancelAction()
202 m_actionCancellable.Cancel();