OSDN Git Service

Small code clean-up.
[mutilities/MUtilities.git] / src / UpdateChecker.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #include <MUtils/Global.h>
23 #include <MUtils/UpdateChecker.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Exception.h>
26
27 #include <QStringList>
28 #include <QFile>
29 #include <QFileInfo>
30 #include <QDir>
31 #include <QProcess>
32 #include <QUrl>
33 #include <QEventLoop>
34 #include <QTimer>
35 #include <QElapsedTimer>
36 #include <QSet>
37 #include <QHash>
38
39 #include "Mirrors.h"
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // CONSTANTS
43 ///////////////////////////////////////////////////////////////////////////////
44
45 static const char *GLOBALHEADER_ID = "!Update";
46
47 static const char *MIRROR_URL_POSTFIX[] = 
48 {
49         "update.ver",
50         "update_beta.ver",
51         NULL
52 };
53
54 static const int MIN_CONNSCORE = 5;
55 static const int QUICK_MIRRORS = 3;
56 static const int MAX_CONN_TIMEOUT = 16000;
57 static const int DOWNLOAD_TIMEOUT = 30000;
58
59 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
60 static char *USER_AGENT_STR = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; /*use something innocuous*/
61
62 ////////////////////////////////////////////////////////////
63 // Utility Macros
64 ////////////////////////////////////////////////////////////
65
66 #define CHECK_CANCELLED() do \
67 { \
68         if(MUTILS_BOOLIFY(m_cancelled)) \
69         { \
70                 m_success.fetchAndStoreOrdered(0); \
71                 log("", "Update check has been cancelled by user!"); \
72                 setProgress(m_maxProgress); \
73                 setStatus(UpdateStatus_CancelledByUser); \
74                 return; \
75         } \
76 } \
77 while(0)
78
79 #define LOG_MESSAGE_HELPER(X) do \
80 { \
81         if (!(X).isNull()) \
82         { \
83                 emit messageLogged((X)); \
84         } \
85 } \
86 while(0)
87
88 #define STRICMP(X,Y) ((X).compare((Y), Qt::CaseInsensitive) == 0)
89
90 ////////////////////////////////////////////////////////////
91 // Helper Functions
92 ////////////////////////////////////////////////////////////
93
94 static QStringList buildRandomList(const char *const values[])
95 {
96         QStringList list;
97         for (int index = 0; values[index]; index++)
98         {
99                 const int pos = MUtils::next_rand_u32() % (index + 1);
100                 list.insert(pos, QString::fromLatin1(values[index]));
101         }
102         return list;
103 }
104
105 static const QHash<QString, QString> *initEnvVars(void)
106 {
107         QHash<QString, QString> *const environment = new QHash<QString, QString>();
108         environment->insert(QLatin1String("CURL_HOME"), QDir::toNativeSeparators(MUtils::temp_folder()));
109         return environment;
110 }
111
112 ////////////////////////////////////////////////////////////
113 // Update Info Class
114 ////////////////////////////////////////////////////////////
115
116 MUtils::UpdateCheckerInfo::UpdateCheckerInfo(void)
117 {
118         resetInfo();
119 }
120         
121 void MUtils::UpdateCheckerInfo::resetInfo(void)
122 {
123         m_buildNo = 0;
124         m_buildDate.setDate(1900, 1, 1);
125         m_downloadSite.clear();
126         m_downloadAddress.clear();
127         m_downloadFilename.clear();
128         m_downloadFilecode.clear();
129         m_downloadChecksum.clear();
130 }
131
132 bool MUtils::UpdateCheckerInfo::isComplete(void)
133 {
134         return (this->m_buildNo > 0) &&
135                 (this->m_buildDate.year() >= 2010) &&
136                 (!this->m_downloadSite.isEmpty()) &&
137                 (!this->m_downloadAddress.isEmpty()) &&
138                 (!this->m_downloadFilename.isEmpty()) &&
139                 (!this->m_downloadFilecode.isEmpty()) &&
140                 (!this->m_downloadChecksum.isEmpty());
141 }
142
143 ////////////////////////////////////////////////////////////
144 // Constructor & Destructor
145 ////////////////////////////////////////////////////////////
146
147 MUtils::UpdateChecker::UpdateChecker(const QString &binCurl, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
148 :
149         m_updateInfo(new UpdateCheckerInfo()),
150         m_binaryCurl(binCurl),
151         m_binaryGnuPG(binGnuPG),
152         m_binaryKeys(binKeys),
153         m_applicationId(applicationId),
154         m_installedBuildNo(installedBuildNo),
155         m_betaUpdates(betaUpdates),
156         m_testMode(testMode),
157         m_maxProgress(MIN_CONNSCORE + 5),
158         m_environment(initEnvVars())
159 {
160         m_status = UpdateStatus_NotStartedYet;
161         m_progress = 0;
162
163         if(m_binaryCurl.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
164         {
165                 MUTILS_THROW("Tools not initialized correctly!");
166         }
167 }
168
169 MUtils::UpdateChecker::~UpdateChecker(void)
170 {
171 }
172
173 ////////////////////////////////////////////////////////////
174 // Public slots
175 ////////////////////////////////////////////////////////////
176
177 void MUtils::UpdateChecker::start(Priority priority)
178 {
179         m_success.fetchAndStoreOrdered(0);
180         m_cancelled.fetchAndStoreOrdered(0);
181         QThread::start(priority);
182 }
183
184 ////////////////////////////////////////////////////////////
185 // Protected functions
186 ////////////////////////////////////////////////////////////
187
188 void MUtils::UpdateChecker::run(void)
189 {
190         qDebug("Update checker thread started!");
191         MUTILS_EXCEPTION_HANDLER(m_testMode ? testMirrorsList() : checkForUpdates());
192         qDebug("Update checker thread completed.");
193 }
194
195 void MUtils::UpdateChecker::checkForUpdates(void)
196 {
197         // ----- Initialization ----- //
198
199         m_updateInfo->resetInfo();
200         setProgress(0);
201
202         // ----- Test Internet Connection ----- //
203
204         log("Checking your Internet connection...", "");
205         setStatus(UpdateStatus_CheckingConnection);
206
207         const int networkStatus = OS::network_status();
208         if(networkStatus == OS::NETWORK_TYPE_NON)
209         {
210                 if (!MUtils::OS::arguments().contains("ignore-network-status"))
211                 {
212                         log("Operating system reports that the computer is currently offline !!!");
213                         setProgress(m_maxProgress);
214                         setStatus(UpdateStatus_ErrorNoConnection);
215                         return;
216                 }
217         }
218         
219         msleep(333);
220         setProgress(1);
221
222         // ----- Test Known Hosts Connectivity ----- //
223
224         int connectionScore = 0;
225         QStringList mirrorList = buildRandomList(known_hosts);
226
227         for(int connectionTimout = 1000; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
228         {
229                 QElapsedTimer elapsedTimer;
230                 elapsedTimer.start();
231                 const int globalTimout = 2 * MIN_CONNSCORE * connectionTimout;
232                 while (!elapsedTimer.hasExpired(globalTimout))
233                 {
234                         const QString hostName = mirrorList.takeFirst();
235                         if (tryContactHost(hostName, connectionTimout))
236                         {
237                                 setProgress(1 + (connectionScore += 1));
238                                 elapsedTimer.restart();
239                                 if (connectionScore >= MIN_CONNSCORE)
240                                 {
241                                         goto endLoop; /*success*/
242                                 }
243                         }
244                         else
245                         {
246                                 mirrorList.append(hostName); /*re-schedule*/
247                         }
248                         CHECK_CANCELLED();
249                         msleep(1);
250                 }
251         }
252
253 endLoop:
254         if(connectionScore < MIN_CONNSCORE)
255         {
256                 log("", "Connectivity test has failed: Internet connection appears to be broken!");
257                 setProgress(m_maxProgress);
258                 setStatus(UpdateStatus_ErrorConnectionTestFailed);
259                 return;
260         }
261
262         // ----- Fetch Update Info From Server ----- //
263
264         log("----", "", "Internet connection is operational, checking for updates online...");
265         setStatus(UpdateStatus_FetchingUpdates);
266
267         int mirrorCount = 0;
268         mirrorList = buildRandomList(update_mirrors);
269
270         while(!mirrorList.isEmpty())
271         {
272                 const QString currentMirror = mirrorList.takeFirst();
273                 const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
274                 if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
275                 {
276                         m_success.ref(); /*success*/
277                         break;
278                 }
279                 if (isQuick)
280                 {
281                         mirrorList.append(currentMirror); /*re-schedule*/
282                 }
283                 CHECK_CANCELLED();
284                 msleep(1);
285         }
286
287         msleep(333);
288         setProgress(MIN_CONNSCORE + 5);
289
290         // ----- Generate final result ----- //
291
292         if(MUTILS_BOOLIFY(m_success))
293         {
294                 if(m_updateInfo->m_buildNo > m_installedBuildNo)
295                 {
296                         setStatus(UpdateStatus_CompletedUpdateAvailable);
297                 }
298                 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
299                 {
300                         setStatus(UpdateStatus_CompletedNoUpdates);
301                 }
302                 else
303                 {
304                         setStatus(UpdateStatus_CompletedNewVersionOlder);
305                 }
306         }
307         else
308         {
309                 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
310         }
311 }
312
313 void MUtils::UpdateChecker::testMirrorsList(void)
314 {
315         QStringList mirrorList;
316         for(int i = 0; update_mirrors[i]; i++)
317         {
318                 mirrorList << QString::fromLatin1(update_mirrors[i]);
319         }
320
321         // ----- Test update mirrors ----- //
322
323         qDebug("\n[Mirror Sites]");
324         log("Testing all known mirror sites...", "", "---");
325
326         UpdateCheckerInfo updateInfo;
327         while (!mirrorList.isEmpty())
328         {
329                 const QString currentMirror = mirrorList.takeFirst();
330                 bool success = false;
331                 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
332                 log("", "Testing:", currentMirror, "");
333                 for (quint8 attempt = 0; attempt < 3; ++attempt)
334                 {
335                         updateInfo.resetInfo();
336                         if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
337                         {
338                                 success = true;
339                                 break;
340                         }
341                 }
342                 if (!success)
343                 {
344                         qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
345                 }
346                 log("", "---");
347         }
348
349         // ----- Test known hosts ----- //
350
351         QStringList knownHostList;
352         for (int i = 0; known_hosts[i]; i++)
353         {
354                 knownHostList << QString::fromLatin1(known_hosts[i]);
355         }
356
357         qDebug("\n[Known Hosts]");
358         log("Testing all known hosts...", "", "---");
359
360         while(!knownHostList.isEmpty())
361         {
362                 const QString currentHost = knownHostList.takeFirst();
363                 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
364                 log(QLatin1String(""), "Testing:", currentHost, "");
365                 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
366                 {
367                         qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
368                 }
369                 log("---");
370         }
371 }
372
373 ////////////////////////////////////////////////////////////
374 // PRIVATE FUNCTIONS
375 ////////////////////////////////////////////////////////////
376
377 void MUtils::UpdateChecker::setStatus(const int status)
378 {
379         if(m_status != status)
380         {
381                 m_status = status;
382                 emit statusChanged(status);
383         }
384 }
385
386 void MUtils::UpdateChecker::setProgress(const int progress)
387 {
388         const int value = qBound(0, progress, m_maxProgress);
389         if(m_progress != value)
390         {
391                 emit progressChanged(m_progress = value);
392         }
393 }
394
395 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
396 {
397         LOG_MESSAGE_HELPER(str1);
398         LOG_MESSAGE_HELPER(str2);
399         LOG_MESSAGE_HELPER(str3);
400         LOG_MESSAGE_HELPER(str4);
401 }
402
403 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
404 {
405         bool success = false;
406         log("", "Trying update mirror:", url, "");
407
408         if (quick)
409         {
410                 setProgress(MIN_CONNSCORE + 1);
411                 if (!tryContactHost(QUrl(url).host(), (MAX_CONN_TIMEOUT / 8)))
412                 {
413                         log("", "Mirror is too slow, skipping!");
414                         return false;
415                 }
416         }
417
418         const QString randPart = next_rand_str();
419         const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
420         const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
421
422         if (!getUpdateInfo(url, outFileVers, outFileSign))
423         {
424                 log("", "Oops: Download of update information has failed!");
425                 goto cleanUp;
426         }
427
428         log("Download completed, verifying signature:", "");
429         setProgress(MIN_CONNSCORE + 4);
430         if (!checkSignature(outFileVers, outFileSign))
431         {
432                 log("", "Bad signature detected, take care !!!");
433                 goto cleanUp;
434         }
435
436         log("", "Signature is valid, parsing update information:", "");
437         success = parseVersionInfo(outFileVers, updateInfo);
438
439 cleanUp:
440         QFile::remove(outFileVers);
441         QFile::remove(outFileSign);
442         return success;
443 }
444
445 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
446 {
447         log("Downloading update information:", "");
448         setProgress(MIN_CONNSCORE + 2);
449         if(getFile(QUrl(QString("%1%2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileVers))
450         {
451                 if (!m_cancelled)
452                 {
453                         log( "Downloading signature file:", "");
454                         setProgress(MIN_CONNSCORE + 3);
455                         if (getFile(QUrl(QString("%1%2.sig2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileSign))
456                         {
457                                 return true; /*completed*/
458                         }
459                 }
460         }
461         return false;
462 }
463
464 //----------------------------------------------------------
465 // PARSE UPDATE INFO
466 //----------------------------------------------------------
467
468 #define _CHECK_HEADER(ID,NAME) \
469         if (STRICMP(name, (NAME))) \
470         { \
471                 sectionId = (ID); \
472                 continue; \
473         }
474
475 #define _PARSE_TEXT(OUT,KEY) \
476         if (STRICMP(key, (KEY))) \
477         { \
478                 (OUT) = val; \
479                 break; \
480         }
481
482 #define _PARSE_UINT(OUT,KEY) \
483         if (STRICMP(key, (KEY))) \
484         { \
485                 bool _ok = false; \
486                 const unsigned int _tmp = val.toUInt(&_ok); \
487                 if (_ok) \
488                 { \
489                         (OUT) = _tmp; \
490                         break; \
491                 } \
492         }
493
494 #define _PARSE_DATE(OUT,KEY) \
495         if (STRICMP(key, (KEY))) \
496         { \
497                 const QDate _tmp = QDate::fromString(val, Qt::ISODate); \
498                 if (_tmp.isValid()) \
499                 { \
500                         (OUT) = _tmp; \
501                         break; \
502                 } \
503         }
504
505 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *const updateInfo)
506 {
507         updateInfo->resetInfo();
508
509         QFile data(file);
510         if(!data.open(QIODevice::ReadOnly))
511         {
512                 qWarning("Cannot open update info file for reading!");
513                 return false;
514         }
515
516         QDate updateInfoDate;
517         int sectionId = 0;
518         QRegExp regex_sec("^\\[(.+)\\]$"), regex_val("^([^=]+)=(.+)$");
519
520         while(!data.atEnd())
521         {
522                 QString line = QString::fromLatin1(data.readLine()).trimmed();
523                 if (regex_sec.indexIn(line) >= 0)
524                 {
525                         sectionId = 0; /*unknown section*/
526                         const QString name = regex_sec.cap(1).trimmed();
527                         log(QString("Sec: [%1]").arg(name));
528                         _CHECK_HEADER(1, GLOBALHEADER_ID)
529                         _CHECK_HEADER(2, m_applicationId)
530                         continue;
531                 }
532                 if (regex_val.indexIn(line) >= 0)
533                 {
534                         const QString key = regex_val.cap(1).trimmed();
535                         const QString val = regex_val.cap(2).trimmed();
536                         log(QString("Val: \"%1\" = \"%2\"").arg(key, val));
537                         switch (sectionId)
538                         {
539                         case 1:
540                                 _PARSE_DATE(updateInfoDate, "TimestampCreated")
541                                 break;
542                         case 2:
543                                 _PARSE_UINT(updateInfo->m_buildNo,          "BuildNo")
544                                 _PARSE_DATE(updateInfo->m_buildDate,        "BuildDate")
545                                 _PARSE_TEXT(updateInfo->m_downloadSite,     "DownloadSite")
546                                 _PARSE_TEXT(updateInfo->m_downloadAddress,  "DownloadAddress")
547                                 _PARSE_TEXT(updateInfo->m_downloadFilename, "DownloadFilename")
548                                 _PARSE_TEXT(updateInfo->m_downloadFilecode, "DownloadFilecode")
549                                 _PARSE_TEXT(updateInfo->m_downloadChecksum, "DownloadChecksum")
550                                 break;
551                         }
552                 }
553         }
554
555         if (!updateInfo->isComplete())
556         {
557                 log("", "WARNING: Update information is incomplete!");
558                 goto failure;
559         }
560
561         if(updateInfoDate.isValid())
562         {
563                 const QDate expiredDate = updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS);
564                 if (expiredDate < OS::current_date())
565                 {
566                         log("", QString("WARNING: Update information has expired at %1!").arg(expiredDate.toString(Qt::ISODate)));
567                         goto failure;
568                 }
569         }
570         else
571         {
572                 log("", "WARNING: Timestamp is missing from update information header!");
573                 goto failure;
574         }
575
576         log("", "Success: Update information is complete.");
577         return true; /*success*/
578
579 failure:
580         updateInfo->resetInfo();
581         return false;
582 }
583
584 //----------------------------------------------------------
585 // EXTERNAL TOOLS
586 //----------------------------------------------------------
587
588 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
589 {
590         QFileInfo output(outFile);
591         output.setCaching(false);
592
593         if (output.exists())
594         {
595                 QFile::remove(output.canonicalFilePath());
596                 if (output.exists())
597                 {
598                         qWarning("Existing output file could not be found!");
599                         return false;
600                 }
601         }
602         
603         QStringList args(QLatin1String("-vsSNqkfL"));
604         args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
605         args << "--max-redirs" << QString::number(maxRedir);
606         args << "-A" << USER_AGENT_STR;
607         args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
608         args << "-o" << output.fileName() << url.toString();
609
610         return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
611 }
612
613 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
614 {
615         log(QString("Connecting to host: %1").arg(hostname), "");
616
617         QStringList args(QLatin1String("-vsSNqkI"));
618         args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
619         args << "-A" << USER_AGENT_STR;
620         args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
621         
622         return execCurl(args, temp_folder(), timeoutMsec);
623 }
624
625 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
626 {
627         if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
628         {
629                 qWarning("CheckSignature: File and signature should be in same folder!");
630                 return false;
631         }
632
633         QString keyRingPath(m_binaryKeys);
634         bool removeKeyring = false;
635         if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
636         {
637                 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
638                 removeKeyring = true;
639                 if (!QFile::copy(m_binaryKeys, keyRingPath))
640                 {
641                         qWarning("CheckSignature: Failed to copy the key-ring file!");
642                         return false;
643                 }
644         }
645
646         QStringList args;
647         args << QStringList() << "--homedir" << ".";
648         args << "--keyring" << QFileInfo(keyRingPath).fileName();
649         args << QFileInfo(signature).fileName();
650         args << QFileInfo(file).fileName();
651
652         const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
653         if (exitCode != INT_MAX)
654         {
655                 log(QString().sprintf("Exited with code %d", exitCode));
656         }
657
658         if (removeKeyring)
659         {
660                 remove_file(keyRingPath);
661         }
662
663         return (exitCode == 0); /*completed*/
664 }
665
666 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
667 {
668         const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
669         if (exitCode != INT_MAX)
670         {
671                 switch (exitCode)
672                 {
673                         case -1:
674                         case  0: log(QLatin1String("DONE: Transfer completed successfully."), "");                     break;
675                         case  6: log(QLatin1String("ERROR: Remote host could not be resolved!"), "");                  break;
676                         case  7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
677                         case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), "");   break;
678                         case 28: log(QLatin1String("ERROR: Operation timed out !!!"), "");                             break;
679                         default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), "");       break;
680                 }
681         }
682
683         return (exitCode == 0); /*completed*/
684 }
685
686 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
687 {
688         QProcess process;
689         init_process(process, workingDir, true, NULL, m_environment.data());
690
691         QEventLoop loop;
692         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
693         connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
694         connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
695
696         QTimer timer;
697         timer.setSingleShot(true);
698         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
699
700         process.start(programFile, args);
701         if (!process.waitForStarted())
702         {
703                 log("PROCESS FAILED TO START !!!", "");
704                 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
705                 return INT_MAX; /*failed to start*/
706         }
707
708         bool bAborted = false;
709         timer.start(qMax(timeout, 1500));
710
711         while (process.state() != QProcess::NotRunning)
712         {
713                 loop.exec();
714                 while (process.canReadLine())
715                 {
716                         const QString line = QString::fromLatin1(process.readLine()).simplified();
717                         if (line.length() > 1)
718                         {
719                                 log(line);
720                         }
721                 }
722                 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
723                 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
724                 {
725                         log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
726                         qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
727                         break; /*abort process*/
728                 }
729         }
730
731         timer.stop();
732         timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
733
734         if (bAborted)
735         {
736                 process.kill();
737                 process.waitForFinished(-1);
738         }
739
740         while (process.canReadLine())
741         {
742                 const QString line = QString::fromLatin1(process.readLine()).simplified();
743                 if (line.length() > 1)
744                 {
745                         log(line);
746                 }
747         }
748
749         return bAborted ? INT_MAX : process.exitCode();
750 }
751
752 ////////////////////////////////////////////////////////////
753 // SLOTS
754 ////////////////////////////////////////////////////////////
755
756 /*NONE*/
757
758 ////////////////////////////////////////////////////////////
759 // EVENTS
760 ////////////////////////////////////////////////////////////
761
762 /*NONE*/