aboutsummaryrefslogtreecommitdiffstats
blob: 0bd4aa5299691c57ed6182feca1ad1d47c88c2c5 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "globalsettings.h"

#include <QFileInfo>
#include <QSettings>
#include <QtDebug>

/***********************************************************************************************************
* GlobalSettings
*   Singleton class to be used from anywhere to allow global access to any configuration values
*   Also handles reading and writing of files using QSettings API to allow for standardized file formats
************************************************************************************************************/

//initialization of static instance variable
GlobalSettings* GlobalSettings::m_instance = NULL;

//private constructor so class must be instantiated through getInstance function
GlobalSettings::GlobalSettings()
{
}

GlobalSettings::~GlobalSettings()
{
}

//FUNCTION: getInstance()
//
//  Checks to see if static instance member variable points to anything,
//  if not, creates instance of settings class, loads the data from a file,
//  then returns the pointer to the instance

GlobalSettings* GlobalSettings::getInstance()
{
    if(m_instance == NULL)
    {
        m_instance = new GlobalSettings;
        m_instance->load();
    }

    return m_instance;
}

//FUNCTION: save()
//
//  Uses standard QSettings Qt API to store the settings in a file that is easily
//  user editable.

bool GlobalSettings::save()
{
    QSettings settingsObject("ti", "thermostat");

    settingsObject.setValue("proxy-host", proxyHost());
    settingsObject.setValue("proxy-port", proxyPort());
    settingsObject.setValue("current-city", currentCity());
    settingsObject.setValue("temperature-format", temperatureFormat());
    settingsObject.setValue("time-format", timeFormat());

    if(settingsObject.status() == QSettings::NoError)
        return true;
    else
        return false;

}

//FUNCTION: load()
//
//  Uses QSettings API to load config values from a previously written file and also can set default values if
//  no configuration file is present

bool GlobalSettings::load()
{
    QSettings settingsObject("ti", "thermostat");

    setProxyInfo(settingsObject.value("proxy-host", "").toString(), settingsObject.value("proxy-port", 0).toInt());
    setCurrentCity(settingsObject.value("current-city", "Dallas,TX").toString());
    setTemperatureFormat(static_cast<GlobalSettings::TemperatureFormat>(settingsObject.value("temperature-format", GlobalSettings::TempFormatF).toInt()));
    setTimeFormat(static_cast<GlobalSettings::TimeFormat>(settingsObject.value("time-format", GlobalSettings::TimeFormat12h).toInt()));
    setDataPath(QFileInfo(settingsObject.fileName()).absolutePath());

    //qDebug() << "Data Path: " << dataPath();

    if(settingsObject.status() == QSettings::NoError)
        return true;
    else
        return false;
}

void GlobalSettings::setProxyInfo(QString proxyHost, qint16 proxyPort)
{
    m_proxyHost = proxyHost;
    m_proxyPort = proxyPort;
}

void GlobalSettings::setCurrentCity(QString currentCity)
{
    m_currentCity = currentCity;
}

QString GlobalSettings::proxyHost()
{
    return m_proxyHost;
}

qint16 GlobalSettings::proxyPort()
{
    return m_proxyPort;
}

QString GlobalSettings::currentCity()
{
    return m_currentCity;
}

void GlobalSettings::setTemperatureFormat(TemperatureFormat temperatureFormat)
{
    m_temperatureFormat = temperatureFormat;
}

GlobalSettings::TemperatureFormat GlobalSettings::temperatureFormat()
{
    return m_temperatureFormat;
}

void GlobalSettings::setTimeFormat(TimeFormat timeFormat)
{
    m_timeFormat = timeFormat;
}

GlobalSettings::TimeFormat GlobalSettings::timeFormat()
{
    return m_timeFormat;
}

void GlobalSettings::setDataPath(QString dataPath)
{
    m_dataPath = dataPath;
}

QString GlobalSettings::dataPath()
{
    return m_dataPath;
}