OSDN Git Service

35450a34f5bb32f5ef5cf7a3d33a498fa63cee71
[lamexp/LameXP.git] / src / Tool_Abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 #include "JobObject.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
32
33 //Qt
34 #include <QProcess>
35 #include <QMutex>
36 #include <QMutexLocker>
37 #include <QLibrary>
38 #include <QProcessEnvironment>
39 #include <QDir>
40
41 /*
42  * Static vars
43  */
44 quint64 AbstractTool::s_lastLaunchTime = 0ui64;
45 QMutex AbstractTool::s_mutex_startProcess;
46 JobObject *AbstractTool::s_jobObject = NULL;
47 unsigned int AbstractTool::s_jobObjRefCount = 0U;
48
49 /*
50  * Const
51  */
52 static const unsigned int START_DELAY = 333;                                    //in milliseconds
53 static const quint64 START_DELAY_NANO = START_DELAY * 1000 * 10; //in 100-nanosecond intervals
54
55 /*
56  * Constructor
57  */
58 AbstractTool::AbstractTool(void)
59 {
60         QMutexLocker lock(&s_mutex_startProcess);
61
62         if(s_jobObjRefCount < 1U)
63         {
64                 s_jobObject = new JobObject();
65                 s_jobObjRefCount = 1U;
66         }
67         else
68         {
69                 s_jobObjRefCount++;
70         }
71
72         m_firstLaunch = true;
73 }
74
75 /*
76  * Destructor
77  */
78 AbstractTool::~AbstractTool(void)
79 {
80         QMutexLocker lock(&s_mutex_startProcess);
81
82         if(s_jobObjRefCount >= 1U)
83         {
84                 s_jobObjRefCount--;
85                 if(s_jobObjRefCount < 1U)
86                 {
87                         MUTILS_DELETE(s_jobObject);
88                 }
89         }
90 }
91
92 /*
93  * Initialize and launch process object
94  */
95 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
96 {
97         QMutexLocker lock(&s_mutex_startProcess);
98
99         if(lamexp_current_file_time() <= s_lastLaunchTime)
100         {
101                 MUtils::OS::sleep_ms(START_DELAY);
102         }
103
104         emit messageLogged(commandline2string(program, args) + "\n");
105         MUtils::init_process(process, QFileInfo(program).absolutePath());
106
107         process.start(program, args);
108         
109         if(process.waitForStarted())
110         {
111                 if(s_jobObject)
112                 {
113                         if(!s_jobObject->addProcessToJob(&process))
114                         {
115                                 qWarning("Failed to assign process to job object!");
116                         }
117                 }
118
119                 lamexp_change_process_priority(&process, -1);
120                 lock.unlock();
121                 
122                 if(m_firstLaunch)
123                 {
124                         emit statusUpdated(0);
125                         m_firstLaunch = false;
126                 }
127                 
128                 s_lastLaunchTime = lamexp_current_file_time() + START_DELAY_NANO;
129                 return true;
130         }
131
132         emit messageLogged("Process creation has failed :-(");
133         QString errorMsg= process.errorString().trimmed();
134         if(!errorMsg.isEmpty()) emit messageLogged(errorMsg);
135
136         process.kill();
137         process.waitForFinished(-1);
138
139         s_lastLaunchTime = lamexp_current_file_time() + START_DELAY_NANO;
140         return false;
141 }
142
143 /*
144  * Convert program arguments to single string
145  */
146 QString AbstractTool::commandline2string(const QString &program, const QStringList &arguments)
147 {
148         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
149         
150         for(int i = 0; i < arguments.count(); i++)
151         {
152                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
153         }
154
155         return commandline;
156 }
157
158