File: mtpstorage.h

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 (144 lines) | stat: -rw-r--r-- 4,330 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
134
135
136
137
138
139
140
141
142
143
144
/*
    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: 2023 Harald Sitter <sitter@kde.org>

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

#ifndef MTPSTORAGE_H
#define MTPSTORAGE_H

#include <optional>

#include <QDBusContext>
#include <QObject>

#include <kmtpfile.h>
#include <libmtp.h>

class MTPDevice;

/**
 * @brief This D-Bus interface is used to access a single MTP storage.
 *
 * This includes storage management like file-, folder- and object-access.
 *
 * As a performance optimization to reduce hardware interaction,
 * a time based cache for file ids, mapping their path to their ID is used.
 */
class MTPStorage : public QObject, protected QDBusContext
{
    Q_OBJECT
    Q_PROPERTY(QString description READ description)
    Q_PROPERTY(quint64 maxCapacity READ maxCapacity)
    Q_PROPERTY(quint64 freeSpaceInBytes READ freeSpaceInBytes)

public:
    explicit MTPStorage(const QString &dbusObjectPath, const LIBMTP_devicestorage_t *mtpStorage, MTPDevice *parent);

    QString dbusObjectPath() const;

    // D-Bus properties
    QString description() const;
    quint64 maxCapacity() const;
    quint64 freeSpaceInBytes();

private:
    void setStorageProperties(const LIBMTP_devicestorage_t *storage);
    void updateStorageInfo();

    LIBMTP_mtpdevice_t *getDevice() const;

    /**
     * @brief Get the correct file/folder from the device.
     *
     * @param path
     * @return
     */
    KMTPFile getFileFromPath(const QString &path);

    /**
     * @brief Get all children files/folders of @a parentId and cache them.
     *
     * @param path      parent path, used for caching
     * @param parentId
     * @return
     */
    KMTPFileList getFilesAndFoldersCached(const QString &path, quint32 parentId);

    /**
     * @brief Find a given filename in the parentId's children
     *
     * @param fileNeedle
     * @param parentPath
     * @param parentId
     * @return std::optional<KMTPFile>
     */
    std::optional<KMTPFile> findEntry(const QString &fileNeedle, const QString &parentPath, quint32 parentId);

    /**
     * @brief Returns the ID of the item at the given path, else 0.
     *
     * Automatically discards old items.
     *
     * @param path The Path to query the cache for
     * @return The ID of the Item if it exists, else 0
     */
    std::optional<quint32> queryPath(const QString &path, int timeToLive = 60);

    /**
     * @brief Adds a Path to the Cache with the given id and ttl.
     *
     * @param path The path of the file/folder
     * @param id The file ID on the storage
     * @param timeToLive The time in seconds the entry should be valid
     */
    void addPath(const QString &path, quint32 id, int timeToLive = 60);

    /**
     * @brief Remove the given path from the cache, i.e. if it got deleted
     *
     * @param path The path that should be removed
     */
    void removePath(const QString &path);

    const QString m_dbusObjectPath;

    // LIBMTP_devicestorage_t properties
    quint32 m_id = 0; /**< Unique ID for this storage */
    quint64 m_maxCapacity = 0; /**< Maximum capability */
    quint64 m_freeSpaceInBytes = 0; /**< Free space in bytes */
    QString m_description; /**< A brief description of this storage */

    QHash<QString, QPair<QDateTime, uint32_t>> m_cache;

public Q_SLOTS:
    // D-Bus methods

    // file management
    KMTPFileList getFilesAndFolders(const QString &path, int &result);
    QDBusObjectPath getFilesAndFolders2(const QString &path);
    KMTPFile getFileMetadata(const QString &path);

    int getFileToHandler(const QString &path);
    int getFileToFileDescriptor(const QDBusUnixFileDescriptor &descriptor, const QString &sourcePath);

    int sendFileFromFileDescriptor(const QDBusUnixFileDescriptor &descriptor, const QString &destinationPath);
    int setFileName(const QString &path, const QString &newName);

    // folder management
    quint32 createFolder(const QString &path);

    // object management
    int deleteObject(const QString &path);

Q_SIGNALS:
    // D-Bus signals
    void dataReady(const QByteArray &data);
    void copyProgress(qulonglong transferredBytes, qulonglong totalBytes);
    void copyFinished(int result);
};

#endif // MTPSTORAGE_H