Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-qml-plugin-calendar] Multiple notebook support
All but the default notebook are marked as readonly.  Each notebook
is assigned a user-specifyable colour and can be individually enabled
and disabled.
  • Loading branch information
Aaron Kennedy committed Aug 12, 2013
1 parent 45b9766 commit e1bdb6c
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 27 deletions.
11 changes: 8 additions & 3 deletions src/calendaragendamodel.cpp
Expand Up @@ -48,7 +48,6 @@ NemoCalendarAgendaModel::NemoCalendarAgendaModel(QObject *parent)
mRoleNames[EventObjectRole] = "event";
mRoleNames[OccurrenceObjectRole] = "occurrence";
mRoleNames[SectionBucketRole] = "sectionBucket";
mRoleNames[NotebookColorRole] = "notebookColor";

#ifndef NEMO_USE_QT5
setRoleNames(mRoleNames);
Expand Down Expand Up @@ -151,6 +150,14 @@ void NemoCalendarAgendaModel::doRefresh(bool reset)
mKCal::ExtendedCalendar::ExpandedIncidenceList newEvents =
calendar->rawExpandedEvents(mStartDate, endDate, false, false, KDateTime::Spec(KDateTime::LocalZone));

// Filter out excluded notebooks
for (int ii = 0; ii < newEvents.count(); ++ii) {
if (!NemoCalendarEventCache::instance()->mNotebooks.contains(NemoCalendarDb::calendar()->notebook(newEvents.at(ii).second))) {
newEvents.remove(ii);
--ii;
}
}

qSort(newEvents.begin(), newEvents.end(), eventsLessThan);

int oldEventCount = mEvents.count();
Expand Down Expand Up @@ -267,8 +274,6 @@ QVariant NemoCalendarAgendaModel::data(const QModelIndex &index, int role) const
return QVariant::fromValue<QObject *>(mEvents.at(index.row()));
case SectionBucketRole:
return mEvents.at(index.row())->startTime().date();
case NotebookColorRole:
return "#00aeef"; // TODO: hardcoded, as we only support local events for now
default:
return QVariant();
}
Expand Down
1 change: 0 additions & 1 deletion src/calendaragendamodel.h
Expand Up @@ -64,7 +64,6 @@ class NemoCalendarAgendaModel : public QAbstractListModel, public QQmlParserStat
EventObjectRole = Qt::UserRole,
OccurrenceObjectRole,
SectionBucketRole,
NotebookColorRole
};

explicit NemoCalendarAgendaModel(QObject *parent = 0);
Expand Down
40 changes: 40 additions & 0 deletions src/calendarapi.cpp
Expand Up @@ -32,9 +32,11 @@

#include "calendarapi.h"

#include <QSettings>
#include <QQmlEngine>
#include "calendardb.h"
#include "calendarevent.h"
#include "calendareventcache.h"

NemoCalendarApi::NemoCalendarApi(QObject *parent)
: QObject(parent)
Expand Down Expand Up @@ -71,6 +73,44 @@ void NemoCalendarApi::remove(const QString &uid, const QDateTime &time)
NemoCalendarDb::storage()->save();
}

QStringList NemoCalendarApi::excludedNotebooks() const
{
mKCal::Notebook::List notebooks = NemoCalendarDb::storage()->notebooks();

QStringList rv;

for (int ii = 0; ii < notebooks.count(); ++ii) {
if (!NemoCalendarEventCache::instance()->mNotebooks.contains(notebooks.at(ii)->uid()))
rv.append(notebooks.at(ii)->uid());
}

return rv;
}

void NemoCalendarApi::setExcludedNotebooks(const QStringList &list)
{
QStringList current = excludedNotebooks();
if (list == current)
return;

QSettings settings("nemo", "nemo-qml-plugin-calendar");

for (int ii = 0; ii < current.count(); ++ii) {
QString uid = current.at(ii);
if (!list.contains(uid))
settings.remove("exclude/" + uid);
}

for (int ii = 0; ii < list.count(); ++ii) {
QString uid = list.at(ii);
if (!current.contains(uid))
settings.setValue("exclude/" + uid, true);
}

emit excludedNotebooksChanged();
NemoCalendarEventCache::instance()->load();
}

QObject *NemoCalendarApi::New(QQmlEngine *e, QJSEngine *)
{
return new NemoCalendarApi(e);
Expand Down
10 changes: 10 additions & 0 deletions src/calendarapi.h
Expand Up @@ -33,6 +33,7 @@
#ifndef CALENDARAPI_H
#define CALENDARAPI_H

#include <QStringList>
#include <QAbstractListModel>

class QJSEngine;
Expand All @@ -42,14 +43,23 @@ class NemoCalendarEvent;
class NemoCalendarApi : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList excludedNotebooks READ excludedNotebooks WRITE setExcludedNotebooks NOTIFY excludedNotebooksChanged)

