Refactoring - Moved instance() to the top of the file
[quandoparte] / application / settings.cpp
1 /*
2
3 Copyright (C) 2011 Luciano Montanaro <mikelima@cirulla.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20 */
21
22 #include "settings.h"
23
24 #include <QDebug>
25 #include <QSettings>
26 #include <QStringList>
27
28 Settings *Settings::instance()
29 {
30     static Settings *settings = 0;
31
32     if (!settings)
33         settings = new Settings();
34     return settings;
35 }
36
37 Settings::Settings(QObject *parent) :
38     QObject(parent)
39 {
40     load();
41 }
42 Settings::~Settings()
43 {
44     save();
45 }
46
47 void Settings::load()
48 {
49     QSettings settings;
50     m_queryBaseUrl = settings.value("QueryURL",
51                                   "http://mobile.viaggiatreno.it/viaggiatreno/mobile/stazione").toString();
52
53     m_recentStations = settings.value("RecentStations").toString().split(",");
54     qDebug() << "RecentStations:" << m_recentStations;
55
56     m_stationViewPreferred = settings.value("StationViewPreferred", false).toBool();
57     qDebug() << "StationsViewPreferred:" << m_stationViewPreferred;
58
59     m_checkingInterval = settings.value("CheckInterval", 0).toInt();
60     qDebug() << "CheckInterval:" << m_checkingInterval;
61
62     m_showArrivalsPreferred = settings.value("StationView/ShowArrivals", false).toBool();
63     qDebug() << "ShowArrivalsPreferred:" << m_showArrivalsPreferred;
64 }
65
66 void Settings::save()
67 {
68     QSettings settings;
69
70     qDebug() << "Saving Settings to" << settings.fileName();
71
72     settings.setValue("QueryURL", m_queryBaseUrl);
73     settings.setValue("RecentStations", m_recentStations.join(","));
74     settings.setValue("CheckInterval", m_checkingInterval);
75     settings.setValue("StationViewPreferred", m_stationViewPreferred);
76     settings.setValue("StationView/ShowArrivals", m_stationViewPreferred);
77 }
78
79 QString Settings::queryBaseUrl()
80 {
81     return m_queryBaseUrl;
82 }
83
84 void Settings::setQueryBaseUrl(const QString &url)
85 {
86     m_queryBaseUrl = url;
87     emit queryBaseUrlChanged(m_queryBaseUrl);
88 }
89
90 QStringList Settings::recentStations()
91 {
92     return m_recentStations;
93 }
94
95 void Settings::setRecentStations(const QStringList &stations)
96 {
97     m_recentStations = stations;
98     emit recentStationsChanged(m_recentStations);
99 }
100
101 int Settings::checkingInterval()
102 {
103     return m_checkingInterval;
104 }
105
106 void Settings::setCheckingInterval(int interval)
107 {
108     m_checkingInterval = interval;
109
110     emit checkingIntervalChanged(m_checkingInterval);
111 }
112
113 bool Settings::stationViewPreferred()
114 {
115     return m_stationViewPreferred;
116 }
117
118 void Settings::setStationViewPreferred(bool preference)
119 {
120     m_stationViewPreferred = preference;
121     emit stationViewPreferredChanged(m_stationViewPreferred);
122 }
123
124 bool Settings::showArrivalsPreferred()
125 {
126     return m_showArrivalsPreferred;
127 }
128
129 void Settings::setShowArrivalsPreferred(bool preference)
130 {
131     m_showArrivalsPreferred = preference;
132     emit showArrivalsPreferredChanged(m_showArrivalsPreferred);
133 }