Filer
TrashHandler.h
1 /*-
2  * Copyright (c) 2022-23 Simon Peter <probono@puredarwin.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef TRASHHANDLER_H
28 #define TRASHHANDLER_H
29 
30 #include <QObject>
31 #include <QString>
32 #include <QVector>
33 #include <QFile>
34 #include <QTextStream>
35 #include <QDir>
36 #include <QMessageBox>
37 
38 // TODO: Support Trash on non-root volumes. Verify the following logic with a Mac and implement it:
39 // When you put a file in the Trash and that file is located on a removable drive
40 // (such as an external hard drive, USB flash drive, or network drive), the file is moved
41 // to a hidden directory on that removable drive.
42 // Within this ".Trashes" directory, there are subdirectories named after the user's UID
43 // (user identifier). The actual numeric UID of the user on the system is used as the name
44 // of these subdirectories to ensure separation of trashed files between different users.
45 // So, the path to the ".Trashes" directory on a removable drive would look something like this:
46 // "/Volumes/YourRemovableDrive/.Trashes"
47 // Inside the ".Trashes" directory, you would find subdirectories named after user UIDs, like:
48 // "/Volumes/YourRemovableDrive/.Trashes/501"
49 // And within each of these UID-named subdirectories, trashed files are stored.
50 
54 class TrashHandler : public QObject {
55 Q_OBJECT
56 
57 public:
62  TrashHandler(QWidget *parent = nullptr);
63 
68  void moveToTrash(const QStringList& paths);
69 
74  static bool emptyTrash();
75 
80  static QString getTrashPath();
81 
86  static bool isEmpty();
87 
88 private:
89  static QString m_trashPath;
90  QWidget *m_parent;
91  bool m_dialogShown = false;
92  bool unmount(const QString &absoluteFilePathWithSymlinksResolved) const;
93 };
94 
95 #endif // TRASHHANDLER_H
The TrashHandler class provides functionality to manage a "Trash" (virtual trash) for files and direc...
Definition: TrashHandler.h:54
static bool emptyTrash()
Empties the "Trash" by deleting all files and directories in the virtual trash.
Definition: TrashHandler.cpp:394
TrashHandler(QWidget *parent=nullptr)
Constructs a TrashHandler object.
Definition: TrashHandler.cpp:46
static bool isEmpty()
Checks if the "Trash" is empty.
Definition: TrashHandler.cpp:457
static QString getTrashPath()
Retrieves the path to the "Trash" directory.
Definition: TrashHandler.cpp:453
void moveToTrash(const QStringList &paths)
Moves files and directories to the "Trash" (virtual trash).
Definition: TrashHandler.cpp:63