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 "cinemaschedule.h"
23 CinemaSchedule::CinemaSchedule()
25 _all_cinemas_loaded(false),
26 _all_movies_loaded(false)
30 void CinemaSchedule::Clear()
32 _lock.AssertLockedForWrite();
34 foreach (Movie *movie, _movies.values()) {
38 _all_movies_loaded = false;
39 foreach (Cinema *cinema, _cinemas.values()) {
43 _all_cinemas_loaded = false;
46 Cinema *CinemaSchedule::FindCinema(const CinemaKey &key)
48 _lock.AssertLockedForWrite();
49 if (!_cinemas.contains(key)) {
55 const Cinema *CinemaSchedule::FindCinema(const CinemaKey &key) const
57 _lock.AssertLockedForRead();
58 if (!_cinemas.contains(key)) {
64 Cinema *CinemaSchedule::AddCinema(const CinemaKey &key)
66 _lock.AssertLockedForWrite();
68 if (_cinemas.contains(key))
70 cinema = _cinemas[key];
75 cinema = new Cinema();
76 cinema->SetName(key.GetName());
77 cinema->SetAddress(key.GetAddress());
78 _cinemas[key] = cinema;
82 Movie *CinemaSchedule::FindMovie(const MovieKey &key)
84 _lock.AssertLockedForWrite();
85 if (!_movies.contains(key)) {
91 const Movie *CinemaSchedule::FindMovie(const MovieKey &key) const
93 _lock.AssertLockedForRead();
94 if (!_movies.contains(key)) {
100 Movie *CinemaSchedule::AddMovie(const MovieKey &key)
102 _lock.AssertLockedForWrite();
104 if (_movies.contains(key)){
105 movie = _movies[key];
111 movie->SetName(key.GetName());
112 _movies[key] = movie;
116 const CinemaSchedule::ScheduleDates CinemaSchedule::GetScheduleDates() const
118 _lock.AssertLockedForRead();
119 ScheduleDates result;
120 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
121 result |= entry.GetDate();
126 const CinemaSchedule::ScheduleDates CinemaSchedule::GetScheduleDates(const Cinema *cinema) const
128 _lock.AssertLockedForRead();
129 ScheduleDates result;
130 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
131 if (entry.GetCinema() == cinema) {
132 result |= entry.GetDate();
138 const CinemaSchedule::ScheduleDates CinemaSchedule::GetScheduleDates(const Movie *movie) const
140 _lock.AssertLockedForRead();
141 ScheduleDates result;
142 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
143 if (entry.GetMovie() == movie) {
144 result |= entry.GetDate();
150 ScheduleEntry CinemaSchedule::FindScheduleEntry(const ScheduleEntryKey &schedule_entry_key) const
152 _lock.AssertLockedForRead();
153 const Cinema *cinema = FindCinema(schedule_entry_key.GetCinemaKey());
154 const Movie *movie = FindMovie(schedule_entry_key.GetMovieKey());
155 ScheduleEntry schedule_entry;
156 if (cinema != 0 && movie != 0) {
157 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
158 if (entry.GetCinema() == cinema && entry.GetMovie() == movie
159 && entry.GetStartTime() == schedule_entry_key.GetStartTime()
160 && entry.GetDate() == schedule_entry_key.GetDate()) {
161 schedule_entry = entry;
166 return schedule_entry;
169 const CinemaSchedule::Schedules CinemaSchedule::GetSchedules(const Cinema *cinema) const
171 _lock.AssertLockedForRead();
173 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
174 if (entry.GetCinema() == cinema) {
181 const CinemaSchedule::Schedules CinemaSchedule::GetSchedules(const Movie *movie) const
183 _lock.AssertLockedForRead();
185 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
186 if (entry.GetMovie() == movie) {
193 const CinemaSchedule::ScheduleKeys CinemaSchedule::GetScheduleKeys(const CinemaKey &cinema_key) const
195 _lock.AssertLockedForRead();
197 const Cinema *cinema = FindCinema(cinema_key);
199 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
200 if (entry.GetCinema() == cinema) {
201 result |= entry.GetKey();
208 const CinemaSchedule::ScheduleKeys CinemaSchedule::GetScheduleKeys(const MovieKey &movie_key) const
210 _lock.AssertLockedForRead();
212 const Movie *movie = FindMovie(movie_key);
214 Q_FOREACH(const ScheduleEntry &entry, _schedules) {
215 if (entry.GetMovie() == movie) {
216 result |= entry.GetKey();
223 void CinemaSchedule::AddSchedule(const Cinema *cinema, const Movie *movie, const QTime &start_time, const QDate &date)
225 _lock.AssertLockedForWrite();
226 _schedules |= ScheduleEntry(cinema, movie, start_time, date);