Incidenceeditor

incidencecategories.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
3 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "incidencecategories.h"
9using namespace Qt::Literals::StringLiterals;
10
11#include "incidenceeditor_debug.h"
12#include "ui_dialogdesktop.h"
13
14#include <Akonadi/CalendarUtils>
15#include <Akonadi/TagAttribute>
16#include <Akonadi/TagCreateJob>
17#include <Akonadi/TagFetchJob>
18#include <Akonadi/TagFetchScope>
19
20using namespace IncidenceEditorNG;
21
22IncidenceCategories::IncidenceCategories(Ui::EventOrTodoDesktop *ui)
23 : mUi(ui)
24{
25 setObjectName("IncidenceCategories"_L1);
26
27 connect(mUi->mTagWidget, &Akonadi::TagWidget::selectionChanged, this, &IncidenceCategories::onSelectionChanged);
28}
29
30void IncidenceCategories::onSelectionChanged(const Akonadi::Tag::List &list)
31{
32 Q_UNUSED(list)
33 mDirty = true;
35}
36
37void IncidenceCategories::load(const KCalendarCore::Incidence::Ptr &incidence)
38{
39 Q_UNUSED(incidence)
40 mDirty = false;
41 mWasDirty = false;
42}
43
44void IncidenceCategories::load(const Akonadi::Item &item)
45{
46 mLoadedIncidence = Akonadi::CalendarUtils::incidence(item);
47 mDirty = false;
48 mWasDirty = false;
49
50 Q_ASSERT(mLoadedIncidence);
51 if (mLoadedIncidence) {
52 mMissingCategories = mLoadedIncidence->categories();
53 const auto tags = item.tags();
54 Akonadi::Tag::List selectedTags;
55 selectedTags.reserve(mMissingCategories.count());
56 for (const auto &tag : tags) {
57 if (mMissingCategories.removeAll(tag.name()) > 0) {
58 selectedTags << tag;
59 }
60 }
61 createMissingCategories();
62 mUi->mTagWidget->blockSignals(true);
63 mUi->mTagWidget->setSelection(selectedTags);
64 mUi->mTagWidget->blockSignals(false);
65 }
66}
67
68void IncidenceCategories::save(const KCalendarCore::Incidence::Ptr &incidence)
69{
70 Q_ASSERT(incidence);
71 if (mDirty) {
72 incidence->setCategories(categories());
73 }
74}
75
76void IncidenceCategories::save(Akonadi::Item &item)
77{
78 const auto &selectedTags = mUi->mTagWidget->selection();
79 if (mDirty) {
80 item.setTags(selectedTags);
81 }
82}
83
84QStringList IncidenceCategories::categories() const
85{
87 const auto &selectedTags = mUi->mTagWidget->selection();
88 list.reserve(selectedTags.count() + mMissingCategories.count());
89 for (const Akonadi::Tag &tag : selectedTags) {
90 list << tag.name();
91 }
92 list << mMissingCategories;
93 return list;
94}
95
96void IncidenceCategories::createMissingCategories()
97{
98 for (const QString &category : std::as_const(mMissingCategories)) {
99 // Either the tag doesn't exist, or Akonadi doesn't have a tag <-> item
100 // relation for this category and instance. Try to create a PLAIN tag.
101 Akonadi::Tag missingTag = Akonadi::Tag(category);
102 auto createJob = new Akonadi::TagCreateJob(missingTag, this);
103 createJob->setMergeIfExisting(true);
104 connect(createJob, &Akonadi::TagCreateJob::result, this, &IncidenceCategories::onMissingTagCreated);
105 }
106}
107
108bool IncidenceCategories::isDirty() const
109{
110 return mDirty;
111}
112
113void IncidenceCategories::printDebugInfo() const
114{
115 qCDebug(INCIDENCEEDITOR_LOG) << "selected categories = " << categories();
116 qCDebug(INCIDENCEEDITOR_LOG) << "mMissingCategories = " << mMissingCategories;
117 qCDebug(INCIDENCEEDITOR_LOG) << "mLoadedIncidence->categories() = " << mLoadedIncidence->categories();
118}
119
120void IncidenceCategories::onMissingTagCreated(KJob *job)
121{
122 if (job->error()) {
123 qCWarning(INCIDENCEEDITOR_LOG) << "Failed to create tag " << job->errorString();
124 return;
125 }
126 auto createJob = static_cast<Akonadi::TagCreateJob *>(job);
127 int count = mMissingCategories.removeAll(createJob->tag().name());
128
129 auto selectedTags{mUi->mTagWidget->selection()};
130 selectedTags += createJob->tag();
131
132 // If the created tag was one of the instance's missing categories,
133 // adding it to the widget doesn't make it dirty.
134 mUi->mTagWidget->blockSignals(count > 0);
135 mUi->mTagWidget->setSelection(selectedTags);
136 mUi->mTagWidget->blockSignals(false);
137}
138
139#include "moc_incidencecategories.cpp"
void checkDirtyStatus()
Checks if the dirty status has changed until last check and emits the dirtyStatusChanged signal if ne...
QSharedPointer< IncidenceT > incidence() const
Convenience method to get a pointer for a specific const Incidence Type.
virtual QString errorString() const
int error() const
void result(KJob *job)
AKONADI_CALENDAR_EXPORT KCalendarCore::Incidence::Ptr incidence(const Akonadi::Item &item)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
qsizetype count() const const
qsizetype removeAll(const AT &t)
void reserve(qsizetype size)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.