File indexing completed on 2024-05-12 04:59:45

0001 /*
0002     This file is part of the KMTP framework, part of the KDE project.
0003 
0004     SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "kmtpdinterface.h"
0010 #include "kmtpdeviceinterface.h"
0011 
0012 KMTPDInterface::KMTPDInterface(QObject *parent)
0013     : QObject(parent)
0014 {
0015     // connect to the KDE MTP daemon over D-Bus
0016     m_dbusInterface = new org::kde::kmtp::Daemon(QStringLiteral("org.kde.kmtpd5"), QStringLiteral("/modules/kmtpd"), QDBusConnection::sessionBus());
0017 
0018     updateDevices();
0019     // sadly this isn't usable for device removal because we don't receive it then
0020     // connect(m_dbusInterface, &org::kde::kmtp::Daemon::devicesChanged, this, &KMTPDInterface::updateDevices);
0021 }
0022 
0023 bool KMTPDInterface::isValid() const
0024 {
0025     return m_dbusInterface->isValid();
0026 }
0027 
0028 KMTPDeviceInterface *KMTPDInterface::deviceFromName(const QString &friendlyName)
0029 {
0030     updateDevices();
0031     auto deviceIt = std::find_if(m_devices.constBegin(), m_devices.constEnd(), [friendlyName](const KMTPDeviceInterface *device) {
0032         return device->friendlyName() == friendlyName;
0033     });
0034 
0035     return deviceIt == m_devices.constEnd() ? nullptr : *deviceIt;
0036 }
0037 
0038 KMTPDeviceInterface *KMTPDInterface::deviceFromUdi(const QString &udi)
0039 {
0040     updateDevices();
0041     auto deviceIt = std::find_if(m_devices.constBegin(), m_devices.constEnd(), [udi](const KMTPDeviceInterface *device) {
0042         return device->udi() == udi;
0043     });
0044 
0045     return deviceIt == m_devices.constEnd() ? nullptr : *deviceIt;
0046 }
0047 
0048 QVector<KMTPDeviceInterface *> KMTPDInterface::devices()
0049 {
0050     // have to always update because we don't receive signal for device removal
0051     updateDevices();
0052     return m_devices;
0053 }
0054 
0055 QString KMTPDInterface::version() const
0056 {
0057     return m_dbusInterface->version();
0058 }
0059 
0060 void KMTPDInterface::updateDevices()
0061 {
0062     qDeleteAll(m_devices);
0063     m_devices.clear();
0064     const auto deviceNames = m_dbusInterface->listDevices().value();
0065     for (const QDBusObjectPath &deviceName : deviceNames) {
0066         m_devices.append(new KMTPDeviceInterface(deviceName.path(), this));
0067     }
0068 }
0069 
0070 QList<QDBusObjectPath> KMTPDInterface::listDevices()
0071 {
0072     return m_dbusInterface->listDevices();
0073 }
0074 
0075 #include "moc_kmtpdinterface.cpp"