Merge https://vcs.maemo.org/git/movie-schedule
[movie-schedule] / src / data / cinemaschedule.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 "cinemaschedule.h"
19
20 #include "cinema.h"
21 #include "movie.h"
22
23 CinemaSchedule::CinemaSchedule()
24     : _lock(),
25     _all_cinemas_loaded(false),
26     _all_movies_loaded(false)
27 {
28 }
29
30 void CinemaSchedule::Clear()
31 {
32     _lock.AssertLockedForWrite();
33     _schedules.clear();
34     foreach (Movie *movie, _movies.values()) {
35         delete movie;
36     }
37     _movies.clear();
38     _all_movies_loaded = false;
39     foreach (Cinema *cinema, _cinemas.values()) {
40         delete cinema;
41     }
42     _cinemas.clear();
43     _all_cinemas_loaded = false;
44 }
45
46 Cinema *CinemaSchedule::FindCinema(const CinemaKey &key)
47 {
48     _lock.AssertLockedForWrite();
49     if (!_cinemas.contains(key)) {
50         return 0;
51     }
52     return _cinemas[key];
53 }
54
55 const Cinema *CinemaSchedule::FindCinema(const CinemaKey &key) const
56 {
57     _lock.AssertLockedForRead();
58     if (!_cinemas.contains(key)) {
59         return 0;
60     }
61     return _cinemas[key];
62 }
63
64 Cinema *CinemaSchedule::AddCinema(const CinemaKey &key)
65 {
66     _lock.AssertLockedForWrite();
67     Cinema *cinema = 0;
68     if (_cinemas.contains(key))
69     {
70         cinema = _cinemas[key];
71         if (cinema != 0) {
72             return cinema;
73         }
74     }
75     cinema = new Cinema();
76     cinema->SetName(key.GetName());
77     cinema->SetAddress(key.GetAddress());
78     _cinemas[key] = cinema;
79     return cinema;
80 }
81
82 Movie *CinemaSchedule::FindMovie(const MovieKey &key)
83 {
84     _lock.AssertLockedForWrite();
85     if (!_movies.contains(key)) {
86         return 0;
87     }
88     return _movies[key];
89 }
90
91 const Movie *CinemaSchedule::FindMovie(const MovieKey &key) const
92 {
93     _lock.AssertLockedForRead();
94     if (!_movies.contains(key)) {
95         return 0;
96     }
97     return _movies[key];
98 }
99
100 Movie *CinemaSchedule::AddMovie(const MovieKey &key)
101 {
102     _lock.AssertLockedForWrite();
103     Movie *movie = 0;
104     if (_movies.contains(key)){
105         movie = _movies[key];
106         if (movie != 0) {
107             return movie;
108         }
109     }
110     movie = new Movie();
111     movie->SetName(key.GetName());
112     _movies[key] = movie;
113     return movie;
114 }
115
116 const CinemaSchedule::ScheduleDates CinemaSchedule::GetScheduleDates() const
117 {
118     _lock.AssertLockedForRead();
119     ScheduleDates result;
120     Q_FOREACH(const ScheduleEntry &entry, _schedules) {
121         result |= entry.GetDate();
122     }
123     return result;
124 }
125
126 const CinemaSchedule::ScheduleDates CinemaSchedule::GetScheduleDates(const Cinema *cinema) const
127 {
128     _lock.AssertLockedForRead();
129     ScheduleDates result;
130     Q_FOREACH(const ScheduleEntry &entry, _schedules) {
131         if (entry.GetCinema() == cinema) {
132             result |= entry.GetDate();
133         }
134     }
135     return result;
136 }
137
138 const CinemaSchedule::ScheduleDates CinemaSchedule::GetScheduleDates(const Movie *movie) const
139 {
140     _lock.AssertLockedForRead();
141     ScheduleDates result;
142     Q_FOREACH(const ScheduleEntry &entry, _schedules) {
143         if (entry.GetMovie() == movie) {
144             result |= entry.GetDate();
145         }
146     }
147     return result;
148 }
149
150 ScheduleEntry CinemaSchedule::FindScheduleEntry(const ScheduleEntryKey &schedule_entry_key) const
151 {
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;
162                 break;
163             }
164         }
165     }
166     return schedule_entry;
167 }
168
169 const CinemaSchedule::Schedules CinemaSchedule::GetSchedules(const Cinema *cinema) const
170 {
171     _lock.AssertLockedForRead();
172     Schedules result;
173     Q_FOREACH(const ScheduleEntry &entry, _schedules) {
174         if (entry.GetCinema() == cinema) {
175             result |= entry;
176         }
177     }
178     return result;
179 }
180
181 const CinemaSchedule::Schedules CinemaSchedule::GetSchedules(const Movie *movie) const
182 {
183     _lock.AssertLockedForRead();
184     Schedules result;
185     Q_FOREACH(const ScheduleEntry &entry, _schedules) {
186         if (entry.GetMovie() == movie) {
187             result |= entry;
188         }
189     }
190     return result;
191 }
192
193 const CinemaSchedule::ScheduleKeys CinemaSchedule::GetScheduleKeys(const CinemaKey &cinema_key) const
194 {
195     _lock.AssertLockedForRead();
196     ScheduleKeys result;
197     const Cinema *cinema = FindCinema(cinema_key);
198     if (cinema != 0) {
199         Q_FOREACH(const ScheduleEntry &entry, _schedules) {
200             if (entry.GetCinema() == cinema) {
201                 result |= entry.GetKey();
202             }
203         }
204     }
205     return result;
206 }
207
208 const CinemaSchedule::ScheduleKeys CinemaSchedule::GetScheduleKeys(const MovieKey &movie_key) const
209 {
210     _lock.AssertLockedForRead();
211     ScheduleKeys result;
212     const Movie *movie = FindMovie(movie_key);
213     if (movie != 0) {
214         Q_FOREACH(const ScheduleEntry &entry, _schedules) {
215             if (entry.GetMovie() == movie) {
216                 result |= entry.GetKey();
217             }
218         }
219     }
220     return result;
221 }
222
223 void CinemaSchedule::AddSchedule(const Cinema *cinema, const Movie *movie, const QTime &start_time, const QDate &date)
224 {
225     _lock.AssertLockedForWrite();
226     _schedules |= ScheduleEntry(cinema, movie, start_time, date);
227 }