summaryrefslogtreecommitdiffstats
path: root/src/userconfig.cpp
blob: 7f7901544447bfa440c6324bde819539aee40f96 (plain) (blame)
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
#include "userconfig.h"
#include "globals.h"

#include <QDebug>
#include <QSettings>
#include <QDir>

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();
}