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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#include "envshortcut.h"
#include "env.h"
#include "executableslist.h"
#include "filesystemutilities.h"
#include "instancemanager.h"
#include <log.h>
#include <utility.h>
namespace env
{
using namespace MOBase;
class ShellLinkException
{
public:
ShellLinkException(QString s) : m_what(std::move(s)) {}
const QString& what() const { return m_what; }
private:
QString m_what;
};
// just a wrapper around IShellLink operations that throws ShellLinkException
// on errors
//
class ShellLinkWrapper
{
public:
ShellLinkWrapper()
{
m_link = createShellLink();
m_file = createPersistFile();
}
void setPath(const QString& s)
{
if (s.isEmpty()) {
throw ShellLinkException("path cannot be empty");
}
const auto r = m_link->SetPath(s.toStdWString().c_str());
throwOnFail(r, QString("failed to set target path '%1'").arg(s));
}
void setArguments(const QString& s)
{
const auto r = m_link->SetArguments(s.toStdWString().c_str());
throwOnFail(r, QString("failed to set arguments '%1'").arg(s));
}
void setDescription(const QString& s)
{
if (s.isEmpty()) {
return;
}
const auto r = m_link->SetDescription(s.toStdWString().c_str());
throwOnFail(r, QString("failed to set description '%1'").arg(s));
}
void setIcon(const QString& file, int i)
{
if (file.isEmpty()) {
return;
}
const auto r = m_link->SetIconLocation(file.toStdWString().c_str(), i);
throwOnFail(r, QString("failed to set icon '%1' @ %2").arg(file).arg(i));
}
void setWorkingDirectory(const QString& s)
{
if (s.isEmpty()) {
return;
}
const auto r = m_link->SetWorkingDirectory(s.toStdWString().c_str());
throwOnFail(r, QString("failed to set working directory '%1'").arg(s));
}
void save(const QString& path)
{
const auto r = m_file->Save(path.toStdWString().c_str(), TRUE);
throwOnFail(r, QString("failed to save link '%1'").arg(path));
}
private:
COMPtr<IShellLink> m_link;
COMPtr<IPersistFile> m_file;
void throwOnFail(HRESULT r, const QString& s)
{
if (FAILED(r)) {
throw ShellLinkException(QString("%1, %2").arg(s).arg(formatSystemMessage(r)));
}
}
COMPtr<IShellLink> createShellLink()
{
void* link = nullptr;
const auto r = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
IID_IShellLink, &link);
throwOnFail(r, "failed to create IShellLink instance");
if (!link) {
throw ShellLinkException("creating IShellLink worked, pointer is null");
}
return COMPtr<IShellLink>(static_cast<IShellLink*>(link));
}
COMPtr<IPersistFile> createPersistFile()
{
void* file = nullptr;
const auto r = m_link->QueryInterface(IID_IPersistFile, &file);
throwOnFail(r, "failed to get IPersistFile interface");
if (!file) {
throw ShellLinkException("querying IPersistFile worked, pointer is null");
}
return COMPtr<IPersistFile>(static_cast<IPersistFile*>(file));
}
};
Shortcut::Shortcut() : m_iconIndex(0) {}
Shortcut::Shortcut(const Executable& exe) : Shortcut()
{
const auto& i = *InstanceManager::singleton().currentInstance();
m_name = MOBase::sanitizeFileName(exe.title());
m_target = QFileInfo(qApp->applicationFilePath()).absoluteFilePath();
m_arguments = QString("\"moshortcut://%1:%2\"")
.arg(i.isPortable() ? "" : i.displayName())
.arg(exe.title());
m_description = QString("Run %1 with ModOrganizer").arg(exe.title());
if (exe.usesOwnIcon()) {
m_icon = exe.binaryInfo().absoluteFilePath();
}
m_workingDirectory = qApp->applicationDirPath();
}
Shortcut& Shortcut::name(const QString& s)
{
m_name = MOBase::sanitizeFileName(s);
return *this;
}
Shortcut& Shortcut::target(const QString& s)
{
m_target = s;
return *this;
}
Shortcut& Shortcut::arguments(const QString& s)
{
m_arguments = s;
return *this;
}
Shortcut& Shortcut::description(const QString& s)
{
m_description = s;
return *this;
}
Shortcut& Shortcut::icon(const QString& s, int index)
{
m_icon = s;
m_iconIndex = index;
return *this;
}
Shortcut& Shortcut::workingDirectory(const QString& s)
{
m_workingDirectory = s;
return *this;
}
bool Shortcut::exists(Locations loc) const
{
const auto path = shortcutPath(loc);
if (path.isEmpty()) {
return false;
}
return QFileInfo(path).exists();
}
bool Shortcut::toggle(Locations loc)
{
if (exists(loc)) {
return remove(loc);
} else {
return add(loc);
}
}
bool Shortcut::add(Locations loc)
{
log::debug("adding shortcut to {}:\n"
" . name: '{}'\n"
" . target: '{}'\n"
" . arguments: '{}'\n"
" . description: '{}'\n"
" . icon: '{}' @ {}\n"
" . working directory: '{}'",
toString(loc), m_name, m_target, m_arguments, m_description, m_icon,
m_iconIndex, m_workingDirectory);
if (m_target.isEmpty()) {
log::error("shortcut: target is empty");
return false;
}
const auto path = shortcutPath(loc);
if (path.isEmpty()) {
return false;
}
log::debug("shorcut file will be saved at '{}'", path);
try {
ShellLinkWrapper link;
link.setPath(m_target);
link.setArguments(m_arguments);
link.setDescription(m_description);
link.setIcon(m_icon, m_iconIndex);
link.setWorkingDirectory(m_workingDirectory);
link.save(path);
return true;
} catch (ShellLinkException& e) {
log::error("{}\nshortcut file was not saved", e.what());
}
return false;
}
bool Shortcut::remove(Locations loc)
{
log::debug("removing shortcut for '{}' from {}", m_name, toString(loc));
const auto path = shortcutPath(loc);
if (path.isEmpty()) {
return false;
}
log::debug("path to shortcut file is '{}'", path);
if (!QFile::exists(path)) {
log::error("can't remove shortcut '{}', file not found", path);
return false;
}
if (!MOBase::shellDelete({path})) {
const auto e = ::GetLastError();
log::error("failed to remove shortcut '{}', {}", path, formatSystemMessage(e));
return false;
}
return true;
}
QString Shortcut::shortcutPath(Locations loc) const
{
const auto dir = shortcutDirectory(loc);
if (dir.isEmpty()) {
return {};
}
const auto file = shortcutFilename();
if (file.isEmpty()) {
return {};
}
return dir + QDir::separator() + file;
}
QString Shortcut::shortcutDirectory(Locations loc) const
{
QString dir;
try {
switch (loc) {
case Desktop:
dir = MOBase::getDesktopDirectory();
break;
case StartMenu:
dir = MOBase::getStartMenuDirectory();
break;
case None:
default:
log::error("shortcut: bad location {}", loc);
break;
}
} catch (std::exception&) {
}
return QDir::toNativeSeparators(dir);
}
QString Shortcut::shortcutFilename() const
{
if (m_name.isEmpty()) {
log::error("shortcut name is empty");
return {};
}
return m_name + ".lnk";
}
QString toString(Shortcut::Locations loc)
{
switch (loc) {
case Shortcut::None:
return "none";
case Shortcut::Desktop:
return "desktop";
case Shortcut::StartMenu:
return "start menu";
default:
return QString("? (%1)").arg(static_cast<int>(loc));
}
}
} // namespace env
|