@@ -18,10 +18,8 @@
19#include "screenshotter.h"
19#include "screenshotter.h"
21−#include <QUuid>
21+#include <QDir>
22−#include <QChar>
22+#include <QDateTime>
24−#include <QStringList>
25#include <QStandardPaths>
23#include <QStandardPaths>
26#include <QtGui/QGuiApplication>
24#include <QtGui/QGuiApplication>
27#include <QtQuick/QQuickWindow>
25#include <QtQuick/QQuickWindow>
@@ -30,15 +28,30 @@
31ScreenShotter::ScreenShotter(QObject *parent)
29ScreenShotter::ScreenShotter(QObject *parent)
32 : QObject(parent),
30 : QObject(parent),
33− screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation)),
34 screenshotQuality(90)
31 screenshotQuality(90)
33+ QDir screenshotsDir(QStandardPaths::displayName(QStandardPaths::PicturesLocation));
36 screenshotsDir.mkdir("Screenshots");
34 screenshotsDir.mkdir("Screenshots");
37 screenshotsDir.cd("Screenshots");
35 screenshotsDir.cd("Screenshots");
36+ if (screenshotsDir.exists())
38+ fileNamePrefix = screenshotsDir.absolutePath();
39+ fileNamePrefix.append("/screenshot");
43+ qWarning() << "ScreenShotter: failed to create directory at: " << screenshotsDir.absolutePath();
40void ScreenShotter::takeScreenshot()
47void ScreenShotter::takeScreenshot()
49+ if (fileNamePrefix.isEmpty())
51+ qWarning() << "ScreenShotter: no directory to save screenshot";
42 QWindowList windows = QGuiApplication::topLevelWindows();
55 QWindowList windows = QGuiApplication::topLevelWindows();
43 if (windows.empty())
56 if (windows.empty())
@@ -54,52 +67,21 @@ void ScreenShotter::takeScreenshot()
56 QImage snapshot = main_window->grabWindow();
69 QImage snapshot = main_window->grabWindow();
57− if (!snapshot.save(generateName(), "JPG", screenshotQuality))
70+ if (!snapshot.save(makeFileName(), getFormat().toLatin1().data(), screenshotQuality))
58 qWarning() << "ScreenShotter: failed to save snapshot!";
71 qWarning() << "ScreenShotter: failed to save snapshot!";
61−QString ScreenShotter::generateUniqueNum()
74+QString ScreenShotter::makeFileName()
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);
68− if (!fileList.empty())
70− Q_FOREACH(QString const& fileName, fileList)
76+ QString fileName(fileNamePrefix);
71− {
77+ fileName.append(QDateTime::currentDateTime().toString("yyyymmdd_hhmmsszzz"));
72− // Just the the four digit number
78+ fileName.append(".");
73− QString fileNumber = fileName.mid(10, 4);
79+ fileName.append(getFormat());
74− bool ok = false;
80+ return fileName;
75− int num = fileNumber.toInt(&ok) + 1;
79− // OK it's an actual number
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'));
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);
94− //No files exist using our numbering scheme
95− return QString("0000");
98−QString ScreenShotter::generateName()
83+QString ScreenShotter::getFormat()
100− QString fileName = screenshotsDir.absolutePath();
85+ //TODO: Maybe this could be configurable
101− fileName.append("/screenshot");
102− fileName.append(generateUniqueNum());
103− fileName.append(".jpg");
104− return fileName;
86+ return "jpg";