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 "locationcontroller.h"
20 #include "ui/locationdialog.h"
21 #include "searchclients/gpsclient.h"
22 #include "data/settings.h"
23 #include "ui/mainwindow.h"
25 static const char *MSG_LOCATION_NOT_FOUND = QT_TRANSLATE_NOOP("LocationController", "Unable to detect current city.");
27 LocationController::LocationController(MainWindow *main_window, Settings *settings) :
29 _main_window(main_window),
31 _active_search_task_id(GpsClient::INVALID_SEARCH_TASK_ID)
35 void LocationController::OpenLocationDialog()
37 LocationDialog *location_dialog = new LocationDialog(_main_window);
38 connect(location_dialog, SIGNAL(Search(Location)), this, SLOT(SearchLocationFromCityDatabase(Location)));
39 connect(location_dialog, SIGNAL(SearchPreviousLocation(Location)), this, SLOT(SearchLocation(Location)));
40 connect(location_dialog, SIGNAL(SearchGPS()), this, SLOT(SearchGPS()));
41 connect(location_dialog, SIGNAL(Cancel()), this, SLOT(CancelLocation()));
42 location_dialog->SetLocation(_settings->GetLocation());
43 location_dialog->SetPreviousLocations(_settings->GetPreviousLocations());
44 location_dialog->SetGPSEnabled(true || _settings->IsGPSEnabled());
45 location_dialog->show();
46 // location_dialog deletes itself
49 void LocationController::CancelAllGpsSearchs()
51 _active_search_task_id = GpsClient::INVALID_SEARCH_TASK_ID;
52 GpsClient::CancelAllRunningSearchs();
55 void LocationController::SearchLocationFromCityDatabase(Location location)
57 // TODO check city database
58 SearchLocation(location);
61 void LocationController::SearchLocation(Location location)
63 location = CleanLocation(location);
64 RememberLocation(location);
65 emit Search(location);
68 void LocationController::SearchGPS()
70 GpsClient *client = new GpsClient();
71 connect(client, SIGNAL(SearchStarted(int)), this, SLOT(GpsSearchStarted(int)));
72 connect(client, SIGNAL(PositionUpdate(int)), this, SLOT(GpsPositionUpdate(int)));
73 connect(client, SIGNAL(SearchForTownStarted(int)), this, SLOT(GpsSearchForTownStarted(int)));
74 connect(client, SIGNAL(TownUpdate(int,QString)), this, SLOT(GpsTownUpdate(int,QString)));
75 connect(client, SIGNAL(SearchError(int)), this, SLOT(GpsSearchError(int)));
76 connect(client, SIGNAL(SearchFinished(int,bool)), this, SLOT(GpsSearchFinished(int,bool)));
77 _active_search_task_id = client->GetSearchTaskId();
78 client->SearchLocation();
79 // gps client deletes itself
82 void LocationController::CancelLocation()
87 Location LocationController::CleanLocation(const Location &location)
89 // TODO clash with city database?
90 QString name = location.GetLocationName().trimmed();
91 if (name.size() > 0) {
92 if (name[0].isLower()) {
93 name[0] = name[0].toUpper();
96 Location cleaned_location;
97 cleaned_location.SetLocationName(name);
98 return cleaned_location;
101 void LocationController::RememberLocation(const Location &location)
103 if (!location.IsValid()) {
106 Settings::Locations locations = _settings->GetPreviousLocations();
107 QMutableListIterator<Location> it(locations);
108 while (it.hasNext()) {
109 const Location &prev_location = it.next();
110 if (prev_location == location) {
114 locations.push_front(location);
115 while (locations.size() > 5) {
116 locations.pop_back();
118 _settings->SetPreviousLocations(locations);
121 void LocationController::GpsSearchStarted(int search_task_id)
123 if (_active_search_task_id != search_task_id) {
126 _main_window->SetBusy(true);
129 void LocationController::GpsPositionUpdate(int search_task_id)
131 if (_active_search_task_id != search_task_id) {
136 void LocationController::GpsSearchForTownStarted(int search_task_id)
138 if (_active_search_task_id != search_task_id) {
143 void LocationController::GpsTownUpdate(int search_task_id, QString town)
145 if (_active_search_task_id != search_task_id) {
149 location.SetLocationName(town);
150 SearchLocation(location);
153 void LocationController::GpsSearchError(int search_task_id)
155 if (_active_search_task_id != search_task_id) {
158 _main_window->SetError(tr(MSG_LOCATION_NOT_FOUND));
161 void LocationController::GpsSearchFinished(int search_task_id, bool success)
163 if (_active_search_task_id != search_task_id) {
166 // In case of success SearchLocation() is started which will
167 // unset busy state on its end
169 _main_window->SetBusy(false);