Akonadi Contacts

categorieseditwidget.cpp
1/*
2 This file is part of Contact Editor.
3
4 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "categorieseditwidget.h"
10
11#include <Akonadi/Item>
12#include <Akonadi/Tag>
13#include <Akonadi/TagWidget>
14#include <QHBoxLayout>
15#include <akonadi/tagcreatejob.h>
16using namespace Akonadi;
17CategoriesEditWidget::CategoriesEditWidget(QWidget *parent)
18 : QWidget(parent)
19 , mTagWidget(new Akonadi::TagWidget(this))
20{
21 auto layout = new QHBoxLayout(this);
22 layout->setContentsMargins({});
23 layout->addWidget(mTagWidget);
24}
25
26CategoriesEditWidget::~CategoriesEditWidget() = default;
27
28void CategoriesEditWidget::setReadOnly(bool readOnly)
29{
30 mTagWidget->setReadOnly(readOnly);
31}
32
33void CategoriesEditWidget::loadContact(const KContacts::Addressee &contact)
34{
35 Akonadi::Tag::List tags;
36
37 const QStringList categories = contact.categories();
38 tags.reserve(categories.count());
39 for (const QString &category : categories) {
40 if (category.startsWith(QLatin1StringView("akonadi:"))) {
41 tags.append(Akonadi::Tag::fromUrl(QUrl(category)));
42 } else {
43 Akonadi::Tag missingTag = Akonadi::Tag(category);
44 auto createJob = new Akonadi::TagCreateJob(missingTag, this);
45 createJob->setMergeIfExisting(true);
46 connect(createJob, &Akonadi::TagCreateJob::result, this, &CategoriesEditWidget::onMissingTagCreated);
47 }
48 }
49
50 mTagWidget->setSelection(tags);
51}
52
53void CategoriesEditWidget::onMissingTagCreated(KJob *job)
54{
55 if (job->error()) {
56 return;
57 }
58 auto createJob = static_cast<Akonadi::TagCreateJob *>(job);
59
60 auto selectedTags{mTagWidget->selection()};
61 selectedTags += createJob->tag();
62 mTagWidget->setSelection(selectedTags);
63}
64
65void CategoriesEditWidget::storeContact(KContacts::Addressee &contact) const
66{
67 QStringList categories;
68
69 const Akonadi::Tag::List tags = mTagWidget->selection();
70 categories.reserve(tags.count());
71 for (const Akonadi::Tag &tag : tags) {
72 categories.append(tag.name());
73 }
74
75 contact.setCategories(categories);
76}
77
78#include "moc_categorieseditwidget.cpp"
void setCategories(const QStringList &category)
QStringList categories() const
int error() const
void result(KJob *job)
A widget for editing the display name of a contact.
Category category(StandardShortcut id)
void append(QList< T > &&value)
qsizetype count() const const
void reserve(qsizetype size)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.