File indexing completed on 2024-05-05 17:56:57

0001 /*
0002     SPDX-FileCopyrightText: 2003 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2003 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef VIRTUALFILESYSTEM_H
0010 #define VIRTUALFILESYSTEM_H
0011 
0012 // QtCore
0013 #include <QHash>
0014 
0015 #include <KConfigCore/KConfig>
0016 
0017 #include "filesystem.h"
0018 
0019 /**
0020  * Custom virtual filesystem implementation: It holds arbitrary lists of files which are only
0021  * virtual references to real files. The filename of a virtual file is the full path of the real
0022  * file.
0023  *
0024  * Only two filesystem levels are supported: On root level only directories can be created; these
0025  * virtual root directories can contain a set of virtual files and directories. Entering a directory
0026  * on the sublevel is out of scope and the real directory will be opened.
0027  *
0028  * The filesystem content is saved in a separate config file and preserved between application runs.
0029  *
0030  * Used at least by bookmarks, locate, search and synchronizer dialog.
0031  */
0032 class VirtualFileSystem : public FileSystem
0033 {
0034     Q_OBJECT
0035 public:
0036     VirtualFileSystem();
0037 
0038     /// Create virtual files in this filesystem. Copy mode and showProgressInfo are ignored.
0039     void copyFiles(const QList<QUrl> &urls,
0040                    const QUrl &destination,
0041                    KIO::CopyJob::CopyMode mode = KIO::CopyJob::Copy,
0042                    bool showProgressInfo = false,
0043                    JobMan::StartMode startMode = JobMan::Start) override;
0044     /// Handle file dropping in this filesystem: Always creates virtual files.
0045     void dropFiles(const QUrl &destination, QDropEvent *event) override;
0046 
0047     /// Add virtual files to the current directory.
0048     void addFiles(const QList<QUrl> &fileUrls, KIO::CopyJob::CopyMode mode = KIO::CopyJob::Copy, const QString &dir = "") override;
0049     /// Create a virtual directory. Only possible in the root directory.
0050     void mkDir(const QString &name) override;
0051     /// Rename a (real) file in the current directory.
0052     void rename(const QString &fileName, const QString &newName) override;
0053     /// Returns the URL of the real file or an empty URL if file with name does not exist.
0054     QUrl getUrl(const QString &name) const override;
0055     bool canMoveToTrash(const QStringList &fileNames) const override;
0056 
0057     /// Remove virtual files or directories. Real files stay untouched.
0058     void remove(const QStringList &fileNames);
0059     /// Set meta information to be displayed in UI for the current directory
0060     void setMetaInformation(const QString &info);
0061 
0062 protected:
0063     bool refreshInternal(const QUrl &origin, bool onlyScan) override;
0064 
0065 private:
0066     /// Return current dir: "/" or pure directory name
0067     inline QString currentDir()
0068     {
0069         const QString path = _currentDirectory.path().mid(1); // remove slash
0070         return path.isEmpty() ? "/" : path;
0071     }
0072     void mkDirInternal(const QString &name);
0073     /// Save the dictionary to file
0074     void save();
0075     /// Restore the dictionary from file
0076     void restore();
0077 
0078     /// Create local or KIO fileItem. Returns 0 if file does not exist
0079     FileItem *createFileItem(const QUrl &url);
0080 
0081     /// Return the configuration file storing the urls of virtual files
0082     KConfig &getVirtDB();
0083 
0084 private slots:
0085     void slotStatResult(KJob *job);
0086 
0087 private:
0088     void showError(const QString &error);
0089     static QHash<QString, QList<QUrl> *> _virtFilesystemDict; // map virtual directories to containing files
0090     static QHash<QString, QString> _metaInfoDict; // map virtual directories to meta info
0091 
0092     QString _metaInfo; // displayable string with information about the current virtual directory
0093     KIO::UDSEntry _fileEntry; // for async call, save stat job result here
0094 };
0095 
0096 #endif