File: config.cpp

package info (click to toggle)
sqlitestudio 3.4.21%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,880 kB
  • sloc: ansic: 406,208; cpp: 123,872; yacc: 2,692; tcl: 497; sh: 462; xml: 426; makefile: 19
file content (85 lines) | stat: -rw-r--r-- 2,160 bytes parent folder | download
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
#include "services/config.h"
#include <QDir>
#include <QCoreApplication>
#include <QSettings>

CFG_DEFINE(Core)

static const QString DB_FILE_NAME = QStringLiteral("settings3");
static QString MASTER_CONFIG_FILE = QString();
Config::AskUserForConfigDirFunc Config::askUserForConfigDirFunc;
QSettings* globalSettingsInstance = nullptr;

Config::~Config()
{
}

void Config::setMasterConfigFile(const QString &path)
{
    MASTER_CONFIG_FILE = path;
}

QString Config::getMasterConfigFile()
{
    return MASTER_CONFIG_FILE;
}

void Config::setAskUserForConfigDirFunc(const AskUserForConfigDirFunc& value)
{
    askUserForConfigDirFunc = value;
}

QString Config::getPortableConfigPath()
{
    QStringList paths = QStringList({"./sqlitestudio-cfg", qApp->applicationDirPath() + "/sqlitestudio-cfg"});
    QSet<QString> pathSet;
    QDir dir;
    for (const QString& path : paths)
    {
        dir = QDir(path);
        pathSet << dir.absolutePath();
    }

    QString potentialPath;
    QFileInfo file;
    for (const QString& path : pathSet)
    {
        dir = QDir(path);
        file = QFileInfo(dir.absolutePath());
        if (!file.exists())
        {
            if (potentialPath.isNull())
                potentialPath = dir.absolutePath();

            continue;
        }

        if (!file.isDir() || !file.isReadable() || !file.isWritable())
            continue;

        for (QFileInfo& entryFile : dir.entryInfoList())
        {
            if (!entryFile.isReadable() || !entryFile.isWritable())
                continue;
        }

        return dir.absolutePath();
    }

    return potentialPath;
}

QSettings* Config::getSettings()
{
    if (globalSettingsInstance == nullptr)
    {
        QString portableConfigPath = Config::getPortableConfigPath();
        QFileInfo portableConfigInfo(portableConfigPath);
        if (portableConfigInfo.exists() && portableConfigInfo.isDir() && portableConfigInfo.isReadable())
            globalSettingsInstance = new QSettings(portableConfigPath + "/settings.ini", QSettings::IniFormat);
        else
            globalSettingsInstance = new QSettings();
    }

    return globalSettingsInstance;
}