File indexing completed on 2024-12-22 04:45:36

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "viewlogwidget.h"
0008 #include "config-ruqola.h"
0009 #include "connection.h"
0010 #include "ddpapi/ddpclient.h"
0011 #include "misc/stdoutqueuejob.h"
0012 #include "rocketchataccount.h"
0013 #include "ruqolawidgets_debug.h"
0014 #include "viewlogplaintextedit.h"
0015 
0016 #include <QJsonArray>
0017 #include <QScrollBar>
0018 #include <QVBoxLayout>
0019 
0020 ViewLogWidget::ViewLogWidget(RocketChatAccount *account, QWidget *parent)
0021     : QWidget(parent)
0022     , mPlainTextEdit(new ViewLogPlainTextEdit(this))
0023     , mRocketChatAccount(account)
0024 {
0025     auto mainLayout = new QVBoxLayout(this);
0026     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0027     mainLayout->setContentsMargins({});
0028     mPlainTextEdit->setObjectName(QStringLiteral("mPlainTextEdit"));
0029     mPlainTextEdit->setReadOnly(true);
0030     mainLayout->addWidget(mPlainTextEdit);
0031     // TODO add search widget
0032 }
0033 
0034 ViewLogWidget::~ViewLogWidget()
0035 {
0036     if (mStdoutIdentifier != 0) {
0037         mRocketChatAccount->ddp()->unsubscribe(mStdoutIdentifier);
0038     }
0039 }
0040 
0041 void ViewLogWidget::showEvent(QShowEvent *event)
0042 {
0043     if (!event->spontaneous() && !mWasInitialized) {
0044         mWasInitialized = true;
0045         initialize();
0046     }
0047     QWidget::showEvent(event);
0048 }
0049 
0050 void ViewLogWidget::initialize()
0051 {
0052     {
0053         QJsonArray params;
0054         params.append(QJsonValue(QStringLiteral("stdout")));
0055         mStdoutIdentifier = mRocketChatAccount->ddp()->subscribe(QStringLiteral("stream-stdout"), params);
0056     }
0057     connect(mRocketChatAccount, &RocketChatAccount::insertStdOutInfo, this, &ViewLogWidget::slotInsertStdOutInfo);
0058 
0059     auto job = new RocketChatRestApi::StdoutQueueJob(this);
0060     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0061     connect(job, &RocketChatRestApi::StdoutQueueJob::stdoutQueueDone, this, &ViewLogWidget::slotStdoutQueueDone);
0062     if (!job->start()) {
0063         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start StdoutQueueJob job";
0064     }
0065 }
0066 
0067 void ViewLogWidget::slotInsertStdOutInfo(const QString &str)
0068 {
0069     if (mHistoryStdoutLoaded) {
0070         insertLine(str);
0071     } else {
0072         mStdoutBeforeLoadingHistory.append(str);
0073     }
0074 }
0075 
0076 void ViewLogWidget::insertLine(const QString &str)
0077 {
0078 #if HAVE_TEXT_CUSTOM_EDITOR
0079     mPlainTextEdit->editor()->appendHtml(QStringLiteral("<p white-space:pre\">%1</p>").arg(str));
0080 #else
0081     mPlainTextEdit->appendHtml(QStringLiteral("<p white-space:pre\">%1</p>").arg(str));
0082 #endif
0083 }
0084 
0085 void ViewLogWidget::slotStdoutQueueDone(const QJsonObject &obj)
0086 {
0087     // qDebug() << " obj" << obj;
0088     const QJsonArray array = obj[QLatin1String("queue")].toArray();
0089     mPlainTextEdit->blockSignals(true);
0090     for (int i = 0; i < array.count(); ++i) {
0091         const QJsonObject objQueue = array.at(i).toObject();
0092         insertLine(objQueue[QLatin1String("string")].toString());
0093     }
0094     mHistoryStdoutLoaded = true;
0095     for (const QString &str : std::as_const(mStdoutBeforeLoadingHistory)) {
0096         slotInsertStdOutInfo(str);
0097     }
0098     mStdoutBeforeLoadingHistory.clear();
0099     mPlainTextEdit->blockSignals(false);
0100 #if HAVE_TEXT_CUSTOM_EDITOR
0101     mPlainTextEdit->editor()->verticalScrollBar()->setValue(mPlainTextEdit->editor()->verticalScrollBar()->maximum());
0102 #else
0103     mPlainTextEdit->verticalScrollBar()->setValue(mPlainTextEdit->verticalScrollBar()->maximum());
0104 #endif
0105 }
0106 
0107 #include "moc_viewlogwidget.cpp"