2 This file is part of Akonadi.
4 Copyright (c) 2006 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 #include "debugwidget.h"
24 #include "tracernotificationinterface.h"
25 #include "connectionpage.h"
27 #include <akonadi/control.h>
29 #include <KFileDialog>
32 #include <QtGui/QPushButton>
33 #include <QtGui/QSplitter>
34 #include <QtGui/QTabWidget>
35 #include <QtGui/QTextEdit>
36 #include <QtGui/QVBoxLayout>
37 #include <QtGui/QCheckBox>
39 using org::freedesktop::Akonadi::DebugInterface
;
41 DebugWidget::DebugWidget( QWidget
*parent
)
44 QVBoxLayout
*layout
= new QVBoxLayout( this );
46 mDebugInterface
= new DebugInterface( "org.freedesktop.Akonadi", "/debug", QDBusConnection::sessionBus(), this );
47 QCheckBox
*cb
= new QCheckBox( i18n("Enable debugger"), this );
48 cb
->setChecked( mDebugInterface
->isValid() && mDebugInterface
->tracer().value() == QLatin1String( "dbus" ) );
49 connect( cb
, SIGNAL(toggled(bool)), SLOT(enableDebugger(bool)) );
50 layout
->addWidget( cb
);
52 QSplitter
*splitter
= new QSplitter( Qt::Vertical
, this );
53 splitter
->setObjectName( "debugSplitter" );
54 layout
->addWidget( splitter
);
56 mConnectionPages
= new QTabWidget( splitter
);
57 mConnectionPages
->setTabsClosable( true );
59 mGeneralView
= new QTextEdit( splitter
);
60 mGeneralView
->setReadOnly( true );
62 ConnectionPage
*page
= new ConnectionPage( "All" );
63 page
->showAllConnections( true );
64 mConnectionPages
->addTab( page
, "All" );
66 connect( mConnectionPages
, SIGNAL(tabCloseRequested(int)), SLOT(tabCloseRequested(int)) );
68 org::freedesktop::Akonadi::TracerNotification
*iface
= new org::freedesktop::Akonadi::TracerNotification( QString(), "/tracing/notifications", QDBusConnection::sessionBus(), this );
70 connect( iface
, SIGNAL(connectionStarted(QString
,QString
)),
71 this, SLOT(connectionStarted(QString
,QString
)) );
72 connect( iface
, SIGNAL(connectionEnded(QString
,QString
)),
73 this, SLOT(connectionEnded(QString
,QString
)) );
74 connect( iface
, SIGNAL(signalEmitted(QString
,QString
)),
75 this, SLOT(signalEmitted(QString
,QString
)) );
76 connect( iface
, SIGNAL(warningEmitted(QString
,QString
)),
77 this, SLOT(warningEmitted(QString
,QString
)) );
78 connect( iface
, SIGNAL(errorEmitted(QString
,QString
)),
79 this, SLOT(errorEmitted(QString
,QString
)) );
81 // in case we started listening when the connection is already ongoing
82 connect( iface
, SIGNAL(connectionDataInput(QString
,QString
)),
83 this, SLOT(connectionStarted(QString
,QString
)) );
84 connect( iface
, SIGNAL(connectionDataOutput(QString
,QString
)),
85 this, SLOT(connectionStarted(QString
,QString
)) );
87 QHBoxLayout
*buttonLayout
= new QHBoxLayout
;
88 layout
->addLayout( buttonLayout
);
90 QPushButton
*clearAllButton
= new QPushButton( "Clear All Tabs", this );
91 QPushButton
*clearCurrentButton
= new QPushButton( "Clear Current Tab", this );
92 QPushButton
*clearGeneralButton
= new QPushButton( "Clear Information View", this );
93 QPushButton
*saveRichtextButton
= new QPushButton( "Save as RichText...", this );
95 buttonLayout
->addWidget( clearAllButton
);
96 buttonLayout
->addWidget( clearCurrentButton
);
97 buttonLayout
->addWidget( clearGeneralButton
);
98 buttonLayout
->addWidget( saveRichtextButton
);
100 connect( clearAllButton
, SIGNAL(clicked()), this, SLOT(clearAllTabs()) );
101 connect( clearCurrentButton
, SIGNAL(clicked()), this, SLOT(clearCurrentTab()) );
102 connect( clearGeneralButton
, SIGNAL(clicked()), mGeneralView
, SLOT(clear()) );
103 connect( saveRichtextButton
, SIGNAL(clicked()), this, SLOT(saveRichText()) );
105 Akonadi::Control::widgetNeedsAkonadi( this );
108 void DebugWidget::connectionStarted( const QString
&identifier
, const QString
&msg
)
111 if ( mPageHash
.contains( identifier
) )
114 ConnectionPage
*page
= new ConnectionPage( identifier
);
115 mConnectionPages
->addTab( page
, identifier
);
117 mPageHash
.insert( identifier
, page
);
120 void DebugWidget::connectionEnded( const QString
&identifier
, const QString
& )
122 if ( !mPageHash
.contains( identifier
) )
125 QWidget
*widget
= mPageHash
[ identifier
];
127 mConnectionPages
->removeTab( mConnectionPages
->indexOf( widget
) );
129 mPageHash
.remove( identifier
);
133 void DebugWidget::tabCloseRequested( int index
)
136 QWidget
*page
= mConnectionPages
->widget( index
);
137 QMutableHashIterator
<QString
, ConnectionPage
*> it( mPageHash
);
138 while ( it
.hasNext() ) {
140 if ( it
.value() == page
) {
146 mConnectionPages
->removeTab( index
);
150 void DebugWidget::clearAllTabs()
152 ConnectionPage
*page
= qobject_cast
<ConnectionPage
*>( mConnectionPages
->widget( 0 ) );
156 QMutableHashIterator
<QString
, ConnectionPage
*> it( mPageHash
);
157 while ( it
.hasNext() )
158 it
.next().value()->clear();
161 void DebugWidget::clearCurrentTab()
163 ConnectionPage
*page
= qobject_cast
<ConnectionPage
*>( mConnectionPages
->currentWidget() );
170 void DebugWidget::signalEmitted( const QString
&signalName
, const QString
&msg
)
172 mGeneralView
->append( QString( "<font color=\"green\">%1 ( %2 )</font>" ).arg( signalName
, msg
) );
175 void DebugWidget::warningEmitted( const QString
&componentName
, const QString
&msg
)
177 mGeneralView
->append( QString( "<font color=\"blue\">%1: %2</font>" ).arg( componentName
, msg
) );
180 void DebugWidget::errorEmitted( const QString
&componentName
, const QString
&msg
)
182 mGeneralView
->append( QString( "<font color=\"red\">%1: %2</font>" ).arg( componentName
, msg
) );
185 void DebugWidget::enableDebugger(bool enable
)
187 mDebugInterface
->setTracer( enable
? QLatin1String( "dbus" ) : QLatin1String( "null" ) );
190 void DebugWidget::saveRichText()
192 ConnectionPage
*page
= qobject_cast
<ConnectionPage
*>( mConnectionPages
->currentWidget() );
196 const QString fileName
= KFileDialog::getSaveFileName();
197 if ( fileName
.isEmpty() )
200 QFile
file( fileName
);
201 if ( !file
.open( QIODevice::WriteOnly
) )
204 file
.write( page
->toHtml().toUtf8() );
208 #include "debugwidget.moc"