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 "kactionmenuaccount.h"
0008 #include "kmail_debug.h"
0009 #include <Akonadi/AgentInstance>
0010 #include <Akonadi/AgentManager>
0011 #include <MailCommon/MailUtil>
0012 #include <QMenu>
0013 
0014 KActionMenuAccount::KActionMenuAccount(QObject *parent)
0015     : KActionMenu(parent)
0016 {
0017     setPopupMode(QToolButton::DelayedPopup);
0018     connect(menu(), &QMenu::aboutToShow, this, &KActionMenuAccount::slotCheckTransportMenu);
0019     connect(menu(), &QMenu::triggered, this, &KActionMenuAccount::slotSelectAccount);
0020     connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceNameChanged, this, &KActionMenuAccount::updateAccountMenu);
0021     connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceRemoved, this, &KActionMenuAccount::updateAccountMenu);
0022     connect(Akonadi::AgentManager::self(), &Akonadi::AgentManager::instanceAdded, this, &KActionMenuAccount::updateAccountMenu);
0023 }
0024 
0025 KActionMenuAccount::~KActionMenuAccount() = default;
0026 
0027 void KActionMenuAccount::setAccountOrder(const QStringList &identifier)
0028 {
0029     if (mOrderIdentifier != identifier) {
0030         mOrderIdentifier = identifier;
0031         mInitialized = false;
0032     }
0033 }
0034 
0035 void KActionMenuAccount::slotSelectAccount(QAction *act)
0036 {
0037     if (!act) {
0038         return;
0039     }
0040 
0041     Akonadi::AgentInstance agent = Akonadi::AgentManager::self()->instance(act->data().toString());
0042     if (agent.isValid()) {
0043         if (!agent.isOnline()) {
0044             agent.setIsOnline(true);
0045         }
0046         agent.synchronize();
0047     } else {
0048         qCDebug(KMAIL_LOG) << "account with identifier" << act->data().toString() << "not found";
0049     }
0050 }
0051 
0052 void KActionMenuAccount::slotCheckTransportMenu()
0053 {
0054     if (!mInitialized) {
0055         mInitialized = true;
0056         updateAccountMenu();
0057     }
0058 }
0059 
0060 bool orderAgentIdentifier(const AgentIdentifier &lhs, const AgentIdentifier &rhs)
0061 {
0062     if ((lhs.mIndex == -1) && (rhs.mIndex == -1)) {
0063         return lhs.mName < rhs.mName;
0064     }
0065     if (lhs.mIndex != rhs.mIndex) {
0066         return lhs.mIndex < rhs.mIndex;
0067     }
0068     // we can't have same index but fallback
0069     return true;
0070 }
0071 
0072 void KActionMenuAccount::updateAccountMenu()
0073 {
0074     if (mInitialized) {
0075         menu()->clear();
0076         const Akonadi::AgentInstance::List lst = MailCommon::Util::agentInstances();
0077         QList<AgentIdentifier> agentIdentifierList;
0078         agentIdentifierList.reserve(lst.count());
0079 
0080         for (const Akonadi::AgentInstance &type : lst) {
0081             // Explicitly make a copy, as we're not changing values of the list but only
0082             // the local copy which is passed to action.
0083             const QString identifierName = type.identifier();
0084             const int index = mOrderIdentifier.indexOf(identifierName);
0085             const AgentIdentifier id(identifierName, QString(type.name()).replace(QLatin1Char('&'), QStringLiteral("&&")), index);
0086             agentIdentifierList << id;
0087         }
0088         std::sort(agentIdentifierList.begin(), agentIdentifierList.end(), orderAgentIdentifier);
0089         const int numberOfAccount(agentIdentifierList.size());
0090         for (int i = 0; i < numberOfAccount; ++i) {
0091             const AgentIdentifier id = agentIdentifierList.at(i);
0092             QAction *action = menu()->addAction(id.mName);
0093             action->setData(id.mIdentifier);
0094         }
0095     }
0096 }
0097 
0098 #include "moc_kactionmenuaccount.cpp"