From cf77e6923cae8b21b8319b7edf6102c80915b45a Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 3 Jun 2019 15:47:47 +0200 Subject: Refactor user .ini class, support "recent VMs" tab, misc. cleanup --- src/choosersettings.cpp | 57 ----------------- src/choosersettings.h | 31 --------- src/dialog.cpp | 166 +++++++++++++++++++++++------------------------- src/dialog.h | 5 +- src/globals.cpp | 5 -- src/globals.h | 2 - src/i18n/de.ts | 67 ++----------------- src/i18n/pt.ts | 96 +++++----------------------- src/main.cpp | 1 - src/ui/dialog.ui | 2 +- src/userconfig.cpp | 79 +++++++++++++++++++++++ src/userconfig.h | 21 ++++++ 12 files changed, 202 insertions(+), 330 deletions(-) delete mode 100644 src/choosersettings.cpp delete mode 100644 src/choosersettings.h create mode 100644 src/userconfig.cpp create mode 100644 src/userconfig.h (limited to 'src') diff --git a/src/choosersettings.cpp b/src/choosersettings.cpp deleted file mode 100644 index 439c9ea..0000000 --- a/src/choosersettings.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * choosersettings.cpp - * - * Created on: Mar 24, 2014 - * Author: nils - */ - -#include "choosersettings.h" -#include "globals.h" - -bool ChooserSettings::settingsLoaded = false; -ChooserSettings* ChooserSettings::chooserSettings = nullptr; - -ChooserSettings::ChooserSettings() { - // test (and make) directory - QDir saveFileDir(QFileInfo(PREVIOUS_SESSION_USER).absoluteDir()); - if (!saveFileDir.exists()) { - if (!saveFileDir.mkpath(saveFileDir.path())) { - return; - } - } - - settings = new QSettings(PREVIOUS_SESSION_USER, QSettings::IniFormat); - settings->setIniCodec("UTF-8"); - - if (settings->status() != QSettings::NoError) { - if (g_debugMode) { - qDebug() << "Settings: " << PREVIOUS_SESSION_USER << " contains erros."; - } - return; - } - - settingsLoaded = true; -} - -ChooserSettings::~ChooserSettings() { - settings->deleteLater(); -} - -QString ChooserSettings::getSetting(QString key) { - if (!settingsLoaded) { - chooserSettings = new ChooserSettings(); - if (!settingsLoaded) return ""; - } - - return chooserSettings->settings->value(key).toString(); -} - -bool ChooserSettings::setSetting(QString key, QString value) { - if (!settingsLoaded) { - chooserSettings = new ChooserSettings(); - if (!settingsLoaded) return false; - } - chooserSettings->settings->setValue(key, value); -// chooserSettings->settings->sync(); - return true; -} diff --git a/src/choosersettings.h b/src/choosersettings.h deleted file mode 100644 index ea0bb76..0000000 --- a/src/choosersettings.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * choosersettings.h - * - * Created on: Mar 24, 2014 - * Author: nils - */ - -#ifndef CHOOSERSETTINGS_H_ -#define CHOOSERSETTINGS_H_ - -#include -#include -#include - -// TODO: Proper name. This is the user specific settings, not global -class ChooserSettings { -public: - static QString getSetting(QString key); - static bool setSetting(QString key, QString value); - -private: - static ChooserSettings* chooserSettings; - static bool settingsLoaded; - - QSettings* settings; - - ChooserSettings(); - virtual ~ChooserSettings(); -}; - -#endif /* CHOOSERSETTINGS_H_ */ diff --git a/src/dialog.cpp b/src/dialog.cpp index 298efc4..8a9a79e 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -20,25 +20,25 @@ #include "sessiontreeitem.h" #include "globals.h" #include "vsession.h" -#include "choosersettings.h" +#include "userconfig.h" #include "filedownloader.h" static bool isProcessRunning(const QString &binary); Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { - model_[0] = new SessionTreeModel(parent); - model_[1] = new SessionTreeModel(parent); - model_[2] = new SessionTreeModel(parent); + model_[TAB_NATIVE] = new SessionTreeModel(parent); + model_[TAB_RECENT_COURSES] = new SessionTreeModel(parent); + model_[TAB_ALL_VMS] = new SessionTreeModel(parent); userInteracted_ = false; genericExpandedOnce_ = false; autoQuit_ = g_autoQuitSeconds; ui->setupUi(this); - tabs_[0] = ui->tabButtonLocal; - tabs_[1] = ui->tabButtonMyClasses; - tabs_[2] = ui->tabButtonAllClasses; + tabs_[TAB_NATIVE] = ui->tabButtonLocal; + tabs_[TAB_RECENT_COURSES] = ui->tabButtonMyClasses; + tabs_[TAB_ALL_VMS] = ui->tabButtonAllClasses; strings_[STR_LOADING] = QCoreApplication::instance()->translate("Dialog", "Loading..."); strings_[STR_URL_ERROR] = QCoreApplication::instance()->translate("Dialog", "URL Error"); @@ -84,7 +84,6 @@ Dialog::Dialog(QWidget *parent) } else { this->onTabButtonChanged(TAB_NATIVE); - this->selectPreviousSession(); } ui->chkAdminMode->setVisible(Config::isSet(Config::ALLOW_VM_EDIT)); @@ -193,8 +192,8 @@ void Dialog::on_treeView_doubleClicked(const QModelIndex& index) // Wait and cleanup the intermediate process waitpid(pid, nullptr, 0); } - ChooserSettings::setSetting("last-session", (s->shortDescription())); - ChooserSettings::setSetting("last-tab", QString::number(activeTab_)); + UserConfig::addLastSession(s->uuid().isEmpty() ? s->shortDescription() : s->uuid()); + UserConfig::setLastTab(activeTab_); centerTimer_->stop(); // Stop the auto-center/auto-quit timer, so we don't kill the session :> setVisible(false); } else { @@ -216,6 +215,21 @@ void Dialog::addItems(const QList& entries, int tab) { if (tab < 0 || tab > 2) { return; } + if (tab != TAB_RECENT_COURSES) { + auto prev = UserConfig::getLastSessions(); + if (!prev.isEmpty()) { + QList matches; + for (auto it : entries) { + if ((!it->uuid().isEmpty() && prev.contains(it->uuid())) + || prev.contains(it->shortDescription())) { + matches.append(it); + } + } + if (!matches.isEmpty()) { + addItems(matches, TAB_RECENT_COURSES); + } + } + } if (Config::isSet(Config::EXAM_MODE) && tab == TAB_NATIVE) return; this->model_[tab]->addItems(entries); @@ -224,30 +238,32 @@ void Dialog::addItems(const QList& entries, int tab) { setListModel(this->model_[tab]); } model_[tab]->updateView(); - selectPreviousSession(); on_filterEdit_textChanged(); + if (tab != TAB_RECENT_COURSES) { + selectPreviousSession(); + } } void Dialog::addStatusString(const int status) { if (status < 0 || status >= STR__MAX) return; - this->model_[1]->addLabelItem(strings_[status]); - this->model_[2]->addLabelItem(strings_[status]); - tabs_[1]->setEnabled(this->model_[1]->rowCount() > 1); - tabs_[2]->setEnabled(this->model_[2]->rowCount() > 1); - model_[1]->updateView(); - model_[2]->updateView(); + this->model_[TAB_RECENT_COURSES]->addLabelItem(strings_[status]); + this->model_[TAB_ALL_VMS]->addLabelItem(strings_[status]); + tabs_[TAB_RECENT_COURSES]->setEnabled(this->model_[TAB_RECENT_COURSES]->rowCount() > 1); + tabs_[TAB_ALL_VMS]->setEnabled(this->model_[TAB_ALL_VMS]->rowCount() > 1); + model_[TAB_RECENT_COURSES]->updateView(); + model_[TAB_ALL_VMS]->updateView(); } void Dialog::removeStatusString(const int status) { if (status < 0 || status >= STR__MAX) return; - this->model_[1]->removeItem(strings_[status]); - this->model_[2]->removeItem(strings_[status]); - tabs_[1]->setEnabled(this->model_[1]->rowCount() > 1); - tabs_[2]->setEnabled(this->model_[1]->rowCount() > 1); - model_[1]->updateView(); - model_[2]->updateView(); + this->model_[TAB_RECENT_COURSES]->removeItem(strings_[status]); + this->model_[TAB_ALL_VMS]->removeItem(strings_[status]); + tabs_[TAB_RECENT_COURSES]->setEnabled(this->model_[TAB_RECENT_COURSES]->rowCount() > 1); + tabs_[TAB_ALL_VMS]->setEnabled(this->model_[TAB_RECENT_COURSES]->rowCount() > 1); + model_[TAB_RECENT_COURSES]->updateView(); + model_[TAB_ALL_VMS]->updateView(); } void Dialog::on_pushButtonAbort_clicked() { @@ -264,17 +280,24 @@ void Dialog::on_btnScreenSetup_clicked() { QProcess::startDetached("beamergui", QStringList("-w")); } -bool Dialog::selectSession(const QString& name) { +bool Dialog::selectSession(const QString& name, int preferredTab) { QModelIndex root(ui->treeView->rootIndex()); - for (int tab = 0; tab < TAB_COUNT; ++tab) { + int bestTab = -1; + QModelIndex bestIndex; + for (int tab = TAB_COUNT - 1; tab >= 0; --tab) { + qDebug() << "bestTab:" << bestTab << "preferred:" << preferredTab << "current:" << tab; + if (bestTab != -1 && preferredTab != tab) // We already have a potential match, only keep going if this is the desired tab + continue; + qDebug() << "checking..."; for (int i = 0; i < model_[tab]->rowCount(root); ++i) { QModelIndex section(model_[tab]->index(i, 0, root)); - if (!section.isValid()) { + qDebug() << "Section" << section << "valid:" << section.isValid(); + if (!section.isValid()) continue; - } for (int j = 0; j < model_[tab]->rowCount(section); ++j) { QModelIndex index(model_[tab]->index(j, 0, section)); + qDebug() << "Item" << index << "valid:" << index.isValid(); if (!index.isValid()) continue; SessionTreeItem* item = static_cast(index.internalPointer()); @@ -282,59 +305,27 @@ bool Dialog::selectSession(const QString& name) { if (s == nullptr) { continue; } - if (s->shortDescription() == name) { - // change the tab - onTabButtonChanged(tab); - // set selection - ui->treeView->selectionModel()->clearSelection(); - ui->treeView->selectionModel()->clear(); - ui->treeView->selectionModel()->select(index, QItemSelectionModel::Select); - ui->treeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select); - ui->treeView->scrollTo(index); - return true; + qDebug() << "Checking session" << s->shortDescription(); + if ((!s->uuid().isEmpty() && s->uuid() == name) || s->shortDescription() == name) { + bestTab = tab; + bestIndex = index; + break; // Break inner, keep checking other tabs } } } } - return false; -} - -bool Dialog::selectSessionByUuid(const QString& name) { - QModelIndex root(ui->treeView->rootIndex()); - - for (int tab = 0; tab < TAB_COUNT; ++tab) { - for (int i = 0; i < model_[tab]->rowCount(root); ++i) { - QModelIndex section(model_[tab]->index(i, 0, root)); - if (!section.isValid()) { - break; - } - for (int j = 0; j < model_[tab]->rowCount(section); ++j) { - QModelIndex index(model_[tab]->index(j, 0, section)); - SessionTreeItem* item = static_cast(index.internalPointer()); - const Session* s(item->session()); - if (s == nullptr) { - continue; - } - /* TODO: implement this here */ - /* check if it is a vsession, then access the uuid and compare with name*/ - if (s->type() == Session::VSESSION) { - /* cast to vsession */ - const VSession* v = static_cast(s); - if (v->uuid() == name) { - // change the tab - onTabButtonChanged(tab); - // set selection - ui->treeView->selectionModel()->clearSelection(); - ui->treeView->selectionModel()->clear(); - ui->treeView->selectionModel()->select(index, QItemSelectionModel::Select); - ui->treeView->selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select); - ui->treeView->scrollTo(index); - return true; - } - } - } - } + if (bestTab != -1) { + // change the tab + qDebug() << "Best tab is" << bestTab; + onTabButtonChanged(bestTab); + // set selection + ui->treeView->selectionModel()->clearSelection(); + ui->treeView->selectionModel()->clear(); + ui->treeView->selectionModel()->select(bestIndex, QItemSelectionModel::Select); + ui->treeView->selectionModel()->setCurrentIndex(bestIndex, QItemSelectionModel::Select); + ui->treeView->scrollTo(bestIndex); + return true; } return false; @@ -345,24 +336,27 @@ void Dialog::selectPreviousSession() { qDebug() << "Not selecting previous session as user interacted or session was already selected"; return; } + int lastTab = UserConfig::getLastTab(); QString lastSession = Config::get(Config::DEFAULT_SESSION); if (lastSession.isEmpty()) { - lastSession = ChooserSettings::getSetting("last-session"); + auto list = UserConfig::getLastSessions(); + if (!list.isEmpty()) { + lastSession = list.back(); + } } if (!lastSession.isEmpty()) { qDebug() << "Trying to select last session: " << lastSession; ui->treeView->clearSelection(); - if (selectSession(lastSession)) { + if (selectSession(lastSession, lastTab)) { qDebug() << "Success"; userInteracted_ = true; return; } } // could not find last session, change to last used tab - QString lastTab = ChooserSettings::getSetting("last-tab"); - if (!lastTab.isEmpty()) { + if (lastTab >= 0 && lastTab < TAB_COUNT) { qDebug() << "Trying to select last tab " << lastTab; - this->onTabButtonChanged(lastTab.toInt()); + this->onTabButtonChanged(lastTab); } else { int defaultTab = Config::get(Config::DEFAULT_TAB).toInt(); qDebug() << "Selected default tab " << defaultTab; @@ -561,13 +555,13 @@ void Dialog::downloadData(const QString& locationIds) { + infoNode.text() + "

