File indexing completed on 2025-03-09 04:54:37

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "remotecontentmenu.h"
0008 #include "remote-content/remotecontentmanager.h"
0009 #include "remotecontentconfiguredialog.h"
0010 #include "remotecontentinfo.h"
0011 #include <KLocalizedString>
0012 #include <QAction>
0013 
0014 using namespace MessageViewer;
0015 RemoteContentMenu::RemoteContentMenu(QWidget *parent)
0016     : QMenu(parent)
0017     , mConfigureRemoteContentAction(new QAction(QIcon::fromTheme(QStringLiteral("settings-configure")), i18n("Configure"), this))
0018 {
0019     setTitle(i18n("Remote Content"));
0020     connect(this, &QMenu::aboutToShow, this, &RemoteContentMenu::updateMenu);
0021 }
0022 
0023 RemoteContentMenu::~RemoteContentMenu() = default;
0024 
0025 void RemoteContentMenu::slotConfigure()
0026 {
0027     MessageViewer::RemoteContentConfigureDialog dlg(this);
0028     dlg.exec();
0029 }
0030 
0031 QStringList RemoteContentMenu::urls() const
0032 {
0033     return mUrls;
0034 }
0035 
0036 void RemoteContentMenu::setUrls(const QStringList &urls)
0037 {
0038     mUrls = urls;
0039 }
0040 
0041 void RemoteContentMenu::updateMenu()
0042 {
0043     for (auto act : std::as_const(mListAction)) {
0044         removeAction(act);
0045     }
0046     const auto numberOfUrl{mUrls.count()};
0047     if (numberOfUrl > 0) {
0048         mListAction.reserve(numberOfUrl + 3);
0049         for (const QString &url : std::as_const(mUrls)) {
0050             QAction *act = addAction(i18n("Authorize %1", url));
0051             connect(act, &QAction::triggered, this, [this, url]() {
0052                 authorize(url);
0053                 mUrls.removeAll(url);
0054             });
0055             mListAction << act;
0056         }
0057         mListAction << addSeparator();
0058         QAction *act = addAction(i18n("Authorize all Urls"));
0059         connect(act, &QAction::triggered, this, [this]() {
0060             for (const QString &url : std::as_const(mUrls)) {
0061                 authorize(url);
0062             }
0063             mUrls.clear();
0064         });
0065         mListAction << act;
0066         mListAction << addSeparator();
0067     }
0068     addAction(mConfigureRemoteContentAction);
0069     connect(mConfigureRemoteContentAction, &QAction::triggered, this, &RemoteContentMenu::slotConfigure);
0070 }
0071 
0072 void RemoteContentMenu::authorize(const QString &url)
0073 {
0074     RemoteContentInfo info;
0075     info.setUrl(url);
0076     info.setStatus(RemoteContentInfo::RemoteContentInfoStatus::Authorized);
0077     RemoteContentManager::self()->addRemoteContent(info);
0078     Q_EMIT updateEmail();
0079 }
0080 
0081 void RemoteContentMenu::clearUrls()
0082 {
0083     mUrls.clear();
0084 }
0085 
0086 void RemoteContentMenu::appendUrl(const QString &url)
0087 {
0088     if (!mUrls.contains(url)) {
0089         mUrls.append(url);
0090     }
0091 }
0092 
0093 #include "moc_remotecontentmenu.cpp"