OSDN Git Service

Updated "success" sound.
[lamexp/LameXP.git] / src / Tool_Abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Tool_Abstract.h"
23
24 #include "Global.h"
25 #include "JobObject.h"
26
27 #include <QProcess>
28 #include <QMutex>
29 #include <QMutexLocker>
30 #include <QLibrary>
31 #include <QProcessEnvironment>
32 #include <QDir>
33
34 /*
35  * Static vars
36  */
37 quint64 AbstractTool::s_lastLaunchTime = 0ui64;
38 QMutex AbstractTool::s_mutex_startProcess;
39 JobObject *AbstractTool::s_jobObject = NULL;
40 unsigned int AbstractTool::s_jobObjRefCount = 0U;
41
42 /*
43  * Const
44  */
45 static const unsigned int START_DELAY = 333;                                    //in milliseconds
46 static const quint64 START_DELAY_NANO = START_DELAY * 1000 * 10; //in 100-nanosecond intervals
47
48 /*
49  * Constructor
50  */
51 AbstractTool::AbstractTool(void)
52 {
53         QMutexLocker lock(&s_mutex_startProcess);
54
55         if(s_jobObjRefCount < 1U)
56         {
57                 s_jobObject = new JobObject();
58                 s_jobObjRefCount = 1U;
59         }
60         else
61         {
62                 s_jobObjRefCount++;
63         }
64
65         m_firstLaunch = true;
66 }
67
68 /*
69  * Destructor
70  */
71 AbstractTool::~AbstractTool(void)
72 {
73         QMutexLocker lock(&s_mutex_startProcess);
74
75         if(s_jobObjRefCount >= 1U)
76         {
77                 s_jobObjRefCount--;
78                 if(s_jobObjRefCount < 1U)
79                 {
80                         LAMEXP_DELETE(s_jobObject);
81                 }
82         }
83 }
84
85 /*
86  * Initialize and launch process object
87  */
88 bool AbstractTool::startProcess(QProcess &process, const QString &program, const QStringList &args)
89 {
90         QMutexLocker lock(&s_mutex_startProcess);
91
92         if(lamexp_current_file_time() <= s_lastLaunchTime)
93         {
94                 lamexp_sleep(START_DELAY);
95         }
96
97         emit messageLogged(commandline2string(program, args) + "\n");
98
99         QProcessEnvironment env = process.processEnvironment();
100         if(env.isEmpty()) env = QProcessEnvironment::systemEnvironment();
101         env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
102         env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
103         process.setProcessEnvironment(env);
104                 
105         process.setProcessChannelMode(QProcess::MergedChannels);
106         process.setReadChannel(QProcess::StandardOutput);
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