@@ -18,10 +18,8 @@
1818
1919#include "screenshotter.h"
2020
21−#include <QUuid>
22−#include <QChar>
23−#include <QString>
24−#include <QStringList>
21+#include <QDir>
22+#include <QDateTime>
2523#include <QStandardPaths>
2624#include <QtGui/QGuiApplication>
2725#include <QtQuick/QQuickWindow>
@@ -30,15 +28,30 @@
3028
3129ScreenShotter::ScreenShotter(QObject *parent)
3230 : QObject(parent),
33− screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation)),
3431 screenshotQuality(90)
3532{
33+ QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
3634 screenshotsDir.mkdir("Screenshots");
3735 screenshotsDir.cd("Screenshots");
36+ if (screenshotsDir.exists())
37+ {
38+ fileNamePrefix = screenshotsDir.absolutePath();
39+ fileNamePrefix.append("/screenshot");
40+ }
41+ else
42+ {
43+ qWarning() << "ScreenShotter: failed to create directory at: " << screenshotsDir.absolutePath();
44+ }
3845}
3946
4047void ScreenShotter::takeScreenshot()
4148{
49+ if (fileNamePrefix.isEmpty())
50+ {
51+ qWarning() << "ScreenShotter: no directory to save screenshot";
52+ return;
53+ }
54+
4255 QWindowList windows = QGuiApplication::topLevelWindows();
4356 if (windows.empty())
4457 {
@@ -54,52 +67,21 @@ void ScreenShotter::takeScreenshot()
5467 }
5568
5669 QImage snapshot = main_window->grabWindow();
57− if (!snapshot.save(generateName(), "JPG", screenshotQuality))
70+ if (!snapshot.save(makeFileName(), getFormat().toLatin1().data(), screenshotQuality))
5871 qWarning() << "ScreenShotter: failed to save snapshot!";
5972}
6073
61−QString ScreenShotter::generateUniqueNum()
74+QString ScreenShotter::makeFileName()
6275{
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");
76+ QString fileName(fileNamePrefix);
77+ fileName.append(QDateTime::currentDateTime().toString("yyyymmdd_hhmmsszzz"));
78+ fileName.append(".");
79+ fileName.append(getFormat());
80+ return fileName;
9681}
9782
98−QString ScreenShotter::generateName()
83+QString ScreenShotter::getFormat()
9984{
100− QString fileName = screenshotsDir.absolutePath();
101− fileName.append("/screenshot");
102− fileName.append(generateUniqueNum());
103− fileName.append(".jpg");
104− return fileName;
85+ //TODO: Maybe this could be configurable
86+ return "jpg";
10587}