1 // Copyright 2010 Jochen Becher
3 // This file is part of MovieSchedule.
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.
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.
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/>.
18 #include "settingscontroller.h"
20 #include "data/settings.h"
21 #include "ui/mainwindow.h"
22 #include "ui/optionsdialog.h"
25 #include <QXmlStreamReader>
26 #include <QXmlStreamWriter>
30 SettingsController::SettingsController(MainWindow *main_window, Settings *settings)
31 : _main_window(main_window),
41 STATE_PREVIOUS_LOCATIONS,
42 STATE_PREVIOUS_LOCATION,
43 STATE_PREVIOUS_LOCATION_NAME,
49 void SettingsController::Load()
51 QFile file(GetSettingsFileName());
52 if (file.open(QIODevice::ReadOnly)) {
53 QXmlStreamReader xml(&file);
54 State state = STATE_DOCUMENT;
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");
77 } else if (state == STATE_SETTINGS && xml.name() == "search") {
79 } else if (state == STATE_SETTINGS && xml.name() == "orientation") {
80 state = STATE_ORIENTATION;
82 state = STATE_DOCUMENT;
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);
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;
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);
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);
129 if (xml.hasError()) {
130 // TODO error message unable to read setting files?
132 QDir().remove(GetSettingsFileName());
134 _settings->SetLocation(location);
135 _settings->SetPreviousLocations(previous_locations);
140 void SettingsController::Save()
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();
157 xml.writeEndElement(); // </previous-locations>
159 xml.writeStartElement("gps");
160 xml.writeAttribute("enabled", _settings->IsGPSEnabled() ? "yes" : "no");
161 xml.writeEndElement(); // </gps>
162 switch (_settings->GetSearchObjectsType()) {
163 case Settings::THEATERS:
166 case Settings::MOVIES:
170 xml.writeTextElement("search", v);
171 switch (_settings->GetOrientationMode()) {
172 case Settings::LANDSCAPE:
175 case Settings::PORTRAIT:
178 case Settings::AUTOROTATION:
182 xml.writeTextElement("orientation", v);
183 xml.writeEndElement(); // </settings>
184 xml.writeEndDocument();
186 // TODO error message unable to create file
187 std::cout << "Failed to open file " << qPrintable(GetSettingsFileName()) << std::endl;
191 void SettingsController::EmitInitialSettings()
193 emit SettingsChanged(*_settings);
196 void SettingsController::OpenSettingsDialog()
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);
204 // dialog deletes itself
208 void SettingsController::AcceptSettingsDialog(const Settings &settings)
210 *_settings = settings;
211 emit SettingsChanged(settings);
214 void SettingsController::CancelSettingsDialog(const Settings &settings)
220 QString SettingsController::GetSettingsFileName()
222 QString dirpath = QDir::homePath() + "/.movie-schedule";
223 QDir().mkpath(dirpath);
224 return dirpath + "/settings.xml";