File: BeaverDebugger.cpp

package info (click to toggle)
monkeystudio 1.9.0.4%2Bgit20161218-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,500 kB
  • ctags: 22,118
  • sloc: cpp: 144,671; ansic: 33,969; python: 2,922; makefile: 127; sh: 122; php: 73; cs: 69
file content (333 lines) | stat: -rw-r--r-- 11,485 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/****************************************************************************
    Copyright (C) 2005 - 2011  Filipe AZEVEDO & The Monkey Studio Team
    http://monkeystudio.org licensing under the GNU GPL.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
****************************************************************************/
/*!
    \file BeaverDebugger.h
    \date 2008-01-14T00:40:08
    \author Andrei KOPATS
    \brief Header file for BeaverDebugger plugin
*/


#include <QDebug>
#include <QIcon>
#include <QLabel>
#include <QMessageBox>

#include <statusbar/StatusBar.h>
#include <pMonkeyStudio.h>
#include <workspace/pFileManager.h>
#include <xupmanager/core/XUPProjectItem.h>
#include <pMenuBar.h>

#include "BeaverDebugger.h"
#include "BeaverDebuggerSettings.h"

void BeaverDebugger::fillPluginInfos()
{
    mPluginInfos.Caption = tr( "Beaver Debugger" );
    mPluginInfos.Description = tr( "Plugin for use Beaver Debugger together with MkS" );
    mPluginInfos.Author = "Andei Kopats aka hlamer <hlamer@tut.by>";
    mPluginInfos.Type = BasePlugin::iBase | BasePlugin::iDebugger;
    mPluginInfos.Name = PLUGIN_NAME;
    mPluginInfos.Version = "1.0.0";
    mPluginInfos.FirstStartEnabled = false;
    mPluginInfos.HaveSettingsWidget = true;
    mPluginInfos.Pixmap = QPixmap( ":/icons/beaverdbg.png" );
}

/*!
    Install plugin to the system
    \return Status code of action
    \retval true Successfull
    \retval false Some error ocurred
*/
bool BeaverDebugger::install()
{
#ifdef Q_OS_WIN
    mBeaverPath = settingsValue("BeaverPath", "C:\\Programm Files\\Beaver Debugger\\beaverdbg.exe").toString();
#else
    mBeaverPath = settingsValue("BeaverPath", "beaverdbg").toString();
#endif

    mBeaverProcess = new QProcess( this );
    
    connect(mBeaverProcess, SIGNAL(stateChanged(QProcess::ProcessState)),
            this, SLOT(beaverStateChanged(QProcess::ProcessState)));
    
    if (OK == tryFindBeaver()) // FIXME debugger found
    {
        mRunBeaver = MonkeyCore::menuBar()->action( "mDebugger/aRunBeaver",  
                                                    tr( "Run Beaver" ), 
                                                    QIcon( ":/icons/beaverdbg.png" ), 
                                                    "F5", // shortcut
                                                    "Start debugging session with the external debugger");
        updateRunAction();
        connect( mRunBeaver, SIGNAL( triggered() ), this, SLOT( runBeaver() ) );
        connect(MonkeyCore::fileManager(), SIGNAL(currentChanged(XUPProjectItem*)),
            this, SLOT(updateRunAction()));
    }
    else // debugger not found
    {
        mWhyCannot = MonkeyCore::menuBar()->action( "mDebugger/aWhyCannot",  
                                                    tr( "Why can't I debug my app" ), 
                                                    QIcon( ":/icons/beaverdbg.png" ), 
                                                    "", // shortcut
                                                    "Check Beaver Debugger status" );
        connect( mWhyCannot, SIGNAL( triggered() ), this, SLOT( explainWhyCannot() ) );
    }
    
    return true;
}

/*!
    Unnstall plugin from the system
    \return Status code of action
    \retval true Successfull
    \retval false Some error ocurred
*/
bool BeaverDebugger::uninstall()
{
    disconnect(MonkeyCore::fileManager(), SIGNAL(currentChanged(XUPProjectItem*)),
               this, SLOT(updateRunAction()));
    
    delete mBeaverProcess;
    delete mWhyCannot;
    delete mRunBeaver;
    delete mStatusLabel;
    return true;
}

/*!
    Get settings widget of plugin
    \return Pointer to created settings widget for plugin
*/
QWidget* BeaverDebugger::settingsWidget() const
{
    return new BeaverDebuggerSettings(const_cast<BeaverDebugger*>(this));
}

QString BeaverDebugger::beaverPath()
{
    if (mBeaverPath.isNull())
        mBeaverPath = "beaverdbg";
    
    return mBeaverPath;
}

void BeaverDebugger::setBeaverPath(const QString& path)
{
    mBeaverPath = path;
    setSettingsValue("BeaverPath", mBeaverPath);
}

/*!
    Shows Beaver Debugger detection dialog
*/

