qa: Modernize invocation of QProcess in ExecWrapper
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 12 Mar 2021 14:26:56 +0000 (15:26 +0100)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sat, 20 Mar 2021 14:55:17 +0000 (15:55 +0100)
Don't use deprecated API for QProcess in ExecWrapper for starting
a script; arguments should be given as a QStringList instead of
a QString. Modernize and improve the parsing of the command line
by switching to QRegularExpression from QRegExp.

Resolves a deprecation warning that was introduced in Qt 5.15.

src/client/execwrapper.cpp

index ee0e864..7eac352 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <QFile>
 #include <QTextCodec>
+#include <QRegularExpression>
 
 #include "client.h"
 #include "messagemodel.h"
@@ -47,28 +48,32 @@ ExecWrapper::ExecWrapper(QObject* parent)
 void ExecWrapper::start(const BufferInfo& info, const QString& command)
 {
     _bufferInfo = info;
-    QString params;
+    _scriptName.clear();
 
-    QRegExp rx(R"(^\s*(\S+)(\s+(.*))?$)");
-    if (!rx.exactMatch(command)) {
+    QStringList params;
+
+    static const QRegularExpression rx{R"(^\s*(\S+)(\s+(.*))?$)"};
+    auto match = rx.match(command);
+    if (!match.hasMatch()) {
         emit error(tr("Invalid command string for /exec: %1").arg(command));
     }
     else {
-        _scriptName = rx.cap(1);
-        params = rx.cap(3);
+        _scriptName = match.captured(1);
+        static const QRegularExpression splitRx{"\\s+"};
+        params = match.captured(3).split(splitRx, QString::SkipEmptyParts);
     }
 
     // Make sure we don't execute something outside a script dir
-    if (_scriptName.contains("../") || _scriptName.contains("..\\"))
+    if (_scriptName.contains("../") || _scriptName.contains("..\\")) {
         emit error(tr(R"(Name "%1" is invalid: ../ or ..\ are not allowed!)").arg(_scriptName));
-
-    else {
+    }
+    else if (!_scriptName.isEmpty()) {
         foreach (QString scriptDir, Quassel::scriptDirPaths()) {
             QString fileName = scriptDir + _scriptName;
             if (!QFile::exists(fileName))
                 continue;
             _process.setWorkingDirectory(scriptDir);
-            _process.start('"' + fileName + "\" " + params);
+            _process.start(_scriptName, params);
             return;
         }
         emit error(tr("Could not find script \"%1\"").arg(_scriptName));