From 328872afb5e68d4579d73bca55f03b57596564bb Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Thu, 23 Feb 2017 20:42:57 +1030 Subject: Converted presets file location to a safe OS agnostic location (~/.local/share/RGBcontroller on Linux) --- qt/RGBController/RGBController.pro.user | 2 +- qt/RGBController/controllerwindow.cpp | 21 +++++++++++++-------- qt/RGBController/controllerwindow.h | 4 ++++ 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'qt/RGBController') diff --git a/qt/RGBController/RGBController.pro.user b/qt/RGBController/RGBController.pro.user index be156cc..f76531e 100755 --- a/qt/RGBController/RGBController.pro.user +++ b/qt/RGBController/RGBController.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/qt/RGBController/controllerwindow.cpp b/qt/RGBController/controllerwindow.cpp index b543ed6..1ff84dd 100755 --- a/qt/RGBController/controllerwindow.cpp +++ b/qt/RGBController/controllerwindow.cpp @@ -4,6 +4,7 @@ controllerWindow::controllerWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::controllerWindow) { + /* TODO convert to QStandardPaths::standardLocations(QStandardPaths::AppDataLocation) */ /* this is our setup function, we set things up here */ ui->setupUi(this); info_log("RGB Controller started"); @@ -50,6 +51,8 @@ controllerWindow::controllerWindow(QWidget *parent) : ui->gto->setValue(0); ui->bfrom->setValue(0); ui->bto->setValue(0); + presetsfile = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/presets.txt"; + tempfile = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/tmp.file"; /* set some crucial ints */ preset_index = 0, r = 0, g = 0, b = 0; /* populate our serial port dropdown box */ @@ -108,7 +111,7 @@ void controllerWindow::load_presets() * -> add [0] to dropdown (name), add [1] to an array that stores all the values */ - QFile inputFile("presets.txt"); + QFile inputFile(presetsfile); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); @@ -125,7 +128,7 @@ void controllerWindow::load_presets() info_log("Presets loaded"); } else { - show_msgbox("Unable to find the presets file.\nThis file needs to be named 'presets.txt' and be located in the same directory as the binary."); + show_msgbox("Unable to load the presets file.\n"); info_log("Presets file not found."); } } @@ -133,7 +136,9 @@ void controllerWindow::load_presets() void controllerWindow::save_preset(QString name) { /* this function will save our preset to file */ - QFile file("presets.txt"); + if (!QDir("Folder").exists()) + QDir().mkdir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); + QFile file(presetsfile); if(!file.open(QIODevice::Append)) { show_msgbox("Fatal error opening presets for appending text."); @@ -155,7 +160,7 @@ void controllerWindow::delete_preset(QString name) { /* * here we will delete a preset from the presets file - * process: + * process: * retrieve preset name * go through preset file line by line and write it to a seperate tmp file * if the [0] of split('=') equals the preset name, don't write it to the tmp file @@ -167,8 +172,8 @@ void controllerWindow::delete_preset(QString name) { case QMessageBox::Ok: { - info_log("deleting preset: " + name); - QFile file("tmp.file"); + info_log("deleting preset: " + name); + QFile file(tempfile); if(!file.open(QIODevice::Append)) { show_msgbox("Fatal error opening temp file for writing"); @@ -185,8 +190,8 @@ void controllerWindow::delete_preset(QString name) } file.close(); /* remove the current presets file then rename the temp file to presets.txt */ - QFile::remove("presets.txt"); - QFile::rename("tmp.file", "presets.txt"); + QFile::remove(presetsfile); + QFile::rename(tempfile, presetsfile); /* reload presets into memory and clear the drop down box */ ui->presets_dropdown->clear(); presets.clear(); diff --git a/qt/RGBController/controllerwindow.h b/qt/RGBController/controllerwindow.h index 8b4456b..0997e82 100755 --- a/qt/RGBController/controllerwindow.h +++ b/qt/RGBController/controllerwindow.h @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "serial_communication.h" #include "ui_controllerwindow.h" @@ -42,6 +44,8 @@ class controllerWindow : public QMainWindow QString port; /* this int will contain the current preset index selected */ int preset_index; + QString presetsfile; + QString tempfile; private slots: /* these slots are used to trigger button clicks and drop down items selections etc */ -- cgit v1.2.3