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 "actioncontroller.h"
20 #include "data/cinemaschedule.h"
21 #include "data/scheduleentry.h"
22 #include "data/cinema.h"
23 #include "data/movie.h"
24 #include "ui/uiutils.h"
25 #include "utils/calendar.h"
26 #include "utils/timeutils.h"
27 #include "utils/assertedlocker.h"
31 #include <QDesktopServices>
34 ActionController::ActionController(const CinemaSchedule *cinema_schedule) :
36 _cinema_schedule(cinema_schedule)
40 void ActionController::AddToCalendar(ScheduleEntryKey schedule_entry_key)
43 ScheduleEntry schedule_entry;
47 AssertedReadLocker locker(_cinema_schedule->GetLock());
48 schedule_entry = _cinema_schedule->FindScheduleEntry(schedule_entry_key);
49 if (schedule_entry.IsValid()) {
50 movie = *schedule_entry.GetMovie();
51 cinema = *schedule_entry.GetCinema();
54 if (schedule_entry.IsValid()) {
55 Calendar calendar(this);
57 start_time.setDate(schedule_entry.GetDate());
58 start_time.setTime(schedule_entry.GetStartTime());
59 int minutes = movie.GetDuration().hour() * 60
60 + movie.GetDuration().minute();
61 minutes = ((minutes + 14) / 15) * 15;
62 QDateTime end_time = start_time.addSecs(minutes * 60);
63 QString location = cinema.GetName();
65 if (!cinema.GetAddress().isEmpty()) {
66 if (!location.isEmpty()) {
69 location += cinema.GetAddress();
71 if (!cinema.GetTelephone().isEmpty()) {
72 if (!description.isEmpty()) {
75 description += cinema.GetTelephone();
77 if (!calendar.AddEvent(movie.GetName(), start_time, end_time,
78 location, description)) {
79 ShowError(tr("Adding calendar event failed."));
82 UiUtils::ShowInformation(
83 tr("%1 (%2 %3) added to your calendar.")
85 .arg(TimeUtils::ToDateString(schedule_entry.GetDate()))
86 .arg(TimeUtils::ToTimeString(schedule_entry.GetStartTime())));
88 ShowError(tr("Adding calendar event failed."));
91 Q_UNUSED(schedule_entry_key);
92 ShowError(tr("Adding calendar event failed."));
96 void ActionController::CallTheaterByPhone(CinemaKey cinema_key)
100 AssertedReadLocker locker(_cinema_schedule->GetLock());
101 const Cinema *cinema_p = _cinema_schedule->FindCinema(cinema_key);
106 if (cinema.IsValid()) {
107 if (!cinema.GetTelephone().isEmpty()) {
108 if (!QDBusConnection::systemBus().isConnected()) {
109 ShowError(tr("Unable to call theater by phone: communication bus not available."));
112 QDBusInterface iface("com.nokia.csd.Call", "/com/nokia/csd/call", "com.nokia.csd.Call", QDBusConnection::systemBus());
113 if (!iface.isValid()) {
114 ShowError(tr("Unable to call theater by phone: phone service not available."));
117 QString phone_number = TrimPhoneNumber(cinema.GetTelephone());
118 QDBusReply<QDBusObjectPath> reply = iface.call("CreateWith", phone_number, 0);
119 if (!reply.isValid()) {
120 std::cout << qPrintable(reply.error().message()) << ": <" << qPrintable(phone_number) << ">" << std::endl;
121 ShowError(tr("Unable to call theater by phone: call failed."));
128 void ActionController::FindRouteToTheater(CinemaKey cinema_key)
132 AssertedReadLocker locker(_cinema_schedule->GetLock());
133 const Cinema *cinema_p = _cinema_schedule->FindCinema(cinema_key);
138 if (cinema.IsValid()) {
139 // TODO implement find route to theater
143 void ActionController::SearchTheaterInWeb(CinemaKey cinema_key)
147 AssertedReadLocker locker(_cinema_schedule->GetLock());
148 const Cinema *cinema = _cinema_schedule->FindCinema(cinema_key);
150 key = cinema->GetName() + " " + cinema->GetAddress();
153 if (!key.isEmpty()) {
158 void ActionController::SearchMovieInWeb(MovieKey movie_key)
162 AssertedReadLocker locker(_cinema_schedule->GetLock());
163 const Movie *movie = _cinema_schedule->FindMovie(movie_key);
165 key = movie->GetName();
168 if (!key.isEmpty()) {
173 void ActionController::ContactAuthor()
175 QUrl url("mailto::Jochen Becher <j.becher@ovi.com>", QUrl::TolerantMode);
176 url.addQueryItem("subject", "Application 'MovieSchedule' for Nokia N900");
177 QDesktopServices::openUrl(url);
180 QString ActionController::TrimPhoneNumber(const QString &phone_number)
183 bool plus_is_valid = true;
184 foreach (QChar ch, phone_number) {
185 switch (ch.toAscii()) {
199 trimmed += ch.toUpper();
209 // TODO plus_is_valid stays true as long as we are parsing a number suppression prefix (e.g. *31#)
210 plus_is_valid = false;
215 void ActionController::SearchInWeb(const QString &key)
217 QUrl url("http://www.google.com/search");
218 url.addQueryItem("q", key);
219 if (!QDesktopServices::openUrl(url)) {
220 ShowError(tr("Unable to search in web: browser service not available."));
225 void ActionController::ShowError(const QString &msg)
227 UiUtils::ShowError(msg);