OSDN Git Service

612e202b7b32614d4611c6c7b9fa83b339ee2dc0
[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 #include <QQueue>
39
40 #include "Mirrors.h"
41
42 ///////////////////////////////////////////////////////////////////////////////
43 // CONSTANTS
44 ///////////////////////////////////////////////////////////////////////////////
45
46 static const char *GLOBALHEADER_ID = "!Update";
47
48 static const char *MIRROR_URL_POSTFIX[] = 
49 {
50         "update.ver",
51         "update_beta.ver",
52         NULL
53 };
54
55 static const int MIN_CONNSCORE = 5;
56 static const int QUICK_MIRRORS = 3;
57 static const int MAX_CONN_TIMEOUT = 16000;
58 static const int DOWNLOAD_TIMEOUT = 30000;
59
60 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
61 static char *USER_AGENT_STR = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; /*use something innocuous*/
62
63 ////////////////////////////////////////////////////////////
64 // Utility Macros
65 ////////////////////////////////////////////////////////////
66
67 #define CHECK_CANCELLED() do \
68 { \
69         if(MUTILS_BOOLIFY(m_cancelled)) \
70         { \
71                 m_success.fetchAndStoreOrdered(0); \
72                 log("", "Update check has been cancelled by user!"); \
73                 setProgress(m_maxProgress); \
74                 setStatus(UpdateStatus_CancelledByUser); \
75                 return; \
76         } \
77 } \
78 while(0)
79
80 #define LOG_MESSAGE_HELPER(X) do \
81 { \
82         if (!(X).isNull()) \
83         { \
84                 emit messageLogged((X)); \
85         } \
86 } \
87 while(0)
88
89 #define STRICMP(X,Y) ((X).compare((Y), Qt::CaseInsensitive) == 0)
90
91 ////////////////////////////////////////////////////////////
92 // Helper Functions
93 ////////////////////////////////////////////////////////////
94
95 static QQueue<QString> buildRandomList(const char *const *values)
96 {
97         QQueue<QString> list;
98         while(*values)
99         {
100                 list.insert(MUtils::next_rand_u32(list.size() + 1), QString::fromLatin1(*(values++)));
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         QQueue<QString> mirrorList = buildRandomList(known_hosts);
226
227         for(int connectionTimeout = 1000; connectionTimeout <= MAX_CONN_TIMEOUT; connectionTimeout *= 2)
228         {
229                 QElapsedTimer elapsedTimer;
230                 elapsedTimer.start();
231                 const int globalTimeout = 2 * MIN_CONNSCORE * connectionTimeout, count = mirrorList.count();
232                 for(int i = 0; i < count; ++i)
233                 {
234                         Q_ASSERT(!mirrorList.isEmpty());
235                         const QString hostName = mirrorList.dequeue();
236                         if (tryContactHost(hostName, connectionTimeout))
237                         {
238                                 setProgress(1 + (++connectionScore));
239                                 if (connectionScore >= MIN_CONNSCORE)
240                                 {
241                                         goto endLoop; /*success*/
242                                 }
243                         }
244                         else
245                         {
246                                 mirrorList.enqueue(hostName);
247                                 if(elapsedTimer.hasExpired(globalTimeout))
248                                 {
249                                         break; /*timer expired*/
250                                 }
251                         }
252                         CHECK_CANCELLED();
253                 }
254         }
255
256 endLoop:
257         if(connectionScore < MIN_CONNSCORE)
258         {
259                 log("", "Connectivity test has failed: Internet connection appears to be broken!");
260                 setProgress(m_maxProgress);
261                 setStatus(UpdateStatus_ErrorConnectionTestFailed);
262                 return;
263         }
264
265         // ----- Fetch Update Info From Server ----- //
266
267         log("----", "", "Internet connection is operational, checking for updates online...");
268         setStatus(UpdateStatus_FetchingUpdates);
269
270         int mirrorCount = 0;
271         mirrorList = buildRandomList(update_mirrors);
272
273         while(!mirrorList.isEmpty())
274         {
275                 const QString currentMirror = mirrorList.takeFirst();
276                 const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
277                 if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
278                 {
279                         m_success.ref(); /*success*/
280                         break;
281                 }
282                 if (isQuick)
283                 {
284                         mirrorList.append(currentMirror); /*re-schedule*/
285                 }
286                 CHECK_CANCELLED();
287                 msleep(1);
288         }
289
290         msleep(333);
291         setProgress(MIN_CONNSCORE + 5);
292
293         // ----- Generate final result ----- //
294
295         if(MUTILS_BOOLIFY(m_success))
296         {
297                 if(m_updateInfo->m_buildNo > m_installedBuildNo)
298                 {
299                         setStatus(UpdateStatus_CompletedUpdateAvailable);
300                 }
301                 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
302                 {
303                         setStatus(UpdateStatus_CompletedNoUpdates);
304                 }
305                 else
306                 {
307                         setStatus(UpdateStatus_CompletedNewVersionOlder);
308                 }
309         }
310         else
311         {
312                 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
313         }
314 }
315
316 void MUtils::UpdateChecker::testMirrorsList(void)
317 {
318         QQueue<QString> mirrorList;
319         for(int i = 0; update_mirrors[i]; i++)
320         {
321                 mirrorList.enqueue(QString::fromLatin1(update_mirrors[i]));
322         }
323
324         // ----- Test update mirrors ----- //
325
326         qDebug("\n[Mirror Sites]");
327         log("Testing all known mirror sites...", "", "---");
328
329         UpdateCheckerInfo updateInfo;
330         while (!mirrorList.isEmpty())
331         {
332                 const QString currentMirror = mirrorList.dequeue();
333                 bool success = false;
334                 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
335                 log("", "Testing mirror:", currentMirror, "");
336                 for (quint8 attempt = 0; attempt < 3; ++attempt)
337                 {
338                         updateInfo.resetInfo();
339                         if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
340                         {
341                                 success = true;
342                                 break;
343                         }
344                 }
345                 if (!success)
346                 {
347                         qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
348                 }
349                 log("", "---");
350         }
351
352         // ----- Test known hosts ----- //
353
354         mirrorList.clear();
355         for (int i = 0; known_hosts[i]; i++)
356         {
357                 mirrorList.enqueue(QString::fromLatin1(known_hosts[i]));
358         }
359
360         qDebug("\n[Known Hosts]");
361         log("Testing all known hosts...", "", "---");
362
363         while(!mirrorList.isEmpty())
364         {
365                 const QString currentHost = mirrorList.dequeue();
366                 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
367                 log(QLatin1String(""), "Testing host:", currentHost, "");
368                 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
369                 {
370                         qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
371                 }
372                 log("---");
373         }
374 }
375
376 ////////////////////////////////////////////////////////////
377 // PRIVATE FUNCTIONS
378 ////////////////////////////////////////////////////////////
379
380 void MUtils::UpdateChecker::setStatus(const int status)
381 {
382         if(m_status != status)
383         {
384                 m_status = status;
385                 emit statusChanged(status);
386         }
387 }
388
389 void MUtils::UpdateChecker::setProgress(const int progress)
390 {
391         const int value = qBound(0, progress, m_maxProgress);
392         if(m_progress != value)
393         {
394                 emit progressChanged(m_progress = value);
395         }
396 }
397
398 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
399 {
400         LOG_MESSAGE_HELPER(str1);
401         LOG_MESSAGE_HELPER(str2);
402         LOG_MESSAGE_HELPER(str3);
403         LOG_MESSAGE_HELPER(str4);
404 }
405
406 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
407 {
408         bool success = false;
409         log("", "Trying update mirror:", url, "");
410
411         if (quick)
412         {
413                 setProgress(MIN_CONNSCORE + 1);
414                 if (!tryContactHost(QUrl(url).host(), (MAX_CONN_TIMEOUT / 8)))
415                 {
416                         log("", "Mirror is too slow, skipping!");
417                         return false;
418                 }
419         }
420
421         const QString randPart = next_rand_str();
422         const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
423         const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
424
425         if (!getUpdateInfo(url, outFileVers, outFileSign))
426         {
427                 log("", "Oops: Download of update information has failed!");
428                 goto cleanUp;
429         }
430
431         log("Download completed, verifying signature:", "");
432         setProgress(MIN_CONNSCORE + 4);
433         if (!checkSignature(outFileVers, outFileSign))
434         {
435                 log("", "Bad signature detected, take care !!!");
436                 goto cleanUp;
437         }
438
439         log("", "Signature is valid, parsing update information:", "");
440         success = parseVersionInfo(outFileVers, updateInfo);
441
442 cleanUp:
443         QFile::remove(outFileVers);
444         QFile::remove(outFileSign);
445         return success;
446 }
447
448 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
449 {
450         log("Downloading update information:", "");
451         setProgress(MIN_CONNSCORE + 2);
452         if(getFile(QUrl(QString("%1%2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileVers))
453         {
454                 if (!m_cancelled)
455                 {
456                         log( "Downloading signature file:", "");
457                         setProgress(MIN_CONNSCORE + 3);
458                         if (getFile(QUrl(QString("%1%2.sig2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileSign))
459                         {
460                                 return true; /*completed*/
461                         }
462                 }
463         }
464         return false;
465 }
466
467 //----------------------------------------------------------
468 // PARSE UPDATE INFO
469 //----------------------------------------------------------
470
471 #define _CHECK_HEADER(ID,NAME) \
472         if (STRICMP(name, (NAME))) \
473         { \
474                 sectionId = (ID); \
475                 continue; \
476         }
477
478 #define _PARSE_TEXT(OUT,KEY) \
479         if (STRICMP(key, (KEY))) \
480         { \
481                 (OUT) = val; \
482                 break; \
483         }
484
485 #define _PARSE_UINT(OUT,KEY) \
486         if (STRICMP(key, (KEY))) \
487         { \
488                 bool _ok = false; \
489                 const unsigned int _tmp = val.toUInt(&_ok); \
490                 if (_ok) \
491                 { \
492                         (OUT) = _tmp; \
493                         break; \
494                 } \
495         }
496
497 #define _PARSE_DATE(OUT,KEY) \
498         if (STRICMP(key, (KEY))) \
499         { \
500                 const QDate _tmp = QDate::fromString(val, Qt::ISODate); \
501                 if (_tmp.isValid()) \
502                 { \
503                         (OUT) = _tmp; \
504                         break; \
505                 } \
506         }
507
508 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *const updateInfo)
509 {
510         updateInfo->resetInfo();
511
512         QFile data(file);
513         if(!data.open(QIODevice::ReadOnly))
514         {
515                 qWarning("Cannot open update info file for reading!");
516                 return false;
517         }
518
519         QDate updateInfoDate;
520         int sectionId = 0;
521         QRegExp regex_sec("^\\[(.+)\\]$"), regex_val("^([^=]+)=(.+)$");
522
523         while(!data.atEnd())
524         {
525                 QString line = QString::fromLatin1(data.readLine()).trimmed();
526                 if (regex_sec.indexIn(line) >= 0)
527                 {
528                         sectionId = 0; /*unknown section*/
529                         const QString name = regex_sec.cap(1).trimmed();
530                         log(QString("Sec: [%1]").arg(name));
531                         _CHECK_HEADER(1, GLOBALHEADER_ID)
532                         _CHECK_HEADER(2, m_applicationId)
533                         continue;
534                 }
535                 if (regex_val.indexIn(line) >= 0)
536                 {
537                         const QString key = regex_val.cap(1).trimmed();
538                         const QString val = regex_val.cap(2).trimmed();
539                         log(QString("Val: \"%1\" = \"%2\"").arg(key, val));
540                         switch (sectionId)
541                         {
542                         case 1:
543                                 _PARSE_DATE(updateInfoDate, "TimestampCreated")
544                                 break;
545                         case 2:
546                                 _PARSE_UINT(updateInfo->m_buildNo,          "BuildNo")
547                                 _PARSE_DATE(updateInfo->m_buildDate,        "BuildDate")
548                                 _PARSE_TEXT(updateInfo->m_downloadSite,     "DownloadSite")
549                                 _PARSE_TEXT(updateInfo->m_downloadAddress,  "DownloadAddress")
550                                 _PARSE_TEXT(updateInfo->m_downloadFilename, "DownloadFilename")
551                                 _PARSE_TEXT(updateInfo->m_downloadFilecode, "DownloadFilecode")
552                                 _PARSE_TEXT(updateInfo->m_downloadChecksum, "DownloadChecksum")
553                                 break;
554                         }
555                 }
556         }
557
558         if (!updateInfo->isComplete())
559         {
560                 log("", "WARNING: Update information is incomplete!");
561                 goto failure;
562         }
563
564         if(updateInfoDate.isValid())
565         {
566                 const QDate expiredDate = updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS);
567                 if (expiredDate < OS::current_date())
568                 {
569                         log("", QString("WARNING: Update information has expired at %1!").arg(expiredDate.toString(Qt::ISODate)));
570                         goto failure;
571                 }
572         }
573         else
574         {
575                 log("", "WARNING: Timestamp is missing from update information header!");
576                 goto failure;
577         }
578
579         log("", "Success: Update information is complete.");
580         return true; /*success*/
581
582 failure:
583         updateInfo->resetInfo();
584         return false;
585 }
586
587 //----------------------------------------------------------
588 // EXTERNAL TOOLS
589 //----------------------------------------------------------
590
591 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
592 {
593         QFileInfo output(outFile);
594         output.setCaching(false);
595
596         if (output.exists())
597         {
598                 QFile::remove(output.canonicalFilePath());
599                 if (output.exists())
600                 {
601                         qWarning("Existing output file could not be found!");
602                         return false;
603                 }
604         }
605         
606         QStringList args(QLatin1String("-vsSNqkfL"));
607         args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
608         args << "--max-redirs" << QString::number(maxRedir);
609         args << "-A" << USER_AGENT_STR;
610         args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
611         args << "-o" << output.fileName() << url.toString();
612
613         return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
614 }
615
616 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
617 {
618         log(QString("Connecting to host: %1").arg(hostname), "");
619
620         QStringList args(QLatin1String("-vsSNqkI"));
621         args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
622         args << "-A" << USER_AGENT_STR;
623         args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
624         
625         return execCurl(args, temp_folder(), timeoutMsec);
626 }
627
628 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
629 {
630         if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
631         {
632                 qWarning("CheckSignature: File and signature should be in same folder!");
633                 return false;
634         }
635
636         QString keyRingPath(m_binaryKeys);
637         bool removeKeyring = false;
638         if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
639         {
640                 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
641                 removeKeyring = true;
642                 if (!QFile::copy(m_binaryKeys, keyRingPath))
643                 {
644                         qWarning("CheckSignature: Failed to copy the key-ring file!");
645                         return false;
646                 }
647         }
648
649         QStringList args;
650         args << QStringList() << "--homedir" << ".";
651         args << "--keyring" << QFileInfo(keyRingPath).fileName();
652         args << QFileInfo(signature).fileName();
653         args << QFileInfo(file).fileName();
654
655         const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
656         if (exitCode != INT_MAX)
657         {
658                 log(QString().sprintf("Exited with code %d", exitCode));
659         }
660
661         if (removeKeyring)
662         {
663                 remove_file(keyRingPath);
664         }
665
666         return (exitCode == 0); /*completed*/
667 }
668
669 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
670 {
671         const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
672         if (exitCode != INT_MAX)
673         {
674                 switch (exitCode)
675                 {
676                         case  0: log(QLatin1String("DONE: Transfer completed successfully."), "");                     break;
677                         case  6: log(QLatin1String("ERROR: Remote host could not be resolved!"), "");                  break;
678                         case  7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
679                         case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), "");   break;
680                         case 28: log(QLatin1String("ERROR: Operation timed out !!!"), "");                             break;
681                         default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), "");       break;
682                 }
683         }
684
685         return (exitCode == 0); /*completed*/
686 }
687
688 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
689 {
690         QProcess process;
691         init_process(process, workingDir, true, NULL, m_environment.data());
692
693         QEventLoop loop;
694         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
695         connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
696         connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
697
698         QTimer timer;
699         timer.setSingleShot(true);
700         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
701
702         process.start(programFile, args);
703         if (!process.waitForStarted())
704         {
705                 log("PROCESS FAILED TO START !!!", "");
706                 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
707                 return INT_MAX; /*failed to start*/
708         }
709
710         bool bAborted = false;
711         timer.start(qMax(timeout, 1500));
712
713         while (process.state() != QProcess::NotRunning)
714         {
715                 loop.exec();
716                 while (process.canReadLine())
717                 {
718                         const QString line = QString::fromLatin1(process.readLine()).simplified();
719                         if (line.length() > 1)
720                         {
721                                 log(line);
722                         }
723                 }
724                 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
725                 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
726                 {
727                         log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
728                         qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
729                         break; /*abort process*/
730                 }
731         }
732
733         timer.stop();
734         timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
735
736         if (bAborted)
737         {
738                 process.kill();
739                 process.waitForFinished(-1);
740         }
741
742         while (process.canReadLine())
743         {
744                 const QString line = QString::fromLatin1(process.readLine()).simplified();
745                 if (line.length() > 1)
746                 {
747                         log(line);
748                 }
749         }
750
751         return bAborted ? INT_MAX : process.exitCode();
752 }
753
754 ////////////////////////////////////////////////////////////
755 // SLOTS
756 ////////////////////////////////////////////////////////////
757
758 /*NONE*/
759
760 ////////////////////////////////////////////////////////////
761 // EVENTS
762 ////////////////////////////////////////////////////////////
763
764 /*NONE*/