-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovetotrash.cpp
189 lines (169 loc) · 6.21 KB
/
movetotrash.cpp
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
/****************************************************************************}
{ movetotrash.cpp - Trash/Recycle Bin partial interface }
{ }
{ Copyright (c) 2017 Alexey Parfenov <[email protected]> }
{ }
{ This file is part of Meson Player. }
{ }
{ Meson Player is free software: you can redistribute it and/or modify it }
{ under the terms of the GNU General Public License as published by }
{ the Free Software Foundation, either version 3 of the License, }
{ or (at your option) any later version. }
{ }
{ Meson Player is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU }
{ General Public License for more details: https://gnu.org/licenses/gpl.html }
{****************************************************************************/
#include "movetotrash.h"
#include <QStandardPaths>
#include <QProcess>
#include <QFile>
#include <QDir>
#ifdef Q_OS_WIN
#include <windows.h>
#include <shobjidl.h>
#include <shellapi.h>
#endif
#ifdef Q_OS_OSX
#import <CoreFoundation/CoreFoundation.h>
#include <QUrl>
#endif
namespace MoveToTrash {
static bool checkIfCanDoIt()
{
#ifdef Q_OS_WIN
return true;
#endif
#ifdef Q_OS_OSX
return true;
#endif
#ifdef Q_OS_LINUX
QStringList apps = {
"kioclient5",
"kioclient",
"gvfs-trash",
"trash"
};
for(QString app : apps)
if(!QStandardPaths::findExecutable(app).isEmpty())
return true;
#endif
return false;
}
bool canDoIt()
{
static enum {
unknown,
canDo,
cannotDo
} status = unknown;
if(status == unknown)
status = checkIfCanDoIt() ? canDo : cannotDo;
return status == canDo;
}
bool doIt(const QString& path)
{
if(!canDoIt() || path.isEmpty())
return false;
QFileInfo info(path);
if(!info.exists())
return false;
QFileInfo dirInfo = QFileInfo(info.dir().path());
if(!dirInfo.isWritable())
return false;
QString filename = QDir::toNativeSeparators(info.absoluteFilePath());
#ifdef Q_OS_WIN
// Windows is the only OS that has a reliable API to move files to the Trash.
// However, look at how many steps it takes.
// It also forces you to use single-threaded COM for the whole app.
if(SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)))
{
bool ok = false;
IFileOperation *pfo;
if(SUCCEEDED(CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo))))
{
if(SUCCEEDED(pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT | FOF_WANTNUKEWARNING)))
{
IShellItem *item = NULL;
std::wstring ws = filename.toStdWString();
if(SUCCEEDED(SHCreateItemFromParsingName(ws.c_str(), NULL, IID_PPV_ARGS(&item))))
{
if(SUCCEEDED(pfo->DeleteItem(item, NULL)))
{
if(SUCCEEDED(pfo->PerformOperations()))
{
if(!QFile::exists(path))
{
ok = true;
}
}
}
}
item->Release();
}
pfo->Release();
}
CoUninitialize();
return ok;
}
#endif
#ifdef Q_OS_OSX
// OSX has a lot of APIs to trash files.
// Sadly, none of them support "Put Back" functionality.
// So, use Finder to do this correctly.
filename.replace("\\", "\\\\").replace("\"", "\\\"");
QString cmd = QString("tell app \"Finder\" to move POSIX file \"")+filename+"\" to trash";
QProcess::execute("osascript", {"-e", cmd});
if(!QFile::exists(path))
return true;
#endif
#ifdef Q_OS_LINUX
// Linux doesn't have any Trash API at all.
// Everything needs to be done manually using X.org specs.
// But since no Linux distribution follows those specs to the letter,
// there's no proper cross-Linux way to trash files.
// So, let's pass this burden to external tools and hope for the best.
QString app;
app = QStandardPaths::findExecutable("kioclient5");
if(!app.isEmpty())
{
QStringList args = {
"--platform", "offscreen",
"move", filename, "trash://"
};
QProcess::execute(app, args);
if(!QFile::exists(path))
return true;
}
app = QStandardPaths::findExecutable("kioclient");
if(!app.isEmpty())
{
QStringList args = {
"--noninteractive",
"move", filename, "trash://"
};
QProcess::execute(app, args);
if(!QFile::exists(path))
return true;
}
app = QStandardPaths::findExecutable("gvfs-trash");
if(!app.isEmpty())
{
QStringList args = {path};
QProcess::execute(app, args);
if(!QFile::exists(path))
return true;
}
app = QStandardPaths::findExecutable("trash");
if(!app.isEmpty())
{
QStringList args = {path};
QProcess::execute(app, args);
if(!QFile::exists(path))
return true;
}
#endif
return false;
}
}