summaryrefslogtreecommitdiffhomepage
path: root/opentrack
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-05-20 12:25:39 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-06-05 14:44:25 +0200
commite9e7e17d798ad4c168183b3dfc5a85a2e2038175 (patch)
tree1abb4dab44e999dfb99908e00a6e1fd3f8ddfa86 /opentrack
parente523ad7584e4e71e393b18c6c8f020370d10e2d7 (diff)
opentrack: implement preset installation
Diffstat (limited to 'opentrack')
-rw-r--r--opentrack/main-window.cpp47
-rw-r--r--opentrack/main-window.hpp3
2 files changed, 45 insertions, 5 deletions
diff --git a/opentrack/main-window.cpp b/opentrack/main-window.cpp
index ac30fd82..b879e773 100644
--- a/opentrack/main-window.cpp
+++ b/opentrack/main-window.cpp
@@ -24,10 +24,14 @@
#include <QMessageBox>
#include <QDesktopServices>
-#include <QDir>
#include <QDesktopWidget>
#include <QApplication>
+#include <QFile>
+#include <QFileInfo>
+#include <QDir>
+#include <QDateTime>
+
extern "C" const char* const opentrack_version;
using namespace options::globals;
@@ -158,6 +162,7 @@ void main_window::init_dylibs()
void main_window::init_profiles()
{
+ copy_presets();
refresh_profile_list();
// implicitly created by `ini_directory()'
if (ini_directory().isEmpty() || !QDir(ini_directory()).isReadable())
@@ -320,7 +325,7 @@ void main_window::create_empty_profile()
QString name;
if (profile_name_from_dialog(name))
{
- (void)maybe_create_profile(name);
+ (void)create_profile_from_preset(name);
refresh_profile_list();
if (profile_list.contains(name))
@@ -786,17 +791,25 @@ void main_window::set_profile(const QString& new_name_, bool migrate)
QSignalBlocker b(ui.iconcomboProfile);
QString new_name = new_name_;
+ bool do_refresh = false;
if (!profile_list.contains(new_name_))
{
new_name = QStringLiteral(OPENTRACK_DEFAULT_PROFILE);
if (!profile_list.contains(new_name))
+ {
migrate = false;
+ do_refresh = true;
+ }
}
- if (maybe_create_profile(new_name))
+ if (create_profile_from_preset(new_name))
migrate = true;
+ if (do_refresh)
+ refresh_profile_list();
+ assert(profile_list.contains(new_name));
+
const bool status = new_name != ini_filename();
if (status)
@@ -1008,7 +1021,33 @@ void main_window::toggle_tracker_()
start_tracker_();
}
-bool main_window::maybe_create_profile(const QString& name)
+void main_window::copy_presets()
+{
+ const QString preset_dir = library_path + "/presets/";
+ const QDir dir{preset_dir};
+ if (!dir.exists())
+ {
+ qDebug() << "no preset dir";
+ return;
+ }
+ with_global_settings_object([&](QSettings& s) {
+ const QString& key = QStringLiteral("last-preset-copy-time");
+ const auto last_time = s.value(key, -1LL).toLongLong();
+ for (const auto& file : dir.entryInfoList({ "*.ini" }, QDir::Files, QDir::Name))
+ {
+ if (file.fileName() == QStringLiteral("default.ini"))
+ continue;
+ if (last_time < file.lastModified().toSecsSinceEpoch())
+ {
+ qDebug() << "copy preset" << file.fileName();
+ (void)QFile::copy(file.filePath(), ini_combine(file.fileName()));
+ }
+ }
+ s.setValue(key, QDateTime::currentSecsSinceEpoch());
+ });
+}
+
+bool main_window::create_profile_from_preset(const QString& name)
{
const QString dest = ini_combine(name);
diff --git a/opentrack/main-window.hpp b/opentrack/main-window.hpp
index 43440ff8..ddb6b725 100644
--- a/opentrack/main-window.hpp
+++ b/opentrack/main-window.hpp
@@ -118,13 +118,14 @@ public:
void restart_tracker_();
void toggle_tracker_();
- [[nodiscard]] bool maybe_create_profile(const QString& name);
+ [[nodiscard]] bool create_profile_from_preset(const QString& name);
void set_profile(const QString& new_name, bool migrate = true);
void set_profile_in_registry(const QString& profile);
void refresh_profile_list();
void die_on_profile_not_writable();
void maybe_start_profile_from_executable();
[[nodiscard]] static bool profile_name_from_dialog(QString& ret);
+ void copy_presets();
void create_empty_profile();
void create_copied_profile();