Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add CalendarEventCache.
This will (eventually) cache CalendarEvent instances between models and handle
updating them etc. For now, just observe data source changes and ask model
instances to reload themselves.
  • Loading branch information
rburchell committed Mar 27, 2013
1 parent 438b611 commit d891877
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/calendaragendamodel.cpp
Expand Up @@ -36,6 +36,7 @@
// mkcal
#include <event.h>

#include "calendareventcache.h"
#include "calendaragendamodel.h"
#include "calendarevent.h"
#include "calendardb.h"
Expand All @@ -48,6 +49,9 @@ NemoCalendarAgendaModel::NemoCalendarAgendaModel(QObject *parent)
roles[SectionBucketRole] = "sectionBucket";
roles[NotebookColorRole] = "notebookColor";
setRoleNames(roles);

connect(this, SIGNAL(startDateChanged()), this, SLOT(load()));
connect(NemoCalendarEventCache::instance(), SIGNAL(modelReset()), this, SLOT(load()));
}

NemoCalendarAgendaModel::~NemoCalendarAgendaModel()
Expand All @@ -67,14 +71,13 @@ void NemoCalendarAgendaModel::setStartDate(const QDate &startDate)

mStartDate = startDate;
emit startDateChanged();
}

// load from database
// TODO: this init code should only run once, probably
NemoCalendarDb::storage()->loadNotebookIncidences(NemoCalendarDb::storage()->defaultNotebook()->uid());

void NemoCalendarAgendaModel::load()
{
// TODO: we really need a centralised event cache
KCalCore::Event::List eventList = NemoCalendarDb::calendar()->rawEventsForDate(startDate, KDateTime::Spec(KDateTime::LocalZone), KCalCore::EventSortStartDate, KCalCore::SortDirectionAscending);
qDebug() << Q_FUNC_INFO << "Loaded " << eventList.count() << " events for " << startDate;
KCalCore::Event::List eventList = NemoCalendarDb::calendar()->rawEventsForDate(mStartDate, KDateTime::Spec(KDateTime::LocalZone), KCalCore::EventSortStartDate, KCalCore::SortDirectionAscending);
qDebug() << Q_FUNC_INFO << "Loaded " << eventList.count() << " events for " << mStartDate;

beginResetModel();
qDeleteAll(mEvents);
Expand Down
3 changes: 3 additions & 0 deletions src/calendaragendamodel.h
Expand Up @@ -62,6 +62,9 @@ class NemoCalendarAgendaModel : public NemoCalendarAbstractModel
signals:
void startDateChanged();

private slots:
void load();

private:
QDate mStartDate;
QList<NemoCalendarEvent *> mEvents;
Expand Down
86 changes: 86 additions & 0 deletions src/calendareventcache.cpp
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Robin Burchell <robin.burchell@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

// Qt
#include <QDebug>

// mkcal
#include <event.h>

#include "calendardb.h"
#include "calendareventcache.h"

NemoCalendarEventCache::NemoCalendarEventCache()
: QObject(0)
, mKCal::ExtendedStorageObserver()
{
NemoCalendarDb::storage()->registerObserver(this);

// initialize storage
NemoCalendarDb::storage()->loadNotebookIncidences(NemoCalendarDb::storage()->defaultNotebook()->uid());
}

NemoCalendarEventCache *NemoCalendarEventCache::instance()
{
static NemoCalendarEventCache *cacheInstance;
if (!cacheInstance)
cacheInstance = new NemoCalendarEventCache;

return cacheInstance;
}

void NemoCalendarEventCache::storageModified(mKCal::ExtendedStorage *storage, const QString &info)
{
// 'info' is either a path to the database (in which case we're screwed, we
// have no idea what changed, so tell all interested models to reload) or a
// space-seperated list of event UIDs.
//
// unfortunately we don't know *what* about these events changed with the
// current mkcal API, so we'll have to try our best to guess when the time
// comes.
//
// for now, let's just ask models to reload whenever a change happens.
NemoCalendarDb::storage()->loadNotebookIncidences(NemoCalendarDb::storage()->defaultNotebook()->uid());
qDebug() << Q_FUNC_INFO << "Emitting reset";
emit modelReset();
}

void NemoCalendarEventCache::storageProgress(mKCal::ExtendedStorage *storage, const QString &info)
{

}

void NemoCalendarEventCache::storageFinished(mKCal::ExtendedStorage *storage, bool error, const QString &info)
{

}

62 changes: 62 additions & 0 deletions src/calendareventcache.h
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2013 Jolla Ltd.
* Contact: Robin Burchell <robin.burchell@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#ifndef CALENDAREVENTCACHE_H
#define CALENDAREVENTCACHE_H

// Qt
#include <QDebug>
#include <QObject>

// mkcal
#include <event.h>
#include <extendedstorage.h>

class NemoCalendarEventCache : public QObject, public mKCal::ExtendedStorageObserver
{
Q_OBJECT
private:
NemoCalendarEventCache();

public:
static NemoCalendarEventCache *instance();

/* mKCal::ExtendedStorageObserver */
void storageModified(mKCal::ExtendedStorage *storage, const QString &info);
void storageProgress(mKCal::ExtendedStorage *storage, const QString &info);
void storageFinished(mKCal::ExtendedStorage *storage, bool error, const QString &info);

signals:
void modelReset();
};

#endif // CALENDAREVENTCACHE_H
6 changes: 4 additions & 2 deletions src/src.pro
Expand Up @@ -9,13 +9,15 @@ SOURCES += \
calendarabstractmodel.cpp \
calendarevent.cpp \
calendaragendamodel.cpp \
calendardb.cpp
calendardb.cpp \
calendareventcache.cpp

HEADERS += \
calendarevent.h \
calendarabstractmodel.h \
calendaragendamodel.h \
calendardb.h
calendardb.h \
calendareventcache.h

MOC_DIR = $$PWD/../.moc
OBJECTS_DIR = $$PWD/../.obj
Expand Down

0 comments on commit d891877

Please sign in to comment.