1/*
2 Copyright (C) 2009 George Kiagiadakis <gkiagia@users.sourceforge.net>
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 2 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#include "debuggermanager.h"
18
19#include <KConfigGroup>
20#include <KGlobal>
21
22#include "debugger.h"
23#include "debuggerlaunchers.h"
24#include "backtracegenerator.h"
25
26struct DebuggerManager::Private
27{
28 BacktraceGenerator *btGenerator;
29 bool debuggerRunning;
30 QList<AbstractDebuggerLauncher*> externalDebuggers;
31 DBusOldInterfaceLauncher *dbusOldInterfaceLauncher;
32};
33
34DebuggerManager::DebuggerManager(const Debugger & internalDebugger,
35 const QList<Debugger> & externalDebuggers,
36 QObject *parent)
37 : QObject(parent), d(new Private)
38{
39 d->debuggerRunning = false;
40 d->btGenerator = new BacktraceGenerator(internalDebugger, this);
41 connect(d->btGenerator, SIGNAL(starting()), SLOT(onDebuggerStarting()));
42 connect(d->btGenerator, SIGNAL(done()), SLOT(onDebuggerFinished()));
43 connect(d->btGenerator, SIGNAL(someError()), SLOT(onDebuggerFinished()));
44 connect(d->btGenerator, SIGNAL(failedToStart()), SLOT(onDebuggerFinished()));
45
46 foreach(const Debugger & debugger, externalDebuggers) {
47 if (debugger.isInstalled()) {
48 AbstractDebuggerLauncher *l = new DefaultDebuggerLauncher(debugger, this); //FIXME
49 d->externalDebuggers.append(l);
50 connect(l, SIGNAL(starting()), SLOT(onDebuggerStarting()));
51 connect(l, SIGNAL(finished()), SLOT(onDebuggerFinished()));
52 connect(l, SIGNAL(invalidated()), SLOT(onDebuggerInvalidated()));
53 }
54 }
55
56 //setup kdevelop compatibility
57 d->dbusOldInterfaceLauncher = new DBusOldInterfaceLauncher(this);
58 connect(d->dbusOldInterfaceLauncher, SIGNAL(starting()), SLOT(onDebuggerStarting()));
59 connect(d->dbusOldInterfaceLauncher, SIGNAL(available()), SLOT(onDBusOldInterfaceDebuggerAvailable()));
60}
61
62DebuggerManager::~DebuggerManager()
63{
64 if (d->btGenerator->state() == BacktraceGenerator::Loading) {
65 //if the debugger is running, kill it and continue the process.
66 delete d->btGenerator;
67 onDebuggerFinished();
68 }
69
70 delete d;
71}
72
73bool DebuggerManager::debuggerIsRunning() const
74{
75 return d->debuggerRunning;
76}
77
78bool DebuggerManager::showExternalDebuggers() const
79{
80 KConfigGroup config(KGlobal::config(), "DrKonqi");
81 bool showDebugger = config.readEntry("ShowDebugButton", false);
82
83 // TODO: remove all these compatibility code when KDE SC 4.11
84 // is considered as totally outdated
85 //
86 //for compatibility with drkonqi 1.0, if "ShowDebugButton" is not specified in the config
87 //and the old "ConfigName" key exists and is set to "developer", we show the debug button.
88 if (!config.hasKey("ShowDebugButton") &&
89 config.readEntry("ConfigName") == "developer") {
90 showDebugger = true;
91 // migrate and remove the long deprecated entry
92 config.writeEntry("ShowDebugButton", true);
93 config.deleteEntry("ConfigName");
94 }
95
96 return showDebugger;
97}
98
99QList<AbstractDebuggerLauncher*> DebuggerManager::availableExternalDebuggers() const
100{
101 return d->externalDebuggers;
102}
103
104BacktraceGenerator* DebuggerManager::backtraceGenerator() const
105{
106 return d->btGenerator;
107}
108
109void DebuggerManager::onDebuggerStarting()
110{
111 d->debuggerRunning = true;
112 emit debuggerStarting();
113 emit debuggerRunning(true);
114}
115
116void DebuggerManager::onDebuggerFinished()
117{
118 d->debuggerRunning = false;
119 emit debuggerFinished();
120 emit debuggerRunning(false);
121}
122
123void DebuggerManager::onDebuggerInvalidated()
124{
125 AbstractDebuggerLauncher *launcher = qobject_cast<AbstractDebuggerLauncher*>(sender());
126 Q_ASSERT(launcher);
127 int index = d->externalDebuggers.indexOf(launcher);
128 Q_ASSERT(index >= 0);
129 d->externalDebuggers.removeAt(index);
130 emit externalDebuggerRemoved(launcher);
131}
132
133void DebuggerManager::onDBusOldInterfaceDebuggerAvailable()
134{
135 d->externalDebuggers.append(d->dbusOldInterfaceLauncher);
136 emit externalDebuggerAdded(d->dbusOldInterfaceLauncher);
137}
138
139#include "debuggermanager.moc"
140