on df7050a help sign in
unity8/plugins/SessionGrabber/sessiongrabber.cpp
133 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 QDir::homePath() + "/.cache/app_shots/";
30static QString cachePath(const QString &appId)
32 return cacheFolder() + appId + ".png";
35SessionGrabber::SessionGrabber(QObject *parent)
36 : QObject(parent)
37 , m_target(nullptr)
41QString SessionGrabber::appId() const
43 return m_appId;
46void SessionGrabber::setAppId(const QString &appId)
48 if (appId != m_appId) {
49 m_appId = appId;
50 const QString path = cachePath(appId);
51 if (QFile::exists(path)) {
52 setPath(path);
53 }
54 }
57QQuickItem *SessionGrabber::target() const
59 return m_target;
62void SessionGrabber::setTarget(QQuickItem *target)
64 if (target != m_target) {
65 m_target = target;
66 Q_EMIT targetChanged();
67 }
70QString SessionGrabber::path() const
72 return m_path;
75void SessionGrabber::setPath(const QString &path)
77 if (path != m_path) {
78 m_path = path;
79 Q_EMIT pathChanged();
80 }
83static QString saveScreenshot(const QString &appId, const QSharedPointer<QQuickItemGrabResult> grabResult)
85 const QImage image = grabResult->image();
86 const QString cPath = cacheFolder();
87 QDir d;
88 d.mkpath(cPath);
89 const QString path = cachePath(appId);
90 if (image.save(path)) {
91 return path;
92 } else {
93 return QString();
94 }
97void SessionGrabber::grab()
99 if (!m_grabResult.isNull()) {
100 qWarning() << "Asked to grab a screenshot when there's one already being grabbed, ignoring";
101 return;
102 }
104 if (m_target && !m_appId.isEmpty()) {
105 // grabToImage runs in a separate thread
106 m_grabResult = m_target->grabToImage();
107 connect(m_grabResult.data(), &QQuickItemGrabResult::ready, this, &SessionGrabber::grabReady);
108 } else {
109 qWarning() << "Can't grab screnshot: appId:" << m_appId << " target:" << m_target;
110 }
113void SessionGrabber::removeScreenshot()
115 const QString path = cachePath(m_appId);
116 QFile::remove(path);
119void SessionGrabber::grabReady()
121 QFuture<QString> f = QtConcurrent::run(saveScreenshot, m_appId, m_grabResult);
122 m_watcher.reset(new QFutureWatcher<QString>(this));
123 m_watcher->setFuture(f);
124 connect(m_watcher.data(), &QFutureWatcher<QString>::finished, this, &SessionGrabber::saveFinished);
127void SessionGrabber::saveFinished()
129 setPath(m_watcher->future().result());
130 m_watcher.clear();
131 m_grabResult.clear();
132 Q_EMIT screenshotGrabbed();