@@ -1,5 +1,5 @@
2− * Copyright (C) 2014 Canonical, Ltd.
2+ * Copyright (C) 2014-2015 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
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
5 * it under the terms of the GNU General Public License as published by
@@ -12,8 +12,6 @@
13 * You should have received a copy of the GNU General Public License
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/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16− * Authors: Alberto Aguirre <alberto.aguirre@canonical.com>
19#include "screengrabber.h"
17#include "screengrabber.h"
@@ -28,27 +26,35 @@
29#include <QDebug>
27#include <QDebug>
31−void saveScreenshot(QImage screenshot, QString filename, QString format, int quality)
29+bool saveScreenshot(const QImage &screenshot, const QString &filename, const QString &format, int quality)
33− if (!screenshot.save(filename, format.toLatin1().data(), quality))
31+ if (!screenshot.save(filename, format.toLatin1().data(), quality)) {
34− qWarning() << "ScreenShotter: failed to save snapshot!";
32+ qWarning() << "ScreenGrabber: failed to save snapshot!";
37ScreenGrabber::ScreenGrabber(QObject *parent)
39ScreenGrabber::ScreenGrabber(QObject *parent)
38− : QObject(parent),
40+ : QObject(parent)
43+ if (qEnvironmentVariableIsSet("UNITY_TESTING")) {
44+ qDebug() << "Using test environment";
45+ QStandardPaths::setTestModeEnabled(true);
46+ screenshotsDir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
48+ qDebug() << "Using real environment";
41− QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
49+ screenshotsDir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
42− screenshotsDir.mkdir("Screenshots");
51+ screenshotsDir.mkpath("Screenshots");
43 screenshotsDir.cd("Screenshots");
52 screenshotsDir.cd("Screenshots");
44− if (screenshotsDir.exists())
53+ if (screenshotsDir.exists()) {
46 fileNamePrefix = screenshotsDir.absolutePath();
54 fileNamePrefix = screenshotsDir.absolutePath();
47 fileNamePrefix.append("/screenshot");
55 fileNamePrefix.append("/screenshot");
51− qWarning() << "ScreenShotter: failed to create directory at: " << screenshotsDir.absolutePath();
57+ qWarning() << "ScreenGrabber: failed to create directory at: " << screenshotsDir.absolutePath();
@@ -74,11 +80,16 @@ void ScreenGrabber::captureAndSave()
77− QImage screenshot = main_window->grabWindow();
83+ const QImage screenshot = main_window->grabWindow();
84+ const QString filename = makeFileName();
85+ qDebug() << "Saving screenshot to" << filename;
78− QtConcurrent::run(saveScreenshot, screenshot, makeFileName(), getFormat(), screenshotQuality);
86+ auto saveOp = QtConcurrent::run(saveScreenshot, screenshot, filename, getFormat(), screenshotQuality);
87+ if (saveOp.result()) {
88+ Q_EMIT screenshotSaved(filename);
81−QString ScreenGrabber::makeFileName()
92+QString ScreenGrabber::makeFileName() const
83 QString fileName(fileNamePrefix);
94 QString fileName(fileNamePrefix);
84 fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz"));
95 fileName.append(QDateTime::currentDateTime().toString("yyyyMMdd_hhmmsszzz"));
@@ -87,7 +98,7 @@ QString ScreenGrabber::makeFileName()
87 return fileName;
98 return fileName;
90−QString ScreenGrabber::getFormat()
101+QString ScreenGrabber::getFormat() const
92 //TODO: This should be configurable (perhaps through gsettings?)
103 //TODO: This should be configurable (perhaps through gsettings?)
93 return "png";
104 return "png";