File: filesystem.h

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (241 lines) | stat: -rw-r--r-- 9,484 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
    SPDX-FileCopyrightText: 2000 Shie Erlich <krusader@users.sourceforge.net>
    SPDX-FileCopyrightText: 2000 Rafi Yanai <krusader@users.sourceforge.net>
    SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>

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

#ifndef FILESYSTEM_H
#define FILESYSTEM_H

#include "dirlisterinterface.h"

// QtCore
#include <QHash>
#include <QList>
#include <QPointer>
#include <QString>
// QtGui
#include <QDropEvent>
// QtWidgets
#include <QWidget>

#include <KIO/CopyJob>
#include <KIO/DirectorySizeJob>

#include "../JobMan/jobman.h"

class FileItem;

/**
 * An abstract filesystem. Use the implementations of this class for all file operations.
 *
 * It represents a directory and gives access to its files. All common file operations
 * are supported. Methods with absolute URL as argument can be used independently from the current
 * directory. Otherwise - if the methods argument is a file name - the operation is performed inside
 * the current directory.
 *
 * Notification signals are emitted if the directory content may have been changed.
 */
class FileSystem : public DirListerInterface
{
    Q_OBJECT
public:
    enum FS_TYPE {
        /// Virtual filesystem. Krusaders custom virt:/ protocol
        FS_VIRTUAL,
        /// Filesystem supporting all KIO protocols (file:/, ftp:/, smb:/, etc.)
        FS_DEFAULT
    };

    FileSystem();
    ~FileSystem() override;

    // DirListerInterface implementation
    inline QList<FileItem *> fileItems() const override
    {
        return _fileItems.values();
    }
    inline unsigned long numFileItems() const override
    {
        return _fileItems.count();
    }
    inline bool isRoot() const override
    {
        const QString path = _currentDirectory.path();
        return path.isEmpty() || path == "/";
    }

    /// Copy (copy, move or link) files in this filesystem.
    /// Destination is absolute URL. May implemented async.
    virtual void copyFiles(const QList<QUrl> &urls,
                           const QUrl &destination,
                           KIO::CopyJob::CopyMode mode = KIO::CopyJob::Copy,
                           bool showProgressInfo = true,
                           JobMan::StartMode startMode = JobMan::Default) = 0;
    /// Handle file dropping in this filesystem. Destination is absolute URL. May implemented async.
    virtual void dropFiles(const QUrl &destination, QDropEvent *event, QWidget *targetWidget) = 0;

    /// Copy (copy, move or link) files to the current filesystem directory or to "dir", the
    /// directory name relative to the current dir. May implemented async.
    virtual void addFiles(const QList<QUrl> &fileUrls, KIO::CopyJob::CopyMode mode, const QString &dir = "") = 0;
    /// Create a new directory in the current directory. May implemented async.
    virtual void mkDir(const QString &name) = 0;
    /// Rename file/directory in the current directory. May implemented async.
    virtual void rename(const QString &fileName, const QString &newName) = 0;

    /// Return an absolute URL for a single file/directory name in the current directory - with no
    /// trailing slash.
    virtual QUrl getUrl(const QString &name) const = 0;
    /// Return a list of URLs for multiple files/directories in the current directory.
    QList<QUrl> getUrls(const QStringList &names) const;
    /// Return true if all files can be moved to trash, else false.
    virtual bool canMoveToTrash(const QStringList &fileNames) const = 0;

    /// Return the filesystem mount point of the current directory. Empty string by default.
    virtual QString mountPoint() const
    {
        return QString();
    }
    /// Returns true if this filesystem implementation does not need to be notified about changes in the
    /// current directory. Else false.
    virtual bool hasAutoUpdate() const
    {
        return false;
    }
    /// Notify this filesystem that the filesystem info of the current directory may have changed.
    virtual void updateFilesystemInfo()
    {
    }

    /**
     * Scan all files and directories in a directory and create the file items for them. Blocking.
     *
     * @param directory if given, the lister tries to change to this directory, else the old
     * directory is refreshed
     * @return true if scan was successful, else (not implemented, scan failed or refresh job
     * was killed) false.
     */
    bool scanDir(const QUrl &directory = QUrl())
    {
        return scanOrRefresh(directory, false);
    }

    /// Change or refresh the current directory and scan it. Blocking.
    /// Returns true if directory was scanned. Returns false if failed or scan job was killed.
    bool refresh(const QUrl &directory = QUrl())
    {
        return scanOrRefresh(directory, false);
    }

