on 3934334 help sign in
unity8/plugins/SessionGrabber/sessiongrabber.cpp
129 lines 3.2 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 cachePath()
27 return QDir::homePath() + "/.cache/app_shots/";
30SessionGrabber::SessionGrabber(QObject *parent)
31 : QObject(parent),
32 m_target(nullptr)
36QString SessionGrabber::appId() const
38 return m_appId;
41void SessionGrabber::setAppdId(const QString &appId)
43 if (appId != m_appId) {
44 m_appId = appId;
45 const QString path = cachePath() + appId + ".png";
46 if (QFile::exists(path)) {
47 setPath(path);
48 }
49 }
52QQuickItem *SessionGrabber::target() const
54 return m_target;
57void SessionGrabber::setTarget(QQuickItem *target)
59 if (target != m_target) {
60 m_target = target;
61 Q_EMIT targetChanged();
62 }
65QString SessionGrabber::path() const
67 return m_path;
70void SessionGrabber::setPath(const QString &path)
72 if (path != m_path) {
73 m_path = path;
74 Q_EMIT pathChanged();
75 }
78static QString saveScreenshot(const QString &appId, const QSharedPointer<QQuickItemGrabResult> grabResult)
80 const QImage image = grabResult->image();
81 const QString cPath = cachePath();
82 QDir d;
83 d.mkpath(cPath);
84 const QString path = cPath + appId + ".png";
85 bool b = image.save(path);
86 if (b) {
87 return path;
88 } else {
89 return QString();
90 }
93void SessionGrabber::take()
95 if (!m_grabResult.isNull()) {
96 qWarning() << "Asked to take a screenshot when there's one already being taken, ignoring";
97 return;
98 }
100 if (m_target && !m_appId.isEmpty()) {
101 // grabToImage runs in a separate thread
102 m_grabResult = m_target->grabToImage();
103 connect(m_grabResult.data(), &QQuickItemGrabResult::ready, this, &SessionGrabber::grabReady);
104 } else {
105 qWarning() << "Can't take screnshot: appId:" << m_appId << " target:" << m_target;
106 }
109void SessionGrabber::removeScreenshot()
111 const QString path = cachePath() + m_appId + ".png";
112 QFile::remove(path);
115void SessionGrabber::grabReady()
117 QFuture<QString> f = QtConcurrent::run(saveScreenshot, m_appId, m_grabResult);
118 m_watcher.reset(new QFutureWatcher<QString>(this));
119 m_watcher->setFuture(f);
120 connect(m_watcher.data(), &QFutureWatcher<QString>::finished, this, &SessionGrabber::saveFinished);
123void SessionGrabber::saveFinished()
125 setPath(m_watcher->future().result());
126 m_watcher.clear();
127 m_grabResult.clear();
128 Q_EMIT screenshotTaken();