File indexing completed on 2024-06-16 04:16:26

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Sharaf Zaman <shzam@sdf.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "DlgAndroidLogcatDumper.h"
0008 
0009 #include <QDebug>
0010 #include <QProcess>
0011 #include <QPushButton>
0012 #include <QScreen>
0013 #include <QScrollBar>
0014 #include <QStandardPaths>
0015 #include <unistd.h>
0016 
0017 DlgAndroidLogcatDumper::DlgAndroidLogcatDumper(QWidget *parent)
0018     : DlgBugInfo(parent, User3)
0019     , m_logcatProcess(parent)
0020     , m_updateWidgetCompressor(100, KisSignalCompressor::FIRST_INACTIVE)
0021 {
0022     QString program = "logcat";
0023     QStringList arguments = {"-v", "time", "--pid", QString::number(getpid())};
0024     m_logcatProcess.start(program, arguments);
0025     QString errorOutput = m_logcatProcess.readAllStandardError();
0026     if (!errorOutput.isEmpty()) {
0027         qWarning() << "Couldn't spawn logcat process" << errorOutput;
0028     }
0029 
0030     setButtonText(User3, i18n("Toggle Logging"));
0031     button(User3)->setCheckable(true);
0032     button(User3)->setChecked(true);
0033 
0034     enableLogging();
0035     connect(this, &KoDialog::user3Clicked, this, [this]() {
0036         QPushButton *loggingButton = button(User3);
0037         if (loggingButton->isChecked()) {
0038             enableLogging();
0039         } else {
0040             disableLogging();
0041         }
0042     });
0043 
0044     QRect screen_rect = QGuiApplication::primaryScreen()->availableGeometry();
0045     int frame_height = parentWidget()->frameGeometry().height() - parentWidget()->size().height();
0046     resize(m_page->size().width(), screen_rect.height() - frame_height);
0047 }
0048 
0049 DlgAndroidLogcatDumper::~DlgAndroidLogcatDumper()
0050 {
0051     m_logcatProcess.kill();
0052     m_logcatProcess.waitForFinished();
0053 }
0054 
0055 void DlgAndroidLogcatDumper::writeTextToWidget()
0056 {
0057     QScrollBar *scrollBar = m_page->txtBugInfo->verticalScrollBar();
0058     const bool scrollbarAtBottom = scrollBar->value() >= scrollBar->maximum();
0059 
0060     QByteArray output = m_logcatProcess.readAllStandardOutput();
0061 
0062     // this allows user's movement of the scrollbar
0063     QTextCursor tmp(m_page->txtBugInfo->document());
0064     tmp.movePosition(QTextCursor::End);
0065     tmp.insertText(output);
0066 
0067     // scroll to the bottom if scrollbar is at the bottom
0068     if (scrollbarAtBottom) {
0069         scrollBar->setValue(scrollBar->maximum());
0070     }
0071 }
0072 
0073 void DlgAndroidLogcatDumper::enableLogging()
0074 {
0075     connect(&m_logcatProcess, SIGNAL(readyReadStandardOutput()), &m_updateWidgetCompressor, SLOT(start()),
0076             Qt::UniqueConnection);
0077     connect(&m_updateWidgetCompressor, SIGNAL(timeout()), this, SLOT(writeTextToWidget()), Qt::UniqueConnection);
0078 
0079     m_updateWidgetCompressor.start();
0080 }
0081 
0082 void DlgAndroidLogcatDumper::disableLogging()
0083 {
0084     disconnect(&m_updateWidgetCompressor, SIGNAL(timeout()), this, SLOT(writeTextToWidget()));
0085     disconnect(&m_logcatProcess, SIGNAL(readyReadStandardOutput()), &m_updateWidgetCompressor, SLOT(start()));
0086 }
0087 
0088 QString DlgAndroidLogcatDumper::defaultNewFileName() { return "kritalogcatdump.txt"; }
0089 
0090 QString DlgAndroidLogcatDumper::originalFileName()
0091 {
0092     return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/krita-logcatdump.log";
0093 }
0094 
0095 QString DlgAndroidLogcatDumper::captionText()
0096 {
0097     return i18nc("Caption of the dialog with Krita's Android system log for bug reports",
0098                  "Krita Logcat Dump: please paste this information to the bug report");
0099 }
0100 
0101 QString DlgAndroidLogcatDumper::replacementWarningText() { return QString(); }