on 2dca232 help sign in
unity8/plugins/Unity/preview.cpp
173 lines 4.6 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 "variantutils.h"
30// Qt
31#include <QDebug>
32#include <QtAlgorithms>
34#include <UnityCore/GenericPreview.h>
35#include <UnityCore/ApplicationPreview.h>
36#include <UnityCore/MoviePreview.h>
37#include <UnityCore/MusicPreview.h>
39Preview::Preview(QObject *parent):
40 QObject(parent),
41 m_unityPreview(nullptr)
45QString Preview::rendererName() const
47 if (m_unityPreview) {
48 return QString::fromStdString(m_unityPreview->renderer_name());
49 } else {
50 qWarning() << "Preview not set";
51 }
52 return QString();
55QString Preview::title() const
57 if (m_unityPreview) {
58 return QString::fromStdString(m_unityPreview->title());
59 } else {
60 qWarning() << "Preview not set";
61 }
62 return QString();
65QString Preview::subtitle () const
67 if (m_unityPreview) {
68 return QString::fromStdString(m_unityPreview->subtitle());
69 } else {
70 qWarning() << "Preview not set";
71 }
72 return QString();
75QString Preview::description() const
77 if (m_unityPreview) {
78 return QString::fromStdString(m_unityPreview->description());
79 } else {
80 qWarning() << "Preview not set";
81 }
82 return QString();
85QVariant Preview::actions()
87 return QVariant::fromValue(m_actions);
90QVariant Preview::infoHints()
92 return QVariant::fromValue(m_infoHints);
95QString Preview::image() const
97 if (m_unityPreview) {
98 return QString::fromUtf8(g_icon_to_string(m_unityPreview->image()));
99 } else {
100 qWarning() << "Preview not set";
101 }
102 return QString::null;
105QString Preview::imageSourceUri() const
107 if (m_unityPreview) {
108 return QString::fromStdString(m_unityPreview->image_source_uri());
109 } else {
110 qWarning() << "Preview not set";
111 }
112 return QString::null;
115Preview* Preview::newFromUnityPreview(unity::dash::Preview::Ptr unityPreview)
117 Preview* preview = nullptr;
119 if (dynamic_cast<unity::dash::GenericPreview *>(unityPreview.get()) != nullptr) {
120 preview = new GenericPreview();
121 } else if (dynamic_cast<unity::dash::MusicPreview *>(unityPreview.get()) != nullptr) {
122 preview = new MusicPreview();
123 } else if (dynamic_cast<unity::dash::MoviePreview *>(unityPreview.get()) != nullptr) {
124 preview = new MoviePreview();
125 } else if (dynamic_cast<unity::dash::ApplicationPreview *>(unityPreview.get()) != nullptr) {
126 preview = new ApplicationPreview();
127 } else {
128 qWarning() << "Unknown preview type: " << typeid(*unityPreview).name();
129 preview = new GenericPreview();
130 }
132 preview->setUnityPreviewBase(unityPreview);
133 preview->setUnityPreview(unityPreview);
135 return preview;
138void Preview::setUnityPreviewBase(unity::dash::Preview::Ptr unityPreview)
140 m_unityPreview = unityPreview;
142 qDeleteAll(m_infoHints);
143 m_infoHints.clear();
144 qDeleteAll(m_actions);
145 m_actions.clear();
147 for (auto unityInfoHint: m_unityPreview->GetInfoHints()) {
148 auto hint = new PreviewInfoHint(this);
149 hint->setUnityInfoHint(unityInfoHint);
150 m_infoHints.append(hint);
151 }
153 for (auto unityAction: m_unityPreview->GetActions()) {
154 auto action = new PreviewAction(this);
155 action->setUnityAction(unityAction);
156 m_actions.append(action);
157 }
160void Preview::setUnityPreview(unity::dash::Preview::Ptr /* unityPreview */)
162 // default implementation does nothing
165void Preview::execute(const QString& actionId, const QHash<QString, QVariant>& hints)
167 if (m_unityPreview) {
168 auto unityHints = convertToHintsMap(hints);
169 m_unityPreview->PerformAction(actionId.toStdString(), unityHints);
170 } else {
171 qWarning() << "Preview not set";
172 }