-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexecutor.cpp
More file actions
454 lines (400 loc) · 15.7 KB
/
executor.cpp
File metadata and controls
454 lines (400 loc) · 15.7 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include "executor.h"
#include <QDebug>
#include <QDesktopServices>
#include <QJSEngine>
#include <QRegExp>
#include <QTextStream>
#include <QUrl>
#include <windows.h>
#include <QMessageBox>
#include <QtConcurrent>
#include "Utils/util.h"
#include <Utils/WinUtility.h>
#include <Utils/cacheiconprovider.h>
Executor::Executor(QObject* parent)
: QObject(parent)
{
//QDir().mkdir(dirPath);
//cmdEditor = new CmdEditor(cmdListPath);
//noteEditor = new NoteEditor(noteListPath);
readCmdList();
connect(qApp, &QApplication::aboutToQuit, this, [=]() { //write
QFile file(runTimesDataPath);
if (!file.open(QFile::WriteOnly)) {
QMessageBox::critical(nullptr, "File Write Error", "Cannot open \"runTimesMap.dat\"");
return;
}
QDataStream data(&file);
data << runTimesMap;
qDebug() << "#Write runTimesMap.dat";
});
qRegisterMetaType<QList<Command>>("QList<Command>");
connect(this, &Executor::appListChanged, this, [=](QList<Command> list) { // 信号与槽,线程安全,无需加锁
appList = list;
qDebug() << "#Updated AppList";
});
if (QFile::exists(runTimesDataPath)) {
QFile file(runTimesDataPath); //read
if (!file.open(QFile::ReadOnly)) {
QMessageBox::critical(nullptr, "File Read Error", "Cannot open \"runTimesMap.dat\"");
return;
}
QDataStream data(&file);
data >> runTimesMap;
qDebug() << "#Read runTimesMap.dat";
}
if (pinyinMap.isEmpty()) {
const QString PinyinFile = QApplication::applicationDirPath() + "\\pinyin.txt";
QFile file(PinyinFile);
if (!file.exists()) {
QMessageBox::critical(nullptr, "File Not Found", QString("Cannot find \"%1\"").arg(PinyinFile));
return;
}
if (!file.open(QFile::ReadOnly | QFile::Text)) {
QMessageBox::critical(nullptr, "File Read Error", QString("Cannot open \"%1\"").arg(PinyinFile));
return;
}
QTextStream in(&file);
in.setCodec("UTF-8"); //明确设置为 UTF-8 编码, 默认貌似不对
while (!in.atEnd()) {
QString line = in.readLine().simplified();
if (line.size() < 3) continue;
pinyinMap[line.at(0)] = line.mid(2).split(','); // 多音字
}
qDebug() << "#Read pinyin.txt" << pinyinMap.size();
}
}
void Executor::readCmdList()
{
if (sys->cmdEditor == nullptr) return;
CmdEditor::TableList list = sys->cmdEditor->getContentList();
cmdList.clear();
for (QStringList& line : list)
cmdList.append({line.at(0), line.at(1), QDir::toNativeSeparators(line.at(2)), line.at(3)});
std::sort(cmdList.begin(), cmdList.end(), [](const Command& a, const Command& b) {
return a.code.length() < b.code.length();
});
}
// ShellExecute
void Executor::runApp(const QString& filename, const QString& parameter, bool runAsAdmin, int nShowMode)
{
if (filename == "") return; //""代表本工作目录
int pos = filename.lastIndexOf('\\');
QString dirPath = filename.left(pos); //缺省目录,防止找不到缺省文件//if pos == -1 return entire string
qDebug() << "#run:" << filename << parameter;
// The default verb is used, if available. If not, the "open" verb is used. If neither verb is available, the system uses the first verb listed in the registry.
ShellExecuteW(0, runAsAdmin ? L"runas" : NULL, filename.toStdWString().c_str(), parameter.toStdWString().c_str(), dirPath.toStdWString().c_str(), nShowMode);
// 对于.lnk,如果使用"open",则会在控制台输出很多shell信息,造成卡顿
//宽字符(Unicode)才能完美转换,否则可能编码错误
//神奇现象:ShellExecute会新开线程,不返回虽然阻塞 但是事件循环继续进行
}
void Executor::editCmd()
{ //hide
emit askHide();
sys->cmdEditor->exec();
readCmdList();
sys->sysTray->showMessage("Update", "CMD List has [Updated]");
emit askShow();
}
void Executor::editInputM()
{
emit askHide();
InputMethodEditor inputMEditor(inputMethodListPath);
inputMEditor.exec();
sys->inputM->readListFile(); //更新
sys->sysTray->showMessage("Update", "InputMethod List has [Updated]");
emit askShow();
}
void Executor::editNote()
{
emit askHide();
sys->noteEditor->exec();
emit askShow();
}
QStringList toPinyin(const QString& str)
{
static QMap<QString, QStringList> pinyinCache;
if (pinyinCache.contains(str)) return pinyinCache[str];
QStringList pinyinList;
for (const QChar& ch : str) {
if (ch.script() == QChar::Script_Han) { // ch.isLetterOrNumber()对于汉字会返回true,离谱
auto candidate = Executor::pinyinMap.value(ch, {ch}); //多音字
if (pinyinList.empty()) {
pinyinList = candidate;
} else {
QStringList tmp;
for (const auto& pinyin: qAsConst(candidate))
for (const auto& preStr: qAsConst(pinyinList))
tmp.push_back(preStr + pinyin);
pinyinList = tmp;
}
} else {
if (pinyinList.empty())
pinyinList.append(ch);
else {
for (auto& str: pinyinList)
str += ch;
}
}
}
if (pinyinList.size() > 5)
qWarning() << "#tooMuchTonePinyin:" << str << pinyinList;
pinyinCache[str] = pinyinList;
return pinyinList;
}
// 判断中文和拼音是否匹配,包含多音字枚举
bool isPinyinMatch(const QString& dst, const QString& str)
{
if (Util::hasChinese(str)) {
qWarning() << "#PinyinMatch: str contains Chinese";
return false;
}
if (!Util::hasChinese(dst)) {
qWarning() << "#PinyinMatch: dst doesn't contain Chinese";
return false;
}
auto dst_pinyins = toPinyin(dst);
for (const auto& pinyin: dst_pinyins)
if (pinyin.contains(str, Qt::CaseInsensitive))
return true;
return false;
}
// TODO: 加入对描述信息的匹配,以及打分排序
bool Executor::isMatch(const QString& dst, const QString& str, Qt::CaseSensitivity cs) //以空格分割,乱序关键字匹配
{
if (isBlank(str)) return false;
if (symbol(dst) != symbol(str)) return false; //不同级别命令不比较
if (dst.compare(str, cs) == 0) return true; //完全匹配
//if ((str.at(0) == '#' && dst.at(0) != '#') || (str.at(0) != '#' && dst.at(0) == '#')) return false; //加入匹配size 符号类别二次匹配 getsymbol!!!!!!!!!!!!!!!!!!!
bool extra = str.endsWith(' '); //(如:"qt "->"qt":false 以保证"qt code"快捷匹配)
static const QRegExp reg("\\W+"); //匹配至少一个[非字母数字], QRegularExpression会把中文视为\W,但是QRegExp不会
QStringList dstList = dst.simplified().split(reg, Qt::SkipEmptyParts);
QStringList strList = str.simplified().split(reg, Qt::SkipEmptyParts);
if (symbol(dst) == Inner_Cmd) dstList.push_front(InnerMark); //复原上一步清除的'#'
if (symbol(str) == Inner_Cmd) strList.push_front(InnerMark);
if (strList.empty()) return false;
if (dstList.size() < strList.size() + extra) return false; //' '顶一个size
for (const QString& _str : qAsConst(strList)) { //存在多次匹配同一个单词问题(不过问题不大)
bool flag = false;
for (const QString& _dst : qAsConst(dstList)) {
if (Util::hasChinese(_dst) && !Util::hasChinese(_str)) {
if (isPinyinMatch(_dst, _str)) {
flag = true;
break;
}
} else {
if (_dst.indexOf(_str, 0, cs) == 0) {
flag = true;
break;
}
}
}
if (!flag) return false;
}
return true;
}
bool Executor::isBlank(const QString& str)
{
return str.simplified() == "";
}
Executor::CmdSymbol Executor::symbol(const QString& str)
{
QChar ch = str.simplified().at(0);
if (ch == InnerMark)
return Inner_Cmd;
else if (ch == JsMark)
return Js_Cmd;
else if (ch == TransMark)
return Trans_Cmd;
else
return Normal_Cmd;
}
void Executor::clearText()
{
echoText.clear();
}
QString Executor::cleanPath(QString path)
{
return path.replace('\"', "");
}
bool Executor::isExistPath(const QString& str)
{
static auto isAbsolutePath = [](const QString& str)->bool {
static QRegularExpression regex("^[A-Za-z]:"); //no need for '\\' or '/' ; for fast input D:
return regex.match(str).hasMatch();
};
QString _str = cleanPath(str);
return QFileInfo::exists(_str) && isAbsolutePath(_str); //增加绝对路径判断,否则可能查询系统目录(如 Windows\System32 (\ja))
}
void Executor::updateAppList()
{
QtConcurrent::run([this](){
ComInitializer COM; // RAII
static QSet<QString> lastLaunchCmdSet;
QSet<QString> launchCmdSet;
for (const Command& cmd : qAsConst(cmdList))
launchCmdSet << (cmd.path.toLower() + cmd.param);
QList<Command> list;
static QList<std::tuple<QString, QString>> lastApps;
auto apps = Win::getAppList();
if (launchCmdSet == lastLaunchCmdSet && apps == lastApps) {
qDebug() << "#AppList & cmdList Unchanged";
return;
}
lastLaunchCmdSet = launchCmdSet;
lastApps = apps;
for (const auto& app : qAsConst(apps)) {
auto [name, path] = app;
if (QFileInfo(path).isShortcut()) { // .lnk, not include .url
auto [target, args] = Win::getShortcutInfo(path);
auto exeCmd = target.toLower() + args;
// 和自定义命令去重
if ((!exeCmd.isEmpty() && launchCmdSet.contains(exeCmd)) || launchCmdSet.contains(path.toLower())) {
// qDebug() << "#Duplicated:" << name;
continue;
}
}
CacheIconProvider::instance().addCache(path); // 缓存图标
list.append({name, "", path, ""});
}
std::sort(list.begin(), list.end(), [](const Command &a, const Command &b) {
bool aHasChinese = Util::hasChinese(a.code);
bool bHasChinese = Util::hasChinese(b.code);
if (aHasChinese != bHasChinese) // 不含汉字的放前面
return !aHasChinese;
// 如果都不包含或都包含汉字,按长度从短到长排序
return a.code.length() < b.code.length();
});
emit appListChanged(list);
});
}
Executor::State Executor::run(const QString& code, bool asAdmin, bool isWithExtra)
{
clearText();
if (code.isEmpty()) return NOCODE;
if (code == OmitMark) return NOCODE;
if (symbol(code) == Js_Cmd) {
QString body = code.simplified().mid(1);
echoText = JsEngine.evaluate(body).toString();
return JSCODE;
} else if (symbol(code) == Trans_Cmd) {
QString body = code.simplified().mid(1);
echoText = body.simplified();
return TRANSLATE;
}
if (Util::maybePath(code)) { // 路径和命令的匹配是互斥的,疑似路径后,不再匹配命令,因为UI不会再显示命令候选
if (isExistPath(code)) {
runApp(code); // 对于路径,不能用runas运行,否则没反应,必须得用open或NULL(默认)
return PATH;
}
return NOCODE;
}
auto apps = this->appList;
auto iter_app = std::find_if(apps.begin(), apps.end(), [=](const Command& cmd) {
return isMatch(cmd.code, code);
});
auto iter = std::find_if(cmdList.begin(), cmdList.end(), [=](const Command& cmd) { //模糊匹配
QString sor = cmd.code;
if (isWithExtra) sor += cmd.extra; //加上extra匹配
return isMatch(sor, code); //cmd.code.indexOf(code, 0, Qt::CaseInsensitive) == 0
});
auto iter_inner = std::find_if(innerCmdList.begin(), innerCmdList.end(), [=](const InnerCommand& cmd) {
return isMatch(cmd.code, code); //cmd.code.indexOf(code, 0, Qt::CaseInsensitive) == 0
});
bool isFind = (iter != cmdList.end());
bool isFind_app = (iter_app != apps.end());
bool isFind_inner = (iter_inner != innerCmdList.end());
if (isFind) {
Command cmd = *iter;
runApp(cmd.path, cmd.param, asAdmin);
// runTimesMap[cmd.filename + cmd.parameter]++; //统计运行次数,filename+param作为唯一标识
//qDebug() << runTimesMap;
return CODE;
} else if (isFind_app) {
Command cmd = *iter_app;
runApp(cmd.path, cmd.param, asAdmin);
// runTimesMap[cmd.filename + cmd.parameter]++;
return CODE;
} else if (isFind_inner) {
InnerCommand cmd = *iter_inner;
//(this->*(cmd.pfunc))();
cmd.func(this); //指定对象
return INNERCODE;
} else { //最后的尝试//对人类最后的求爱
runApp("cmd.exe", "/k " + code, asAdmin); //加上"/c"(close),不然命令不会执行// "/k"(keep)也行
return CMD;
}
}
QList<QPair<QString, QString>> Executor::matchString(const QString& str, State* state, int limit, Qt::CaseSensitivity cs) //
{
clearText();
if (state) *state = NOCODE;
QList<QPair<QString, QString>> list;
if (str.isEmpty()) return list;
if (symbol(str) == Js_Cmd) {
echoText = "Inputing JavaScript Code...";
if (state) *state = JSCODE;
return list;
}
if (symbol(str) == Trans_Cmd) {
echoText = "Inputing Sth. to Translate...";
if (state) *state = TRANSLATE;
return list;
}
if (isExistPath(str)) {
echoText = "Maybe a Path...";
if (state) *state = PATH;
return list;
}
QMap<QString, Command> codeFile;
for (const Command& cmd : qAsConst(cmdList))
if (isMatch(cmd.code, str, cs)) { //忽略大小写//cmd.code.indexOf(str, 0, Qt::CaseInsensitive) == 0)
list << qMakePair(cmd.code + cmd.extra, cmd.path);
codeFile[cmd.code + cmd.extra + cmd.path] = cmd; //indexing
}
for (const Command& cmd : qAsConst(appList))
if (isMatch(cmd.code, str, cs)) {
list << qMakePair(cmd.code + cmd.extra, cmd.path);
codeFile[cmd.code + cmd.extra + cmd.path] = cmd;
}
//无需去重 可能出现同名不同path等 让用户选择
// if (!list.empty()) {
// std::sort(list.begin(), list.end(), [=, &codeFile](const QPair<QString, QString>& a, const QPair<QString, QString>& b) -> bool {
// const Command& ca = codeFile[a.first + a.second];
// const Command& cb = codeFile[b.first + b.second];
// if (ca.code.compare(str, cs) == 0) //全匹配优先级最高
// return true;
// else if (cb.code.compare(str, cs) == 0)
// return false;
// else //降序
// return runTimesMap.value(ca.filename + ca.parameter) > runTimesMap.value(cb.filename + cb.parameter);
// });
// }
QSet<QString> codeSet;
for (const InnerCommand& cmd : qAsConst(innerCmdList))
if (isMatch(cmd.code, str, cs)) { //忽略大小写
QString str = cmd.showCode.isEmpty() ? cmd.code : cmd.showCode;
if (!codeSet.contains(str)) { //去重
codeSet << str;
list << qMakePair(str, QString());
}
}
if (state) *state = CODE; //统一标识INNER & CODE 在此不好做区分
if (list.size() > limit) {
list = list.mid(0, limit);
list << qMakePair(OmitMark, QString(""));
}
return list;
}
bool Executor::hasText()
{
return !echoText.isEmpty();
}
QString Executor::text()
{
return echoText;
}
QList<Command> Executor::getCMDList()
{
return cmdList;
}