void BeaverDebugger::explainWhyCannot()
{
    bool try_again = true;
    
    while (try_again)
    {
        try_again = false;
        
        TryFindResult res = tryFindBeaver();
        
        QString fullText;
        
        switch (res)
        {
            case OK:
                fullText += "Beaver Debugger found!\n"
                            "You can use it now.\n";
            break;
            case NOT_FINISHED:
                fullText += tr("Failed to identify Beaver Debugger. "
                                "System is too slow, or applications works incorrectly.\n");
                break;
                case FAILED_TO_START:
                fullText += tr("Failed to start Beaver Debugger. Executable file not found, "
                                "or you have no permissions to execute it.\n\n");
#ifdef Q_OS_LINUX   
                fullText += tr("Beaver Debugger might be included to your Linux distribution."
                                "Package name probably is 'beaverdbg'.\n");
#endif
                fullText += tr("For install it using official release, download installer or sources from "
                            "http://beaverdbg.googlecode.com and follow installation instructions.\n");
                fullText += "\n";
                fullText += tr("If Beaver Debugger is installed, but not found, "
                            "go to plugin configuration dialog and configure path to it.\n");
                fullText += "\n";
            break;
            case CRASHED:
                fullText += tr("Beaver Debugger crashed during atempt to start it.\n");
            break;
            case UNKNOWN_ERROR:
                fullText += tr("Unknown error.\n");
            break;
            case NOT_BEAVER:
                fullText += tr("Beaver Debugger executable found, but not identified as Beaver Debugger. "
                                "It might be not a Beaver Debugger, or version is unsupported.\n");
                break;
        }
        
        if (res != OK)
        {
            fullText += "\n";
            fullText += tr("Press Retry for try to detect debugger again, or Open for open configuration dialog");
            
            QMessageBox::StandardButton answer  = 
                        QMessageBox::information(NULL,
                                                tr("Beaver Debugger"),
                                                fullText,
                                                QMessageBox::Retry | QMessageBox::Open | QMessageBox::Cancel);
            switch (answer)
            {
                case QMessageBox::Retry:
                    try_again = true;
                break;
                case QMessageBox::Open:
                    static_cast<QDialog*>(settingsWidget())->exec();
                    try_again = true;
                break;
                default:
                break;
            }
        }
        else // found, OK
        {
            QMessageBox::information(NULL,
                                        tr("Beaver Debugger"),
                                        fullText,
                                        QMessageBox::Ok);
                uninstall();
                install();
        }
    }
}

void BeaverDebugger::runBeaver()
{
    if (mBeaverProcess->state() == QProcess::NotRunning)
    {
        XUPProjectItem* project = MonkeyCore::fileManager()->currentProject();
        if (project)
        {
            QString target = project->targetFilePath(XUPProjectItem::DebugTarget);
            QFileInfo finfo(target);
            if (target.isEmpty())
            {
                QMessageBox::critical(NULL,
                                     tr("Beaver Debugger"),
                                     tr("Target file for the project is unknown."),
                                     QMessageBox::Ok);
                return;
            }
            else if (!finfo.exists())
            {
                QMessageBox::critical(NULL,
                                     tr("Beaver Debugger"),
                                     tr("Target file '%1' not found.").arg(target),
                                     QMessageBox::Ok);
                return;
            }
            else if (!finfo.isExecutable())
            {
                QMessageBox::critical(NULL,
                                     tr("Beaver Debugger"),
                                     tr("Target file '%11 is not an executable.").arg(target),
                                     QMessageBox::Ok);
                return;
            }
            
            qDebug() << "atempt to run" << target;
            mBeaverProcess->start(mBeaverPath, QStringList() << target);
        }
        else
        {
            Q_ASSERT_X(0, "BeaverDebugger", "Atempt to run debugger without active project");
        }
    }
    else
    {
        mBeaverProcess->terminate();
    }
}

void BeaverDebugger::beaverStateChanged(QProcess::ProcessState state)
{
    switch (state)
    {
        case QProcess::NotRunning:
            if (mStatusLabel)
            {
                delete mStatusLabel;
                mStatusLabel = NULL;
            }
        break;
        case QProcess::Starting:
            if (! mStatusLabel)
            {
                mStatusLabel = new QLabel(tr("Beaver is running"));
                MonkeyCore::statusBar()->addPermanentWidget(mStatusLabel);
            }
        break;
        default:
        break;          
    }
    
    updateRunAction();
}

BeaverDebugger::TryFindResult BeaverDebugger::tryFindBeaver() const
{
    switch ( QProcess::execute( mBeaverPath, QStringList( "--version" ) ) ) {
        // QProcess::FailedToStart
        case -2:
            return FAILED_TO_START;
        // QProcess::Crashed
        case -1:
            return CRASHED;
        // Ok
        default:
            return OK;
    }
}

void BeaverDebugger::updateRunAction()
{
    if (mBeaverProcess->state() == QProcess::NotRunning)
    {
        mRunBeaver->setText(tr("Debug current project"));
        mRunBeaver->setToolTip(tr("Start debugging session with the Beaver Debugger"));
        mRunBeaver->setStatusTip(tr("Start debugging session with the Beaver Debugger"));
    }
    else
    {
        mRunBeaver->setText(tr("Stop Beaver"));
        mRunBeaver->setToolTip(tr("Stop executed debugger"));
        mRunBeaver->setStatusTip(tr("Stop executed debugger"));
    }
    mRunBeaver->setEnabled(MonkeyCore::fileManager()->currentProject() != NULL);
}

#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2( BaseBeaverDebugger, BeaverDebugger )
#endif