public:
NemoCalendarApi(QObject *parent = 0);

Q_INVOKABLE NemoCalendarEvent *createEvent();
Q_INVOKABLE void remove(const QString &);
Q_INVOKABLE void remove(const QString &, const QDateTime &);

QStringList excludedNotebooks() const;
void setExcludedNotebooks(const QStringList &);

static QObject *New(QQmlEngine *, QJSEngine *);

signals:
void excludedNotebooksChanged();

};

#endif // CALENDARAPI_H
3 changes: 3 additions & 0 deletions src/calendardb.cpp
Expand Up @@ -32,6 +32,8 @@

#include "calendardb.h"

#include <notebook.h>
#include <QDebug>
mKCal::ExtendedCalendar::Ptr &NemoCalendarDb::calendar()
{
static mKCal::ExtendedCalendar::Ptr ptr;
Expand All @@ -50,5 +52,6 @@ mKCal::ExtendedStorage::Ptr &NemoCalendarDb::storage()

ptr = calendar()->defaultStorage(calendar());
ptr->open();

return ptr;
}
9 changes: 8 additions & 1 deletion src/calendarevent.cpp
Expand Up @@ -346,7 +346,8 @@ QString NemoCalendarEvent::uniqueId() const

QString NemoCalendarEvent::color() const
{
return "#00aeef"; // TODO: hardcoded, as we only support local events for now
QString eventNotebook = NemoCalendarDb::calendar()->notebook(mEvent);
return NemoCalendarEventCache::instance()->notebookColor(eventNotebook);
}

QString NemoCalendarEvent::alarmProgram() const
Expand Down Expand Up @@ -385,6 +386,12 @@ void NemoCalendarEvent::setAlarmProgram(const QString &program)
alarm->setProcedureAlarm(program, uniqueId());
}

bool NemoCalendarEvent::readonly() const
{
QString eventNotebook = NemoCalendarDb::calendar()->notebook(mEvent);
return eventNotebook != NemoCalendarDb::storage()->defaultNotebook()->uid();
}

