Debian Codegraph @

src/plugin/test_folderlistmodel/simpleUI/terminalfolderapp.cpp

raw symbols SCIP
1 /**************************************************************************
2 *
3 * Copyright 2013 Canonical Ltd.
4 * Copyright 2013 Carlos J Mazieri <[email protected]>
5 *
6 * You may use this file under the terms of the BSD license as follows:
7 *
8 * "Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Nemo Mobile nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
32 *
33 * Filename: terminalfolderapp.cpp
34 * Created : 29 Jul 2013
35 **/
36
37
38 #include "terminalfolderapp.h"
39
40 #include <QDir>
41 #include <QFileInfo>
42 #include <QDebug>
43 #include <QCoreApplication>
44
45 #if defined(Q_OS_MSDOS) || defined(Q_OS_WIN)
46 // defined WIN Process
47 #else // Q_OS_UNIX
48 # include <sys/types.h>
49 # include <signal.h>
50 # include <unistd.h>
51 #endif
52
53 #define DEBUG_MESSAGES 1
54
55 TerminalFolderApp::TerminalFolderApp(QObject *parent) :
56 QObject(parent)
57 {
58 findTerminalApp();
59 }
60
61 TerminalFolderApp::~TerminalFolderApp()
62 {
63 }
64
65
66 //=======================================================================================================================
67 /*!
68 * \brief TerminalFolderApp::openTerminal() tries to open a terminal
69 *
70 * It tries to open the terminal application in the sequence:
71 * \li "meego-terminal" for Nemo Mobile and Nokia N9
72 * \li "terminal-app" for Ubuntu Touch
73 * \li "x-terminal-emulator" for any Desktop such as KDE and Gnome
74 * \li TERM environment variable if defined
75 *
76 * \param currentDir
77 * \return true if the terminal could be opened
78 */
79 bool TerminalFolderApp::openTerminal(const QString& currentDir)
80 {
81 QFileInfo f(currentDir);
82 bool launched = false;
83 bool ok = false;
84 Q_PID pid = 0;
85 if (!m_terminalApp.isEmpty() &&
86 f.isDir() &&
87 f.isReadable() &&
88 f.isExecutable())
89 {
90 launched = true;
91 switch(m_params.count())
92 {
93 case 2: m_params.append(currentDir); break;
94 case 3: m_params[2] = currentDir;
95 default: break;
96 }
97 #if DEBUG_MESSAGES
98 qDebug() << Q_FUNC_INFO << "starting" << m_terminalApp
99 << m_params << "at" << currentDir;
100 #endif
101 ok = QProcess::startDetached(m_terminalApp,
102 m_params, currentDir, &pid);
103 if (ok)
104 {
105 #if ENABLE_CLOSING
106 emit openCounterChanged(m_openPids.count() + 1);
107 #endif
108 m_openPids.append(pid);
109 }
110 }//end found a terminal application and currentDir is good
111 #if DEBUG_MESSAGES
112 qDebug() << Q_FUNC_INFO << "launched" << launched << "ok" << ok << "returning pid" << pid;
113 #endif
114
115 return ok;
116 }
117
118
119
120 //=======================================================================================================================
121 /*!
122 * \brief TerminalFolderApp::findTerminalApp() finds a suitable terminal application, called only in the creator
123 */
124 void TerminalFolderApp::findTerminalApp()
125 {
126 #if defined(Q_OS_MSDOS) || defined(Q_OS_WIN)
127 m_terminalApp = "cmd.exe";
128 #else // Q_OS_UNIX
129 char terminalEnvVar[128];
130 terminalEnvVar[0] = 0;
131 QByteArray term = qgetenv("TERM");
132 if (term.size() > 0)
133 {
134 ::qstrcpy(terminalEnvVar, term.constData());
135 }
136
137 // Ubuntu Touch
138 // appid://com.ubports.terminal/terminal/current-user-version"
139
140
141 const char *desktop_terminal_emulator = "x-terminal-emulator";
142 const char * apps [] =
143 {
144 "meego-terminal" // Nemo Mobile and Nokia N9
145 ,"terminal-app" // Ubuntu Touch
146 ,desktop_terminal_emulator // Desktop (any) KDE/GNOME
147 ,terminalEnvVar // TERM environment variable
148 };
149
150 QByteArray usr_bin("/usr/bin");
151 QByteArray path = qgetenv("PATH");
152 if (path.size() == 0 ) {
153 path = usr_bin;
154 }
155 QList<QByteArray> dirs(path.split(':'));
156 if (!dirs.contains(usr_bin)) {
157 dirs.append(usr_bin);
158 }
159 bool found = false;
160 for (int counter = 0; !found && counter < dirs.count(); ++counter)
161 {
162 for(size_t counter2=0 ; !found && counter2 < sizeof(apps)/sizeof(apps[0]); ++counter2)
163 {
164 if (apps[counter2][0])
165 {
166 QString terminalPath = dirs[counter];
167 terminalPath += QDir::separator();
168 terminalPath += apps[counter2];
169 QFileInfo terminal(terminalPath);
170 if (terminal.exists() && terminal.isExecutable())
171 {
172 m_terminalApp = terminalPath;
173 found = true;
174 if (terminalPath.endsWith(QLatin1String(desktop_terminal_emulator)))
175 {
176 findDesktopParameter();
177 }
178 }
179 }
180 }
181 }
182 #if DEBUG_MESSAGES
183 qDebug() << Q_FUNC_INFO << m_terminalApp;
184 #endif
185 #endif
186 }
187
188
189 //=======================================================================================================================
190 /*!
191 * \brief TerminalFolderApp::findDesktopParameter() sets the working directory parameter
192 *
193 * Options:
194 * \li if konsole --nofork --workdir <dir>
195 * \li if gnome-terminal "--disable-factory --working-dir <dir>
196 */
197 void TerminalFolderApp::findDesktopParameter()
198 {
199 QFileInfo app(m_terminalApp);
200 int counter = 10;
201 while (counter-- && app.isSymLink())
202 {
203 app.setFile(app.symLinkTarget());
204 #if DEBUG_MESSAGES
205 qDebug() << Q_FUNC_INFO << "testing" << app.fileName();
206 #endif
207 }
208 if (app.absoluteFilePath().endsWith(QLatin1String("konsole")))
209 {
210 m_params.append(QLatin1String("--nofork"));
211 m_params.append(QLatin1String("--workdir"));
212 if (app.isAbsolute())
213 {
214 m_terminalApp = app.absoluteFilePath();
215 }
216 }
217 else
218 if (app.absoluteFilePath().endsWith(QLatin1String("gnome-terminal")))
219 {
220 if (app.isAbsolute())
221 {
222 QFileInfo gnomeTerm(app.absolutePath(),QLatin1String("gnome-terminal"));
223 if (gnomeTerm.isExecutable() && gnomeTerm.isFile())
224 {
225 m_terminalApp = gnomeTerm.absoluteFilePath();
226 }
227 }
228 m_params.append(QLatin1String("--disable-factory"));
229 m_params.append(QLatin1String("--working-dir"));
230 }
231 }
232
233
234 #if ENABLE_CLOSING
235
236 //=======================================================================================================================
237 /*!
238 * \brief TerminalFolderApp::closeTerminal() tries to close the terminal app
239 * \param index index of opened terminals, if more than one.
240 * \return true could close the terminal
241 */
242 bool TerminalFolderApp::closeTerminal(int index)
243 {
244 Q_PID pid = 0;
245 bool closed = false;
246 int counter = m_openPids.count();
247 int before = counter;
248 while (counter--)
249 {
250 // check if any terminal that was closed by the user
251 pid = m_openPids.at(counter);
252 int test = ::kill(pid, 0);
253 #if DEBUG_MESSAGES
254 qDebug() << Q_FUNC_INFO << "pid" << pid
255 << "ret from kill" << test;
256 #endif
257 if ( test != 0 )
258 {
259 m_openPids.takeAt(counter);
260 }
261 }
262 #if DEBUG_MESSAGES
263 qDebug() << Q_FUNC_INFO << "active terminals" << m_openPids.count();
264 #endif
265 if (index >= 0 && m_openPids.count() > 0)
266 {
267 if (index >= m_openPids.count())
268 {
269 index = m_openPids.count() -1;
270 }
271 pid = m_openPids.takeAt(index);
272 if (!(closed = killPid(pid)))
273 {
274 m_openPids.append(pid);
275 }
276 }
277 if (before != m_openPids.count())
278 {
279 emit openCounterChanged(m_openPids.count());
280 }
281 return closed;
282 }
283
284
285
286 //=======================================================================================================================
287 /*!
288 kill a process.
289
290 Tries to kill using SIGTERM which is the default signal to finish a process.
291 If the process does not finish then sends a SIGKILL which is equivalment to "kill -9" command
292
293 \return TRUE the process does not exist anymore (it was successfully killed).
294 FALSE could not kill the process
295 */
296 //=======================================================================================================================
297 bool TerminalFolderApp::killPid(Q_PID pid)
298 {
299 bool processIsNotRunning = true;
300 if (::kill(pid, SIGTERM) == 0)
301 {
302 int counter = 15;
303 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
304 while(counter-- && ::kill(pid, 0) == 0)
305 {
306 ::usleep(50);
307 switch(counter)
308 {
309 case 13: ::kill(pid, SIGKILL); break;
310 default: break;
311 }
312 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
313 }
314 if (::kill(pid, 0) == 0)
315 {
316 processIsNotRunning = false;
317 }
318 }
319 #if DEBUG_MESSAGES
320 qDebug() << Q_FUNC_INFO << "pid" << pid << "ret" << processIsNotRunning;
321 #endif
322 return processIsNotRunning;
323 }
324
325
326 #endif // ENABLE_CLOSING