Warning, file /pim/mailcommon/src/tag/addtagdialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "addtagdialog.h"
0008 #include "mailcommon_debug.h"
0009 #include "tag/tagwidget.h"
0010 
0011 #include <KJob>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <QLineEdit>
0015 
0016 #include <QDialogButtonBox>
0017 #include <QPushButton>
0018 #include <QVBoxLayout>
0019 
0020 using namespace MailCommon;
0021 class MailCommon::AddTagDialogPrivate
0022 {
0023 public:
0024     AddTagDialogPrivate() = default;
0025 
0026     QString mLabel;
0027     QString mGid;
0028     MailCommon::TagWidget *mTagWidget = nullptr;
0029     QList<MailCommon::Tag::Ptr> mTags;
0030     Akonadi::Tag mTag;
0031     QPushButton *mOkButton = nullptr;
0032 };
0033 
0034 AddTagDialog::AddTagDialog(const QList<KActionCollection *> &actions, QWidget *parent)
0035     : QDialog(parent)
0036     , d(new MailCommon::AddTagDialogPrivate)
0037 {
0038     setModal(true);
0039     setWindowTitle(i18nc("@title:window", "Add Tag"));
0040     auto mainLayout = new QVBoxLayout(this);
0041 
0042     d->mTagWidget = new MailCommon::TagWidget(actions, this);
0043     mainLayout->addWidget(d->mTagWidget);
0044 
0045     connect(d->mTagWidget->tagNameLineEdit(), &QLineEdit::textChanged, this, &AddTagDialog::slotTagNameChanged);
0046 
0047     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0048     d->mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0049     d->mOkButton->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Return));
0050     connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTagDialog::slotSave);
0051     connect(buttonBox, &QDialogButtonBox::rejected, this, &AddTagDialog::reject);
0052     d->mOkButton->setDefault(true);
0053     d->mOkButton->setEnabled(false);
0054 
0055     mainLayout->addWidget(buttonBox);
0056 }
0057 
0058 AddTagDialog::~AddTagDialog() = default;
0059 
0060 void AddTagDialog::setTags(const QList<MailCommon::Tag::Ptr> &tags)
0061 {
0062     d->mTags = tags;
0063 }
0064 
0065 void AddTagDialog::slotTagNameChanged(const QString &text)
0066 {
0067     d->mOkButton->setEnabled(!text.trimmed().isEmpty());
0068 }
0069 
0070 void AddTagDialog::slotSave()
0071 {
0072     const QString name(d->mTagWidget->tagNameLineEdit()->text());
0073 
0074     for (const MailCommon::Tag::Ptr &tag : std::as_const(d->mTags)) {
0075         if (tag->name() == name) {
0076             KMessageBox::error(this, i18n("Tag %1 already exists", name));
0077             d->mTagWidget->tagNameLineEdit()->setFocus();
0078             d->mTagWidget->tagNameLineEdit()->selectAll();
0079             return;
0080         }
0081     }
0082 
0083     MailCommon::Tag::Ptr tag(Tag::createDefaultTag(name));
0084     d->mTagWidget->recordTagSettings(tag);
0085     const Akonadi::Tag akonadiTag = tag->saveToAkonadi();
0086     auto createJob = new Akonadi::TagCreateJob(akonadiTag, this);
0087     connect(createJob, &Akonadi::TagCreateJob::result, this, &AddTagDialog::onTagCreated);
0088 
0089     d->mLabel = name;
0090 }
0091 
0092 void AddTagDialog::onTagCreated(KJob *job)
0093 {
0094     if (job->error()) {
0095         qCWarning(MAILCOMMON_LOG) << "Failed to create tag: " << job->errorString();
0096         reject();
0097         return;
0098     }
0099     auto createJob = static_cast<Akonadi::TagCreateJob *>(job);
0100     d->mTag = createJob->tag();
0101     accept();
0102 }
0103 
0104 QString AddTagDialog::label() const
0105 {
0106     return d->mLabel;
0107 }
0108 
0109 Akonadi::Tag AddTagDialog::tag() const
0110 {
0111     return d->mTag;
0112 }
0113 
0114 #include "moc_addtagdialog.cpp"