on f89409c help sign in
unity8/plugins/SessionGrabber/sessiongrabber.cpp
135 lines 3.3 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
18#include "sessiongrabber.h"
20#include <QDebug>
21#include <QQuickItem>
22#include <QQuickItemGrabResult>
23#include <QtConcurrent>
25static QString cacheFolder()
27 return QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/app_shots/";
30static QString cachePath(const QString &appId)
32 return cacheFolder() + appId + ".png";
35SessionGrabber::SessionGrabber(QObject *parent)
36 : QObject(parent)
37 , m_target(nullptr)
38 , m_watcher(nullptr)
42QString SessionGrabber::appId() const
44 return m_appId;
47void SessionGrabber::setAppId(const QString &appId)
49 if (appId != m_appId) {
50 m_appId = appId;
51 const QString path = cachePath(appId);
52 if (QFile::exists(path)) {
53 setPath(path);
54 }
55 }
58QQuickItem *SessionGrabber::target() const
60 return m_target;
63void SessionGrabber::setTarget(QQuickItem *target)
65 if (target != m_target) {
66 m_target = target;
67 Q_EMIT targetChanged();
68 }
71QString SessionGrabber::path() const
73 return m_path;
76void SessionGrabber::setPath(const QString &path)
78 if (path != m_path) {
79 m_path = path;
80 Q_EMIT pathChanged();
81 }
84static QString saveScreenshot(const QString &appId, const QSharedPointer<QQuickItemGrabResult> grabResult)
86 const QImage image = grabResult->image();
87 const QString cPath = cacheFolder();
88 QDir d;
89 d.mkpath(cPath);
90 const QString path = cachePath(appId);
91 if (image.save(path)) {
92 return path;
93 } else {
94 return QString();
95 }
98void SessionGrabber::grab()
100 if (!m_grabResult.isNull()) {
101 qWarning() << "Asked to grab a screenshot when there's one already being grabbed, ignoring";
102 return;
103 }
105 if (m_target && !m_appId.isEmpty()) {
106 // grabToImage runs in a separate thread
107 m_grabResult = m_target->grabToImage();
108 connect(m_grabResult.data(), &QQuickItemGrabResult::ready, this, &SessionGrabber::grabReady);
109 } else {
110 qWarning() << "Can't grab screnshot: appId:" << m_appId << " target:" << m_target;
111 }
114void SessionGrabber::removeScreenshot()
116 const QString path = cachePath(m_appId);
117 QFile::remove(path);
120void SessionGrabber::grabReady()
122 QFuture<QString> f = QtConcurrent::run(saveScreenshot, m_appId, m_grabResult);
123 m_watcher = new QFutureWatcher<QString>(this);
124 m_watcher->setFuture(f);
125 connect(m_watcher, &QFutureWatcher<QString>::finished, this, &SessionGrabber::saveFinished);
128void SessionGrabber::saveFinished()
130 setPath(m_watcher->future().result());
131 delete m_watcher;
132 m_watcher = nullptr;
133 m_grabResult.clear();
134 Q_EMIT screenshotGrabbed();