1 /*
2 This file is part of the MTP KIOD module, part of the KDE project.
3
4 SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net>
5
6 SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "mtpdevice.h"
10
11 #include <QDBusConnection>
12
13 #include <Solid/Device>
14 #include <Solid/GenericInterface>
15 #include <Solid/DeviceNotifier>
16
17 #include "mtpstorage.h"
18 #include "kiod_kmtpd_debug.h"
19
20 // D-Bus adaptors
21 #include "deviceadaptor.h"
22
23 /**
24 * Creates a Cached Device that has a predefined lifetime (default: 10000 msec)s
25 * The lifetime is reset every time the device is accessed. After it expires it
26 * will be released.
27 *
28 * @param device The LIBMTP_mtpdevice_t pointer to cache
29 * @param udi The UDI of the new device to cache
30 */
MTPDevice(const QString & dbusObjectPath,LIBMTP_mtpdevice_t * device,LIBMTP_raw_device_t * rawdevice,const QString & udi,qint32 timeout,QObject * parent)31 MTPDevice::MTPDevice(const QString &dbusObjectPath, LIBMTP_mtpdevice_t *device, LIBMTP_raw_device_t *rawdevice, const QString &udi, qint32 timeout, QObject *parent)
32 : QObject(parent),
33 m_dbusObjectName(dbusObjectPath),
34 m_timeout(timeout),
35 m_mtpdevice(device),
36 m_rawdevice(*rawdevice),
37 m_udi(udi)
38 {
39 const char *deviceName = LIBMTP_Get_Friendlyname(device);
40 const char *deviceModel = LIBMTP_Get_Modelname(device);
41
42 // prefer friendly devicename over model
43 if (!deviceName || strlen(deviceName) == 0) {
44 m_friendlyName = QString::fromUtf8(deviceModel);
45 } else {
46 m_friendlyName = QString::fromUtf8(deviceName);
47 }
48
49 qCDebug(LOG_KIOD_KMTPD) << "Created device " << m_friendlyName << " with udi=" << udi << " and timeout " << timeout;
50
51 new DeviceAdaptor(this);
52 QDBusConnection::sessionBus().registerObject(m_dbusObjectName, this);
53
54 int index = 0;
55 for (LIBMTP_devicestorage_t *storage = device->storage; storage != nullptr; storage = storage->next) {
56 m_storages.append(new MTPStorage(QStringLiteral("%1/storage%2").arg(m_dbusObjectName).arg(index++), storage, this));
57 }
58 }
59
~MTPDevice()60 MTPDevice::~MTPDevice()
61 {
62 qCDebug(LOG_KIOD_KMTPD) << "release device:" << m_friendlyName;
63 LIBMTP_Release_Device(m_mtpdevice);
64 }
65
getDevice()66 LIBMTP_mtpdevice_t *MTPDevice::getDevice()
67 {
68 return m_mtpdevice;
69 }
70
dbusObjectName() const71 QString MTPDevice::dbusObjectName() const
72 {
73 return m_dbusObjectName;
74 }
75
udi() const76 QString MTPDevice::udi() const
77 {
78 return m_udi;
79 }
80
friendlyName() const81 QString MTPDevice::friendlyName() const
82 {
83 return m_friendlyName;
84 }
85
setFriendlyName(const QString & friendlyName)86 int MTPDevice::setFriendlyName(const QString &friendlyName)
87 {
88 if (m_friendlyName == friendlyName) {
89 return 1;
90 }
91
92 const int result = LIBMTP_Set_Friendlyname(m_mtpdevice, friendlyName.toUtf8().constData());
93 if (!result) {
94 m_friendlyName = friendlyName;
95 Q_EMIT friendlyNameChanged(m_friendlyName);
96
97 }
98 return result;
99 }
100
listStorages() const101 QList<QDBusObjectPath> MTPDevice::listStorages() const
102 {
103 QList<QDBusObjectPath> list;
104 list.reserve(m_storages.count());
105 for (const MTPStorage *storage : m_storages) {
106 list.append(QDBusObjectPath(storage->dbusObjectPath()));
107 }
108 return list;
109 }
110
111 #include "moc_mtpdevice.cpp"
112