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"
23 #include <QXmlStreamReader>
24 #include <QXmlStreamWriter>
28 SettingsController::SettingsController(Settings *settings)
38 STATE_PREVIOUS_LOCATIONS,
39 STATE_PREVIOUS_LOCATION,
40 STATE_PREVIOUS_LOCATION_NAME,
45 void SettingsController::Load()
47 QFile file(GetSettingsFileName());
48 if (file.open(QIODevice::ReadOnly)) {
49 QXmlStreamReader xml(&file);
50 State state = STATE_DOCUMENT;
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");
73 } else if (state == STATE_SETTINGS && xml.name() == "search") {
76 state = STATE_DOCUMENT;
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);
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;
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);
113 if (xml.hasError()) {
114 // TODO error message unable to read setting files?
116 QDir().remove(GetSettingsFileName());
118 _settings->SetLocation(location);
119 _settings->SetPreviousLocations(previous_locations);
124 void SettingsController::Save()
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();
141 xml.writeEndElement();
143 xml.writeStartElement("gps");
144 xml.writeAttribute("enabled", _settings->IsGPSEnabled() ? "yes" : "no");
145 xml.writeEndElement();
146 switch (_settings->GetSearchObjectsType()) {
147 case Settings::THEATERS:
150 case Settings::MOVIES:
154 xml.writeTextElement("search", v);
155 xml.writeEndElement();
156 xml.writeEndDocument();
158 // TODO error message unable to create file
159 std::cout << "Failed to open file " << qPrintable(GetSettingsFileName()) << std::endl;
163 QString SettingsController::GetSettingsFileName()
165 QString dirpath = QDir::homePath() + "/.movie-schedule";
166 QDir().mkpath(dirpath);
167 return dirpath + "/settings.xml";