~artmello/gallery-app/gallery-app-fix_crash_adding_files

« back to all changes in this revision

Viewing changes to src/event/event-collection.cpp

  • Committer: Jim Nelson
  • Date: 2012-01-20 22:12:49 UTC
  • Revision ID: jim@yorba.org-20120120221249-81cc1q2jzdkej57k
Event and EventCollection introduced, represented in QML by
QmlEventCollectionModel.  This is work toward completing bug #915655,
the Event Timeline view.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Canonical Ltd
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authors:
 
17
 * Jim Nelson <jim@yorba.org>
 
18
 */
 
19
 
 
20
#include "event/event-collection.h"
 
21
 
 
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"
 
27
 
 
28
EventCollection* EventCollection::instance_ = NULL;
 
29
 
 
30
EventCollection::EventCollection()
 
31
  : SourceCollection("EventCollection") {
 
32
  SetComparator(Comparator);
 
33
  
 
34
  // Monitor MediaCollection to create/destroy Events, one for each day of
 
35
  // media found
 
36
  QObject::connect(
 
37
    MediaCollection::instance(),
 
38
    SIGNAL(contents_altered(const QSet<DataObject*>*,const QSet<DataObject*>*)),
 
39
    this,
 
40
    SLOT(on_media_added_removed(const QSet<DataObject*>*,const QSet<DataObject*>*)));
 
41
  
 
42
  // seed what's already present
 
43
  on_media_added_removed(&MediaCollection::instance()->GetAsSet(), NULL);
 
44
}
 
45
 
 
46
void EventCollection::InitInstance() {
 
47
  Q_ASSERT(instance_ == NULL);
 
48
  
 
49
  instance_ = new EventCollection();
 
50
}
 
51
 
 
52
EventCollection* EventCollection::instance() {
 
53
  Q_ASSERT(instance_ != NULL);
 
54
  
 
55
  return instance_;
 
56
}
 
57
 
 
58
Event* EventCollection::EventForDate(const QDate& date) const {
 
59
  return date_map_.value(date);
 
60
}
 
61
 
 
62
Event* EventCollection::EventForMediaSource(MediaSource* media) const {
 
63
  // TODO: Could use lookup table here, but this is fine for now
 
64
  Event* event;
 
65
  foreach (event, GetAllAsType<Event*>()) {
 
66
    if (event->Contains(media))
 
67
      return event;
 
68
  }
 
69
  
 
70
  return NULL;
 
71
}
 
72
 
 
73
bool EventCollection::Comparator(DataObject* a, DataObject* b) {
 
74
  Event* eventa = qobject_cast<Event*>(a);
 
75
  Q_ASSERT(eventa != NULL);
 
76
  
 
77
  Event* eventb = qobject_cast<Event*>(b);
 
78
  Q_ASSERT(eventb != NULL);
 
79
  
 
80
  return eventa->date() < eventb->date();
 
81
}
 
82
 
 
83
void EventCollection::on_media_added_removed(const QSet<DataObject *> *added,
 
84
  const QSet<DataObject *> *removed) {
 
85
  if (added != NULL) {
 
86
    DataObject* object;
 
87
    foreach (object, *added) {
 
88
      MediaSource* media = qobject_cast<MediaSource*>(object);
 
89
      Q_ASSERT(media != NULL);
 
90
      
 
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());
 
95
        
 
96
        Add(existing);
 
97
      }
 
98
      
 
99
      existing->Attach(media);
 
100
    }
 
101
  }
 
102
  
 
103
  // TODO: Deal with removed case
 
104
  if (removed != NULL) {
 
105
  }
 
106
}
 
107
 
 
108
void EventCollection::notify_contents_altered(const QSet<DataObject *> *added,
 
109
  const QSet<DataObject *> *removed) {
 
110
  if (added != NULL) {
 
111
    DataObject* object;
 
112
    foreach (object, *added) {
 
113
      Event* event = qobject_cast<Event*>(object);
 
114
      Q_ASSERT(event != NULL);
 
115
      
 
116
      // One Event per day
 
117
      Q_ASSERT(!date_map_.contains(event->date()));
 
118
      date_map_.insert(event->date(), event);
 
119
    }
 
120
  }
 
121
  
 
122
  if (removed != NULL) {
 
123
    DataObject* object;
 
124
    foreach (object, *removed) {
 
125
      Event* event = qobject_cast<Event*>(object);
 
126
      Q_ASSERT(event != NULL);
 
127
      
 
128
      Q_ASSERT(date_map_.contains(event->date()));
 
129
      date_map_.remove(event->date());
 
130
    }
 
131
  }
 
132
  
 
133
  SourceCollection::notify_contents_altered(added, removed);
 
134
}