Commit 124c8b13 authored by Volker Krause's avatar Volker Krause
Browse files

Add calendar list model, backed by the KCalendarCore calendar plugin

parent 7e1b8f2e
Pipeline #189118 passed with stage
in 3 minutes and 27 seconds
......@@ -5,7 +5,8 @@ add_library(KCalendarCoreExtras)
generate_export_header(KCalendarCoreExtras BASE_NAME KCalendarCoreExtras)
target_sources(KCalendarCoreExtras PRIVATE
calendarmodel.cpp
calendarlistmodel.cpp
calendarpluginloader.cpp
)
if (ANDROID)
......
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "calendarlistmodel.h"
#include "calendarpluginloader.h"
#include <KCalendarCore/CalendarPlugin>
using namespace KCalendarCore;
namespace KCalendarCore {
class CalendarListModelPrivate
{
public:
QVector<Calendar::Ptr> calendars;
};
}
CalendarListModel::CalendarListModel(QObject *parent)
: QAbstractListModel(parent)
, d(new CalendarListModelPrivate)
{
if (CalendarPluginLoader::hasPlugin()) {
d->calendars = CalendarPluginLoader::plugin()->calendars();
connect(CalendarPluginLoader::plugin(), &CalendarPlugin::calendarsChanged, this, [this]() {
beginResetModel();
d->calendars = CalendarPluginLoader::plugin()->calendars();
endResetModel();
});
}
}
CalendarListModel::~CalendarListModel() = default;
int CalendarListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() || !CalendarPluginLoader::hasPlugin()) {
return 0;
}
return d->calendars.size();
}
QVariant CalendarListModel::data(const QModelIndex &index, int role) const
{
if (!checkIndex(index) || !CalendarPluginLoader::hasPlugin()) {
return {};
}
const auto &cal = d->calendars.at(index.row());
switch (role) {
case Qt::DisplayRole:
return cal->name();
case Qt::DecorationRole:
return cal->icon();
case CalendarRole:
return QVariant::fromValue(cal.get());
case AccessModeRole:
return cal->accessMode();
}
return {};
}
QHash<int, QByteArray> CalendarListModel::roleNames() const
{
auto n = QAbstractListModel::roleNames();
n.insert(Qt::DisplayRole, "name");
n.insert(CalendarRole, "calendar");
n.insert(AccessModeRole, "accessMode");
return n;
}
/*
SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KCALENDARCORE_CALENDARLISTMODEL_H
#define KCALENDARCORE_CALENDARLISTMODEL_H
#include "kcalendarcoreextras_export.h"
#include <QAbstractListModel>
#include <memory>
namespace KCalendarCore {
class CalendarListModelPrivate;
/** Model adaptor for KCalendarCore::CalendarPlugin::calendars(). */
class KCALENDARCOREEXTRAS_EXPORT CalendarListModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit CalendarListModel(QObject *parent = nullptr);
~CalendarListModel();
enum Role {
CalendarRole = Qt::UserRole,
AccessModeRole,
};
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex & index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
private:
std::unique_ptr<CalendarListModelPrivate> d;
};
}
#endif // KCALENDARCORE_CALENDARLISTMODEL_H
  • This, or the related commits, installs (even on Linux) an unversioned .so library (libKCalendarCoreExtras.so). Is this intended?

  • That being a dynamic library is indeed not strictly necessary on non-Android so far, turned into a static library now.

Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment