File indexing completed on 2024-12-22 05:01:16

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kactionmenutransport.h"
0008 #include <MailTransport/TransportManager>
0009 #include <QMenu>
0010 
0011 KActionMenuTransport::KActionMenuTransport(QObject *parent)
0012     : KActionMenu(parent)
0013 {
0014     setPopupMode(QToolButton::DelayedPopup);
0015     connect(MailTransport::TransportManager::self(), &MailTransport::TransportManager::transportsChanged, this, &KActionMenuTransport::updateTransportMenu);
0016     connect(menu(), &QMenu::aboutToShow, this, &KActionMenuTransport::slotCheckTransportMenu);
0017     connect(menu(), &QMenu::triggered, this, &KActionMenuTransport::slotSelectTransport);
0018 }
0019 
0020 KActionMenuTransport::~KActionMenuTransport() = default;
0021 
0022 void KActionMenuTransport::slotCheckTransportMenu()
0023 {
0024     if (!mInitialized) {
0025         mInitialized = true;
0026         updateTransportMenu();
0027     }
0028 }
0029 
0030 void KActionMenuTransport::updateTransportMenu()
0031 {
0032     if (mInitialized) {
0033         menu()->clear();
0034         const QList<MailTransport::Transport *> transports = MailTransport::TransportManager::self()->transports();
0035         QMap<QString, int> menuTransportLst;
0036 
0037         for (MailTransport::Transport *transport : transports) {
0038             const QString name = transport->name().replace(QLatin1Char('&'), QStringLiteral("&&"));
0039             menuTransportLst.insert(name, transport->id());
0040         }
0041         QMapIterator<QString, int> i(menuTransportLst);
0042         while (i.hasNext()) {
0043             i.next();
0044             auto action = new QAction(i.key(), this);
0045             action->setData(i.value());
0046             menu()->addAction(action);
0047         }
0048     }
0049 }
0050 
0051 void KActionMenuTransport::slotSelectTransport(QAction *act)
0052 {
0053     const QList<int> availTransports = MailTransport::TransportManager::self()->transportIds();
0054     const int transportId = act->data().toInt();
0055     if (availTransports.contains(transportId)) {
0056         MailTransport::Transport *transport = MailTransport::TransportManager::self()->transportById(transportId);
0057         Q_EMIT transportSelected(transport);
0058     }
0059 }
0060 
0061 #include "moc_kactionmenutransport.cpp"