Don't share mailto
[kdepim.git] / akonadiconsole / debugwidget.cpp
blob5ff68835369fa91c0547dd3e863fac40a42f2aa9
1 /*
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,
19 USA.
22 #include "debugwidget.h"
24 #include "tracernotificationinterface.h"
25 #include "connectionpage.h"
27 #include <AkonadiWidgets/controlgui.h>
29 #include <KLocalizedString>
30 #include <QTabWidget>
31 #include <KTextEdit>
32 #include <AkonadiCore/ServerManager>
34 #include <QPushButton>
35 #include <QSplitter>
36 #include <QVBoxLayout>
37 #include <QCheckBox>
38 #include <QFileDialog>
40 using org::freedesktop::Akonadi::DebugInterface;
42 DebugWidget::DebugWidget(QWidget *parent)
43 : QWidget(parent)
45 QVBoxLayout *layout = new QVBoxLayout(this);
47 QString service = QStringLiteral("org.freedesktop.Akonadi");
48 if (Akonadi::ServerManager::hasInstanceIdentifier()) {
49 service += QLatin1String(".") + Akonadi::ServerManager::instanceIdentifier();
51 mDebugInterface = new DebugInterface(service, QStringLiteral("/debug"), QDBusConnection::sessionBus(), this);
52 QCheckBox *cb = new QCheckBox(QStringLiteral("Enable debugger"), this);
53 cb->setChecked(mDebugInterface->isValid() && mDebugInterface->tracer().value() == QLatin1String("dbus"));
54 connect(cb, &QCheckBox::toggled, this, &DebugWidget::enableDebugger);
55 layout->addWidget(cb);
57 QSplitter *splitter = new QSplitter(Qt::Vertical, this);
58 splitter->setObjectName(QStringLiteral("debugSplitter"));
59 layout->addWidget(splitter);
61 mConnectionPages = new QTabWidget(splitter);
62 mConnectionPages->setTabsClosable(true);
64 mGeneralView = new KTextEdit(splitter);
65 mGeneralView->setReadOnly(true);
67 ConnectionPage *page = new ConnectionPage(QStringLiteral("All"));
68 page->showAllConnections(true);
69 mConnectionPages->addTab(page, QStringLiteral("All"));
71 connect(mConnectionPages, &QTabWidget::tabCloseRequested, this, &DebugWidget::tabCloseRequested);
73 org::freedesktop::Akonadi::TracerNotification *iface = new org::freedesktop::Akonadi::TracerNotification(QString(), QStringLiteral("/tracing/notifications"), QDBusConnection::sessionBus(), this);
75 connect(iface, &org::freedesktop::Akonadi::TracerNotification::connectionStarted, this, &DebugWidget::connectionStarted);
76 connect(iface, &org::freedesktop::Akonadi::TracerNotification::connectionEnded, this, &DebugWidget::connectionEnded);
77 connect(iface, &org::freedesktop::Akonadi::TracerNotification::signalEmitted, this, &DebugWidget::signalEmitted);
78 connect(iface, &org::freedesktop::Akonadi::TracerNotification::warningEmitted, this, &DebugWidget::warningEmitted);
79 connect(iface, &org::freedesktop::Akonadi::TracerNotification::errorEmitted, this, &DebugWidget::errorEmitted);
81 // in case we started listening when the connection is already ongoing
82 connect(iface, &org::freedesktop::Akonadi::TracerNotification::connectionDataInput, this, &DebugWidget::connectionStarted);
83 connect(iface, &org::freedesktop::Akonadi::TracerNotification::connectionDataOutput, this, &DebugWidget::connectionStarted);
85 QHBoxLayout *buttonLayout = new QHBoxLayout;
86 layout->addLayout(buttonLayout);
88 QPushButton *clearAllButton = new QPushButton(QStringLiteral("Clear All Tabs"), this);
89 QPushButton *clearCurrentButton = new QPushButton(QStringLiteral("Clear Current Tab"), this);
90 QPushButton *clearGeneralButton = new QPushButton(QStringLiteral("Clear Information View"), this);
91 QPushButton *closeAllTabsButton = new QPushButton(QStringLiteral("Close All Tabs"), this);
92 QPushButton *saveRichtextButton = new QPushButton(QStringLiteral("Save as RichText..."), this);
94 buttonLayout->addWidget(clearAllButton);
95 buttonLayout->addWidget(clearCurrentButton);
96 buttonLayout->addWidget(clearGeneralButton);
97 buttonLayout->addWidget(closeAllTabsButton);
98 buttonLayout->addWidget(saveRichtextButton);
100 connect(clearAllButton, &QPushButton::clicked, this, &DebugWidget::clearAllTabs);
101 connect(clearCurrentButton, &QPushButton::clicked, this, &DebugWidget::clearCurrentTab);
102 connect(clearGeneralButton, &QPushButton::clicked, mGeneralView, &KTextEdit::clear);
103 connect(closeAllTabsButton, &QPushButton::clicked, this, &DebugWidget::closeAllTabs);
104 connect(saveRichtextButton, &QPushButton::clicked, this, &DebugWidget::saveRichText);
106 Akonadi::ControlGui::widgetNeedsAkonadi(this);
109 void DebugWidget::connectionStarted(const QString &identifier, const QString &msg)
111 Q_UNUSED(msg);
112 if (mPageHash.contains(identifier)) {
113 return;
116 ConnectionPage *page = new ConnectionPage(identifier);
117 mConnectionPages->addTab(page, identifier);
119 mPageHash.insert(identifier, page);
122 void DebugWidget::connectionEnded(const QString &identifier, const QString &)
124 if (!mPageHash.contains(identifier)) {
125 return;
128 QWidget *widget = mPageHash[ identifier ];
130 mConnectionPages->removeTab(mConnectionPages->indexOf(widget));
132 mPageHash.remove(identifier);
133 delete widget;
136 void DebugWidget::tabCloseRequested(int index)
138 if (index != 0) {
139 QWidget *page = mConnectionPages->widget(index);
140 QMutableHashIterator<QString, ConnectionPage *> it(mPageHash);
141 while (it.hasNext()) {
142 it.next();
143 if (it.value() == page) {
144 it.remove();
145 break;
149 mConnectionPages->removeTab(index);
150 delete page;
154 void DebugWidget::clearAllTabs()
156 ConnectionPage *page = qobject_cast<ConnectionPage *>(mConnectionPages->widget(0));
157 if (page) {
158 page->clear();
161 QMutableHashIterator<QString, ConnectionPage *> it(mPageHash);
162 while (it.hasNext()) {
163 it.next().value()->clear();
167 void DebugWidget::clearCurrentTab()
169 ConnectionPage *page = qobject_cast<ConnectionPage *>(mConnectionPages->currentWidget());
170 if (!page) {
171 return;
174 page->clear();
177 void DebugWidget::closeAllTabs()
179 ConnectionPage *page = qobject_cast<ConnectionPage *>(mConnectionPages->widget(0));
180 if (page) {
181 page->clear();
184 while (mConnectionPages->count() > 1) {
185 mConnectionPages->removeTab(1);
187 qDeleteAll(mPageHash);
188 mPageHash.clear();
191 void DebugWidget::signalEmitted(const QString &signalName, const QString &msg)
193 mGeneralView->append(QStringLiteral("<font color=\"green\">%1 ( %2 )</font>").arg(signalName, msg));
196 void DebugWidget::warningEmitted(const QString &componentName, const QString &msg)
198 mGeneralView->append(QStringLiteral("<font color=\"blue\">%1: %2</font>").arg(componentName, msg));
201 void DebugWidget::errorEmitted(const QString &componentName, const QString &msg)
203 mGeneralView->append(QStringLiteral("<font color=\"red\">%1: %2</font>").arg(componentName, msg));
206 void DebugWidget::enableDebugger(bool enable)
208 mDebugInterface->setTracer(enable ? QStringLiteral("dbus") : QStringLiteral("null"));
211 void DebugWidget::saveRichText()
213 ConnectionPage *page = qobject_cast<ConnectionPage *>(mConnectionPages->currentWidget());
214 if (!page) {
215 return;
218 const QString fileName = QFileDialog::getSaveFileName(this);
219 if (fileName.isEmpty()) {
220 return;
223 QFile file(fileName);
224 if (!file.open(QIODevice::WriteOnly)) {
225 return;
228 file.write(page->toHtml().toUtf8());
229 file.close();