")); } - if (ChooserSettings::getSetting("last-news").toUInt() < timestamp.toTime_t()) { + if (UserConfig::getLastNewsTime() < timestamp.toTime_t()) { // show news if not seen before on_helpNewsButton_clicked(); } // update ini - ChooserSettings::setSetting("last-news", QString::number(timestamp.toTime_t())); + UserConfig::setLastNewsTime(timestamp.toTime_t()); // make backup QFile file(TEMP_PATH_NEWS); @@ -713,7 +707,7 @@ void Dialog::on_tabButtonLocal_clicked() { void Dialog::on_tabButtonMyClasses_clicked() { userInteracted_ = true; - onTabButtonChanged(TAB_MY_COURSES); + onTabButtonChanged(TAB_RECENT_COURSES); } void Dialog::on_tabButtonAllClasses_clicked() { @@ -776,7 +770,7 @@ void Dialog::on_filterEdit_textChanged() { void Dialog::setListModel(SessionTreeModel *model) { QAbstractItemModel *old = nullptr; - if (ui->treeView->model() == model_[0] || ui->treeView->model() == model_[1] || ui->treeView->model() == model_[2]) { + if (ui->treeView->model() == model_[TAB_NATIVE] || ui->treeView->model() == model_[TAB_RECENT_COURSES] || ui->treeView->model() == model_[TAB_ALL_VMS]) { } else { old = ui->treeView->model(); } @@ -830,7 +824,7 @@ void Dialog::keyPressEvent(QKeyEvent* event) { void Dialog::iconDownloaded(const QUrl& url, const QIcon&) { qDebug() << "Icon downloaded... (" << url << ")"; // TODO: Check which model(s) contain an entry with this icon - model_[TAB_MY_COURSES]->updateView(); + model_[TAB_RECENT_COURSES]->updateView(); model_[TAB_ALL_VMS]->updateView(); } @@ -903,7 +897,7 @@ bool Dialog::eventFilter(QObject*, QEvent *event) { } void Dialog::checkAutostart() { if (!autoStartEntry_.isEmpty()) { - if (this->selectSessionByUuid(autoStartEntry_) || this->selectSession(autoStartEntry_)) { + if (this->selectSession(autoStartEntry_)) { this->on_treeView_doubleClicked(ui->treeView->selectionModel()->currentIndex()); } else { QMessageBox::critical(this, "Autostart", QString::fromUtf8("Konnte %1 nicht starten.").arg(autoStartEntry_)); diff --git a/src/dialog.h b/src/dialog.h index 0d74111..5c3af17 100644 --- a/src/dialog.h +++ b/src/dialog.h @@ -26,7 +26,7 @@ class Dialog : public QDialog { static const int STR_NO_ITEMS = 2; static const int STR__MAX = 3; static const int TAB_NATIVE = 0; - static const int TAB_MY_COURSES = 1; + static const int TAB_RECENT_COURSES = 1; static const int TAB_ALL_VMS = 2; static const int TAB_COUNT = 3; @@ -36,8 +36,7 @@ class Dialog : public QDialog { void addItems(const QList&, int tab); void addStatusString(const int status); void removeStatusString(const int status); - bool selectSession(const QString& name); - bool selectSessionByUuid(const QString& name); + bool selectSession(const QString& name, int lastTab = -1); void selectPreviousSession(); void setTheme(); void startSession(const QString& name); diff --git a/src/globals.cpp b/src/globals.cpp index c21598a..7780e8a 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -3,14 +3,9 @@ #include "globals.h" #include "session.h" -static const QString userPath(QDir::homePath() + "/.config/openslx"); - - const QString SESSION_START_SCRIPT(VMCHOOSER_SESSION_START_SCRIPT); -const QString CONFIG_FILE_USER(userPath + "/vmchooser.conf"); const QString CONFIG_FILE_XSESSIONS("/opt/openslx/vmchooser/config/xsessions.conf"); -const QString PREVIOUS_SESSION_USER(userPath + "/vmchooser2.ini"); bool g_debugMode = false; bool g_fullscreen = false; diff --git a/src/globals.h b/src/globals.h index 81bf2f5..3b4f344 100644 --- a/src/globals.h +++ b/src/globals.h @@ -24,9 +24,7 @@ extern int g_autoQuitSeconds; // Script that is run before the xsession/vsession is started extern const QString SESSION_START_SCRIPT; -extern const QString CONFIG_FILE_USER; extern const QString CONFIG_FILE_XSESSIONS; -extern const QString PREVIOUS_SESSION_USER; extern const QString TEMP_PATH_ICONS; extern const QString TEMP_PATH_XML_LIST; diff --git a/src/i18n/de.ts b/src/i18n/de.ts index 3a9965b..e14267c 100644 --- a/src/i18n/de.ts +++ b/src/i18n/de.ts @@ -3,69 +3,10 @@ Console - - vmchooser: failed to run session - vmchooser: Fehler beim Start der Sitzung - - - vmchooser: invalid session file - vmchooser: ungültige Sitzungsdatei - vmchooser: invalid size argument Ungültige Größe - - Requested --config not found: - Gewünschte --config nicht gefunden: - - - Using user specific config: - Benutzerspezifische Konfiguration wird benutzt: - - - Using global config: - Nutze globale Konfiguration: - - - No config file found or given - Keine Konfiguration angegeben und/oder gefunden - - - Usage: vmchooser [ OPTIONS ] - - --allow-vm-edit show the 'edit this vm' checkbox - --autoquit [secs] automatically exit if no selection is made after secs seconds - -b --base base directory where VM images are accessible - -d, --default name of default session - -c, --config alternative config file - -l, --locations location id(s), space separated - --location-mode how to treat entries for this location (IGNORE, BUMP or EXCLUSIVE) - --exam-mode enable exam mode - -P, --pool one or more pool names to display (comma separated) - -f, --file direct boot .desktop file - -x, --xpath path of X Session .desktop files - -u, --url url of vmware .xml file - --url-list url of vms' list as .xml file - --url-news url of the news to display - --url-help url of the help to display - -s, --size window size <width>x<height> - -t, --theme theme - --template-mode how to treat template entries (IGNORE or BUMP) - -p, --pvs show pvs options - --pvs-checked check pvs box by default - -D, --debug print debug information - -v, --version print version and exit - -h, --help print usage information and exit - -S, --runscript change path to run-virt.sh - -T --tab default tab (0=xsession, 1=my vms, 2=all vms) - --no-vtx Host doesn't support VT-x/AMD-V (mark 64bit guests) - --start-uuid start lecture with the given uuid - -FILE can be a vmware .xml or an X .desktop file - - - Dialog @@ -105,10 +46,6 @@ FILE can be a vmware .xml or an X .desktop file Local Natives Linux - - My Classes - Meine Kurse - All Classes Alle Kurse @@ -213,6 +150,10 @@ FILE can be a vmware .xml or an X .desktop file Disable ScreenSaver and Standby Bildschirmschoner deaktivieren + + Recently used + Zuletzt verwendet + QObject diff --git a/src/i18n/pt.ts b/src/i18n/pt.ts index 3260fb3..22804e9 100755 --- a/src/i18n/pt.ts +++ b/src/i18n/pt.ts @@ -4,73 +4,7 @@ Console - - Usage: vmchooser [ OPTIONS ] - - --allow-vm-edit show the 'edit this vm' checkbox - --autoquit [secs] automatically exit if no selection is made after secs seconds - -b --base base directory where VM images are accessible - -d, --default name of default session - -c, --config alternative config file - -l, --locations location id(s), space separated - --location-mode how to treat entries for this location (IGNORE, BUMP or EXCLUSIVE) - --exam-mode enable exam mode - -P, --pool one or more pool names to display (comma separated) - -f, --file direct boot .desktop file - -x, --xpath path of X Session .desktop files - -u, --url url of vmware .xml file - --url-list url of vms' list as .xml file - --url-news url of the news to display - --url-help url of the help to display - -s, --size window size <width>x<height> - -t, --theme theme - --template-mode how to treat template entries (IGNORE or BUMP) - -p, --pvs show pvs options - --pvs-checked check pvs box by default - -D, --debug print debug information - -v, --version print version and exit - -h, --help print usage information and exit - -S, --runscript change path to run-virt.sh - -T --tab default tab (0=xsession, 1=my vms, 2=all vms) - --no-vtx Host doesn't support VT-x/AMD-V (mark 64bit guests) - --start-uuid start lecture with the given uuid - -FILE can be a vmware .xml or an X .desktop file - - - - - - vmchooser: failed to run session - vmchooser: falhou ao executar a sessão - - - - vmchooser: invalid session file - vmchooser: arquivo de sessão inválido - - - - Requested --config not found: - - - - - Using user specific config: - - - - - Using global config: - - - - - No config file found or given - - - - + vmchooser: invalid size argument @@ -79,7 +13,7 @@ FILE can be a vmware .xml or an X .desktop file Dialog - + vmchooser seletor @@ -100,7 +34,7 @@ FILE can be a vmware .xml or an X .desktop file - My Classes + Recently used @@ -184,22 +118,22 @@ FILE can be a vmware .xml or an X .desktop file Iniciar - + Loading... - + URL Error - + No Items - + join PVS(limited) @@ -209,32 +143,32 @@ FILE can be a vmware .xml or an X .desktop file - + Native Linux - + Could not get news. - + Could not get news. (//news/date or //news/info missing) - + Could not get help. - + Could not get help (XML has no //news/info) - + Vmchooser failed to run the selected session! O seletor da VM falhou ao executar a sessão selecionada! @@ -262,12 +196,12 @@ FILE can be a vmware .xml or an X .desktop file QObject - + Warning - + The selected session is based on a 64 bit operating system, but this computer doesn't seem to support this (VT-x/AMD-V not supported by CPU, or disabled in BIOS). You will probably get an error message while the virtualizer is initializing. diff --git a/src/main.cpp b/src/main.cpp index e6939c2..9874f00 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,6 @@ #include "dialog.h" #include "globals.h" #include "xsession.h" -#include "choosersettings.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); diff --git a/src/ui/dialog.ui b/src/ui/dialog.ui index e4c4e41..b6d7822 100644 --- a/src/ui/dialog.ui +++ b/src/ui/dialog.ui @@ -236,7 +236,7 @@ p, li { white-space: pre-wrap; } - My Classes + Recently used true diff --git a/src/userconfig.cpp b/src/userconfig.cpp new file mode 100644 index 0000000..7f79015 --- /dev/null +++ b/src/userconfig.cpp @@ -0,0 +1,79 @@ +#include "userconfig.h" +#include "globals.h" + +#include +#include +#include + +static const QString PREVIOUS_SESSION_USER(QDir::homePath() + "/.config/openslx/vmchooser2.ini"); +static const QString KEY_LAST_SESSIONS("last-sessions"); +static const QString KEY_LAST_TAB("last-tab"); +static const QString KEY_LAST_NEWS("last-news"); + +static QSettings *settings = nullptr; + +void UserConfig::init() +{ + if (settings != nullptr) + return; + QDir dir(QFileInfo(PREVIOUS_SESSION_USER).absoluteDir()); + dir.mkpath(dir.path()); + settings = new QSettings(PREVIOUS_SESSION_USER, QSettings::IniFormat); + if (settings->status() != QSettings::NoError) { + qDebug() << "Wiping old user config as it contains errors"; + dir.remove(PREVIOUS_SESSION_USER); + settings->deleteLater(); + settings = new QSettings(PREVIOUS_SESSION_USER, QSettings::IniFormat); + } + settings->setIniCodec("UTF-8"); +} + +void UserConfig::addLastSession(QString nameOrId) +{ + init(); + auto list = getLastSessions(); + list.removeAll(nameOrId); + list.push_back(nameOrId); + settings->setValue(KEY_LAST_SESSIONS, list); +} + +QStringList UserConfig::getLastSessions() +{ + init(); + auto ret = settings->value(KEY_LAST_SESSIONS).toStringList(); + if (ret.isEmpty()) { + // Legacy + QString old = settings->value("last-session").toString(); + if (!old.isEmpty()) { + ret.append(old); + } + } + while (ret.size() > 10) { + ret.pop_front(); + } + return ret; +} + +void UserConfig::setLastTab(int tab) +{ + init(); + settings->setValue(KEY_LAST_TAB, tab); +} + +int UserConfig::getLastTab() +{ + init(); + return settings->value(KEY_LAST_TAB, -1).toInt(); +} + +void UserConfig::setLastNewsTime(uint t) +{ + init(); + settings->setValue(KEY_LAST_NEWS, t); +} + +uint UserConfig::getLastNewsTime() +{ + init(); + return settings->value(KEY_LAST_NEWS).toUInt(); +} diff --git a/src/userconfig.h b/src/userconfig.h new file mode 100644 index 0000000..7f27e1b --- /dev/null +++ b/src/userconfig.h @@ -0,0 +1,21 @@ +#ifndef _USERCONFIG_H_ +#define _USERCONFIG_H_ + +#include +#include + +class UserConfig { +public: + static QStringList getLastSessions(); + static int getLastTab(); + static uint getLastNewsTime(); + static void addLastSession(QString nameOrId); + static void setLastTab(int tab); + static void setLastNewsTime(uint t); + +private: + UserConfig() {} + static void init(); +}; + +#endif /* _USERCONFIG_H_ */ -- cgit v1.2.3-55-g7522