2 * Copyright (C) 2015 Canonical, Ltd.
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.
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.
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/>.
18#include "sessiongrabber.h"
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)
41QString SessionGrabber::appId() const
46void SessionGrabber::setAppId(const QString &appId)
48 if (appId != m_appId) {
50 const QString path = cachePath(appId);
51 if (QFile::exists(path)) {
57QQuickItem *SessionGrabber::target() const
62void SessionGrabber::setTarget(QQuickItem *target)
64 if (target != m_target) {
66 Q_EMIT targetChanged();
70QString SessionGrabber::path() const
75void SessionGrabber::setPath(const QString &path)
83static QString saveScreenshot(const QString &appId, const QSharedPointer<QQuickItemGrabResult> grabResult)
85 const QImage image = grabResult->image();
86 const QString cPath = cacheFolder();
89 const QString path = cachePath(appId);
90 if (image.save(path)) {
97void SessionGrabber::grab()
99 if (!m_grabResult.isNull()) {
100 qWarning() << "Asked to grab a screenshot when there's one already being grabbed, ignoring";
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);
109 qWarning() << "Can't grab screnshot: appId:" << m_appId << " target:" << m_target;
113void SessionGrabber::removeScreenshot()
115 const QString path = cachePath(m_appId);
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());
131 m_grabResult.clear();
132 Q_EMIT screenshotGrabbed();
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list