-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathCliParser.cpp
More file actions
executable file
·346 lines (338 loc) · 16 KB
/
Copy pathCliParser.cpp
File metadata and controls
executable file
·346 lines (338 loc) · 16 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
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
/** \file CliParser.cpp
\brief To group into one class, the CLI parsing
\author alpha_one_x86
\licence GPL3, see the file COPYING */
#include "CliParser.h"
#include "cpp11addition.h"
#include "Core.h"
#ifdef Q_OS_WIN32
#include <windows.h>
#endif
//this is just to support clipboard
#include <QClipboard>
#include <QDir>
#include <QGuiApplication>
#include <QRegularExpression>
#include <QDebug>
/* The caller's working directory travels as argument 0 (LocalListener inserts it), so a RELATIVE
* path handed to the resident instance is resolved against the directory it was typed in, not the
* resident's own (typically $HOME): "cd /data && ultracopier mv notes.txt /backup" moved
* $HOME/notes.txt when it existed, else failed with "path not found". */
static std::string resolveFromCallerDirectory(const std::string &callerDirectory,const std::string &path)
{
if(path=="?" || path.empty() || path.find("://")!=std::string::npos
|| QDir::isAbsolutePath(QString::fromStdString(path)))
return path;
return QDir(QString::fromStdString(callerDirectory)).absoluteFilePath(QString::fromStdString(path)).toStdString();
}
CliParser::CliParser(QObject *parent) :
QObject(parent)
{
//this->core=core;
}
/** \brief method to parse the ultracopier arguments
\param ultracopierArguments the argument list
\param external true if the arguments come from other instance of ultracopier
*/
void CliParser::cli(const std::vector<std::string> &ultracopierArguments, const bool &external, const bool &onlyCheck, const bool &fromStart)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"ultracopierArguments: "+stringimplode(ultracopierArguments,';'));
if(ultracopierArguments.size()==1)
{
if(external)
{
//if(!core->startNewTransferOneUniqueCopyEngine())
{
#ifdef Q_OS_WIN32
QString message(tr("Ultracopier is already running, right click on its system tray icon (near the clock) to use it or just copy and paste"));
#else
QString message(tr("Ultracopier is already running, view all notification area icons (near the clock), right click on its system tray icon to use it or just copy and paste"));
#endif
QMessageBox::warning(NULL,tr("Warning"),message);
showSystrayMessage(message.toStdString());
}
}
// else do nothing, is normal starting without arguements
return;
}
else if(ultracopierArguments.size()==2)
{
if(ultracopierArguments.back()=="quit")
{
if(onlyCheck)
return;
QCoreApplication::exit();
return;
}
else if(ultracopierArguments.back()=="uninstall")
{
if(onlyCheck)
return;
#ifdef Q_OS_WIN32
HKEY ultracopier_regkey;
if(RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, 0, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, 0, &ultracopier_regkey, 0)==ERROR_SUCCESS)
{
RegDeleteValue(ultracopier_regkey, TEXT("ultracopier"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy32.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy32d.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy64.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy64d.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy32all.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy32alld.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy64all.dll"));
RegDeleteValue(ultracopier_regkey, TEXT("catchcopy64alld.dll"));
RegCloseKey(ultracopier_regkey);
}
#endif
QCoreApplication::exit();
return;
}
else if(ultracopierArguments.back()=="--help")
{
showHelp(ultracopierArguments,fromStart,false);
return;
}
else if(ultracopierArguments.back()=="--options")
{
emit showOptions();
return;
}
else if(stringEndsWith(ultracopierArguments.back(),".urc"))
{
tryLoadPlugin(resolveFromCallerDirectory(ultracopierArguments.at(0),ultracopierArguments.back()));
return;
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Command line not understand");
showHelp(ultracopierArguments,fromStart);
return;
}
else if(ultracopierArguments.size()==3)
{
if(ultracopierArguments.at(1)=="Transfer-list")
{
if(onlyCheck)
return;
const std::string transferListPath=resolveFromCallerDirectory(ultracopierArguments.at(0),ultracopierArguments.back());
QFile transferFile(QString::fromStdString(transferListPath));
if(transferFile.open(QIODevice::ReadOnly))
{
QString content;
QByteArray data=transferFile.readLine(64);
if(data.size()<=0)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Problem reading file, or file size is 0");
QMessageBox::warning(NULL,tr("Warning"),tr("Problem reading file, or file size is 0"));
transferFile.close();
return;
}
content=QString::fromUtf8(data);
std::vector<std::string> transferListArguments=stringsplit(content.toStdString(),';');
/* The header has EXACTLY 4 fields ("Ultracopier;Transfer-list;<mode>;Ultracopier").
* Check the SIZE before touching any of them: transferListArguments[3] on a shorter
* line is out-of-bounds -- pointing ultracopier at any other file (a text file, a
* truncated list) SEGFAULTED the whole app right here. */
if(transferListArguments.size()<4)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"This file is not supported transfer list");
QMessageBox::warning(NULL,tr("Warning"),tr("This file is not supported transfer list"));
transferFile.close();
return;
}
transferListArguments[3].erase(std::remove(transferListArguments[3].begin(), transferListArguments[3].end(),'\n'),transferListArguments[3].end());
transferListArguments[3].erase(std::remove(transferListArguments[3].begin(), transferListArguments[3].end(),'\r'),transferListArguments[3].end());
if(transferListArguments.at(0)!="Ultracopier" ||
transferListArguments.at(1)!="Transfer-list" ||
(transferListArguments.at(2)!="Transfer" && transferListArguments.at(2)!="Copy" && transferListArguments.at(2)!="Move") ||
transferListArguments.at(3)!="Ultracopier"
)
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"This file is not supported transfer list");
QMessageBox::warning(NULL,tr("Warning"),tr("This file is not supported transfer list"));
transferFile.close();
return;
}
transferFile.close();
/* The 4th field is the FORMAT marker, not a plugin name -- ListThread's importer
* accepts no other value than "Ultracopier" there. Forwarding it as the engine NAME
* made Core look for a plugin called "Ultracopier" while the engine is named
* "Ultracopier Spec", so opening a list Ultracopier had exported ITSELF always died
* with "Cannot find any engine with this name". An empty name = any compatible
* engine, which is what this format means. */
emit newTransferList(std::string(),transferListArguments.at(2),transferListPath);
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Unable to open the transfer list file: "+transferFile.errorString().toStdString());
QMessageBox::warning(NULL,tr("Warning"),tr("Unable to open the transfer list file"));
return;
}
return;
}
if(ultracopierArguments.at(1)=="CBmv" || ultracopierArguments.at(1)=="CBcp")
{
if(ultracopierArguments.size()==3)
{
QClipboard *clipboard = QGuiApplication::clipboard();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const QStringList &l=clipboard->text().split(QRegularExpression("[\n\r]+"));
#else
const QStringList &l=clipboard->text().split(QRegularExpression("[\n\r]+"),Qt::SkipEmptyParts);
#endif
/*linux:
file:///path/test/master.zip
file:///path/test/New text file
Windows:
file://Z:/X
*/
std::vector<std::string> sourceList;
int index=0;
while(index<l.size())
{
//sourceList.push_back(l.at(index).toStdString());
const QString &uriString = l.at(index);
QUrl url(uriString);
if(url.isValid() && url.isLocalFile())
{
QString localPath = url.toLocalFile();
if (!localPath.isEmpty())
sourceList.push_back(localPath.toStdString());
else
sourceList.push_back(l.at(index).toStdString());
}
else
sourceList.push_back(l.at(index).toStdString());
index++;
}
if(ultracopierArguments.back()=="?")
{
if(ultracopierArguments.at(1)=="CBmv")
emit newMoveWithoutDestination(sourceList);
else
emit newCopyWithoutDestination(sourceList);
}
else
{
const std::string destination=resolveFromCallerDirectory(ultracopierArguments.at(0),ultracopierArguments.back());
if(ultracopierArguments.at(1)=="CBmv")
emit newMove(sourceList,destination);
else
emit newCopy(sourceList,destination);
}
return;
}
else
{
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Command line not understand");
showHelp(ultracopierArguments,fromStart);
return;
}
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Command line not understand");
showHelp(ultracopierArguments,fromStart);
return;
}
else if(ultracopierArguments.size()>3)
{
if(ultracopierArguments.at(1)=="Copy" || ultracopierArguments.at(1)=="cp" || ultracopierArguments.at(1)=="CBcp")
{
if(onlyCheck)
return;
std::vector<std::string> transferList=ultracopierArguments;
transferList.erase(transferList.cbegin());//app path
transferList.erase(transferList.cbegin());//command
for(std::string &path : transferList)
path=resolveFromCallerDirectory(ultracopierArguments.at(0),path);
if(transferList.back()=="?")
{
transferList.pop_back();
emit newCopyWithoutDestination(transferList);
}
else
{
std::string destination=transferList.back();
transferList.pop_back();
emit newCopy(transferList,destination);
}
return;
}
else if(ultracopierArguments.at(1)=="Move" || ultracopierArguments.at(1)=="mv" || ultracopierArguments.at(1)=="CBmv")
{
if(onlyCheck)
return;
std::vector<std::string> transferList=ultracopierArguments;
transferList.erase(transferList.cbegin());//app path
transferList.erase(transferList.cbegin());//command
for(std::string &path : transferList)
path=resolveFromCallerDirectory(ultracopierArguments.at(0),path);
if(transferList.back()=="?")
{
transferList.pop_back();
emit newMoveWithoutDestination(transferList);
}
else
{
std::string destination=transferList.back();
transferList.pop_back();
emit newMove(transferList,destination);
}
return;
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Command line not understand");
showHelp(ultracopierArguments,fromStart);
return;
}
ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,"Command line not understand");
showHelp(ultracopierArguments,fromStart);
}
/** \brief show the help
*\param incorrectArguments if the help is call because the arguments are wrong */
void CliParser::showHelp(const std::vector<std::string> &ultracopierArguments, const bool &fromStart, const bool &incorrectArguments)
{
if(incorrectArguments)
{
qDebug() << "Incorrect arguments detected";
unsigned int index=0;
while(index<ultracopierArguments.size())
{
std::string line(ultracopierArguments.at(index));
if(fromStart && line.find("ReactOS")!=std::string::npos)
return;
qDebug() << QString::fromStdString(line);
index++;
}
}
qDebug() << tr("The arguments possible are:");
qDebug() << "--help : "+tr("To display this help");
qDebug() << "--options : "+tr("To display the options");
qDebug() << "quit : "+tr("To quit the other instances (if running)");
qDebug() << "Transfer-list [transfer list file] : "+tr("Open transfer list");
qDebug() << "cp [source [source2]] [destination] : "+tr("To copy sources to destination, separated by space. If destination is \"?\", ultracopier will ask the user");
qDebug() << "mv [source [source2]] [destination] : "+tr("To move sources to destination, separated by space. If destination is \"?\", ultracopier will ask the user");
QString message;
if(incorrectArguments)
{
message+="<b>"+tr("Command not valid")+"</b><br />\n";
unsigned int index=0;
while(index<ultracopierArguments.size())
{
std::string line(ultracopierArguments.at(index));
if(fromStart && line.find("ReactOS")!=std::string::npos)
return;
message+=QString::fromStdString(line)+"<br />\n";
index++;
}
}
message+=+"<b></b>"+tr("The arguments possible are:")+"\n<ul>";
message+="<li><b>--help</b> : "+tr("To display this help")+"</li>\n";
message+="<li><b>--options</b> : "+tr("To display the options")+"</li>\n";
message+="<li><b>quit</b> : "+tr("To quit the other instances (if running)")+"</li>\n";
message+="<li><b>Transfer-list [transfer list file]</b> : "+tr("Open transfer list")+"</li>\n";
message+="<li><b>cp [source [source2]] [destination]</b> : "+tr("To copy sources to destination, separated by space. If destination is \"?\", ultracopier will ask the user")+"</li>\n";
message+="<li><b>mv [source [source2]] [destination]</b> : "+tr("To move sources to destination, separated by space. If destination is \"?\", ultracopier will ask the user")+"</li>\n";
message+=+"</ul>";
if(incorrectArguments)
QMessageBox::warning(NULL,tr("Warning"),message);
else
QMessageBox::information(NULL,tr("Help"),message);
}