aa883630bd17aee1f6ff863330745c5b8a8c419d
[movie-schedule] / settingscontroller.cpp
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule 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 3 of the License, or
8 // (at your option) any later version.
9 //
10 // MovieSchedule 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "settingscontroller.h"
19
20 #include "data/settings.h"
21 #include "ui/mainwindow.h"
22 #include "ui/optionsdialog.h"
23
24 #include <QFile>
25 #include <QXmlStreamReader>
26 #include <QXmlStreamWriter>
27 #include <QDir>
28 #include <iostream>
29
30 SettingsController::SettingsController(MainWindow *main_window, Settings *settings)
31     : _main_window(main_window),
32     _settings(settings)
33 {
34 }
35
36 enum State {
37     STATE_DOCUMENT,
38     STATE_SETTINGS,
39     STATE_LOCATION,
40     STATE_LOCATION_NAME,
41     STATE_PREVIOUS_LOCATIONS,
42     STATE_PREVIOUS_LOCATION,
43     STATE_PREVIOUS_LOCATION_NAME,
44     STATE_GPS,
45     STATE_SEARCH,
46     STATE_ORIENTATION
47 };
48
49 void SettingsController::Load()
50 {
51     QFile file(GetSettingsFileName());
52     if (file.open(QIODevice::ReadOnly)) {
53         QXmlStreamReader xml(&file);
54         State state = STATE_DOCUMENT;
55         Location location;
56         Settings::Locations previous_locations;
57         Location previous_location;
58         while (!xml.atEnd()) {
59             QXmlStreamReader::TokenType token = xml.readNext();
60             if (token == QXmlStreamReader::StartElement) {
61                 if (state == STATE_DOCUMENT && xml.name() == "settings") {
62                     state = STATE_SETTINGS;
63                 } else if (state == STATE_SETTINGS && xml.name() == "location") {
64                     state = STATE_LOCATION;
65                 } else if (state == STATE_LOCATION && xml.name() == "name") {
66                     state = STATE_LOCATION_NAME;
67                 } else if (state == STATE_SETTINGS && xml.name() == "previous-locations") {
68                     state = STATE_PREVIOUS_LOCATIONS;
69                 } else if (state == STATE_PREVIOUS_LOCATIONS && xml.name() == "location") {
70                     state = STATE_PREVIOUS_LOCATION;
71                 } else if (state == STATE_PREVIOUS_LOCATION && xml.name() == "name") {
72                     state = STATE_PREVIOUS_LOCATION_NAME;
73                 } else if (state == STATE_SETTINGS && xml.name() == "gps") {
74                     QString enabled = xml.attributes().value("enabled").toString();
75                     _settings->SetGPSEnabled(enabled == "yes");
76                     state = STATE_GPS;
77                 } else if (state == STATE_SETTINGS && xml.name() == "search") {
78                     state = STATE_SEARCH;
79                 } else if (state == STATE_SETTINGS && xml.name() == "orientation") {
80                     state = STATE_ORIENTATION;
81                 } else {
82                     state = STATE_DOCUMENT;
83                 }
84             } else if (token == QXmlStreamReader::EndElement) {
85                 if (state == STATE_ORIENTATION) {
86                     state = STATE_SETTINGS;
87                 } else if (state == STATE_SEARCH) {
88                     state = STATE_SETTINGS;
89                 } else if (state == STATE_GPS) {
90                     state = STATE_SETTINGS;
91                 } else if (state == STATE_PREVIOUS_LOCATION_NAME) {
92                     state = STATE_PREVIOUS_LOCATION;
93                 } else if (state == STATE_PREVIOUS_LOCATION) {
94                     if (previous_location.IsValid()) {
95                         previous_locations.append(previous_location);
96                     }
97                     state = STATE_PREVIOUS_LOCATIONS;
98                 } else if (state == STATE_PREVIOUS_LOCATIONS) {
99                     state = STATE_SETTINGS;
100                 } else if (state == STATE_LOCATION_NAME) {
101                     state = STATE_LOCATION;
102                 } else if (state == STATE_LOCATION) {
103                     state = STATE_SETTINGS;
104                 } else if (state == STATE_SETTINGS) {
105                     state = STATE_DOCUMENT;
106                 }
107             } else if (token == QXmlStreamReader::Characters) {
108                 if (state == STATE_LOCATION_NAME) {
109                     location.SetLocationName(xml.text().toString());
110                 } else if (state == STATE_PREVIOUS_LOCATION_NAME) {
111                     previous_location.SetLocationName(xml.text().toString());
112                 } else if (state == STATE_SEARCH) {
113                     if (xml.text() == "theaters") {
114                         _settings->SetSearchObjectsType(Settings::THEATERS);
115                     } else if (xml.text() == "movies") {
116                         _settings->SetSearchObjectsType(Settings::MOVIES);
117                     }
118                 } else if (state == STATE_ORIENTATION) {
119                     if (xml.text() == "landscape") {
120                         _settings->SetOrientationMode(Settings::LANDSCAPE);
121                     } else if (xml.text() == "portrait") {
122                         _settings->SetOrientationMode(Settings::PORTRAIT);
123                     } else if (xml.text() == "autorotation") {
124                         _settings->SetOrientationMode(Settings::AUTOROTATION);
125                     }
126                 }
127             }
128         }
129         if (xml.hasError()) {
130             // TODO error message unable to read setting files?
131             file.close();
132             QDir().remove(GetSettingsFileName());
133         } else {
134             _settings->SetLocation(location);
135             _settings->SetPreviousLocations(previous_locations);
136         }
137     }
138 }
139
140 void SettingsController::Save()
141 {
142     QFile file(GetSettingsFileName());
143     if (file.open(QIODevice::WriteOnly)) {
144         QXmlStreamWriter xml(&file);
145         xml.setAutoFormatting(true);
146         xml.writeStartDocument();
147         xml.writeStartElement("settings");
148         xml.writeStartElement("location");
149         xml.writeTextElement("name", _settings->GetLocation().GetLocationName());
150         xml.writeEndElement(); // </location>
151         xml.writeStartElement("previous-locations");
152         Q_FOREACH(Location location, _settings->GetPreviousLocations()) {
153             xml.writeStartElement("location");
154             xml.writeTextElement("name", location.GetLocationName());
155             xml.writeEndElement();
156         }
157         xml.writeEndElement(); // </previous-locations>
158         QString v;
159         xml.writeStartElement("gps");
160         xml.writeAttribute("enabled", _settings->IsGPSEnabled() ? "yes" : "no");
161         xml.writeEndElement(); // </gps>
162         switch (_settings->GetSearchObjectsType()) {
163         case Settings::THEATERS:
164             v = "theaters";
165             break;
166         case Settings::MOVIES:
167             v = "movies";
168             break;
169         }
170         xml.writeTextElement("search", v);
171         switch (_settings->GetOrientationMode()) {
172         case Settings::LANDSCAPE:
173             v = "landscape";
174             break;
175         case Settings::PORTRAIT:
176             v = "portrait";
177             break;
178         case Settings::AUTOROTATION:
179             v = "autorotation";
180             break;
181         }
182         xml.writeTextElement("orientation", v);
183         xml.writeEndElement(); // </settings>
184         xml.writeEndDocument();
185     } else {
186         // TODO error message unable to create file
187         std::cout << "Failed to open file " << qPrintable(GetSettingsFileName()) << std::endl;
188     }
189 }
190
191 void SettingsController::EmitInitialSettings()
192 {
193     emit SettingsChanged(*_settings);
194 }
195
196 void SettingsController::OpenSettingsDialog()
197 {
198 #ifdef Q_WS_MAEMO_5
199     OptionsDialog *dialog = new OptionsDialog(_main_window);
200     connect(dialog, SIGNAL(Accept(Settings)), this, SLOT(AcceptSettingsDialog(Settings)));
201     connect(dialog, SIGNAL(Cancel(Settings)), this, SLOT(CancelSettingsDialog(Settings)));
202     dialog->SetSettings(*_settings);
203     dialog->show();
204     // dialog deletes itself
205 #endif
206 }
207
208 void SettingsController::AcceptSettingsDialog(const Settings &settings)
209 {
210     *_settings = settings;
211     emit SettingsChanged(settings);
212 }
213
214 void SettingsController::CancelSettingsDialog(const Settings &settings)
215 {
216     Q_UNUSED(settings);
217     // do nothing
218 }
219
220 QString SettingsController::GetSettingsFileName()
221 {
222     QString dirpath = QDir::homePath() + "/.movie-schedule";
223     QDir().mkpath(dirpath);
224     return dirpath + "/settings.xml";
225 }