summaryrefslogtreecommitdiff
path: root/shared/shell_execute.h
blob: 2cabbed1b03ee66dda40cf0f26b0119aeb7df77d (plain)
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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#ifndef EXECUTE_HEADER_23482134578134134
#define EXECUTE_HEADER_23482134578134134

#include <wx/string.h>
#include <wx/msgdlg.h>

#ifdef FFS_WIN
#include "string_tools.h"
#include "last_error.h"
#include "i18n.h"
#include <wx/msw/wrapwin.h> //includes "windows.h"

#elif defined FFS_LINUX
#include <wx/utils.h>
#endif


namespace zen
{
//launch commandline and report errors via popup dialog
//windows: COM needs to be initialized before calling this function!
namespace
{
enum ExecutionType
{
    EXEC_TYPE_SYNC,
    EXEC_TYPE_ASYNC
};

void shellExecute(const wxString& command, ExecutionType type = EXEC_TYPE_ASYNC)
{
#ifdef FFS_WIN
    //parse commandline
    std::vector<std::wstring> argv;
    {
        int argc = 0;
        LPWSTR* tmp = ::CommandLineToArgvW(command.c_str(), &argc);
        for (int i = 0; i < argc; ++i)
            argv.push_back(tmp[i]);
        ::LocalFree(tmp);
    }

    wxString filename;
    wxString arguments;
    if (!argv.empty())
    {
        filename = argv[0];
        for (std::vector<std::wstring>::const_iterator i = argv.begin() + 1; i != argv.end(); ++i)
            arguments += (i != argv.begin() ? L" " : L"") +
                         (i->empty() || std::find_if(i->begin(), i->end(), &cStringIsWhiteSpace<wchar_t>) != i->end() ? L"\"" + *i + L"\"" : *i);
    }

    SHELLEXECUTEINFO execInfo = {};
    execInfo.cbSize       = sizeof(execInfo);

    //SEE_MASK_NOASYNC is equal to SEE_MASK_FLAG_DDEWAIT, but former is defined not before Win SDK 6.0
    execInfo.fMask        = type == EXEC_TYPE_SYNC ? (SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_DDEWAIT) : 0; //don't use SEE_MASK_ASYNCOK -> returns successful despite errors!
    execInfo.fMask       |= SEE_MASK_UNICODE | SEE_MASK_FLAG_NO_UI; //::ShellExecuteEx() shows a non-blocking pop-up dialog on errors -> we want a blocking one
    execInfo.lpVerb       = L"open";
    execInfo.lpFile       = filename.c_str();
    execInfo.lpParameters = arguments.c_str();
    execInfo.nShow        = SW_SHOWNORMAL;

    if (!::ShellExecuteEx(&execInfo)) //__inout  LPSHELLEXECUTEINFO lpExecInfo
    {
        wxString errorMsg = _("Invalid commandline: \"%x\"");
        wxString cmdFmt = wxString(L"\nFile: ") + filename + L"\nArg: " + arguments;

        errorMsg.Replace(L"%x", cmdFmt);
        wxMessageBox(errorMsg + L"\n\n" + getLastErrorFormatted());
        return;
    }

    if (type == EXEC_TYPE_SYNC)
    {
        if (execInfo.hProcess != 0)
        {
            ::WaitForSingleObject(execInfo.hProcess, INFINITE);
            ::CloseHandle(execInfo.hProcess);
        }
    }

#elif defined FFS_LINUX
    //by default wxExecute uses a zero sized dummy window as a hack to keep focus which leaves a useless empty icon in ALT-TAB list
    //=> use wxEXEC_NODISABLE and roll our own window disabler!                   (see comment in  app.cpp: void *wxGUIAppTraits::BeforeChildWaitLoop())
    wxWindowDisabler dummy; //disables all top level windows
    wxExecute(command, (type == EXEC_TYPE_ASYNC ? wxEXEC_ASYNC : wxEXEC_SYNC) | wxEXEC_NODISABLE);
#endif
}
}
}

#endif //EXECUTE_HEADER_23482134578134134
bgstack15