1- // Action.cpp: implementation of the Action class .
1+ // This file is part of Myoddweb.Piger .
22//
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>.
515#include " stdafx.h"
616#include " Action.h"
717#include " ActionMonitor.h"
818#include " activeaction.h"
919#include " activepythonaction.h"
10- #include " activeluaaction.h"
11- #include " activepluginaction.h"
20+ #include " ActiveVirtualMachineAction.h"
1221#include " activedefaultaction.h"
1322#include " activebatchaction.h"
1423#include " activecmdaction.h"
1524#include " activecomaction.h"
1625#include " activeexeaction.h"
17- #include " activeshellAction.h"
18-
19- #include " os/os.h"
20- #include " ActivePowershellAction.h"
21- #include " ActiveCsAction.h"
2226
2327/* *
2428 * \brief Constructor
29+ * \param application the application manager.
2530 */
26- Action::Action ()
31+ Action::Action (IApplication& application) :
32+ _application(application)
2733{
2834 Reset ();
2935}
3036
3137/* *
3238 * \brief Constructor
39+ * \param application the application manager.
3340 * \param szCommand the name of the action, (the the user will enter)
3441 * \param szPath the full path of the action that we will execute.
3542 * \return none
3643 */
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)
3846{
3947 if (szCommand.length () == 0 )
4048 {
4149 // we cannot have nullptr commands.
4250 throw -1 ;
4351 }
4452
45- // reset everything
46- Reset ();
47-
4853 // set the command and make sure it is valid.
4954 _szCommand = szCommand;
5055 myodd::strings::Trim (_szCommand);
@@ -79,9 +84,9 @@ Action::Action(const std::wstring& szCommand, const std::wstring& szPath )
7984 * @version 0.1
8085 * @return none
8186 */
82- Action::Action ( const Action&action)
87+ Action::Action ( const Action&action) :
88+ Action( action._application )
8389{
84- Reset ();
8590 *this = action;
8691}
8792
@@ -121,12 +126,13 @@ void Action::SetCommandPath(const std::wstring& szPath )
121126
122127/* *
123128 * \brief Run the command, we take into account the current selection and command parameters given.
129+ * \param virtualMachines the virtual machine.
124130 * \param pWnd the last foreground window.
125131 * \param szCommandLine the command line argument.
126132 * \param isPrivileged if we need administrator privilege to run this.
127133 * \return BOOL true.
128134 */
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
130136{
131137 // not sure how to do that...
132138 if ( Len () == 0 )
@@ -141,7 +147,7 @@ ActiveAction* Action::CreateActiveAction(IVirtualMachines& virtualMachines, CWnd
141147 // if we are here then we are going to load a user command
142148 //
143149 // If the user did not pass any arguments/command line then we must get them from the clipboard.
144- ActiveAction * aa;
150+ IActiveAction * aa;
145151 if ( szCommandLine.length () == 0 )
146152 {
147153 aa = CreateActiveActionWithNoCommandLine ( virtualMachines, pWnd, isPrivileged);
@@ -154,17 +160,17 @@ ActiveAction* Action::CreateActiveAction(IVirtualMachines& virtualMachines, CWnd
154160
155161 // now that we are back from calling the plugin, restore the keyboard state.
156162 hook_RejectKeyboad ( bThen );
157-
158163 return aa;
159164}
160165
161166/* *
162167 * \brief Try and do it when we have no command line
168+ * \param virtualMachines
163169 * \param pWnd the last foreground window.
164170 * \param isPrivileged if this action is privileged or not.
165171 * \return bool success or not.
166172 */
167- ActiveAction * Action::CreateActiveActionWithNoCommandLine (IVirtualMachines& virtualMachines, CWnd* pWnd, const bool isPrivileged ) const
173+ IActiveAction * Action::CreateActiveActionWithNoCommandLine (IVirtualMachines& virtualMachines, CWnd* pWnd, const bool isPrivileged ) const
168174{
169175 // the command line we will try and make.
170176 std::wstring szCommandLine = L" " ;
@@ -183,7 +189,7 @@ ActiveAction* Action::CreateActiveActionWithNoCommandLine(IVirtualMachines& virt
183189 // there will probably only be a conflict with explorer, (of any flavor)
184190 // that could copy text and/or file names.
185191 //
186- const auto foregroundWnd = CActionMonitorApp:: GetLastForegroundWindow ();
192+ const auto foregroundWnd = _application. GetLastForegroundWindow ();
187193 Clipboard clipboard (foregroundWnd, maxClipboardMemory );
188194
189195 // any other values are rejected, (bitmaps and so on).
@@ -256,118 +262,16 @@ const std::wstring& Action::Command() const
256262 return _szCommand;
257263}
258264
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-
362265/* *
363266 * \brief Launch a single action with all the command line arguments.
364267 * 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.
366270 * \param szCommandLine the command and the arguments we are launching this file with.
367271 * \param isPrivileged if we need administrator privilege to run this.
368272 * \return TRUE|FALSE success or not.
369273 */
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
371275{
372276 // sanity check
373277 if (0 == _szFile.length ())
@@ -383,7 +287,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
383287 //
384288 if (LuaVirtualMachine::IsExt (_szFile ))
385289 {
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);
387292 if (ala->Initialize ())
388293 {
389294 return ala;
@@ -399,7 +304,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
399304 //
400305 if (PythonVirtualMachine::IsExt (_szFile))
401306 {
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);
403309 if (apa->Initialize () )
404310 {
405311 return apa;
@@ -415,7 +321,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
415321 //
416322 if (PowershellVirtualMachine::IsExt (_szFile))
417323 {
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);
419326 if (apa->Initialize ())
420327 {
421328 return apa;
@@ -431,7 +338,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
431338 //
432339 if (ShellVirtualMachine::IsExt (_szFile))
433340 {
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);
435343 if (asa->Initialize ())
436344 {
437345 return asa;
@@ -447,7 +355,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
447355 //
448356 if (CsVirtualMachine::IsExt (_szFile))
449357 {
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);
451360 if (acsa->Initialize ())
452361 {
453362 return acsa;
@@ -463,7 +372,8 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
463372 //
464373 if (PluginVirtualMachine::IsExt (_szFile))
465374 {
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);
467377 if (apa->Initialize ())
468378 {
469379 return apa;
@@ -477,21 +387,21 @@ ActiveAction* Action::CreateActiveActionDirect(IVirtualMachines& virtualMachines
477387 // Batch files...
478388 if ( myodd::files::IsExtension (_szFile, _T (" bat" )))
479389 {
480- return new ActiveBatchAction (* this , virtualMachines , hTopHWnd, szCommandLine );
390+ return new ActiveBatchAction (_application, * this , hTopHWnd, szCommandLine );
481391 }
482392 if (myodd::files::IsExtension (_szFile, _T (" cmd" )))
483393 {
484- return new ActiveCmdAction (* this , virtualMachines , hTopHWnd, szCommandLine);
394+ return new ActiveCmdAction (_application, * this , hTopHWnd, szCommandLine);
485395 }
486396 if (myodd::files::IsExtension (_szFile, _T (" com" )))
487397 {
488- return new ActiveComAction (* this , virtualMachines , hTopHWnd, szCommandLine);
398+ return new ActiveComAction (_application, * this , hTopHWnd, szCommandLine);
489399 }
490400 if (myodd::files::IsExtension (_szFile, _T (" exe" )))
491401 {
492- return new ActiveExeAction (* this , virtualMachines , hTopHWnd, szCommandLine, isPrivileged);
402+ return new ActiveExeAction (_application, * this , hTopHWnd, szCommandLine, isPrivileged);
493403 }
494404
495405 // run the default action.
496- return new ActiveDefaultAction ( *this , virtualMachines , hTopHWnd, szCommandLine, isPrivileged );
406+ return new ActiveDefaultAction (_application, *this , hTopHWnd, szCommandLine, isPrivileged );
497407}
0 commit comments