OSDN Git Service

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