File: mtpdevice.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (133 lines) | stat: -rw-r--r-- 3,451 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
    This file is part of the MTP KIOD module, part of the KDE project.

    SPDX-FileCopyrightText: 2018 Andreas Krutzler <andreas.krutzler@gmx.net>
    SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "mtpdevice.h"

#include <QDBusConnection>

#include <Solid/Device>
#include <Solid/DeviceNotifier>
#include <Solid/GenericInterface>

#include "kiod_kmtpd_debug.h"
#include "mtpstorage.h"

// D-Bus adaptors
#include "deviceadaptor.h"

/**
 * Creates a Cached Device that has a predefined lifetime (default: 10000 msec)s
 * The lifetime is reset every time the device is accessed. After it expires it
 * will be released.
 *
 * @param device The LIBMTP_mtpdevice_t pointer to cache
 * @param udi The UDI of the new device to cache
 */
MTPDevice::MTPDevice(const QString &dbusObjectPath, LIBMTP_mtpdevice_t *device, LIBMTP_raw_device_t *rawdevice, const QString &udi, QObject *parent)
    : QObject(parent)
    , m_dbusObjectName(dbusObjectPath)
    , m_mtpdevice(device)
    , m_rawdevice(*rawdevice)
    , m_udi(udi)
    , m_devicesUpdated(true)
{
    const char *deviceName = LIBMTP_Get_Friendlyname(device);
    const char *deviceModel = LIBMTP_Get_Modelname(device);

    // prefer friendly devicename over model
    if (!deviceName || strlen(deviceName) == 0) {
        m_friendlyName = QString::fromUtf8(deviceModel);
    } else {
        m_friendlyName = QString::fromUtf8(deviceName);
    }

    qCDebug(LOG_KIOD_KMTPD) << "Created device " << m_friendlyName << "  with udi=" << udi;

    new DeviceAdaptor(this);
    QDBusConnection::sessionBus().registerObject(m_dbusObjectName, this);

    int index = 0;
    for (LIBMTP_devicestorage_t *storage = device->storage; storage != nullptr; storage = storage->next) {
        m_storages.append(new MTPStorage(QStringLiteral("%1/storage%2").arg(m_dbusObjectName).arg(index++), storage, this));
    }
}

MTPDevice::~MTPDevice()
{
    qCDebug(LOG_KIOD_KMTPD) << "release device:" << m_friendlyName;
    LIBMTP_Release_Device(m_mtpdevice);
}

void MTPDevice::setDevicesUpdatedStatus(bool value)
{
    m_devicesUpdated = value;
}

bool MTPDevice::devicesUpdated() const
{
    return m_devicesUpdated;
}

LIBMTP_mtpdevice_t *MTPDevice::getDevice()
{
    return m_mtpdevice;
}

QString MTPDevice::dbusObjectName() const
{
    return m_dbusObjectName;
}

QString MTPDevice::udi() const
{
    return m_udi;
}

QString MTPDevice::friendlyName() const
{
    return m_friendlyName;
}

int MTPDevice::setFriendlyName(const QString &friendlyName)
{
    if (m_friendlyName == friendlyName) {
        return 1;
    }

    const int result = LIBMTP_Set_Friendlyname(m_mtpdevice, friendlyName.toUtf8().constData());
    if (!result) {
        m_friendlyName = friendlyName;
        Q_EMIT friendlyNameChanged(m_friendlyName);
    }
    return result;
}

QList<QDBusObjectPath> MTPDevice::listStorages()
{
    QList<QDBusObjectPath> list;
    list.reserve(m_storages.count());
    for (const MTPStorage *storage : m_storages) {
        list.append(QDBusObjectPath(storage->dbusObjectPath()));
    }

    // New storages have been collected. No new changes anymore.
    setDevicesUpdatedStatus(false);

    return list;
}

QUrl MTPDevice::url() const
{
    QUrl friendlyUrl;
    friendlyUrl.setScheme(QStringLiteral("mtp"));
    friendlyUrl.setPath(QLatin1Char('/') + friendlyName());
    return friendlyUrl;
}

#include "moc_mtpdevice.cpp"