summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell-plugins/FileSelector.cpp
blob: 3a0537b6bc126849eb859427ae44550ef57ef2e3 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "FileSelector.hpp"

#include <sstream>
#include <vector>
#include <random>
#include <algorithm>

#include <iostream>
#include <format>

namespace fs = std::filesystem;

FileSelector::FileSelector(QObject* parent): QObject(parent) {
  QObject::connect(&this->timer, &QTimer::timeout, this, &FileSelector::onTimeout);
}

QString FileSelector::directory() const {
  return QString::fromStdString(this->mDirectory->string());
}
void FileSelector::setDirectory(QString directory) {
  this->mDirectory = directory.toStdString();
  if (this->mDirectory && this->mEpoch)
    this->update();
  emit this->directoryChanged();
}

unsigned int FileSelector::epoch() const {
  return std::chrono::duration_cast<std::chrono::milliseconds>(*this->mEpoch).count();
}
void FileSelector::setEpoch(unsigned int epoch) {
  this->mEpoch = std::chrono::milliseconds{epoch};
  if (this->mDirectory && this->mEpoch)
    this->update();
  emit this->epochChanged();
}

QString FileSelector::seed() const {
  return this->mSeed;
}
void FileSelector::setSeed(QString seed) {
  this->mSeed = seed;
  emit this->seedChanged();
}

QString FileSelector::selected() const {
  if (!this->mDirectory || !this->mEpoch)
    return QString();

  std::vector<fs::path> shuffled(this->mFiles.begin(), this->mFiles.end());
  std::sort(shuffled.begin(), shuffled.end());

  auto currentTime = std::chrono::system_clock::now();
  uint64_t currentEpoch = currentTime.time_since_epoch() / *this->mEpoch;
  std::chrono::milliseconds timeInEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime.time_since_epoch()) % *this->mEpoch;

  std::ostringstream seed;
  seed << this->mSeed.size() << ";" << this->mSeed.toStdString() << ";";
  seed << *this->mEpoch << ";";
  seed << currentEpoch << ";";
  seed << this->mDirectory->string().size() << ";" << *this->mDirectory << ";";
  seed << this->mFiles.size() << ";";
  for (const fs::path& p: this->mFiles)
    seed << p.string().size() << ";" << p << ";";

  std::vector<std::seed_seq::result_type> v;
  v.reserve(seed.str().size());
  for (const char& c: seed.str())
    v.push_back(c);

  std::seed_seq engine_seed(v.begin(), v.end());
  std::mt19937 g(engine_seed);
  std::shuffle(shuffled.begin(), shuffled.end(), g);

  std::vector<fs::path>::size_type ix = shuffled.size() * timeInEpoch / *this->mEpoch;
  return QString::fromStdString((*this->mDirectory / shuffled[ix]).string());
}

void FileSelector::onTimeout() {
  if (!this->mFiles.size())
    return;

  auto currentTime = std::chrono::system_clock::now();
  uint64_t currentMinorEpoch = currentTime.time_since_epoch() / (*this->mEpoch / this->mFiles.size());
  auto nextTime = std::chrono::time_point<std::chrono::system_clock>((2 * currentMinorEpoch + 3) * (*this->mEpoch / (this->mFiles.size() * 2)));
  this->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(nextTime - currentTime).count());

  emit this->selectedChanged();
}

void FileSelector::update() {
  this->mFiles = std::set<fs::path>{};
  for (const fs::directory_entry& entry:
	 fs::recursive_directory_iterator(*this->mDirectory, fs::directory_options::follow_directory_symlink))
    {
      this->mFiles.insert(fs::relative(entry, *this->mDirectory));
    }

  this->onTimeout();
}