File: processinvoker.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (201 lines) | stat: -rw-r--r-- 6,608 bytes parent folder | download
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
#include "processinvoker.hpp"

#include <QCoreApplication>
#include <QDir>
#include <QMessageBox>
#include <QString>
#include <QStringList>

Process::ProcessInvoker::ProcessInvoker(QObject* parent)
    : QObject(parent)
{
    mProcess = new QProcess(this);

    connect(mProcess, &QProcess::errorOccurred, this, &ProcessInvoker::processError);

    connect(
        mProcess, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &ProcessInvoker::processFinished);

    mName = QString();
    mArguments = QStringList();
}

Process::ProcessInvoker::~ProcessInvoker() {}

// void Process::ProcessInvoker::setProcessName(const QString &name)
//{
//     mName = name;
// }

// void Process::ProcessInvoker::setProcessArguments(const QStringList &arguments)
//{
//     mArguments = arguments;
// }

QProcess* Process::ProcessInvoker::getProcess()
{
    return mProcess;
}

// QString Process::ProcessInvoker::getProcessName()
//{
//     return mName;
// }

// QStringList Process::ProcessInvoker::getProcessArguments()
//{
//     return mArguments;
// }

bool Process::ProcessInvoker::startProcess(const QString& name, const QStringList& arguments, bool detached)
{
    //    mProcess = new QProcess(this);
    mName = name;
    mArguments = arguments;
    mIgnoreErrors = false;

    QString path(name);
#ifdef Q_OS_WIN
    path.append(QLatin1String(".exe"));
#endif
    QDir dir(QCoreApplication::applicationDirPath());
    path = dir.absoluteFilePath(path);

    QFileInfo info(path);

    if (!info.exists())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle(tr("Error starting executable"));
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setText(
            tr("<html><head/><body><p><b>Could not find %1</b></p>"
               "<p>The application is not found.</p>"
               "<p>Please make sure OpenMW is installed correctly and try again.</p></body></html>")
                .arg(info.fileName()));
        msgBox.exec();
        return false;
    }

    if (!info.isExecutable())
    {
        QMessageBox msgBox;
        msgBox.setWindowTitle(tr("Error starting executable"));
        msgBox.setIcon(QMessageBox::Warning);
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setText(
            tr("<html><head/><body><p><b>Could not start %1</b></p>"
               "<p>The application is not executable.</p>"
               "<p>Please make sure you have the right permissions and try again.</p></body></html>")
                .arg(info.fileName()));
        msgBox.exec();
        return false;
    }

    // Start the executable
    if (detached)
    {
        if (!mProcess->startDetached(path, arguments))
        {
            QMessageBox msgBox;
            msgBox.setWindowTitle(tr("Error starting executable"));
            msgBox.setIcon(QMessageBox::Critical);
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.setText(
                tr("<html><head/><body><p><b>Could not start %1</b></p>"
                   "<p>An error occurred while starting %1.</p>"
                   "<p>Press \"Show Details...\" for more information.</p></body></html>")
                    .arg(info.fileName()));
            msgBox.setDetailedText(mProcess->errorString());
            msgBox.exec();
            return false;
        }
    }
    else
    {
        mProcess->start(path, arguments);

        /*
        if (!mProcess->waitForFinished()) {
            QMessageBox msgBox;
            msgBox.setWindowTitle(tr("Error starting executable"));
            msgBox.setIcon(QMessageBox::Critical);
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.setText(tr("<html><head/><body><p><b>Could not start %1</b></p> \
                              <p>An error occurred while starting %1.</p> \
                              <p>Press \"Show Details...\" for more
        information.</p></body></html>").arg(info.fileName())); msgBox.setDetailedText(mProcess->errorString());
            msgBox.exec();

            return false;
        }

        if (mProcess->exitCode() != 0 || mProcess->exitStatus() == QProcess::CrashExit) {
            QString error(mProcess->readAllStandardError());
            error.append(tr("\nArguments:\n"));
            error.append(arguments.join(" "));

            QMessageBox msgBox;
            msgBox.setWindowTitle(tr("Error running executable"));
            msgBox.setIcon(QMessageBox::Critical);
            msgBox.setStandardButtons(QMessageBox::Ok);
            msgBox.setText(tr("<html><head/><body><p><b>Executable %1 returned an error</b></p> \
                              <p>An error occurred while running %1.</p> \
                              <p>Press \"Show Details...\" for more
        information.</p></body></html>").arg(info.fileName())); msgBox.setDetailedText(error); msgBox.exec();

            return false;
        }
        */
    }

    return true;
}

void Process::ProcessInvoker::processError(QProcess::ProcessError error)
{
    if (mIgnoreErrors)
        return;
    QMessageBox msgBox;
    msgBox.setWindowTitle(tr("Error running executable"));
    msgBox.setIcon(QMessageBox::Critical);
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.setText(
        tr("<html><head/><body><p><b>Executable %1 returned an error</b></p>"
           "<p>An error occurred while running %1.</p>"
           "<p>Press \"Show Details...\" for more information.</p></body></html>")
            .arg(mName));
    msgBox.setDetailedText(mProcess->errorString());
    msgBox.exec();
}

void Process::ProcessInvoker::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (exitCode != 0 || exitStatus == QProcess::CrashExit)
    {
        if (mIgnoreErrors)
            return;
        QString error(mProcess->readAllStandardError());
        error.append(tr("\nArguments:\n"));
        error.append(mArguments.join(" "));

        QMessageBox msgBox;
        msgBox.setWindowTitle(tr("Error running executable"));
        msgBox.setIcon(QMessageBox::Critical);
        msgBox.setStandardButtons(QMessageBox::Ok);
        msgBox.setText(
            tr("<html><head/><body><p><b>Executable %1 returned an error</b></p>"
               "<p>An error occurred while running %1.</p>"
               "<p>Press \"Show Details...\" for more information.</p></body></html>")
                .arg(mName));
        msgBox.setDetailedText(error);
        msgBox.exec();
    }
}

void Process::ProcessInvoker::killProcess()
{
    mIgnoreErrors = true;
    mProcess->kill();
}