1
- // Action.cpp: implementation of the Action class .
1
+ // This file is part of Myoddweb.Piger .
2
2
//
3
- // ////////////////////////////////////////////////////////////////////
4
-
3
+ // Myoddweb.Piger is free software: you can redistribute it and/or modify
4
+ // it under the terms of the GNU General Public License as published by
5
+ // the Free Software Foundation, either version 3 of the License, or
6
+ // (at your option) any later version.
7
+ //
8
+ // Myoddweb.Piger is distributed in the hope that it will be useful,
9
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
11
+ // GNU General Public License for more details.
12
+ //
13
+ // You should have received a copy of the GNU General Public License
14
+ // along with Myoddweb.Piger. If not, see<https://www.gnu.org/licenses/gpl-3.0.en.html>.
5
15
#include " stdafx.h"
6
16
#include " Action.h"
7
17
#include " ActionMonitor.h"
8
18
#include " activeaction.h"
9
19
#include " activepythonaction.h"
10
- #include " activeluaaction.h"
11
- #include " activepluginaction.h"
20
+ #include " ActiveVirtualMachineAction.h"
12
21
#include " activedefaultaction.h"
13
22
#include " activebatchaction.h"
14
23
#include " activecmdaction.h"
15
24
#include " activecomaction.h"
16
25
#include " activeexeaction.h"
17
- #include " activeshellAction.h"
18
-
19
- #include " os/os.h"
20
- #include " ActivePowershellAction.h"
21
- #include " ActiveCsAction.h"
22
26
23
27
/* *
24
28
* \brief Constructor
29
+ * \param application the application manager.
25
30
*/
26
- Action::Action ()
31
+ Action::Action (IApplication& application) :
32
+ _application(application)
27
33
{
28
34
Reset ();
29
35
}
30
36
31
37
/* *
32
38
* \brief Constructor
39
+ * \param application the application manager.
33
40
* \param szCommand the name of the action, (the the user will enter)
34
41
* \param szPath the full path of the action that we will execute.
35
42
* \return none
36
43
*/
37
- Action::Action (const std::wstring& szCommand, const std::wstring& szPath )
44
+ Action::Action (IApplication& application, const std::wstring& szCommand, const std::wstring& szPath ) :
45
+ Action(application)
38
46
{
39
47
if (szCommand.length () == 0 )
40
48
{
41
49
// we cannot have nullptr commands.
42
50
throw -1 ;
43
51
}
44
52
45
- // reset everything
46
- Reset ();
47
-
48
53
// set the command and make sure it is valid.
49
54
_szCommand = szCommand;
50
55
myodd::strings::Trim (_szCommand);
@@ -79,9 +84,9 @@ Action::Action(const std::wstring& szCommand, const std::wstring& szPath )
79
84
* @version 0.1
80
85
* @return none
81
86
*/
82
- Action::Action ( const Action&action)
87
+ Action::Action ( const Action&action) :
88
+ Action( action._application )
83
89
{
84
- Reset ();
85
90
*this = action;
86
91
}
87
92
@@ -121,12 +126,13 @@ void Action::SetCommandPath(const std::wstring& szPath )
121
126
122
127
/* *
123
128
* \brief Run the command, we take into account the current selection and command parameters given.
129
+ * \param virtualMachines the virtual machine.
124
130
* \param pWnd the last foreground window.
125
131
* \param szCommandLine the command line argument.
126
132
* \param isPrivileged if we need administrator privilege to run this.
127
133
* \return BOOL true.
128
134
*/
129
- ActiveAction * Action::CreateActiveAction (IVirtualMachines& virtualMachines, CWnd* pWnd, const std::wstring& szCommandLine, const bool isPrivileged) const
135
+ IActiveAction * Action::CreateActiveAction (IVirtualMachines& virtualMachines, CWnd* pWnd, const std::wstring& szCommandLine, const bool isPrivileged) const
130
136
{
131
137
// not sure how to do that...
132
138
if ( Len () == 0 )
@@ -141,7 +147,7 @@ ActiveAction* Action::CreateActiveAction(IVirtualMachines& virtualMachines, CWnd
141
147
// if we are here then we are going to load a user command
142
148
//
143
149
// If the user did not pass any arguments/command line then we must get them from the clipboard.
144
- ActiveAction * aa;
150
+ IActiveAction * aa;
145
151
if ( szCommandLine.length () == 0 )
146
152
{
147
153
aa = CreateActiveActionWithNoCommandLine ( virtualMachines, pWnd, isPrivileged);
@@ -154,17 +160,17 @@ ActiveAction* Action::CreateActiveAction(IVirtualMachines& virtualMachines, CWnd
154
160
155
161
// now that we are back from calling the plugin, restore the keyboard state.
156
162
hook_RejectKeyboad ( bThen );
157
-
158
163
return aa;
159
164
}
160
165
161
166
/* *
162
167
* \brief Try and do it when we have no command line
168
+ * \param virtualMachines
163
169
* \param pWnd the last foreground window.
164
170
* \param isPrivileged if this action is privileged or not.
165
171
* \return bool success or not.
166
172
*/
167
- ActiveAction * Action::CreateActiveActionWithNoCommandLine (IVirtualMachines& virtualMachines, CWnd* pWnd, const bool isPrivileged ) const
173
+ IActiveAction * Action::CreateActiveActionWithNoCommandLine (IVirtualMachines& virtualMachines, CWnd* pWnd, const bool isPrivileged ) const
168
174
{
169
175
// the command line we will try and make.
170
176
std::wstring szCommandLine = L" " ;
@@ -183,7 +189,7 @@ ActiveAction* Action::CreateActiveActionWithNoCommandLine(IVirtualMachines& virt
183
189
// there will probably only be a conflict with explorer, (of any flavor)
184
190
// that could copy text and/or file names.
185
191
//
186
- const auto foregroundWnd = CActionMonitorApp:: GetLastForegroundWindow ();
192
+ const auto foregroundWnd = _application. GetLastForegroundWindow ();
187
193
Clipboard clipboard (foregroundWnd, maxClipboardMemory );
188
194
189
195
// any other values are rejected, (bitmaps and so on).
@@ -256,118 +262,16 @@ const std::wstring& Action::Command() const
256
262
return _szCommand;
257
263
}
258
264
259
- /* *
260
- * \brief Execute a file.
261
- * We will expend all the environment variables as needed.
262
- * \param argv [0] the file path, [1] the arguments to launch with, (optional).
263
- * \param isPrivileged if we need administrator privilege to run this.
264
- * \param hProcess if this value is not nullptr, we will return the handle of the started process.
265
- * it is then up to the calling application to close this handle when done with it...
266
- * \return bool true|false success or not.
267
- */
268
- bool Action::Execute (const std::vector<std::wstring>& argv, const bool isPrivileged, HANDLE* hProcess)
269
- {
270
- // get the number of arguments.
271
- auto argc = argv.size ();
272
-
273
- // sanity check
274
- if ( argc < 1 || argc > 2 )
275
- {
276
- ASSERT ( 0 ); // wrong number of arguments.
277
- return false ;
278
- }
279
-
280
- LPTSTR argvModule = nullptr ;
281
- LPTSTR argvCmd = nullptr ;
282
-
283
- // get the module name, (what we are running).
284
- // Expand the values that might have been passed.
285
- if ( !myodd::files::ExpandEnvironment ( argv[ 0 ].c_str (), argvModule ) )
286
- {
287
- myodd::log::LogError (_T (" Could not execute statement: Unable to expand command line '%s'" ), argv[0 ].c_str ());
288
- return false ;
289
- }
290
-
291
- // But we might also have a command line item.
292
- if ( 2 == argc )
293
- {
294
- // Expand the values that might have been passed.
295
- if ( !myodd::files::ExpandEnvironment ( argv[ 1 ].c_str (), argvCmd ) )
296
- {
297
- myodd::log::LogError (_T (" Could not execute statement: Unable to expand arguments '%s'" ), argv[1 ].c_str ());
298
- delete [] argvModule;
299
- return false ;
300
- }
301
- }
302
-
303
- //
304
- // ShellExecuteEx
305
- // https://msdn.microsoft.com/en-us/library/windows/desktop/bb759784(v=vs.85).aspx
306
- //
307
- SHELLEXECUTEINFO sei = {};
308
- sei.cbSize = sizeof (sei); // in, required, sizeof of this structure
309
- sei.fMask = SEE_MASK_DEFAULT; // in, SEE_MASK_XXX values
310
- sei.hwnd = nullptr ; // in, optional
311
- sei.lpFile = argvModule; // in, either this value or lpIDList must be specified
312
- sei.lpParameters = argvCmd; // in, optional
313
- sei.lpDirectory = nullptr ; // in, optional
314
- sei.nShow = SW_NORMAL; // in, required
315
- // sei.hInstApp; // out when SEE_MASK_NOCLOSEPROCESS is specified
316
- // sei.lpIDList; // in, valid when SEE_MASK_IDLIST is specified, PCIDLIST_ABSOLUTE, for use with SEE_MASK_IDLIST & SEE_MASK_INVOKEIDLIST
317
- // sei.lpClass; // in, valid when SEE_MASK_CLASSNAME is specified
318
- // sei.hkeyClass; // in, valid when SEE_MASK_CLASSKEY is specified
319
- // sei.dwHotKey; // in, valid when SEE_MASK_HOTKEY is specified
320
- if (isPrivileged == true && !myodd::os::IsElevated () )
321
- {
322
- sei.lpVerb = _T (" runas" ); // in, optional when unspecified the default verb is choosen
323
- }
324
- else
325
- {
326
- // launch as a normal file.
327
- sei.lpVerb = _T (" open" ); // in, optional when unspecified the default verb is choosen
328
- }
329
-
330
- // did the user pass a handle?
331
- // if they did then they want to take ownership of the process and close the handle.
332
- if (hProcess != nullptr )
333
- {
334
- sei.fMask = SEE_MASK_NOCLOSEPROCESS; // in, SEE_MASK_XXX values
335
- }
336
-
337
- // Assume error
338
- auto result = false ;
339
- if (!ShellExecuteEx (&sei))
340
- {
341
- myodd::log::LogError (_T (" Could not execute statement: could not execute '%s'" ), argvModule);
342
- myodd::log::LogError (_T (" Could not execute statement: Last error '%d'" ), ::GetLastError ());
343
- }
344
- else
345
- {
346
- result = true ;
347
- if (hProcess != nullptr )
348
- {
349
- // return the handle.
350
- *hProcess = sei.hProcess ;
351
- }
352
- }
353
-
354
- // clean up the expended variable.
355
- delete [] argvCmd;
356
- delete [] argvModule;
357
-
358
- // return what we found
359
- return result;
360
- }
361
-
362
265
/* *
363
266
* \brief Launch a single action with all the command line arguments.
364
267
* TODO : The API calls ignore the values been passed to them, so we should first check that we have all the values.
365
- * \param pWnd the last forground window.
268
+ * \param virtualMachines the virtual machines manager.
269
+ * \param pWnd the last foreground window.
366
270
* \param szCommandLine the command and the arguments we are launching this file with.
367
271
* \param isPrivileged if we need administrator privilege to run this.
368
272
* \return TRUE|FALSE success or not.
369
273
*/
370
- ActiveAction * Action::CreateActiveActionDirect (IVirtualMachines& virtualMachines, CWnd* pWnd, const std::wstring& szCommandLine, const bool isPrivileged) const
274
+ IActiveAction * Action::CreateActiveActionDirect (IVirtualMachines& virtualMachines, CWnd* pWnd, const std::wstring& szCommandLine, const bool isPrivileged) const
371
275
{
372
276
// sanity check
373
277
if (0 == _szFile.length ())
@@ -383,7 +287,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
383
287
//
384
288
if (LuaVirtualMachine::IsExt (_szFile ))
385
289
{
386
- auto ala = new ActiveLuaAction (*this , virtualMachines, hTopHWnd, szCommandLine, isPrivileged);
290
+ auto & virtualMachine = virtualMachines.Get (IVirtualMachines::Type::Lua);
291
+ auto ala = new ActiveVirtualMachineAction (_application, *this , virtualMachine, hTopHWnd, szCommandLine, isPrivileged);
387
292
if (ala->Initialize ())
388
293
{
389
294
return ala;
@@ -399,7 +304,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
399
304
//
400
305
if (PythonVirtualMachine::IsExt (_szFile))
401
306
{
402
- auto apa = new ActivePythonAction (*this , virtualMachines, hTopHWnd, szCommandLine, isPrivileged);
307
+ auto & virtualMachine = virtualMachines.Get (IVirtualMachines::Type::Python);
308
+ auto apa = new ActivePythonAction (_application, *this , virtualMachine, hTopHWnd, szCommandLine, isPrivileged);
403
309
if (apa->Initialize () )
404
310
{
405
311
return apa;
@@ -415,7 +321,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
415
321
//
416
322
if (PowershellVirtualMachine::IsExt (_szFile))
417
323
{
418
- auto apa = new ActivePowershellAction (*this , virtualMachines, hTopHWnd, szCommandLine, isPrivileged);
324
+ auto & virtualMachine = virtualMachines.Get (IVirtualMachines::Type::Powershell);
325
+ auto apa = new ActiveVirtualMachineAction (_application, *this , virtualMachine, hTopHWnd, szCommandLine, isPrivileged);
419
326
if (apa->Initialize ())
420
327
{
421
328
return apa;
@@ -431,7 +338,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
431
338
//
432
339
if (ShellVirtualMachine::IsExt (_szFile))
433
340
{
434
- auto asa = new ActiveShellAction (*this , virtualMachines, hTopHWnd, szCommandLine, isPrivileged);
341
+ auto & virtualMachine = virtualMachines.Get (IVirtualMachines::Type::Shell);
342
+ auto asa = new ActiveVirtualMachineAction (_application, *this , virtualMachine, hTopHWnd, szCommandLine, isPrivileged);
435
343
if (asa->Initialize ())
436
344
{
437
345
return asa;
@@ -447,7 +355,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
447
355
//
448
356
if (CsVirtualMachine::IsExt (_szFile))
449
357
{
450
- auto acsa = new ActiveCsAction (*this , virtualMachines, hTopHWnd, szCommandLine, isPrivileged);
358
+ auto & virtualMachine = virtualMachines.Get (IVirtualMachines::Type::CSharp);
359
+ auto acsa = new ActiveVirtualMachineAction (_application, *this , virtualMachine, hTopHWnd, szCommandLine, isPrivileged);
451
360
if (acsa->Initialize ())
452
361
{
453
362
return acsa;
@@ -463,7 +372,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
463
372
//
464
373
if (PluginVirtualMachine::IsExt (_szFile))
465
374
{
466
- auto apa = new ActivePluginAction (*this , virtualMachines, hTopHWnd, szCommandLine, isPrivileged);
375
+ auto & virtualMachine = virtualMachines.Get (IVirtualMachines::Type::LegacyPlugin);
376
+ auto apa = new ActiveVirtualMachineAction (_application, *this , virtualMachine, hTopHWnd, szCommandLine, isPrivileged);
467
377
if (apa->Initialize ())
468
378
{
469
379
return apa;
@@ -477,21 +387,21 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
477
387
// Batch files...
478
388
if ( myodd::files::IsExtension (_szFile, _T (" bat" )))
479
389
{
480
- return new ActiveBatchAction (* this , virtualMachines , hTopHWnd, szCommandLine );
390
+ return new ActiveBatchAction (_application, * this , hTopHWnd, szCommandLine );
481
391
}
482
392
if (myodd::files::IsExtension (_szFile, _T (" cmd" )))
483
393
{
484
- return new ActiveCmdAction (* this , virtualMachines , hTopHWnd, szCommandLine);
394
+ return new ActiveCmdAction (_application, * this , hTopHWnd, szCommandLine);
485
395
}
486
396
if (myodd::files::IsExtension (_szFile, _T (" com" )))
487
397
{
488
- return new ActiveComAction (* this , virtualMachines , hTopHWnd, szCommandLine);
398
+ return new ActiveComAction (_application, * this , hTopHWnd, szCommandLine);
489
399
}
490
400
if (myodd::files::IsExtension (_szFile, _T (" exe" )))
491
401
{
492
- return new ActiveExeAction (* this , virtualMachines , hTopHWnd, szCommandLine, isPrivileged);
402
+ return new ActiveExeAction (_application, * this , hTopHWnd, szCommandLine, isPrivileged);
493
403
}
494
404
495
405
// run the default action.
496
- return new ActiveDefaultAction ( *this , virtualMachines , hTopHWnd, szCommandLine, isPrivileged );
406
+ return new ActiveDefaultAction (_application, *this , hTopHWnd, szCommandLine, isPrivileged );
497
407
}
0 commit comments