File indexing completed on 2025-01-05 04:49:48

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #include "pimcalendarsmodel.h"
0009 #include "settingschangenotifier.h"
0010 
0011 #include <Akonadi/CollectionFetchScope>
0012 #include <Akonadi/EntityDisplayAttribute>
0013 #include <Akonadi/EntityTreeModel>
0014 #include <Akonadi/Monitor>
0015 
0016 #include <KCalendarCore/Event>
0017 #include <KCalendarCore/Todo>
0018 
0019 #include <KConfigGroup>
0020 #include <KSharedConfig>
0021 
0022 PimCalendarsModel::PimCalendarsModel(QObject *parent)
0023     : QSortFilterProxyModel(parent)
0024 {
0025     setSortRole(Qt::DisplayRole);
0026     setSortCaseSensitivity(Qt::CaseInsensitive);
0027     setSortLocaleAware(true);
0028     setDynamicSortFilter(true);
0029 
0030     auto cr = new Akonadi::Monitor(this);
0031     cr->setMimeTypeMonitored(KCalendarCore::Event::eventMimeType());
0032     cr->setMimeTypeMonitored(KCalendarCore::Todo::todoMimeType());
0033     cr->setTypeMonitored(Akonadi::Monitor::Collections);
0034     cr->collectionFetchScope().setListFilter(Akonadi::CollectionFetchScope::Enabled);
0035 
0036     mEtm = new Akonadi::EntityTreeModel(cr, this);
0037     mEtm->setItemPopulationStrategy(Akonadi::EntityTreeModel::NoItemPopulation);
0038     mEtm->setListFilter(Akonadi::CollectionFetchScope::Enabled);
0039     connect(mEtm, &Akonadi::EntityTreeModel::collectionTreeFetched, this, [this]() {
0040         sort(0, Qt::AscendingOrder);
0041     });
0042 
0043     setSourceModel(mEtm);
0044 
0045     auto config = KSharedConfig::openConfig();
0046     auto group = config->group(QStringLiteral("PIMEventsPlugin"));
0047     const auto enabledCalendarIds = group.readEntry(QStringLiteral("calendars"), QList<qint64>());
0048     mEnabledCalendars = QSet<qint64>(enabledCalendarIds.begin(), enabledCalendarIds.end());
0049 }
0050 
0051 PimCalendarsModel::~PimCalendarsModel() = default;
0052 
0053 QHash<int, QByteArray> PimCalendarsModel::roleNames() const
0054 {
0055     return {
0056         {CollectionIdRole, "collectionId"},
0057         {NameRole, "name"},
0058         {EnabledRole, "enabled"},
0059         {CheckedRole, "checked"},
0060         {IconNameRole, "iconName"},
0061     };
0062 }
0063 
0064 QVariant PimCalendarsModel::data(const QModelIndex &index, int role) const
0065 {
0066     Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
0067 
0068     if (role == Qt::DisplayRole) {
0069         return QSortFilterProxyModel::data(index, role);
0070     }
0071 
0072     const auto &col = QSortFilterProxyModel::data(index, Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0073 
0074     if (!col.isValid()) {
0075         return {};
0076     }
0077 
0078     switch (role) {
0079     case CollectionIdRole:
0080         return col.id();
0081     case NameRole:
0082         return col.displayName();
0083     case EnabledRole: {
0084         const auto mts = col.contentMimeTypes();
0085         return mts.contains(KCalendarCore::Event::eventMimeType()) || mts.contains(KCalendarCore::Todo::todoMimeType());
0086     }
0087     case CheckedRole:
0088         return mEnabledCalendars.contains(col.id());
0089     case IconNameRole: {
0090         const auto attr = col.attribute<Akonadi::EntityDisplayAttribute>();
0091         const QString icon = attr ? attr->iconName() : QString();
0092     }
0093     default:
0094         return {};
0095     }
0096 }
0097 
0098 void PimCalendarsModel::setChecked(qint64 collectionId, bool checked)
0099 {
0100     bool done = false;
0101     if (checked) {
0102         done = !mEnabledCalendars.contains(collectionId);
0103         mEnabledCalendars.insert(collectionId);
0104     } else {
0105         done = mEnabledCalendars.remove(collectionId);
0106     }
0107     if (done) {
0108         const QModelIndex idx = Akonadi::EntityTreeModel::modelIndexForCollection(this, Akonadi::Collection(collectionId));
0109         Q_EMIT dataChanged(idx, idx);
0110     }
0111 }
0112 
0113 void PimCalendarsModel::saveConfig()
0114 {
0115     auto config = KSharedConfig::openConfig();
0116     auto group = config->group(QStringLiteral("PIMEventsPlugin"));
0117     auto savedList = group.readEntry("calendars", QList<qint64>());
0118     auto currentList = mEnabledCalendars.values();
0119     std::sort(savedList.begin(), savedList.end());
0120     std::sort(currentList.begin(), currentList.end());
0121 
0122     if (currentList != savedList) {
0123         group.writeEntry("calendars", currentList);
0124         SettingsChangeNotifier::self()->notifySettingsChanged();
0125     }
0126 }
0127 
0128 #include "moc_pimcalendarsmodel.cpp"