Merge https://vcs.maemo.org/git/movie-schedule
[movie-schedule] / src / control / 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
22 #include <QFile>
23 #include <QXmlStreamReader>
24 #include <QXmlStreamWriter>
25 #include <QDir>
26 #include <iostream>
27
28 SettingsController::SettingsController(Settings *settings)
29     : _settings(settings)
30 {
31 }
32
33 enum State {
34     STATE_DOCUMENT,
35     STATE_SETTINGS,
36     STATE_LOCATION,
37     STATE_LOCATION_NAME,
38     STATE_PREVIOUS_LOCATIONS,
39     STATE_PREVIOUS_LOCATION,
40     STATE_PREVIOUS_LOCATION_NAME,
41     STATE_GPS,
42     STATE_SEARCH,
43 };
44
45 void SettingsController::Load()
46 {
47     QFile file(GetSettingsFileName());
48     if (file.open(QIODevice::ReadOnly)) {
49         QXmlStreamReader xml(&file);
50         State state = STATE_DOCUMENT;
51         Location location;
52         Settings::Locations previous_locations;
53         Location previous_location;
54         while (!xml.atEnd()) {
55             QXmlStreamReader::TokenType token = xml.readNext();
56             if (token == QXmlStreamReader::StartElement) {
57                 if (state == STATE_DOCUMENT && xml.name() == "settings") {
58                     state = STATE_SETTINGS;
59                 } else if (state == STATE_SETTINGS && xml.name() == "location") {
60                     state = STATE_LOCATION;
61                 } else if (state == STATE_LOCATION && xml.name() == "name") {
62                     state = STATE_LOCATION_NAME;
63                 } else if (state == STATE_SETTINGS && xml.name() == "previous-locations") {
64                     state = STATE_PREVIOUS_LOCATIONS;
65                 } else if (state == STATE_PREVIOUS_LOCATIONS && xml.name() == "location") {
66                     state = STATE_PREVIOUS_LOCATION;
67                 } else if (state == STATE_PREVIOUS_LOCATION && xml.name() == "name") {
68                     state = STATE_PREVIOUS_LOCATION_NAME;
69                 } else if (state == STATE_SETTINGS && xml.name() == "gps") {
70                     QString enabled = xml.attributes().value("enabled").toString();
71                     _settings->SetGPSEnabled(enabled == "yes");
72                     state = STATE_GPS;
73                 } else if (state == STATE_SETTINGS && xml.name() == "search") {
74                     state = STATE_SEARCH;
75                 } else {
76                     state = STATE_DOCUMENT;
77                 }
78             } else if (token == QXmlStreamReader::EndElement) {
79                 if (state == STATE_SEARCH) {
80                     state = STATE_SETTINGS;
81                 } else if (state == STATE_GPS) {
82                     state = STATE_SETTINGS;
83                 } else if (state == STATE_PREVIOUS_LOCATION_NAME) {
84                     state = STATE_PREVIOUS_LOCATION;
85                 } else if (state == STATE_PREVIOUS_LOCATION) {
86                     if (previous_location.IsValid()) {
87                         previous_locations.append(previous_location);
88                     }
89                     state = STATE_PREVIOUS_LOCATIONS;
90                 } else if (state == STATE_PREVIOUS_LOCATIONS) {
91                     state = STATE_SETTINGS;
92                 } else if (state == STATE_LOCATION_NAME) {
93                     state = STATE_LOCATION;
94                 } else if (state == STATE_LOCATION) {
95                     state = STATE_SETTINGS;
96                 } else if (state == STATE_SETTINGS) {
97                     state = STATE_DOCUMENT;
98                 }
99             } else if (token == QXmlStreamReader::Characters) {
100                 if (state == STATE_LOCATION_NAME) {
101                     location.SetLocationName(xml.text().toString());
102                 } else if (state == STATE_PREVIOUS_LOCATION_NAME) {
103                     previous_location.SetLocationName(xml.text().toString());
104                 } else if (state == STATE_SEARCH) {
105                     if (xml.text() == "theaters") {
106                         _settings->SetSearchObjectsType(Settings::THEATERS);
107                     } else if (xml.text() == "movies") {
108                         _settings->SetSearchObjectsType(Settings::MOVIES);
109                     }
110                 }
111             }
112         }
113         if (xml.hasError()) {
114             // TODO error message unable to read setting files?
115             file.close();
116             QDir().remove(GetSettingsFileName());
117         } else {
118             _settings->SetLocation(location);
119             _settings->SetPreviousLocations(previous_locations);
120         }
121     }
122 }
123
124 void SettingsController::Save()
125 {
126     QFile file(GetSettingsFileName());
127     if (file.open(QIODevice::WriteOnly)) {
128         QXmlStreamWriter xml(&file);
129         xml.setAutoFormatting(true);
130         xml.writeStartDocument();
131         xml.writeStartElement("settings");
132         xml.writeStartElement("location");
133         xml.writeTextElement("name", _settings->GetLocation().GetLocationName());
134         xml.writeEndElement();
135         xml.writeStartElement("previous-locations");
136         Q_FOREACH(Location location, _settings->GetPreviousLocations()) {
137             xml.writeStartElement("location");
138             xml.writeTextElement("name", location.GetLocationName());
139             xml.writeEndElement();
140         }
141         xml.writeEndElement();
142         QString v;
143         xml.writeStartElement("gps");
144         xml.writeAttribute("enabled", _settings->IsGPSEnabled() ? "yes" : "no");
145         xml.writeEndElement();
146         switch (_settings->GetSearchObjectsType()) {
147         case Settings::THEATERS:
148             v = "theaters";
149             break;
150         case Settings::MOVIES:
151             v = "movies";
152             break;
153         }
154         xml.writeTextElement("search", v);
155         xml.writeEndElement();
156         xml.writeEndDocument();
157     } else {
158         // TODO error message unable to create file
159         std::cout << "Failed to open file " << qPrintable(GetSettingsFileName()) << std::endl;
160     }
161 }
162
163 QString SettingsController::GetSettingsFileName()
164 {
165     QString dirpath = QDir::homePath() + "/.movie-schedule";
166     QDir().mkpath(dirpath);
167     return dirpath + "/settings.xml";
168 }