File indexing completed on 2024-11-24 04:53:04

0001 /* Copyright (C) 2012 Mildred <mildred-pub@mildred.fr>
0002    Copyright (C) 2015 Erik Quaeghebeur <trojita@equaeghe.nospammail.net>
0003    Copyright (C) 2006 - 2015 Jan Kundrát <jkt@kde.org>
0004 
0005    This file is part of the Trojita Qt IMAP e-mail client,
0006    http://trojita.flaska.net/
0007 
0008    This program is free software; you can redistribute it and/or
0009    modify it under the terms of the GNU General Public License as
0010    published by the Free Software Foundation; either version 2 of
0011    the License or (at your option) version 3 or any later version
0012    accepted by the membership of KDE e.V. (or its successor approved
0013    by the membership of KDE e.V.), which shall act as a proxy
0014    defined in Section 14 of version 3 of the license.
0015 
0016    This program is distributed in the hope that it will be useful,
0017    but WITHOUT ANY WARRANTY; without even the implied warranty of
0018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019    GNU General Public License for more details.
0020 
0021    You should have received a copy of the GNU General Public License
0022    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0023 */
0024 
0025 #include <QHBoxLayout>
0026 #include <QInputDialog>
0027 #include <QLabel>
0028 #include <QMessageBox>
0029 #include <QPushButton>
0030 #include <QStringList>
0031 
0032 #include "TagListWidget.h"
0033 #include "FlowLayout.h"
0034 #include "TagAddDialog.h"
0035 #include "TagWidget.h"
0036 #include "Gui/Util.h"
0037 #include "Imap/Model/SpecialFlagNames.h"
0038 
0039 namespace Gui
0040 {
0041 
0042 TagListWidget::TagListWidget(QWidget *parent, Imap::Mailbox::FavoriteTagsModel *m_favoriteTags) :
0043     QWidget(parent)
0044     , m_favoriteTags(m_favoriteTags)
0045 {
0046     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
0047     parentLayout = new FlowLayout(this, 0);
0048     setLayout(parentLayout);
0049 
0050     addButton = TagWidget::addingWidget();
0051     connect(addButton, &TagWidget::addingClicked, this, &TagListWidget::newTagsRequested);
0052 
0053     unsupportedReservedKeywords.insert(Imap::Mailbox::FlagNames::mdnsent.toLower());
0054     unsupportedReservedKeywords.insert(Imap::Mailbox::FlagNames::submitted.toLower());
0055     unsupportedReservedKeywords.insert(Imap::Mailbox::FlagNames::submitpending.toLower());
0056 }
0057 
0058 void TagListWidget::setTagList(QStringList list)
0059 {
0060     empty();
0061     parentLayout->removeWidget(addButton);
0062 
0063     Q_FOREACH(const QString &tagName, list) {
0064         QString tagNameLowerCase = tagName.toLower();
0065         if (Imap::Mailbox::FlagNames::toCanonical.contains(tagNameLowerCase)) {
0066             if (unsupportedReservedKeywords.contains(tagNameLowerCase)) {
0067                 TagWidget *lbl = TagWidget::systemFlag(tagName);
0068                 parentLayout->addWidget(lbl);
0069                 children << lbl;
0070             } else {
0071                 continue;
0072             }
0073         } else {
0074             TagWidget *lbl = TagWidget::userKeyword(tagName, m_favoriteTags);
0075             parentLayout->addWidget(lbl);
0076             connect(lbl, &TagWidget::removeClicked, this, &TagListWidget::tagRemoved);
0077             children << lbl;
0078         }
0079     }
0080 
0081     parentLayout->addWidget(addButton);
0082 }
0083 
0084 void TagListWidget::empty()
0085 {
0086     qDeleteAll(children.begin(), children.end());
0087     children.clear();
0088 }
0089 
0090 void TagListWidget::newTagsRequested()
0091 {
0092     QStringList tagList = TagAddDialog::getTags(this, m_favoriteTags);
0093 
0094     // Check whether any text has been entered
0095     if (tagList.isEmpty()) {
0096         return;
0097     }
0098 
0099     // Check whether reserved keywords have been entered
0100     QStringList reservedTagsList = QStringList();
0101     for (QStringList::const_iterator it = tagList.constBegin(); it != tagList.constEnd(); ++it) {
0102         if (Imap::Mailbox::FlagNames::toCanonical.contains(it->toLower())) {
0103             reservedTagsList << *it;
0104         }
0105     }
0106     if (!reservedTagsList.isEmpty()) {
0107         Gui::Util::messageBoxWarning(this, tr("Disallowed tag value"),
0108                                      tr("No tags were set because the following given tag(s) are reserved "
0109                                         "and have been disallowed from being set in this way: %1.")
0110                                      .arg(reservedTagsList.join(QStringLiteral(", "))));
0111         return;
0112     }
0113 
0114     emit tagAdded(tagList.join(QLatin1Char(' ')));
0115 }
0116 
0117 }