    /// Returns the current directory path of this filesystem.
    inline QUrl currentDirectory() const
    {
        return _currentDirectory;
    }
    /// Return the file item for a file name in the current directory. Or 0 if not found.
    FileItem *getFileItem(const QString &name) const;
    /// The total size of all files in the current directory (only valid after scan).
    // TODO unused
    KIO::filesize_t totalSize() const;
    /// Return the filesystem type.
    inline FS_TYPE type() const
    {
        return _type;
    }
    /// Return true if the current directory is local (without recognizing mount points).
    inline bool isLocal() const
    {
        return _currentDirectory.isLocalFile();
    }
    /// Return true if the current directory is a remote (network) location.
    inline bool isRemote() const
    {
        const QString sc = _currentDirectory.scheme();
        return (sc == "fish" || sc == "ftp" || sc == "sftp" || sc == "nfs" || sc == "smb" || sc == "webdav");
    }
    /// Returns true if this filesystem is currently refreshing the current directory.
    inline bool isRefreshing() const
    {
        return _isRefreshing;
    }

    /// Delete or trash arbitrary files. Implemented async. Universal refresh not fully implemented.
    void deleteFiles(const QList<QUrl> &urls, bool moveToTrash);

    /// Return the input URL with a trailing slash if absent.
    static QUrl ensureTrailingSlash(const QUrl &url);
    /// Return the input URL without trailing slash.
    static QUrl cleanUrl(const QUrl &url)
    {
        return url.adjusted(QUrl::StripTrailingSlash);
    }
    /// Add 'file' scheme to non-empty URL without scheme
    static QUrl preferLocalUrl(const QUrl &url);

    /// Return a file item for a local file inside a directory
    static FileItem *createLocalFileItem(const QString &name, const QString &directory, bool virt = false);
    /// Return a file item for a KIO result. Returns 0 if entry is not needed
    static FileItem *createFileItemFromKIO(const KIO::UDSEntry &entry, const QUrl &directory, bool virt = false);

    /// Read a symlink with an extra precaution
    static QString readLinkSafely(const char *path);

    /// Set the parent window to be used for dialogs
    void setParentWindow(QWidget *widget)
    {
        parentWindow = widget;
    }

signals:
    /// Emitted when this filesystem is currently refreshing the filesystem directory.
    void refreshJobStarted(KIO::Job *job);
    /// Emitted when an error occurred in this filesystem during refresh.
    void error(const QString &msg);
    /// Emitted when the content of a directory was changed by this filesystem.
    void fileSystemChanged(const QUrl &directory, bool removed);
    /// Emitted when the information for the filesystem of the current directory changed.
    /// Information is either
    /// * 'metaInfo': a displayable string about the fs, empty by default, OR
    /// * 'fsType', 'total' and 'free': filesystem type, size and free space,
    ///   empty string or 0 by default
    void fileSystemInfoChanged(const QString &metaInfo, const QString &fsType, KIO::filesize_t total, KIO::filesize_t free);
    /// Emitted before a directory path is opened for reading. Used for automounting.
    void aboutToOpenDir(const QString &path);

protected:
    /// Fill the filesystem dictionary with file items, must be implemented for each filesystem.
    virtual bool refreshInternal(const QUrl &origin, bool stayInDir) = 0;

    /// Connect the result signal of a file operation job - source URLs.
    void connectJobToSources(KJob *job, const QList<QUrl> &urls);
    /// Connect the result signal of a file operation job - destination URL.
    void connectJobToDestination(KJob *job, const QUrl &destination);
    /// Returns true if showing hidden files is set in config.
    bool showHiddenFiles();
    /// Add a new file item to the internal dictionary (while refreshing).
    void addFileItem(FileItem *item);

    FS_TYPE _type; // the filesystem type.
    QUrl _currentDirectory; // the path or file the filesystem originates from.
    bool _isRefreshing; // true if filesystem is busy with refreshing
    QPointer<QWidget> parentWindow;

protected slots:
    /// Handle result after job (except when refreshing!) finished
    void slotJobResult(KJob *job, bool refresh);

private:
    typedef QHash<QString, FileItem *> FileItemDict;

    // optional TODO: add an async version of this
    bool scanOrRefresh(const QUrl &directory, bool onlyScan);

    /// Delete and clear file items.
    void clear(FileItemDict &fileItems);

    FileItemDict _fileItems; // the list of files in the current dictionary
};

#endif