Mailcommon

addtagdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "addtagdialog.h"
8#include "mailcommon_debug.h"
9#include "tag/tagwidget.h"
10
11#include <KJob>
12#include <KLocalizedString>
13#include <KMessageBox>
14#include <QLineEdit>
15
16#include <QDialogButtonBox>
17#include <QPushButton>
18#include <QVBoxLayout>
19
20using namespace MailCommon;
21class MailCommon::AddTagDialogPrivate
22{
23public:
24 AddTagDialogPrivate() = default;
25
26 QString mLabel;
27 QString mGid;
28 MailCommon::TagWidget *mTagWidget = nullptr;
30 Akonadi::Tag mTag;
31 QPushButton *mOkButton = nullptr;
32};
33
34AddTagDialog::AddTagDialog(const QList<KActionCollection *> &actions, QWidget *parent)
35 : QDialog(parent)
36 , d(new MailCommon::AddTagDialogPrivate)
37{
38 setModal(true);
39 setWindowTitle(i18nc("@title:window", "Add Tag"));
40 auto mainLayout = new QVBoxLayout(this);
41
42 d->mTagWidget = new MailCommon::TagWidget(actions, this);
43 mainLayout->addWidget(d->mTagWidget);
44
45 connect(d->mTagWidget->tagNameLineEdit(), &QLineEdit::textChanged, this, &AddTagDialog::slotTagNameChanged);
46
48 d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
49 d->mOkButton->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Return));
50 connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTagDialog::slotSave);
51 connect(buttonBox, &QDialogButtonBox::rejected, this, &AddTagDialog::reject);
52 d->mOkButton->setDefault(true);
53 d->mOkButton->setEnabled(false);
54
55 mainLayout->addWidget(buttonBox);
56}
57
58AddTagDialog::~AddTagDialog() = default;
59
60void AddTagDialog::setTags(const QList<MailCommon::Tag::Ptr> &tags)
61{
62 d->mTags = tags;
63}
64
65void AddTagDialog::slotTagNameChanged(const QString &text)
66{
67 d->mOkButton->setEnabled(!text.trimmed().isEmpty());
68}
69
70void AddTagDialog::slotSave()
71{
72 const QString name(d->mTagWidget->tagNameLineEdit()->text());
73
74 for (const MailCommon::Tag::Ptr &tag : std::as_const(d->mTags)) {
75 if (tag->name() == name) {
76 KMessageBox::error(this, i18n("Tag %1 already exists", name));
77 d->mTagWidget->tagNameLineEdit()->setFocus();
78 d->mTagWidget->tagNameLineEdit()->selectAll();
79 return;
80 }
81 }
82
83 MailCommon::Tag::Ptr tag(Tag::createDefaultTag(name));
84 d->mTagWidget->recordTagSettings(tag);
85 const Akonadi::Tag akonadiTag = tag->saveToAkonadi();
86 auto createJob = new Akonadi::TagCreateJob(akonadiTag, this);
87 connect(createJob, &Akonadi::TagCreateJob::result, this, &AddTagDialog::onTagCreated);
88
89 d->mLabel = name;
90}
91
92void AddTagDialog::onTagCreated(KJob *job)
93{
94 if (job->error()) {
95 qCWarning(MAILCOMMON_LOG) << "Failed to create tag: " << job->errorString();
96 reject();
97 return;
98 }
99 auto createJob = static_cast<Akonadi::TagCreateJob *>(job);
100 d->mTag = createJob->tag();
101 accept();
102}
103
104QString AddTagDialog::label() const
105{
106 return d->mLabel;
107}
108
109Akonadi::Tag AddTagDialog::tag() const
110{
111 return d->mTag;
112}
113
114#include "moc_addtagdialog.cpp"
virtual QString errorString() const
int error() const
void result(KJob *job)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString name(StandardAction id)
The filter dialog.
virtual void accept()
virtual void reject()
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QString trimmed() const const
Key_Return
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:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.