diff --git a/src/incidencecategories.cpp b/src/incidencecategories.cpp index 1d29dcb..fe3d76a 100644 --- a/src/incidencecategories.cpp +++ b/src/incidencecategories.cpp @@ -1,161 +1,156 @@ /* Copyright (c) 2010 Bertjan Broeksema Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "incidencecategories.h" #include "editorconfig.h" #include "ui_dialogdesktop.h" #include #include "incidenceeditor_debug.h" #include #include #include #include using namespace IncidenceEditorNG; IncidenceCategories::IncidenceCategories(Ui::EventOrTodoDesktop *ui) : mUi(ui) , mDirty(false) { setObjectName(QStringLiteral("IncidenceCategories")); connect(mUi->mTagWidget, &Akonadi::TagWidget::selectionChanged, this, &IncidenceCategories::onSelectionChanged); } void IncidenceCategories::onSelectionChanged(const Akonadi::Tag::List &list) { Q_UNUSED(list); mDirty = true; checkDirtyStatus(); } void IncidenceCategories::load(const KCalCore::Incidence::Ptr &incidence) { mLoadedIncidence = incidence; mDirty = false; mWasDirty = false; mMissingCategories.clear(); if (mLoadedIncidence) { Akonadi::TagFetchJob *fetchJob = new Akonadi::TagFetchJob(this); fetchJob->fetchScope().fetchAttribute(); connect(fetchJob, &Akonadi::TagFetchJob::result, this, &IncidenceCategories::onTagsFetched); } } -void IncidenceCategories::load(const Akonadi::Item &item) -{ - Q_UNUSED(item); -} - void IncidenceCategories::save(const KCalCore::Incidence::Ptr &incidence) { Q_ASSERT(incidence); if (mDirty) { incidence->setCategories(categories()); } } void IncidenceCategories::save(Akonadi::Item &item) { const auto &selectedTags = mUi->mTagWidget->selection(); if (mDirty) { item.setTags(selectedTags); } } QStringList IncidenceCategories::categories() const { QStringList list; const auto &selectedTags = mUi->mTagWidget->selection(); list.reserve(selectedTags.count() + mMissingCategories.count()); for (const Akonadi::Tag &tag : selectedTags) { list << tag.name(); } list << mMissingCategories; return list; } void IncidenceCategories::createMissingCategories() { for (const QString &category : qAsConst(mMissingCategories)) { Akonadi::Tag missingTag = Akonadi::Tag::genericTag(category); Akonadi::TagCreateJob *createJob = new Akonadi::TagCreateJob(missingTag, this); connect(createJob, &Akonadi::TagCreateJob::result, this, &IncidenceCategories::onMissingTagCreated); } } bool IncidenceCategories::isDirty() const { return mDirty; } void IncidenceCategories::printDebugInfo() const { qCDebug(INCIDENCEEDITOR_LOG) << "selected categories = " << categories(); qCDebug(INCIDENCEEDITOR_LOG) << "mMissingCategories = " << mMissingCategories; qCDebug(INCIDENCEEDITOR_LOG) << "mLoadedIncidence->categories() = " << mLoadedIncidence->categories(); } void IncidenceCategories::onTagsFetched(KJob *job) { if (job->error()) { qCWarning(INCIDENCEEDITOR_LOG) << "Failed to load tags " << job->errorString(); return; } Akonadi::TagFetchJob *fetchJob = static_cast(job); const Akonadi::Tag::List jobTags = fetchJob->tags(); Q_ASSERT(mLoadedIncidence); mMissingCategories = mLoadedIncidence->categories(); Akonadi::Tag::List selectedTags; selectedTags.reserve(mMissingCategories.count()); for (const auto &tag : jobTags) { if (mMissingCategories.removeAll(tag.name()) > 0) { selectedTags << tag; } } createMissingCategories(); mUi->mTagWidget->setSelection(selectedTags); } void IncidenceCategories::onMissingTagCreated(KJob *job) { if (job->error()) { qCWarning(INCIDENCEEDITOR_LOG) << "Failed to create tag " << job->errorString(); return; } Akonadi::TagCreateJob *createJob = static_cast(job); int count = mMissingCategories.removeAll(createJob->tag().name()); Q_ASSERT(count > 0); QVector selectedTags; selectedTags.reserve(mUi->mTagWidget->selection().count() + 1); selectedTags << mUi->mTagWidget->selection() << createJob->tag(); mUi->mTagWidget->setSelection(selectedTags); } diff --git a/src/incidencecategories.h b/src/incidencecategories.h index 55c1713..f368e87 100644 --- a/src/incidencecategories.h +++ b/src/incidencecategories.h @@ -1,74 +1,73 @@ /* Copyright (c) 2010 Bertjan Broeksema Copyright (c) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef INCIDENCEEDITOR_INCIDENCECATEGORIES_H #define INCIDENCEEDITOR_INCIDENCECATEGORIES_H #include "incidenceeditor-ng.h" namespace Ui { class EventOrTodoDesktop; } namespace IncidenceEditorNG { class IncidenceCategories : public IncidenceEditor { Q_OBJECT public: explicit IncidenceCategories(Ui::EventOrTodoDesktop *ui); void load(const KCalCore::Incidence::Ptr &incidence) override; - void load(const Akonadi::Item &item) override; void save(const KCalCore::Incidence::Ptr &incidence) override; void save(Akonadi::Item &item) override; /** * Returns the list of currently selected categories. */ QStringList categories() const; bool isDirty() const override; void printDebugInfo() const override; private: void createMissingCategories(); private Q_SLOTS: void onSelectionChanged(const Akonadi::Tag::List &); void onTagsFetched(KJob *); void onMissingTagCreated(KJob *); private: Ui::EventOrTodoDesktop *mUi; /** * List of categories for which no tag might exist. * * For each category of the editted incidence, we want to make sure that there exists a * corresponding tag in Akonadi. For missing categories, a \a TagCreateJob is issued. * Eventually, there should be no missing categories left. In case tag creation fails for some * categories, this list still holds these categories so they don't get lost */ QStringList mMissingCategories; bool mDirty; }; } #endif