- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpfunc.cpp
More file actions
Latest commit
257 lines (226 loc) · 6.34 KB
/
Copy pathhelpfunc.cpp
File metadata and controls
257 lines (226 loc) · 6.34 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include"helpfunc.h"
#include<QCoreApplication>
#include<QDir>
#include<QFile>
#include<QFileIconProvider>
#include<QFileInfo>
#include<QApplication>
#include<QStandardPaths>
#include<QtGlobal>
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
# ifdef Q_OS_WIN32
# include<windows.h>
# include<shellapi.h>
# include<vector>
# else
# include<QProcess>
# endif
#endif
namespace {
QString readOsReleaseValue(const QString& key)
{
QFile file(QStringLiteral("/etc/os-release"));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
returnQString();
}
while (!file.atEnd())
{
const QString line = QString::fromUtf8(file.readLine()).trimmed();
if (!line.startsWith(key + QLatin1Char('=')))
{
continue;
}
QString value = line.mid(key.size() + 1).trimmed();
if (value.startsWith(QLatin1Char('"')) && value.endsWith(QLatin1Char('"')) && value.size() >= 2)
{
value = value.mid(1, value.size() - 2);
}
return value;
}
returnQString();
}
} // namespace
QString notesRoot()
{
#ifdef Q_OS_WIN32
const QString path = QDir(QCoreApplication::applicationDirPath()).filePath("data");
#else
const QString path = QDir(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)).filePath("EasyNote");
#endif
returnQDir::toNativeSeparators(path);
}
QString configRoot()
{
#ifdef Q_OS_WIN32
const QString path = QCoreApplication::applicationDirPath();
#else
const QString path = QDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)).filePath("EasyNote");
#endif
returnQDir::toNativeSeparators(path);
}
QString settingsFile()
{
const QString path = QDir(configRoot()).filePath("EasyNote.ini");
returnQDir::toNativeSeparators(path);
}
QString notebookPath(const QString ¬ebookName)
{
const QString path = QDir(notesRoot()).filePath(notebookName);
returnQDir::toNativeSeparators(path);
}
QString groupPath(const QString ¬ebookName, const QString &groupName)
{
if (groupName.isEmpty())
{
returnnotebookPath(notebookName);
}
const QString path = QDir(notebookPath(notebookName)).filePath(groupName);
returnQDir::toNativeSeparators(path);
}
QString noteFilePath(const QString ¬ebookName, const QString &fileName)
{
const QString path = QDir(notebookPath(notebookName)).filePath(fileName + ".txt");
returnQDir::toNativeSeparators(path);
}
QString noteFilePath(const QString ¬ebookName, const QString &groupName, const QString &fileName)
{
const QString path = QDir(groupPath(notebookName, groupName)).filePath(fileName + ".txt");
returnQDir::toNativeSeparators(path);
}
QString normalizedEntryName(const QString &name)
{
return name.trimmed();
}
boolisValidEntryName(const QString &name)
{
return !name.isEmpty()
&& name != QLatin1String(".")
&& name != QLatin1String("..")
&& !name.contains(QLatin1Char('/'))
&& !name.contains(QLatin1Char('\\'))
&& !name.contains(QLatin1Char('|'));
}
QIcon stableStandardIcon(QStyle::StandardPixmap sp)
{
QPixmap pm = QApplication::style()->standardIcon(sp).pixmap(16, 16);
QIcon icon(pm);
icon.addPixmap(pm, QIcon::Selected);
return icon;
}
QIcon fileIcon(const QString &path)
{
QFileIconProvider provider;
QIcon icon = provider.icon(QFileInfo(path));
QPixmap pm = icon.pixmap(16, 16);
icon.addPixmap(pm, QIcon::Selected);
return icon;
}
voidensureAppDirs()
{
QDir dir;
dir.mkpath(notesRoot());
dir.mkpath(configRoot());
}
boolmoveToTrash(const QString &path, QString *errorMessage)
{
const QString nativePath = QDir::toNativeSeparators(path);
if (!QFileInfo::exists(nativePath))
{
if (errorMessage)
{
*errorMessage = QStringLiteral("path does not exist: %1").arg(nativePath);
}
returnfalse;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
QFile file(nativePath);
if (file.moveToTrash())
{
returntrue;
}
if (errorMessage)
{
*errorMessage = file.errorString();
}
returnfalse;
#else
#ifdef Q_OS_WIN32
// SHFileOperationW 要求 pFrom 以双 \0 结尾
QString winPath = QDir::toNativeSeparators(nativePath);
std::vector<wchar_t> buf(static_cast<size_t>(winPath.size()) + 2, L'\0');
winPath.toWCharArray(buf.data());
SHFILEOPSTRUCTW op;
ZeroMemory(&op, sizeof(op));
op.wFunc = FO_DELETE;
op.pFrom = buf.data();
op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
constint ret = SHFileOperationW(&op);
if (ret == 0 && !op.fAnyOperationsAborted)
{
returntrue;
}
if (errorMessage)
{
*errorMessage = QStringLiteral("SHFileOperation failed, code=%1").arg(ret);
}
returnfalse;
#else
// Linux: 优先 gio trash, 退化到 kioclient5 / gvfs-trash
const QStringList candidates = {
QStringLiteral("gio"),
QStringLiteral("kioclient5"),
QStringLiteral("gvfs-trash"),
};
for (const QString &tool : candidates)
{
QStringList args;
if (tool == QStringLiteral("gio"))
{
args << QStringLiteral("trash") << nativePath;
}
elseif (tool == QStringLiteral("kioclient5"))
{
args << QStringLiteral("move") << nativePath << QStringLiteral("trash:/");
}
else
{
args << nativePath;
}
QProcess proc;
proc.start(tool, args);
if (!proc.waitForStarted(2000))
{
continue;
}
proc.waitForFinished(10000);
if (proc.exitStatus() == QProcess::NormalExit && proc.exitCode() == 0)
{
returntrue;
}
}
if (errorMessage)
{
*errorMessage = QStringLiteral("no trash backend available (gio/kioclient5/gvfs-trash)");
}
returnfalse;
#endif
#endif
}
boolisUos()
{
#ifdef Q_OS_LINUX
staticconstbool isUosSystem = []() {
const QString id = readOsReleaseValue(QStringLiteral("ID")).trimmed().toLower();
if (id == QStringLiteral("uos"))
{
returntrue;
}
const QString name = readOsReleaseValue(QStringLiteral("NAME")).trimmed().toLower();
return name == QStringLiteral("uos");
}();
return isUosSystem;
#else
returnfalse;
#endif
}