diff options
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp')
-rw-r--r-- | accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp new file mode 100644 index 00000000..d7051d2a --- /dev/null +++ b/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp | |||
@@ -0,0 +1,102 @@ | |||
1 | #include "FileSelector.hpp" | ||
2 | |||
3 | #include <sstream> | ||
4 | #include <vector> | ||
5 | #include <random> | ||
6 | #include <algorithm> | ||
7 | |||
8 | #include <iostream> | ||
9 | #include <format> | ||
10 | |||
11 | namespace fs = std::filesystem; | ||
12 | |||
13 | FileSelector::FileSelector(QObject* parent): QObject(parent) { | ||
14 | QObject::connect(&this->timer, &QTimer::timeout, this, &FileSelector::onTimeout); | ||
15 | this->timer.setTimerType(Qt::PreciseTimer); | ||
16 | } | ||
17 | |||
18 | QString FileSelector::directory() const { | ||
19 | return QString::fromStdString(this->mDirectory->string()); | ||
20 | } | ||
21 | void FileSelector::setDirectory(QString directory) { | ||
22 | this->mDirectory = directory.toStdString(); | ||
23 | if (this->mDirectory && this->mEpoch) | ||
24 | this->update(); | ||
25 | emit this->directoryChanged(); | ||
26 | } | ||
27 | |||
28 | unsigned int FileSelector::epoch() const { | ||
29 | return std::chrono::duration_cast<std::chrono::milliseconds>(*this->mEpoch).count(); | ||
30 | } | ||
31 | void FileSelector::setEpoch(unsigned int epoch) { | ||
32 | this->mEpoch = std::chrono::milliseconds{epoch}; | ||
33 | if (this->mDirectory && this->mEpoch) | ||
34 | this->update(); | ||
35 | emit this->epochChanged(); | ||
36 | } | ||
37 | |||
38 | QString FileSelector::seed() const { | ||
39 | return this->mSeed; | ||
40 | } | ||
41 | void FileSelector::setSeed(QString seed) { | ||
42 | this->mSeed = seed; | ||
43 | emit this->seedChanged(); | ||
44 | if (this->mDirectory && this->mEpoch) | ||
45 | emit this->selectedChanged(); | ||
46 | } | ||
47 | |||
48 | QString FileSelector::selected() const { | ||
49 | if (!this->mDirectory || !this->mEpoch) | ||
50 | return QString(); | ||
51 | |||
52 | std::vector<fs::path> shuffled(this->mFiles.begin(), this->mFiles.end()); | ||
53 | std::sort(shuffled.begin(), shuffled.end()); | ||
54 | |||
55 | auto currentTime = std::chrono::system_clock::now(); | ||
56 | uint64_t currentEpoch = currentTime.time_since_epoch() / *this->mEpoch; | ||
57 | std::chrono::milliseconds timeInEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime.time_since_epoch()) % *this->mEpoch; | ||
58 | |||
59 | std::ostringstream seed; | ||
60 | seed << this->mSeed.size() << ";" << this->mSeed.toStdString() << ";"; | ||
61 | seed << *this->mEpoch << ";"; | ||
62 | seed << currentEpoch << ";"; | ||
63 | seed << this->mDirectory->string().size() << ";" << *this->mDirectory << ";"; | ||
64 | seed << this->mFiles.size() << ";"; | ||
65 | for (const fs::path& p: this->mFiles) | ||
66 | seed << p.string().size() << ";" << p << ";"; | ||
67 | |||
68 | std::vector<std::seed_seq::result_type> v; | ||
69 | v.reserve(seed.str().size()); | ||
70 | for (const char& c: seed.str()) | ||
71 | v.push_back(c); | ||
72 | |||
73 | std::seed_seq engine_seed(v.begin(), v.end()); | ||
74 | std::mt19937 g(engine_seed); | ||
75 | std::shuffle(shuffled.begin(), shuffled.end(), g); | ||
76 | |||
77 | std::vector<fs::path>::size_type ix = shuffled.size() * timeInEpoch / *this->mEpoch; | ||
78 | return QString::fromStdString((*this->mDirectory / shuffled[ix]).string()); | ||
79 | } | ||
80 | |||
81 | void FileSelector::onTimeout() { | ||
82 | if (!this->mFiles.size()) | ||
83 | return; | ||
84 | |||
85 | auto currentTime = std::chrono::system_clock::now(); | ||
86 | uint64_t currentMinorEpoch = currentTime.time_since_epoch() / (*this->mEpoch / this->mFiles.size()); | ||
87 | auto nextTime = std::chrono::time_point<std::chrono::system_clock>((currentMinorEpoch + 1) * (*this->mEpoch / this->mFiles.size())); | ||
88 | this->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime)); | ||
89 | |||
90 | emit this->selectedChanged(); | ||
91 | } | ||
92 | |||
93 | void FileSelector::update() { | ||
94 | this->mFiles = std::set<fs::path>{}; | ||
95 | for (const fs::directory_entry& entry: | ||
96 | fs::recursive_directory_iterator(*this->mDirectory, fs::directory_options::follow_directory_symlink)) | ||
97 | { | ||
98 | this->mFiles.insert(fs::relative(entry, *this->mDirectory)); | ||
99 | } | ||
100 | |||
101 | this->onTimeout(); | ||
102 | } | ||