File indexing completed on 2024-05-12 05:12:44

0001 /*
0002     This file is part of Akonadi.
0003 
0004     SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #include "connectionpage.h"
0010 
0011 #include "debugfiltermodel.h"
0012 #include "debugmodel.h"
0013 
0014 #include <Libkdepim/KCheckComboBox>
0015 
0016 #include <QHeaderView>
0017 #include <QLabel>
0018 #include <QTableView>
0019 
0020 #include <KLocalizedString>
0021 #include <QFontDatabase>
0022 #include <QVBoxLayout>
0023 
0024 #include "tracernotificationinterface.h"
0025 #include <QStandardItemModel>
0026 
0027 Q_DECLARE_METATYPE(DebugModel::Message)
0028 
0029 ConnectionPage::ConnectionPage(const QString &identifier, QWidget *parent)
0030     : QWidget(parent)
0031     , mIdentifier(identifier)
0032 {
0033     auto layout = new QVBoxLayout(this);
0034     auto h = new QHBoxLayout;
0035     layout->addLayout(h);
0036 
0037     h->addWidget(new QLabel(i18n("Programs:")));
0038     h->addWidget(mSenderFilter = new KPIM::KCheckComboBox());
0039     h->setStretchFactor(mSenderFilter, 2);
0040 
0041     mModel = new DebugModel(this);
0042     mModel->setSenderFilterModel(qobject_cast<QStandardItemModel *>(mSenderFilter->model()));
0043 
0044     mFilterModel = new DebugFilterModel(this);
0045     mFilterModel->setSourceModel(mModel);
0046     mFilterModel->setSenderFilter(mSenderFilter);
0047 
0048     auto mDataView = new QTableView(this);
0049     mDataView->setModel(mFilterModel);
0050     mDataView->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0051     mDataView->horizontalHeader()->setStretchLastSection(true);
0052     mDataView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
0053 
0054     layout->addWidget(mDataView);
0055 
0056     auto iface = new org::freedesktop::Akonadi::TracerNotification(QString(), QStringLiteral("/tracing/notifications"), QDBusConnection::sessionBus(), this);
0057 
0058     connect(iface, &OrgFreedesktopAkonadiTracerNotificationInterface::connectionDataInput, this, &ConnectionPage::connectionDataInput);
0059     connect(iface, &OrgFreedesktopAkonadiTracerNotificationInterface::connectionDataOutput, this, &ConnectionPage::connectionDataOutput);
0060     connect(mDataView->horizontalHeader(), &QHeaderView::sectionResized, mDataView, &QTableView::resizeRowsToContents);
0061     connect(mFilterModel, &QAbstractItemModel::modelReset, mDataView, &QTableView::resizeRowsToContents);
0062     connect(mFilterModel, &QAbstractItemModel::layoutChanged, mDataView, &QTableView::resizeRowsToContents);
0063 }
0064 
0065 void ConnectionPage::connectionDataInput(const QString &identifier, const QString &msg)
0066 {
0067     if (mShowAllConnections || identifier == mIdentifier) {
0068         mModel->addMessage(identifier, DebugModel::ClientToServer, msg);
0069     }
0070 }
0071 
0072 void ConnectionPage::connectionDataOutput(const QString &identifier, const QString &msg)
0073 {
0074     if (mShowAllConnections || identifier == mIdentifier) {
0075         mModel->addMessage(identifier, DebugModel::ServerToClient, msg);
0076     }
0077 }
0078 
0079 void ConnectionPage::showAllConnections(bool show)
0080 {
0081     mShowAllConnections = show;
0082 }
0083 
0084 QString ConnectionPage::toHtml(QAbstractItemModel *model) const
0085 {
0086     QString ret;
0087     int anz = model->rowCount();
0088     for (int row = 0; row < anz; row++) {
0089         const auto message = model->data(model->index(row, 0), DebugModel::MessageRole).value<DebugModel::Message>();
0090         const auto &sender = model->data(model->index(row, DebugModel::SenderColumn)).toString();
0091 
0092         ret += (message.direction == DebugModel::ClientToServer ? QStringLiteral("<- ") : QStringLiteral("-> "));
0093         ret += sender + QStringLiteral(" ") + message.message + QStringLiteral("\n");
0094     }
0095     return ret;
0096 }
0097 
0098 QString ConnectionPage::toHtmlFiltered() const
0099 {
0100     return toHtml(mFilterModel);
0101 }
0102 
0103 QString ConnectionPage::toHtml() const
0104 {
0105     return toHtml(mModel);
0106 }
0107 
0108 void ConnectionPage::clear()
0109 {
0110     mModel->removeRows(0, mModel->rowCount());
0111 }
0112 
0113 void ConnectionPage::clearFiltered()
0114 {
0115     mFilterModel->removeRows(0, mFilterModel->rowCount());
0116 }
0117 
0118 #include "moc_connectionpage.cpp"