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
|
/*
networkreplywidget.cpp
This file is part of GammaRay, the Qt application inspection and manipulation tool.
SPDX-FileCopyrightText: 2019 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Volker Krause <volker.krause@kdab.com>
SPDX-License-Identifier: GPL-2.0-or-later
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "networkreplywidget.h"
#include "clientnetworkreplymodel.h"
#include "networkreplymodeldefs.h"
#include "ui_networkreplywidget.h"
#include "networksupportclient.h"
#include <ui/contextmenuextension.h>
#include <common/objectbroker.h>
#include <QClipboard>
#include <QGuiApplication>
#include <QMenu>
#include <QPlainTextEdit>
#include <QJsonDocument>
#include <QBuffer>
#include <QXmlStreamReader>
#include <QLabel>
#include <QStringDecoder>
using namespace GammaRay;
static QObject *createClientNetworkSupportInterface(const QString & /*name*/, QObject *parent)
{
return new NetworkSupportClient(parent);
}
NetworkReplyWidget::NetworkReplyWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::NetworkReplyWidget)
{
ui->setupUi(this);
ObjectBroker::registerClientObjectFactoryCallback<NetworkSupportInterface *>(
createClientNetworkSupportInterface);
auto interface = ObjectBroker::object<NetworkSupportInterface *>();
auto srcModel = ObjectBroker::model(QStringLiteral("com.kdab.GammaRay.NetworkReplyModel"));
auto proxy = new ClientNetworkReplyModel(this);
proxy->setSourceModel(srcModel);
ui->replyView->setModel(proxy);
ui->replyView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
ui->replyView->expandAll();
// auto-expand parents of new replies
connect(proxy, &QAbstractItemModel::rowsInserted, this, [this](const QModelIndex &parent, int, int) {
if (!parent.isValid()) {
return;
}
ui->replyView->expand(parent);
});
connect(ui->replyView, &QWidget::customContextMenuRequested, this, &NetworkReplyWidget::contextMenu);
connect(ui->replyView->selectionModel(), &QItemSelectionModel::currentChanged, this, [this](const QModelIndex ¤t, const QModelIndex &) {
const auto objColumn = current.sibling(current.row(), NetworkReplyModelColumn::ObjectColumn);
auto response = objColumn.data(NetworkReplyModelRole::ReplyResponseRole).toByteArray();
const auto contentType = ( NetworkReply::ContentType )objColumn.data(NetworkReplyModelRole::ReplyContentType).toInt();
ui->imageLabel->clear();
switch (contentType) {
case NetworkReply::Json:
response = QJsonDocument::fromJson(response).toJson(QJsonDocument::JsonFormat::Indented);
break;
case NetworkReply::Xml: {
QXmlStreamReader reader(response);
QByteArray formattedResponse;
QXmlStreamWriter writer(&formattedResponse);
writer.setAutoFormatting(true);
while (!reader.atEnd()) {
reader.readNext();
if (reader.isWhitespace()) {
continue;
}
writer.writeCurrentToken(reader);
}
if (reader.hasError()) {
qWarning() << "Error while parsing XML:" << reader.errorString();
break;
}
response.swap(formattedResponse);
break;
}
case NetworkReply::Image:
ui->imageLabel->setPixmap(QPixmap::fromImage(QImage::fromData(response)));
response.clear();
break;
default:
break;
}
QStringDecoder decoder(QStringDecoder::Utf8);
QByteArrayView bav(response.constData(), response.size());
const QString text = decoder.decode(bav);
if (!decoder.hasError()) {
ui->responseTextEdit->setPlainText(text);
}
});
ui->responseTextEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
connect(ui->responseTextEdit, &QPlainTextEdit::textChanged, this, [this]() {
ui->responseTextEdit->setVisible(!ui->responseTextEdit->toPlainText().isEmpty());
});
connect(ui->captureResponse, &QCheckBox::toggled, interface, [interface](bool checked) {
interface->setProperty("captureResponse", checked);
});
}
NetworkReplyWidget::~NetworkReplyWidget() = default;
void NetworkReplyWidget::contextMenu(QPoint pos)
{
const auto index = ui->replyView->indexAt(pos);
if (!index.isValid())
return;
const auto objectId = index.sibling(index.row(), NetworkReplyModelColumn::ObjectColumn).data(NetworkReplyModelRole::ObjectIdRole).value<ObjectId>();
const auto url = index.sibling(index.row(), NetworkReplyModelColumn::UrlColumn).data(Qt::DisplayRole).toString();
QMenu menu;
if (!url.isEmpty()) {
auto action = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), tr("Copy URL"));
connect(action, &QAction::triggered, this, [url]() {
QGuiApplication::clipboard()->setText(url);
});
menu.addSeparator();
}
ContextMenuExtension ext(objectId);
ext.populateMenu(&menu);
menu.exec(ui->replyView->viewport()->mapToGlobal(pos));
}
|