File indexing completed on 2024-05-12 04:59:44
0001 /* 0002 This file is part of the MTP KIOD module, part of the KDE project. 0003 0004 SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net> 0005 SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "mtpdevice.h" 0011 0012 #include <QDBusConnection> 0013 0014 #include <Solid/Device> 0015 #include <Solid/DeviceNotifier> 0016 #include <Solid/GenericInterface> 0017 0018 #include "kiod_kmtpd_debug.h" 0019 #include "mtpstorage.h" 0020 0021 // D-Bus adaptors 0022 #include "deviceadaptor.h" 0023 0024 /** 0025 * Creates a Cached Device that has a predefined lifetime (default: 10000 msec)s 0026 * The lifetime is reset every time the device is accessed. After it expires it 0027 * will be released. 0028 * 0029 * @param device The LIBMTP_mtpdevice_t pointer to cache 0030 * @param udi The UDI of the new device to cache 0031 */ 0032 MTPDevice::MTPDevice(const QString &dbusObjectPath, LIBMTP_mtpdevice_t *device, LIBMTP_raw_device_t *rawdevice, const QString &udi, QObject *parent) 0033 : QObject(parent) 0034 , m_dbusObjectName(dbusObjectPath) 0035 , m_mtpdevice(device) 0036 , m_rawdevice(*rawdevice) 0037 , m_udi(udi) 0038 , m_devicesUpdated(true) 0039 { 0040 const char *deviceName = LIBMTP_Get_Friendlyname(device); 0041 const char *deviceModel = LIBMTP_Get_Modelname(device); 0042 0043 // prefer friendly devicename over model 0044 if (!deviceName || strlen(deviceName) == 0) { 0045 m_friendlyName = QString::fromUtf8(deviceModel); 0046 } else { 0047 m_friendlyName = QString::fromUtf8(deviceName); 0048 } 0049 0050 qCDebug(LOG_KIOD_KMTPD) << "Created device " << m_friendlyName << " with udi=" << udi; 0051 0052 new DeviceAdaptor(this); 0053 QDBusConnection::sessionBus().registerObject(m_dbusObjectName, this); 0054 0055 int index = 0; 0056 for (LIBMTP_devicestorage_t *storage = device->storage; storage != nullptr; storage = storage->next) { 0057 m_storages.append(new MTPStorage(QStringLiteral("%1/storage%2").arg(m_dbusObjectName).arg(index++), storage, this)); 0058 } 0059 } 0060 0061 MTPDevice::~MTPDevice() 0062 { 0063 qCDebug(LOG_KIOD_KMTPD) << "release device:" << m_friendlyName; 0064 LIBMTP_Release_Device(m_mtpdevice); 0065 } 0066 0067 void MTPDevice::setDevicesUpdatedStatus(bool value) 0068 { 0069 m_devicesUpdated = value; 0070 } 0071 0072 bool MTPDevice::devicesUpdated() const 0073 { 0074 return m_devicesUpdated; 0075 } 0076 0077 LIBMTP_mtpdevice_t *MTPDevice::getDevice() 0078 { 0079 return m_mtpdevice; 0080 } 0081 0082 QString MTPDevice::dbusObjectName() const 0083 { 0084 return m_dbusObjectName; 0085 } 0086 0087 QString MTPDevice::udi() const 0088 { 0089 return m_udi; 0090 } 0091 0092 QString MTPDevice::friendlyName() const 0093 { 0094 return m_friendlyName; 0095 } 0096 0097 int MTPDevice::setFriendlyName(const QString &friendlyName) 0098 { 0099 if (m_friendlyName == friendlyName) { 0100 return 1; 0101 } 0102 0103 const int result = LIBMTP_Set_Friendlyname(m_mtpdevice, friendlyName.toUtf8().constData()); 0104 if (!result) { 0105 m_friendlyName = friendlyName; 0106 Q_EMIT friendlyNameChanged(m_friendlyName); 0107 } 0108 return result; 0109 } 0110 0111 QList<QDBusObjectPath> MTPDevice::listStorages() 0112 { 0113 QList<QDBusObjectPath> list; 0114 list.reserve(m_storages.count()); 0115 for (const MTPStorage *storage : m_storages) { 0116 list.append(QDBusObjectPath(storage->dbusObjectPath())); 0117 } 0118 0119 // New storages have been collected. No new changes anymore. 0120 setDevicesUpdatedStatus(false); 0121 0122 return list; 0123 } 0124 0125 QUrl MTPDevice::url() const 0126 { 0127 QUrl friendlyUrl; 0128 friendlyUrl.setScheme(QStringLiteral("mtp")); 0129 friendlyUrl.setPath(QLatin1Char('/') + friendlyName()); 0130 return friendlyUrl; 0131 } 0132 0133 #include "moc_mtpdevice.cpp"