2
* Copyright (C) 2012 Canonical Ltd
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 3 as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Jim Nelson <jim@yorba.org>
20
#include "event/event-collection.h"
22
#include "event/event.h"
23
#include "media/media-collection.h"
24
#include "media/media-source.h"
25
#include "util/collections.h"
26
#include "util/time.h"
28
EventCollection* EventCollection::instance_ = NULL;
30
EventCollection::EventCollection()
31
: SourceCollection("EventCollection") {
32
SetComparator(Comparator);
34
// Monitor MediaCollection to create/destroy Events, one for each day of
37
MediaCollection::instance(),
38
SIGNAL(contents_altered(const QSet<DataObject*>*,const QSet<DataObject*>*)),
40
SLOT(on_media_added_removed(const QSet<DataObject*>*,const QSet<DataObject*>*)));
42
// seed what's already present
43
on_media_added_removed(&MediaCollection::instance()->GetAsSet(), NULL);
46
void EventCollection::InitInstance() {
47
Q_ASSERT(instance_ == NULL);
49
instance_ = new EventCollection();
52
EventCollection* EventCollection::instance() {
53
Q_ASSERT(instance_ != NULL);
58
Event* EventCollection::EventForDate(const QDate& date) const {
59
return date_map_.value(date);
62
Event* EventCollection::EventForMediaSource(MediaSource* media) const {
63
// TODO: Could use lookup table here, but this is fine for now
65
foreach (event, GetAllAsType<Event*>()) {
66
if (event->Contains(media))
73
bool EventCollection::Comparator(DataObject* a, DataObject* b) {
74
Event* eventa = qobject_cast<Event*>(a);
75
Q_ASSERT(eventa != NULL);
77
Event* eventb = qobject_cast<Event*>(b);
78
Q_ASSERT(eventb != NULL);
80
return eventa->date() < eventb->date();
83
void EventCollection::on_media_added_removed(const QSet<DataObject *> *added,
84
const QSet<DataObject *> *removed) {
87
foreach (object, *added) {
88
MediaSource* media = qobject_cast<MediaSource*>(object);
89
Q_ASSERT(media != NULL);
91
Event* existing = date_map_.value(media->exposure_date());
92
if (existing == NULL) {
93
qDebug("Creating new event for %s", qPrintable(media->exposure_date().toString()));
94
existing = new Event(media->exposure_date());
99
existing->Attach(media);
103
// TODO: Deal with removed case
104
if (removed != NULL) {
108
void EventCollection::notify_contents_altered(const QSet<DataObject *> *added,
109
const QSet<DataObject *> *removed) {
112
foreach (object, *added) {
113
Event* event = qobject_cast<Event*>(object);
114
Q_ASSERT(event != NULL);
117
Q_ASSERT(!date_map_.contains(event->date()));
118
date_map_.insert(event->date(), event);
122
if (removed != NULL) {
124
foreach (object, *removed) {
125
Event* event = qobject_cast<Event*>(object);
126
Q_ASSERT(event != NULL);
128
Q_ASSERT(date_map_.contains(event->date()));
129
date_map_.remove(event->date());
133
SourceCollection::notify_contents_altered(added, removed);