on 0bfbf81 help sign in
unity8/plugins/Unity/preview.cpp
185 lines 4.9 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>
33#include <UnityCore/GenericPreview.h>
34#include <UnityCore/ApplicationPreview.h>
35#include <UnityCore/MoviePreview.h>
36#include <UnityCore/MusicPreview.h>
38Preview::Preview(QObject *parent):
39 QObject(parent),
40 m_unityPreview(nullptr)
44QString Preview::rendererName() const
46 if (m_unityPreview) {
47 return QString::fromStdString(m_unityPreview->renderer_name());
48 } else {
49 qWarning() << "Preview not set";
50 }
51 return "";
54QString Preview::title() const
56 if (m_unityPreview) {
57 return QString::fromStdString(m_unityPreview->title());
58 } else {
59 qWarning() << "Preview not set";
60 }
61 return "";
64QString Preview::subtitle () const
66 if (m_unityPreview) {
67 return QString::fromStdString(m_unityPreview->subtitle());
68 } else {
69 qWarning() << "Preview not set";
70 }
71 return "";
74QString Preview::description() const
76 if (m_unityPreview) {
77 return QString::fromStdString(m_unityPreview->description());
78 } else {
79 qWarning() << "Preview not set";
80 }
81 return "";
84QVariant Preview::actions()
86 QList<QObject *> alist;
87 if (m_unityPreview) {
88 for (auto unityAction: m_unityPreview->GetActions()) {
89 auto action = new PreviewAction(this); // preview is the owner of actions
90 action->setUnityAction(unityAction);
91 alist.append(action);
92 }
93 } else {
94 qWarning() << "Preview not set";
95 }
97 return QVariant::fromValue(alist);
100QVariant Preview::infoHints()
102 QList<QObject *> hints;
103 if (m_unityPreview) {
104 for (auto unityInfoHint: m_unityPreview->GetInfoHints()) {
105 auto hint = new PreviewInfoHint(this); // preview is the owner of hints
106 hint->setUnityInfoHint(unityInfoHint);
107 hints.append(hint);
108 }
109 } else {
110 qWarning() << "Preview not set";
111 }
113 return QVariant::fromValue(hints);
116QString Preview::image() const
118 if (m_unityPreview) {
119 return QString::fromUtf8(g_icon_to_string(m_unityPreview->image()));
120 } else {
121 qWarning() << "Preview not set";
122 }
123 return QString::null;
126QString Preview::imageSourceUri() const
128 if (m_unityPreview) {
129 return QString::fromStdString(m_unityPreview->image_source_uri());
130 } else {
131 qWarning() << "Preview not set";
132 }
133 return QString::null;
136Preview* Preview::newFromUnityPreview(unity::dash::Preview::Ptr unityPreview)
138 Preview* preview = nullptr;
140 if (typeid(*unityPreview) == typeid(unity::dash::GenericPreview)) {
141 preview = new GenericPreview();
142 }
143 else if (typeid(*unityPreview) == typeid(unity::dash::MusicPreview)) {
144 preview = new MusicPreview();
145 }
146 else if (typeid(*unityPreview) == typeid(unity::dash::MoviePreview)) {
147 preview = new MoviePreview();
148 }
149 else if (typeid(*unityPreview) == typeid(unity::dash::ApplicationPreview)) {
150 preview = new ApplicationPreview();
151 } else {
152 qWarning() << "Unknown preview type: " << typeid(*unityPreview).name();
153 preview = new GenericPreview();
154 }
156 preview->Preview::setUnityPreview(unityPreview);
157 preview->setUnityPreview(unityPreview);
159 return preview;
162void Preview::setUnityPreview(unity::dash::Preview::Ptr unityPreview)
164 m_unityPreview = unityPreview;
166 Q_EMIT rendererNameChanged();
167 Q_EMIT titleChanged();
168 Q_EMIT subtitleChanged();
169 Q_EMIT descriptionChanged();
170 Q_EMIT actionsChanged();
171 Q_EMIT infoHintsChanged();
172 Q_EMIT imageChanged();
173 Q_EMIT imageSourceUriChanged();
176void Preview::execute(const QString& actionId, const QHash<QString, QVariant>& hints)
178 if (m_unityPreview) {
179 auto unityHints = convertToHintsMap(hints);
180 m_unityPreview->PerformAction(actionId.toStdString(), unityHints);
181 } else {
182 qWarning() << "Preview not set";
183 }