void NemoCalendarEvent::save()
{
if (mNewEvent) {
Expand Down
8 changes: 7 additions & 1 deletion src/calendarevent.h
Expand Up @@ -55,8 +55,9 @@ class NemoCalendarEvent : public QObject
Q_PROPERTY(int recurExceptions READ recurExceptions NOTIFY recurExceptionsChanged)
Q_PROPERTY(Reminder reminder READ reminder WRITE setReminder NOTIFY reminderChanged)
Q_PROPERTY(QString uniqueId READ uniqueId CONSTANT)
Q_PROPERTY(QString color READ color CONSTANT)
Q_PROPERTY(QString color READ color NOTIFY colorChanged)
Q_PROPERTY(QString alarmProgram READ alarmProgram WRITE setAlarmProgram NOTIFY alarmProgramChanged)
Q_PROPERTY(bool readonly READ readonly CONSTANT)

public:
enum Recur {
Expand Down Expand Up @@ -118,6 +119,8 @@ class NemoCalendarEvent : public QObject
QString alarmProgram() const;
void setAlarmProgram(const QString &);

bool readonly() const;

Q_INVOKABLE void save();
Q_INVOKABLE void remove();

Expand All @@ -135,8 +138,11 @@ class NemoCalendarEvent : public QObject
void recurExceptionsChanged();
void reminderChanged();
void alarmProgramChanged();
void colorChanged();

private:
friend class NemoCalendarEventCache;

bool mNewEvent:1;
KCalCore::Event::Ptr mEvent;
};
Expand Down
88 changes: 67 additions & 21 deletions src/calendareventcache.cpp
Expand Up @@ -32,6 +32,7 @@

// Qt
#include <QDebug>
#include <QSettings>

// mkcal
#include <event.h>
Expand All @@ -46,8 +47,50 @@ NemoCalendarEventCache::NemoCalendarEventCache()
{
NemoCalendarDb::storage()->registerObserver(this);

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

void NemoCalendarEventCache::load()
{
QSettings settings("nemo", "nemo-qml-plugin-calendar");

mKCal::Notebook::List notebooks = NemoCalendarDb::storage()->notebooks();
mNotebooks.clear();
mNotebookColors.clear();

QStringList defaultNotebookColors = QStringList() << "#00aeef" << "red" << "blue" << "green" << "pink" << "yellow";
int nextDefaultNotebookColor = 0;

for (int ii = 0; ii < notebooks.count(); ++ii) {
QString uid = notebooks.at(ii)->uid();
if (!settings.value("exclude/" + uid, false).toBool()) {
mNotebooks.insert(uid);
NemoCalendarDb::storage()->loadNotebookIncidences(uid);
}

QString color = settings.value("colors/" + uid, QString()).toString();
if (color.isEmpty())
color = defaultNotebookColors.at((nextDefaultNotebookColor++) % defaultNotebookColors.count());

mNotebookColors.insert(uid, color);
}

mKCal::ExtendedCalendar::Ptr calendar = NemoCalendarDb::calendar();

for (QSet<NemoCalendarEvent *>::Iterator iter = mEvents.begin(); iter != mEvents.end(); ++iter) {
QString uid = (*iter)->event()->uid();
KCalCore::Event::Ptr event = calendar->event(uid);
(*iter)->setEvent(event);
}

for (QSet<NemoCalendarEventOccurrence *>::Iterator iter = mEventOccurrences.begin();
iter != mEventOccurrences.end(); ++iter) {
QString uid = (*iter)->event()->uid();
KCalCore::Event::Ptr event = calendar->event(uid);
(*iter)->setEvent(event);
}

emit modelReset();
}

NemoCalendarEventCache *NemoCalendarEventCache::instance()
Expand All @@ -71,25 +114,7 @@ void NemoCalendarEventCache::storageModified(mKCal::ExtendedStorage *storage, co
//
// for now, let's just ask models to reload whenever a change happens.

NemoCalendarDb::storage()->loadNotebookIncidences(NemoCalendarDb::storage()->defaultNotebook()->uid());
mKCal::ExtendedCalendar::Ptr calendar = NemoCalendarDb::calendar();

for (QSet<NemoCalendarEvent *>::Iterator iter = mEvents.begin(); iter != mEvents.end(); ++iter) {
QString uid = (*iter)->event()->uid();
KCalCore::Event::Ptr event = calendar->event(uid);
(*iter)->setEvent(event);
}

for (QSet<NemoCalendarEventOccurrence *>::Iterator iter = mEventOccurrences.begin();
iter != mEventOccurrences.end(); ++iter) {
QString uid = (*iter)->event()->uid();
KCalCore::Event::Ptr event = calendar->event(uid);
(*iter)->setEvent(event);
}


qDebug() << Q_FUNC_INFO << "Emitting reset";
emit modelReset();
load();
}

void NemoCalendarEventCache::storageProgress(mKCal::ExtendedStorage *storage, const QString &info)
Expand All @@ -102,3 +127,24 @@ void NemoCalendarEventCache::storageFinished(mKCal::ExtendedStorage *storage, bo

}

QString NemoCalendarEventCache::notebookColor(const QString &notebook) const
{
return mNotebookColors.value(notebook, "black");
}

void NemoCalendarEventCache::setNotebookColor(const QString &notebook, const QString &color)
{
if (!mNotebookColors.contains(notebook))
return;

QSettings settings("nemo", "nemo-qml-plugin-calendar");

mNotebookColors[notebook] = color;
settings.setValue("colors/" + notebook, color);

for (QSet<NemoCalendarEvent *>::Iterator iter = mEvents.begin(); iter != mEvents.end(); ++iter) {
NemoCalendarEvent *e = *iter;
if (NemoCalendarDb::calendar()->notebook(e->event()) == notebook)
emit e->colorChanged();
}
}
12 changes: 12 additions & 0 deletions src/calendareventcache.h
Expand Up @@ -51,18 +51,30 @@ class NemoCalendarEventCache : public QObject, public mKCal::ExtendedStorageObse

public:
static NemoCalendarEventCache *instance();
void load();

/* 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);

QString notebookColor(const QString &) const;
void setNotebookColor(const QString &, const QString &);

signals:
void modelReset();

private:

friend class NemoCalendarApi;
friend class NemoCalendarEvent;
friend class NemoCalendarAgendaModel;
friend class NemoCalendarEventOccurrence;

QStringList mDefaultNotebookColors;

QSet<QString> mNotebooks;
QHash<QString, QString> mNotebookColors;
QSet<NemoCalendarEvent *> mEvents;
QSet<NemoCalendarEventOccurrence *> mEventOccurrences;
};
Expand Down

0 comments on commit e1bdb6c

Please sign in to comment.