Mailcommon

addtagdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2012-2022 Laurent Montel <[email protected]>
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 
20 using namespace MailCommon;
21 class MailCommon::AddTagDialogPrivate
22 {
23 public:
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 
34 AddTagDialog::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 
58 AddTagDialog::~AddTagDialog() = default;
59 
60 void AddTagDialog::setTags(const QVector<MailCommon::Tag::Ptr> &tags)
61 {
62  d->mTags = tags;
63 }
64 
65 void AddTagDialog::slotTagNameChanged(const QString &text)
66 {
67  d->mOkButton->setEnabled(!text.trimmed().isEmpty());
68 }
69 
70 void 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 
92 void 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 
104 QString AddTagDialog::label() const
105 {
106  return d->mLabel;
107 }
108 
109 Akonadi::Tag AddTagDialog::tag() const
110 {
111  return d->mTag;
112 }
QList< QAction * > actions() const const
void result(KJob *job)
QString trimmed() const const
void setModal(bool modal)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString i18n(const char *text, const TYPE &arg...)
void textChanged(const QString &text)
bool isEmpty() const const
void setWindowTitle(const QString &)
Key_Return
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString name(StandardShortcut id)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
virtual QString errorString() const
int error() const
The filter dialog.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:14 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.