-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileShuffler.cpp
58 lines (49 loc) · 1.59 KB
/
FileShuffler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "FileShuffler.h"
#include <QDateTime>
#include <QDirIterator>
#include <QSettings>
#include <QTimer>
#include <ctime>
FileShuffler::FileShuffler(QFileInfo const& location, QObject* parent) :
QObject(parent),
m_timer(new QTimer(this))
{
QDirIterator it(location.canonicalFilePath(), QDir::Files,
QDirIterator::Subdirectories);
while (it.hasNext())
{
m_files.append(it.next());
}
qsrand(time(0));
QSettings settings(location.canonicalFilePath() + "/MOS.ini",
QSettings::IniFormat);
m_ageMsec = settings.value("ageMsec", qlonglong(2592000000LL)).toLongLong();
settings.setValue("ageMsec", m_ageMsec);
int viewMsec(settings.value("viewMsec", 5000).toInt());
settings.setValue("viewMsec", viewMsec);
m_timer->setInterval(viewMsec);
connect(m_timer, SIGNAL(timeout()), this, SLOT(selectNextFile()));
}
void FileShuffler::selectNextFile()
{
for (bool success(false); !success; /*inside*/)
{
int index(qrand() % m_files.size());
QString filename(m_files.at(index));
QSettings settings(QFileInfo(filename).canonicalPath() + "/MOS.ini",
QSettings::IniFormat);
QDateTime lastViewed(
settings.value(filename.toUtf8().toHex()).toDateTime());
if (lastViewed.addMSecs(m_ageMsec) < QDateTime::currentDateTimeUtc())
{
settings.setValue(filename.toUtf8().toHex(),
QDateTime::currentDateTimeUtc());
emit nextFile(filename);
success = true;
}
}
}
void FileShuffler::start()
{
m_timer->start();
}