Warning, file /plasma/plasma-mobile/kcms/cellularnetwork/sim.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // SPDX-FileCopyrightText: 2021 Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "sim.h"
0005 
0006 #include <KLocalizedString>
0007 
0008 #include <QDBusReply>
0009 
0010 Sim::Sim(QObject *parent, Modem *modem, ModemManager::Sim::Ptr mmSim, ModemManager::Modem::Ptr mmModem, ModemManager::Modem3gpp::Ptr mmModem3gpp)
0011     : QObject{parent}
0012     , m_modem{modem}
0013     , m_mmSim{mmSim}
0014     , m_mmModem{mmModem}
0015     , m_mmModem3gpp{mmModem3gpp}
0016 {
0017     if (m_mmSim) {
0018         connect(m_mmSim.data(), &ModemManager::Sim::imsiChanged, this, [this]() -> void {
0019             Q_EMIT imsiChanged();
0020         });
0021         connect(m_mmSim.data(), &ModemManager::Sim::operatorIdentifierChanged, this, [this]() -> void {
0022             Q_EMIT operatorIdentifierChanged();
0023         });
0024         connect(m_mmSim.data(), &ModemManager::Sim::operatorNameChanged, this, [this]() -> void {
0025             Q_EMIT operatorNameChanged();
0026         });
0027         connect(m_mmSim.data(), &ModemManager::Sim::simIdentifierChanged, this, [this]() -> void {
0028             Q_EMIT simIdentifierChanged();
0029         });
0030     }
0031 
0032     if (m_mmModem) {
0033         connect(m_mmModem.data(), &ModemManager::Modem::unlockRequiredChanged, this, [this]() -> void {
0034             Q_EMIT lockedChanged();
0035             Q_EMIT lockedReasonChanged();
0036         });
0037     }
0038 
0039     if (m_mmModem3gpp) {
0040         connect(m_mmModem3gpp.data(), &ModemManager::Modem3gpp::enabledFacilityLocksChanged, this, [this]() -> void {
0041             Q_EMIT pinEnabledChanged();
0042         });
0043     }
0044 }
0045 
0046 bool Sim::enabled()
0047 {
0048     return uni() != QStringLiteral("/");
0049 }
0050 
0051 bool Sim::pinEnabled()
0052 {
0053     return m_mmModem3gpp && (m_mmModem3gpp->enabledFacilityLocks() & MM_MODEM_3GPP_FACILITY_SIM);
0054 }
0055 
0056 int Sim::unlockRetriesLeft()
0057 {
0058     return m_mmModem && m_mmModem->unlockRetries()[MM_MODEM_LOCK_SIM_PIN];
0059 }
0060 
0061 bool Sim::locked()
0062 {
0063     return m_mmModem && m_mmModem->unlockRequired() == MM_MODEM_LOCK_SIM_PIN;
0064 }
0065 
0066 QString Sim::lockedReason()
0067 {
0068     if (!m_mmModem) {
0069         return {};
0070     }
0071 
0072     switch (m_mmModem->unlockRequired()) {
0073     case MM_MODEM_LOCK_UNKNOWN:
0074         return i18n("Lock reason unknown.");
0075     case MM_MODEM_LOCK_NONE:
0076         return i18n("Modem is unlocked.");
0077     case MM_MODEM_LOCK_SIM_PIN:
0078         return i18n("SIM requires the PIN code.");
0079     case MM_MODEM_LOCK_SIM_PIN2:
0080         return i18n("SIM requires the PIN2 code.");
0081     case MM_MODEM_LOCK_SIM_PUK:
0082         return i18n("SIM requires the PUK code.");
0083     case MM_MODEM_LOCK_SIM_PUK2:
0084         return i18n("SIM requires the PUK2 code.");
0085     case MM_MODEM_LOCK_PH_SP_PIN:
0086         return i18n("Modem requires the service provider PIN code.");
0087     case MM_MODEM_LOCK_PH_SP_PUK:
0088         return i18n("Modem requires the service provider PUK code.");
0089     case MM_MODEM_LOCK_PH_NET_PIN:
0090         return i18n("Modem requires the network PIN code.");
0091     case MM_MODEM_LOCK_PH_NET_PUK:
0092         return i18n("Modem requires the network PUK code.");
0093     case MM_MODEM_LOCK_PH_SIM_PIN:
0094         return i18n("Modem requires the PIN code.");
0095     case MM_MODEM_LOCK_PH_CORP_PIN:
0096         return i18n("Modem requires the corporate PIN code.");
0097     case MM_MODEM_LOCK_PH_CORP_PUK:
0098         return i18n("Modem requires the corporate PUK code.");
0099     case MM_MODEM_LOCK_PH_FSIM_PIN:
0100         return i18n("Modem requires the PH-FSIM PIN code.");
0101     case MM_MODEM_LOCK_PH_FSIM_PUK:
0102         return i18n("Modem requires the PH-FSIM PUK code.");
0103     case MM_MODEM_LOCK_PH_NETSUB_PIN:
0104         return i18n("Modem requires the network subset PIN code.");
0105     case MM_MODEM_LOCK_PH_NETSUB_PUK:
0106         return i18n("Modem requires the network subset PUK code.");
0107     }
0108     return {};
0109 }
0110 
0111 QString Sim::imsi()
0112 {
0113     return m_mmSim ? m_mmSim->imsi() : QString{};
0114 }
0115 
0116 QString Sim::eid()
0117 {
0118     return {}; // TODO add in mm-qt
0119 }
0120 
0121 QString Sim::operatorIdentifier()
0122 {
0123     return m_mmSim ? m_mmSim->operatorIdentifier() : QString{};
0124 }
0125 
0126 QString Sim::operatorName()
0127 {
0128     return m_mmSim ? m_mmSim->operatorName() : QString{};
0129 }
0130 
0131 QString Sim::simIdentifier()
0132 {
0133     return m_mmSim ? m_mmSim->simIdentifier() : QString{};
0134 }
0135 
0136 QStringList Sim::emergencyNumbers()
0137 {
0138     return {}; // TODO add in mm-qt
0139 }
0140 
0141 QString Sim::uni()
0142 {
0143     return m_mmSim ? m_mmSim->uni() : QString{};
0144 }
0145 
0146 QString Sim::displayId()
0147 {
0148     // in the form /org/freedesktop/ModemManager1/Sim/0
0149     QStringList uniSplit = uni().split(QStringLiteral("/"));
0150     return (uniSplit.count() == 0 || uni() == "/") ? i18n("(empty)") : QString(uniSplit[uniSplit.size() - 1]);
0151 }
0152 
0153 Modem *Sim::modem()
0154 {
0155     return m_modem;
0156 }
0157 
0158 QCoro::Task<void> Sim::togglePinEnabled(const QString &pin)
0159 {
0160     bool isPinEnabled = pinEnabled();
0161     QDBusReply<void> reply = co_await m_mmSim->enablePin(pin, !isPinEnabled);
0162     if (!reply.isValid()) {
0163         qWarning() << QStringLiteral("Error toggling SIM lock to") << isPinEnabled << QStringLiteral(":") << reply.error().message();
0164         CellularNetworkSettings::instance()->addMessage(InlineMessage::Error, i18n("Error toggling SIM lock: %1", reply.error().message()));
0165     }
0166 }
0167 
0168 QCoro::Task<void> Sim::changePin(const QString &oldPin, const QString &newPin)
0169 {
0170     QDBusReply<void> reply = co_await m_mmSim->changePin(oldPin, newPin);
0171     if (!reply.isValid()) {
0172         qWarning() << QStringLiteral("Error changing the PIN:") << reply.error().message();
0173         CellularNetworkSettings::instance()->addMessage(InlineMessage::Error, i18n("Error changing the PIN: %1", reply.error().message()));
0174     }
0175 }
0176 
0177 QCoro::Task<void> Sim::sendPin(const QString &pin)
0178 {
0179     if (!m_mmModem || !m_mmSim || m_mmModem->unlockRequired() == MM_MODEM_LOCK_NONE) {
0180         co_return;
0181     }
0182 
0183     QDBusReply<void> reply = co_await m_mmSim->sendPin(pin);
0184     if (!reply.isValid()) {
0185         qWarning() << QStringLiteral("Error sending the PIN:") << reply.error().message();
0186         CellularNetworkSettings::instance()->addMessage(InlineMessage::Error, i18n("Error sending the PIN: %1", reply.error().message()));
0187     }
0188 }
0189 
0190 QCoro::Task<void> Sim::sendPuk(const QString &pin, const QString &puk)
0191 {
0192     if (!m_mmModem || !m_mmSim || m_mmModem->unlockRequired() != MM_MODEM_LOCK_NONE) {
0193         co_return;
0194     }
0195 
0196     QDBusReply<void> reply = co_await m_mmSim->sendPuk(pin, puk);
0197     if (!reply.isValid()) {
0198         qWarning() << QStringLiteral("Error sending the PUK:") << reply.error().message();
0199         CellularNetworkSettings::instance()->addMessage(InlineMessage::Error, i18n("Error sending the PUK: %1", reply.error().message()));
0200     }
0201 }