d/control: Recommend gdb
[gammaray-debian.git] / injector / styleinjector.cpp
blob3d23a126e2aea346f97fb210aedb8678b4e958ae
1 /*
2 styleinjector.cpp
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
7 Copyright (C) 2010-2011 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <config-gammaray.h>
26 #include "styleinjector.h"
27 #include "interactiveprocess.h"
29 #include <QFileInfo>
30 #include <QProcess>
31 #include <cstdlib>
33 using namespace GammaRay;
35 StyleInjector::StyleInjector() :
36 mExitCode(-1),
37 mProcessError(QProcess::UnknownError),
38 mExitStatus(QProcess::NormalExit)
42 bool StyleInjector::launch(const QStringList &programAndArgs,
43 const QString &probeDll, const QString &probeFunc)
45 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
46 env.insert("GAMMARAY_STYLEINJECTOR_PROBEDLL", probeDll);
47 env.insert("GAMMARAY_STYLEINJECTOR_PROBEFUNC", probeFunc);
49 QString qtPluginPath = env.value("QT_PLUGIN_PATH");
50 if (!qtPluginPath.isEmpty()) {
51 qtPluginPath.append(":");
53 qtPluginPath.append(GAMMARAY_LIB_INSTALL_DIR "/qt4/plugins");
54 env.insert("QT_PLUGIN_PATH", qtPluginPath);
56 InteractiveProcess proc;
57 proc.setProcessEnvironment(env);
58 proc.setProcessChannelMode(QProcess::ForwardedChannels);
60 QStringList args = programAndArgs;
62 if (env.value("GAMMARAY_GDB").toInt()) {
63 QStringList newArgs;
64 newArgs << "gdb" << "--eval-command" << "run" << "--args";
65 newArgs += args;
66 args = newArgs;
67 } else if (env.value("GAMMARAY_MEMCHECK").toInt()) {
68 QStringList newArgs;
69 newArgs << "valgrind" << "--tool=memcheck" << "--track-origins=yes" << "--num-callers=25";
70 newArgs += args;
71 args = newArgs;
72 } else if (env.value("GAMMARAY_HELGRIND").toInt()) {
73 QStringList newArgs;
74 newArgs << "valgrind" << "--tool=helgrind";
75 newArgs += args;
76 args = newArgs;
79 const QString program = args.takeFirst();
80 args << QLatin1String("-style") << QLatin1String("gammaray-injector");
81 proc.start(program, args);
82 proc.waitForFinished(-1);
84 mExitCode = proc.exitCode();
85 mProcessError = proc.error();
86 mExitStatus = proc.exitStatus();
87 mErrorString = proc.errorString();
89 return mExitCode == EXIT_SUCCESS && mExitStatus == QProcess::NormalExit;
92 bool StyleInjector::selfTest()
94 // TODO: be a bit more clever in finding the plugin location (also when actually using it above)
95 #ifndef Q_OS_WIN
96 const QString stylePath =
97 QLatin1String(GAMMARAY_LIB_INSTALL_DIR "/qt4/plugins/styles/gammaray_injector_style.so");
98 #else
99 const QString stylePath =
100 QLatin1String(GAMMARAY_LIB_INSTALL_DIR "/qt4/plugins/styles/gammaray_injector_style.dll");
101 #endif
103 QFileInfo fi(stylePath);
104 if (!fi.exists() || !fi.isFile() || !fi.isReadable()) {
105 mErrorString =
106 QObject::tr("Injector style plugin does not exists or is not readable at %1.").arg(stylePath);
107 return false;
110 return true;
113 int StyleInjector::exitCode()
115 return mExitCode;
118 QProcess::ProcessError StyleInjector::processError()
120 return mProcessError;
123 QProcess::ExitStatus StyleInjector::exitStatus()
125 return mExitStatus;
128 QString StyleInjector::errorString()
130 return mErrorString;