/
cliapplication.cpp
741 lines (558 loc) · 22.8 KB
/
cliapplication.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR 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.
OpenCOR 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// CLI application
//==============================================================================
#include "cliapplication.h"
#include "cliinterface.h"
#include "cliutils.h"
#include "plugininterface.h"
#include "pluginmanager.h"
#include "solverinterface.h"
//==============================================================================
#include <iostream>
//==============================================================================
#include <QCoreApplication>
#include <QDir>
#include <QSettings>
//==============================================================================
namespace OpenCOR {
//==============================================================================
CliApplication::CliApplication(int &pArgC, char *pArgV[]) // NOLINT(hicpp-avoid-c-arrays, modernize-avoid-c-arrays)
{
// Create our CLI application
mCliApplication = new QCoreApplication(pArgC, pArgV);
}
//==============================================================================
CliApplication::~CliApplication()
{
// Keep track of the settings of our various plugins
QSettings settings;
settings.beginGroup(SettingsPlugins);
for (auto plugin : mLoadedPluginPlugins) {
settings.beginGroup(plugin->name());
qobject_cast<PluginInterface *>(plugin->instance())->saveSettings(settings);
settings.endGroup();
}
settings.endGroup();
// Finalise our loaded plugins, if any
for (auto plugin : mLoadedPluginPlugins) {
qobject_cast<PluginInterface *>(plugin->instance())->finalizePlugin();
}
// Delete some internal objects
delete mCliApplication;
delete mPluginManager;
}
//==============================================================================
void CliApplication::loadPlugins()
{
// Load all the plugins by creating our plugin manager
mPluginManager = new PluginManager(false);
// Retrieve some categories of plugins
for (auto plugin : mPluginManager->loadedPlugins()) {
if (qobject_cast<CliInterface *>(plugin->instance()) != nullptr) {
mLoadedCliPlugins << plugin;
}
if (qobject_cast<PluginInterface *>(plugin->instance()) != nullptr) {
mLoadedPluginPlugins << plugin;
}
if (qobject_cast<SolverInterface *>(plugin->instance()) != nullptr) {
mLoadedSolverPlugins << plugin;
}
}
// Initialise the plugins themselves
for (auto plugin : mLoadedPluginPlugins) {
qobject_cast<PluginInterface *>(plugin->instance())->initializePlugin();
}
// Let our various plugins know that all of them have been initialised
for (auto plugin : mLoadedPluginPlugins) {
qobject_cast<PluginInterface *>(plugin->instance())->pluginsInitialized(mPluginManager->loadedPlugins());
}
// Retrieve the settings of our various plugins
QSettings settings;
settings.beginGroup(SettingsPlugins);
for (auto plugin : mLoadedPluginPlugins) {
settings.beginGroup(plugin->name());
qobject_cast<PluginInterface *>(plugin->instance())->loadSettings(settings);
settings.endGroup();
}
settings.endGroup();
}
//==============================================================================
void CliApplication::includePlugins(const QStringList &pPluginNames,
bool pInclude) const
{
// Retrieve all the plugins that are available
QString pluginsDir = QCoreApplication::libraryPaths().first()+"/"+qAppName();
QFileInfoList fileInfoList = QDir(pluginsDir).entryInfoList(QStringList("*"+PluginExtension), QDir::Files);
QStringList availablePluginNames = QStringList();
for (const auto &fileInfo : fileInfoList) {
availablePluginNames << Plugin::name(fileInfo.canonicalFilePath());
}
// Include/exclude the given plugins to/from the GUI version of OpenCOR,
// after having sorted them and made sure that they actually exist
QStringList pluginNames = pPluginNames;
std::sort(pluginNames.begin(), pluginNames.end());
for (const auto &pluginName : pluginNames) {
QString status;
if (availablePluginNames.contains(pluginName)) {
// Make sure that the plugin is selectable before including/excluding it
QString errorMessage;
PluginInfo *pluginInfo = Plugin::info(Plugin::fileName(pluginsDir, pluginName), &errorMessage);
if (pluginInfo != nullptr) {
if (pluginInfo->isSelectable()) {
Plugin::setLoad(pluginName, pInclude);
status = QString("%1 the GUI version of OpenCOR").arg(pInclude?
"included to":
"excluded from");
} else {
status = QString("cannot be directly %1").arg(pInclude?
"included":
"excluded");
}
} else {
status = QString("plugin information not found%1").arg(errorMessage.isEmpty()?
QString():
QString(" (%1)").arg(errorMessage));
}
} else {
status = "unknown plugin";
}
std::cout << " - " << pluginName.toStdString() << ": " << status.toStdString() << "." << std::endl;
}
}
//==============================================================================
void CliApplication::about() const
{
// Output some information about OpenCOR
version();
std::cout << prettyProductName().toStdString() << std::endl;
std::cout << copyright().toStdString() << std::endl;
std::cout << std::endl;
std::cout << applicationDescription(false).toStdString() << std::endl;
}
//==============================================================================
bool CliApplication::command(const QString &pCommand,
const QStringList &pArguments, int &pRes) const
{
// Determine whether the command is to be executed by all the CLI plugins or
// only a given CLI plugin
static const QString CommandSeparator = "::";
QString commandName = pCommand;
QString commandPlugin = commandName;
int commandSeparatorPosition = commandName.indexOf(CommandSeparator);
if (commandSeparatorPosition != -1) {
commandPlugin = commandPlugin.remove(commandSeparatorPosition, commandName.length()-commandSeparatorPosition);
commandName = commandName.remove(0, commandPlugin.length()+CommandSeparator.length());
} else {
commandName = QString();
}
// Make sure that the plugin to which the command is to be sent exists
if (!commandPlugin.isEmpty()) {
bool pluginFound = false;
bool pluginHasCliSupport = false;
for (auto plugin : mPluginManager->loadedPlugins()) {
if (commandPlugin == plugin->name()) {
pluginFound = true;
pluginHasCliSupport = qobject_cast<CliInterface *>(plugin->instance()) != nullptr;
break;
}
}
if (!pluginFound) {
std::cout << "The " << commandPlugin.toStdString() << " plugin could not be found." << std::endl;
pRes = -1;
return true;
}
if (!pluginHasCliSupport) {
std::cout << "The " << commandPlugin.toStdString() << " plugin does not support the execution of commands." << std::endl;
pRes = -1;
return true;
}
}
// Make sure that we have at least one CLI-enabled plugin
if (mLoadedCliPlugins.isEmpty()) {
std::cout << "No plugins could be found to run the command." << std::endl;
pRes = -1;
return true;
}
// Send the command to the plugin(s)
bool firstPlugin = true;
for (auto plugin : mLoadedCliPlugins) {
if ( commandPlugin.isEmpty()
|| (commandPlugin == plugin->name())) {
if (firstPlugin) {
firstPlugin = false;
} else {
std::cout << std::endl;
}
if (!qobject_cast<CliInterface *>(plugin->instance())->executeCommand(commandName, pArguments, pRes)) {
pRes = -1;
}
}
}
return true;
}
//==============================================================================
void CliApplication::exclude(const QStringList &pPluginNames) const
{
// Exclude the given plugins from the GUI version of OpenCOR
includePlugins(pPluginNames, false);
}
//==============================================================================
void CliApplication::help() const
{
// Output some help
std::cout << "Usage: " << qAppName().toStdString()
<< " [-a|--about] [-c|--command [<plugin>]::<command> [<argument> ...]] [-e|--exclude <plugins>] [-h|--help] [-i|--include <plugins>] [-p|--plugins] [-r|--reset] [-s|--status] [-v|--version] [<files>]"
<< std::endl;
std::cout << " -a, --about Display some information about OpenCOR"
<< std::endl;
std::cout << " -c, --command Send a command to one or all the CLI plugins"
<< std::endl;
std::cout << " -e, --exclude Exclude the given plugin(s)"
<< std::endl;
std::cout << " -h, --help Display this help information"
<< std::endl;
std::cout << " -i, --include Include the given plugin(s)"
<< std::endl;
std::cout << " -p, --plugins Display all the CLI plugins"
<< std::endl;
std::cout << " -r, --reset Reset all your settings"
<< std::endl;
std::cout << " -s, --status Display the status of all the plugins"
<< std::endl;
std::cout << " -v, --version Display the version of OpenCOR"
<< std::endl;
}
//==============================================================================
void CliApplication::include(const QStringList &pPluginNames) const
{
// Include the given plugins to the GUI version of OpenCOR
includePlugins(pPluginNames);
}
//==============================================================================
void CliApplication::plugins() const
{
// Output some information about our CLI plugins, so first make sure that we
// have at least one of them
if (mLoadedCliPlugins.isEmpty()) {
std::cout << "No CLI plugins could be found." << std::endl;
return;
}
// First, we retrieve all the CLI plugins information
QStringList pluginsInfo = QStringList();
for (auto plugin : mLoadedCliPlugins) {
// Retrieve the CLI plugin and its default description
QString pluginInfo = plugin->name();
QString pluginDesc = plainString(plugin->info()->description());
if (!pluginDesc.isEmpty()) {
pluginInfo += ": "+pluginDesc;
}
// Add the plugin information to our list
pluginsInfo << pluginInfo;
}
// Now, we can output the plugin information in alphabetical order
pluginsInfo.sort(Qt::CaseInsensitive);
if (pluginsInfo.count() == 1) {
std::cout << "The following CLI plugin is available:" << std::endl;
} else {
std::cout << "The following CLI plugins are available:" << std::endl;
}
for (const auto &pluginInfo : pluginsInfo) {
std::cout << " - " << pluginInfo.toStdString() << std::endl;
}
}
//==============================================================================
void CliApplication::reset() const
{
QSettings settings;
settings.clear();
std::cout << "All your settings have been reset." << std::endl;
}
//==============================================================================
void CliApplication::status() const
{
// Output the status of all the plugins that should (have) normally be(en)
// loaded, so first make sure that we have at least one of them
if (mPluginManager->loadedPlugins().isEmpty()) {
std::cout << "No plugins could be found." << std::endl;
return;
}
// First, we retrieve all the plugins information
QStringList pluginsInfo = QStringList();
for (auto plugin : mPluginManager->plugins()) {
// Retrieve the plugin and its status
QString pluginInfo = plugin->name()+": ";
// Retrieve the plugin's status
switch (plugin->status()) {
case Plugin::Status::NotWanted:
pluginInfo += "the plugin is not wanted.";
break;
case Plugin::Status::NotNeeded:
pluginInfo += "the plugin is not needed.";
break;
case Plugin::Status::Loaded:
pluginInfo += "the plugin is loaded and fully functional.";
break;
case Plugin::Status::NotLoaded:
pluginInfo += QString("the plugin could not be loaded due to the following problem: %1.").arg(formatMessage(plugin->statusErrors()));
break;
case Plugin::Status::NotPlugin:
pluginInfo += "this is not a plugin.";
break;
case Plugin::Status::OldPlugin:
pluginInfo += "the plugin could not be loaded (one or several of the interfaces it supports are too old).";
break;
case Plugin::Status::NotCorePlugin:
pluginInfo += "the plugin claims to be the core plugin, but it is not.";
break;
case Plugin::Status::InvalidCorePlugin:
pluginInfo += "the plugin claims to be the core plugin, but it does not support the core interface.";
break;
case Plugin::Status::NotCliPluginNoCliSupport:
pluginInfo += "the plugin supports the CLI interface, but it does not claim to be CLI-capable.";
break;
case Plugin::Status::NotCliPluginNoCliInterface:
pluginInfo += "the plugin claims to be CLI-capable, but it does not support the CLI interface.";
break;
case Plugin::Status::MissingOrInvalidDependencies:
if (plugin->statusErrorsCount() == 1) {
pluginInfo += QString("the plugin could not be loaded due to the %1 plugin being missing or invalid.").arg(plugin->statusErrors());
} else {
pluginInfo += QString("the plugin could not be loaded due to missing or invalid plugins:\n%1").arg(plugin->statusErrors());
}
break;
}
// Add the plugin information to our list
pluginsInfo << pluginInfo;
}
// Now, we can output the plugin information in alphabetical order
pluginsInfo.sort(Qt::CaseInsensitive);
if (pluginsInfo.count() == 1) {
std::cout << "The following plugin is available:" << std::endl;
} else {
std::cout << "The following plugins are available:" << std::endl;
}
for (const auto &pluginInfo : pluginsInfo) {
std::cout << " - " << pluginInfo.toStdString() << std::endl;
}
}
//==============================================================================
void CliApplication::version() const
{
// Output the version of OpenCOR
std::cout << OpenCOR::version().toStdString() << std::endl;
}
//==============================================================================
bool CliApplication::run(int &pRes)
{
// See what needs doing with the CLI options, if anything
pRes = 0; // By default, everything is fine
enum Option {
NoOption,
AboutOption,
CommandOption,
ExcludeOption,
HelpOption,
IncludeOption,
PluginsOption,
ResetOption,
StatusOption,
VersionOption
};
Option option = NoOption;
QStringList appArguments = qApp->arguments();
QStringList arguments = QStringList();
appArguments.removeFirst();
// Note: we remove the first argument since it corresponds to the full path
// to our executable, which we are not interested in...
static const QString A = "-a"; static const QString About = "--about";
static const QString C = "-c"; static const QString Command = "--command";
static const QString E = "-e"; static const QString Exclude = "--exclude";
static const QString H = "-h"; static const QString Help = "--help";
static const QString I = "-i"; static const QString Include = "--include";
static const QString P = "-p"; static const QString Plugins = "--plugins";
static const QString R = "-r"; static const QString Reset = "--reset";
static const QString S = "-s"; static const QString Status = "--status";
static const QString V = "-v"; static const QString Version = "--version";
for (const auto &appArgument : appArguments) {
if ( (option == CommandOption)
|| (option == ExcludeOption)
|| (option == IncludeOption)) {
// Keep track of the arguments passed to a command, an exclude or an
// include call
arguments << appArgument;
} else if ((appArgument == A) || (appArgument == About)) {
if (option == NoOption) {
option = AboutOption;
} else {
pRes = -1;
}
} else if ((appArgument == C) || (appArgument == Command)) {
if (option == NoOption) {
option = CommandOption;
} else {
pRes = -1;
}
} else if ((appArgument == E) || (appArgument == Exclude)) {
if (option == NoOption) {
option = ExcludeOption;
} else {
pRes = -1;
}
} else if ((appArgument == H) || (appArgument == Help)) {
if (option == NoOption) {
option = HelpOption;
} else {
pRes = -1;
}
} else if ((appArgument == I) || (appArgument == Include)) {
if (option == NoOption) {
option = IncludeOption;
} else {
pRes = -1;
}
} else if ((appArgument == P) || (appArgument == Plugins)) {
if (option == NoOption) {
option = PluginsOption;
} else {
pRes = -1;
}
} else if ((appArgument == R) || (appArgument == Reset)) {
if (option == NoOption) {
option = ResetOption;
} else {
pRes = -1;
}
} else if ((appArgument == S) || (appArgument == Status)) {
if (option == NoOption) {
option = StatusOption;
} else {
pRes = -1;
}
} else if ((appArgument == V) || (appArgument == Version)) {
if (option == NoOption) {
option = VersionOption;
} else {
pRes = -1;
}
} else if (appArgument.startsWith('-')) {
// The user provided at least one unknown option
pRes = -1;
break;
}
}
// Handle the option the user requested, if any
if (pRes == 0) {
switch (option) {
case NoOption:
return false;
case AboutOption:
if (appArguments.count() != 1) {
pRes = -1;
help();
} else {
about();
}
break;
case CommandOption:
// Make sure that we have at least one argument (which would be the
// command itself) before loading the plugins and then sending the
// command to the plugin(s)
if (arguments.isEmpty()) {
pRes = -1;
help();
} else {
loadPlugins();
QString command = arguments.first();
arguments.removeFirst();
if (!CliApplication::command(command, arguments, pRes)) {
pRes = -1;
help();
}
}
break;
case ExcludeOption:
// Make sure that we have at least one argument (i.e. the name of a
// plugin)
if (arguments.isEmpty()) {
pRes = -1;
help();
} else {
exclude(arguments);
}
break;
case HelpOption:
if (appArguments.count() != 1) {
pRes = -1;
help();
} else {
help();
}
break;
case IncludeOption:
// Make sure that we have at least one argument (i.e. the name of a
// plugin)
if (arguments.isEmpty()) {
pRes = -1;
help();
} else {
include(arguments);
}
break;
case PluginsOption:
if (appArguments.count() != 1) {
pRes = -1;
help();
} else {
loadPlugins();
plugins();
}
break;
case ResetOption:
if (appArguments.count() != 1) {
pRes = -1;
help();
} else {
reset();
}
break;
case StatusOption:
if (appArguments.count() != 1) {
pRes = -1;
help();
} else {
loadPlugins();
status();
}
break;
case VersionOption:
if (appArguments.count() != 1) {
pRes = -1;
help();
} else {
version();
}
break;
}
} else {
help();
}
return true;
}
//==============================================================================
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================