@@ -0,0 +1,105 @@
1+/*
2+ * Copyright (C) 2014 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+ * Authors: Alberto Aguirre <alberto.aguirre@canonical.com>
17+ */
18+
19+#include "screenshotter.h"
20+
21+#include <QUuid>
22+#include <QChar>
23+#include <QString>
24+#include <QStringList>
25+#include <QStandardPaths>
26+#include <QtGui/QGuiApplication>
27+#include <QtQuick/QQuickWindow>
28+
29+#include <QDebug>
30+
31+ScreenShotter::ScreenShotter(QObject *parent)
32+ : QObject(parent),
33+ screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation)),
34+ screenshotQuality(90)
35+{
36+ screenshotsDir.mkdir("Screenshots");
37+ screenshotsDir.cd("Screenshots");
38+}
39+
40+void ScreenShotter::takeScreenshot()
41+{
42+ QWindowList windows = QGuiApplication::topLevelWindows();
43+ if (windows.empty())
44+ {
45+ qWarning() << "ScreenShotter: no top level windows found!";
46+ return;
47+ }
48+
49+ QQuickWindow *main_window = qobject_cast<QQuickWindow *>(windows[0]);
50+ if (!main_window)
51+ {
52+ qWarning() << "ScreenShotter: can only take screenshots of QQuickWindows";
53+ return;
54+ }
55+
56+ QImage snapshot = main_window->grabWindow();
57+ if (!snapshot.save(generateName(), "JPG", screenshotQuality))
58+ qWarning() << "ScreenShotter: failed to save snapshot!";
59+}
60+
61+QString ScreenShotter::generateUniqueNum()
62+{
63+ // First lets look for existing files using our numbering pattern
64+ QStringList nameFilter;
65+ nameFilter << "screenshot????.jpg";
66+ QStringList fileList = screenshotsDir.entryList(nameFilter, QDir::Files, QDir::Name | QDir::Reversed);
67+
68+ if (!fileList.empty())
69+ {
70+ Q_FOREACH(QString const& fileName, fileList)
71+ {
72+ // Just the the four digit number
73+ QString fileNumber = fileName.mid(10, 4);
74+ bool ok = false;
75+ int num = fileNumber.toInt(&ok) + 1;
76+ if (!ok)
77+ continue;
78+
79+ // OK it's an actual number
80+ if (num <= 9999)
81+ {
82+ // Produce a string that has the next sequential number, appending 0's when necessary
83+ return QString("%1").arg(num, 4, 10, QChar('0'));
84+ }
85+ else
86+ {
87+ // 10000 screenshots huh? ummm, well let's start using uuids then...
88+ QString uuid = QUuid::createUuid().toString();
89+ // Remove the curly brackets
90+ return uuid.mid(1, 36);
91+ }
92+ }
93+ }
94+ //No files exist using our numbering scheme
95+ return QString("0000");
96+}
97+
98+QString ScreenShotter::generateName()
99+{
100+ QString fileName = screenshotsDir.absolutePath();
101+ fileName.append("/screenshot");
102+ fileName.append(generateUniqueNum());
103+ fileName.append(".jpg");
104+ return fileName;
105+}