1 /*  QWinFF - a qt4 gui frontend for ffmpeg
2  *  Copyright (C) 2011-2013 Timothy Lin <lzh9102@gmail.com>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "exepath.h"
19 #include <QMap>
20 #include <QSettings>
21 #include <QProcess>
22 
23 #ifdef OPERATION_TIMEOUT
24 #define TIMEOUT OPERATION_TIMEOUT
25 #else
26 #define TIMEOUT 30000
27 #endif
28 
29 namespace
30 {
31 typedef QMap<QString, QString> Map;
32 Map program_path;
33 }
34 
setPath(QString program,QString path)35 void ExePath::setPath(QString program, QString path)
36 {
37     program_path.insert(program, path);
38 }
39 
getPath(QString program)40 QString ExePath::getPath(QString program)
41 {
42     if (program_path.contains(program))
43         return program_path[program];
44     else
45         Q_ASSERT_X(false, "ExePath::getPath"
46                    , QString("Program path of '%1' has not been set.")
47                    .arg(program).toStdString().c_str());
48     return "";
49 }
50 
checkProgramAvailability(QString program)51 bool ExePath::checkProgramAvailability(QString program)
52 {
53     if (!program_path.contains(program)) // the program is not set
54         return false;
55     QProcess proc;
56     QStringList param;
57     // try to run the program
58     proc.start(ExePath::getPath(program), param);
59     if (!proc.waitForStarted(TIMEOUT))
60         return false; // failed to start the program
61     // successfully started the program, kill it immediately
62     proc.kill();
63     proc.waitForFinished(TIMEOUT);
64     return true;
65 }
66 
saveSettings()67 void ExePath::saveSettings()
68 {
69     QSettings settings;
70     foreach (QString name, program_path.keys()) {
71         QString path = program_path[name];
72         settings.setValue("exepath/" + name, path);
73     }
74 }
75 
loadSettings()76 void ExePath::loadSettings()
77 {
78     QSettings settings;
79     foreach (QString name, program_path.keys()) {
80         QString path = settings.value("exepath/" + name
81                                       , program_path[name]).toString();
82         program_path[name] = path;
83     }
84 }
85 
getPrograms()86 QList<QString> ExePath::getPrograms()
87 {
88     return program_path.keys();
89 }
90