OSDN Git Service

Some code refactoring in UpdateChecker class.
authorLoRd_MuldeR <mulder2@gmx.de>
Wed, 17 Oct 2018 19:26:47 +0000 (21:26 +0200)
committerLoRd_MuldeR <mulder2@gmx.de>
Wed, 17 Oct 2018 19:26:47 +0000 (21:26 +0200)
include/MUtils/UpdateChecker.h
src/UpdateChecker.cpp

index 07c1bb4..c397965 100644 (file)
@@ -141,6 +141,7 @@ namespace MUtils
                bool checkSignature(const QString &file, const QString &signature);
                bool tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick);
 
-               bool invokeCurl(const QStringList &args, const QString &workingDir, const int timeout);
+               bool execCurl(const QStringList &args, const QString &workingDir, const int timeout);
+               int execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout);
        };
 }
index 54972c0..103c1ef 100644 (file)
@@ -205,10 +205,13 @@ void MUtils::UpdateChecker::checkForUpdates(void)
        const int networkStatus = OS::network_status();
        if(networkStatus == OS::NETWORK_TYPE_NON)
        {
-               log("Operating system reports that the computer is currently offline !!!");
-               setProgress(m_maxProgress);
-               setStatus(UpdateStatus_ErrorNoConnection);
-               return;
+               if (!MUtils::OS::arguments().contains("--ignore-network-status"))
+               {
+                       log("Operating system reports that the computer is currently offline !!!");
+                       setProgress(m_maxProgress);
+                       setStatus(UpdateStatus_ErrorNoConnection);
+                       return;
+               }
        }
        
        msleep(500);
@@ -589,7 +592,7 @@ bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, con
        args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
        args << "-o" << output.fileName() << url.toString();
 
-       return invokeCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
+       return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
 }
 
 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
@@ -601,73 +604,55 @@ bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &t
        args << "-A" << USER_AGENT_STR;
        args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
        
-       return invokeCurl(args, temp_folder(), timeoutMsec);
+       return execCurl(args, temp_folder(), timeoutMsec);
 }
 
-bool MUtils::UpdateChecker::invokeCurl(const QStringList &args, const QString &workingDir, const int timeout)
+bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
 {
-       QProcess process;
-       init_process(process, workingDir, true, NULL, m_environment.data());
-
-       QEventLoop loop;
-       connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
-       connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
-       connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
-
-       QTimer timer;
-       timer.setSingleShot(true);
-       connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
-
-       process.start(m_binaryCurl, args);
-       if (!process.waitForStarted())
+       if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
        {
+               qWarning("CheckSignature: File and signature should be in same folder!");
                return false;
        }
 
-       bool bAborted = false;
-       timer.start(qMax((timeout + (timeout / 2)), 1500));
-
-       while (process.state() != QProcess::NotRunning)
+       QString keyRingPath(m_binaryKeys);
+       bool removeKeyring = false;
+       if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
        {
-               loop.exec();
-               while (process.canReadLine())
-               {
-                       const QString line = QString::fromLatin1(process.readLine()).simplified();
-                       if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
-                       {
-                               log(line);
-                       }
-               }
-               const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
-               if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
+               keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
+               removeKeyring = true;
+               if (!QFile::copy(m_binaryKeys, keyRingPath))
                {
-                       log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
-                       qWarning("WARNING: cURL process %s!", bCancelled ? "cancelled" : "timed out");
-                       break; /*abort process*/
+                       qWarning("CheckSignature: Failed to copy the key-ring file!");
+                       return false;
                }
        }
 
-       timer.stop();
-       timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
+       QStringList args;
+       args << QStringList() << "--homedir" << ".";
+       args << "--keyring" << QFileInfo(keyRingPath).fileName();
+       args << QFileInfo(signature).fileName();
+       args << QFileInfo(file).fileName();
 
-       if (bAborted)
+       const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
+       if (exitCode != INT_MAX)
        {
-               process.kill();
-               process.waitForFinished(-1);
+               log(QString().sprintf("Exited with code %d", exitCode));
        }
 
-       while (process.canReadLine())
+       if (removeKeyring)
        {
-               const QString line = QString::fromLatin1(process.readLine()).simplified();
-               if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
-               {
-                       log(line);
-               }
+               remove_file(keyRingPath);
        }
 
-       if (!bAborted)
+       return (exitCode == 0); /*completed*/
+}
+
+bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
+{
+       const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
+       if (exitCode != INT_MAX)
        {
-               const int exitCode = process.exitCode();
                switch (exitCode)
                {
                        case -1:
@@ -678,72 +663,75 @@ bool MUtils::UpdateChecker::invokeCurl(const QStringList &args, const QString &w
                        case 28: log(QLatin1String("ERROR: Operation timed out !!!"), "");                             break;
                        default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), "");       break;
                }
-               return (exitCode == 0);
        }
-       
-       return false; /*aborted*/
+
+       return (exitCode == 0); /*completed*/
 }
 
-bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
+int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
 {
-       if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
-       {
-               qWarning("CheckSignature: File and signature should be in same folder!");
-               return false;
-       }
-
-       QString keyRingPath(m_binaryKeys);
-       bool removeKeyring = false;
-       if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
-       {
-               keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
-               removeKeyring = true;
-               if (!QFile::copy(m_binaryKeys, keyRingPath))
-               {
-                       qWarning("CheckSignature: Failed to copy the key-ring file!");
-                       return false;
-               }
-       }
-
        QProcess process;
-       init_process(process, QFileInfo(file).absolutePath());
+       init_process(process, workingDir, true, NULL, m_environment.data());
 
        QEventLoop loop;
        connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
        connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
        connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
 
-       process.start(m_binaryGnuPG, QStringList() << "--homedir" << "." << "--keyring" << QFileInfo(keyRingPath).fileName() << QFileInfo(signature).fileName() << QFileInfo(file).fileName());
+       QTimer timer;
+       timer.setSingleShot(true);
+       connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
 
+       process.start(programFile, args);
        if (!process.waitForStarted())
        {
-               if (removeKeyring)
-               {
-                       remove_file(keyRingPath);
-               }
-               return false;
+               log("PROCESS FAILED TO START !!!", "");
+               qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
+               return INT_MAX; /*failed to start*/
        }
 
+       bool bAborted = false;
+       timer.start(qMax(timeout, 1500));
+
        while (process.state() != QProcess::NotRunning)
        {
                loop.exec();
                while (process.canReadLine())
                {
                        const QString line = QString::fromLatin1(process.readLine()).simplified();
-                       if (!line.isEmpty())
+                       if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
                        {
                                log(line);
                        }
                }
+               const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
+               if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
+               {
+                       log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
+                       qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
+                       break; /*abort process*/
+               }
        }
 
-       if (removeKeyring)
+       timer.stop();
+       timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
+
+       if (bAborted)
        {
-               remove_file(keyRingPath);
+               process.kill();
+               process.waitForFinished(-1);
+       }
+
+       while (process.canReadLine())
+       {
+               const QString line = QString::fromLatin1(process.readLine()).simplified();
+               if ((!line.isEmpty()) && line.compare(QLatin1String("<")) && line.compare(QLatin1String(">")))
+               {
+                       log(line);
+               }
        }
 
-       log(QString().sprintf("Exited with code %d", process.exitCode()));
-       return (process.exitCode() == 0);
+       return bAborted ? INT_MAX : process.exitCode();
 }
 
 ////////////////////////////////////////////////////////////