OSDN Git Service

Updated Web Updater binary. Now takes an additional "Checksum" argument.
[lamexp/LameXP.git] / src / Tool_Abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Tool_Abstract.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/Global.h>
30 #include <MUtils/OSSupport.h>
31 #include <MUtils/JobObject.h>
32
33 //Qt
34 #include <QProcess>
35 #include <QMutex>
36 #include <QMutexLocker>
37 #include <QLibrary>
38 #include <QProcessEnvironment>
39 #include <QDir>
40 #include <QElapsedTimer>
41
42 /*
43  * Static Objects
44  */
45 QScopedPointer<MUtils::JobObject> AbstractTool::s_jobObjectInstance;
46 QScopedPointer<QElapsedTimer>     AbstractTool::s_startProcessTimer;
47
48 /*
49  * Synchronization
50  */
51 QMutex AbstractTool::s_startProcessMutex;
52 QMutex AbstractTool::s_createObjectMutex;
53
54 /*
55  * Ref Counter
56  */
57 quint64 AbstractTool::s_referenceCounter = 0ui64;
58
59 /*
60  * Const
61  */
62 static const qint64 START_DELAY = 64i64;        //in milliseconds
63
64 /*
65  * Constructor
66  */
67 AbstractTool::AbstractTool(void)
68 :
69         m_firstLaunch(true)
70
71 {
72         QMutexLocker lock(&s_createObjectMutex);
73
74         if(s_referenceCounter++ == 0)
75         {
76                 s_jobObjectInstance.reset(new MUtils::JobObject());
77                 s_startProcessTimer.reset(new QElapsedTimer());
78                 if(!MUtils::OS::setup_timer_resolution())
79                 {
80                         qWarning("Failed to setup system timer resolution!");
81                 }
82         }
83 }
84
85 /*
86  * Destructor
87  */
88 AbstractTool::~AbstractTool(void)
89 {
90         QMutexLocker lock(&s_createObjectMutex);
91
92         if(--s_referenceCounter == 0)
93         {
94                 s_jobObjectInstance.reset(NULL);
95                 s_startProcessTimer.reset(NULL);
96                 if(!MUtils::OS::reset_timer_resolution())
97                 {
98                         qWarning("Failed to reset system timer resolution!");
99                 }
100         }
101 }
102
103 /*
104  * Initialize and launch process object
105  */
106 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
107 {
108         QMutexLocker lock(&s_startProcessMutex);
109         
110         if((!s_startProcessTimer.isNull()) && s_startProcessTimer->isValid())
111         {
112                 qint64 elapsed = s_startProcessTimer->elapsed();
113                 while(elapsed < START_DELAY)
114                 {
115                         lock.unlock();
116                         MUtils::OS::sleep_ms((size_t)(START_DELAY - elapsed));
117                         lock.relock();
118                         elapsed = s_startProcessTimer->elapsed();
119                 }
120         }
121
122         emit messageLogged(commandline2string(program, args) + "\n");
123         MUtils::init_process(process, QFileInfo(program).absolutePath());
124
125         process.start(program, args);
126         
127         if(process.waitForStarted())
128         {
129                 if(!s_jobObjectInstance.isNull())
130                 {
131                         if(!s_jobObjectInstance->addProcessToJob(&process))
132                         {
133                                 qWarning("Failed to assign process to job object!");
134                         }
135                 }
136
137                 MUtils::OS::change_process_priority(&process, -1);
138                 
139                 if(m_firstLaunch)
140                 {
141                         emit statusUpdated(0);
142                         m_firstLaunch = false;
143                 }
144                 
145                 s_startProcessTimer->start();
146                 return true;
147         }
148
149         emit messageLogged("Process creation has failed :-(");
150         QString errorMsg= process.errorString().trimmed();
151         if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
152
153         process.kill();
154         process.waitForFinished(-1);
155
156         s_startProcessTimer->start();
157         return false;
158 }
159
160 /*
161  * Convert program arguments to single string
162  */
163 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
164 {
165         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
166         
167         for(int i = 0; i < arguments.count(); i++)
168         {
169                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
170         }
171
172         return commandline;
173 }
174
175