File indexing completed on 2024-04-21 05:41:05

0001 /*
0002     SPDX-FileCopyrightText: 2019-2020 Nikolai Krasheninnikov <nkrasheninnikov@yandex.ru>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "svnprogressdialog.h"
0008 
0009 #include <QProcess>
0010 #include <QDebug>
0011 
0012 #include "svncommands.h"
0013 
0014 SvnProgressDialog::SvnProgressDialog(const QString& title, const QString& workingDir, QWidget *parent) :
0015     QDialog(parent),
0016     m_svnTerminated(false),
0017     m_workingDir(workingDir)
0018 {
0019     m_ui.setupUi(this);
0020 
0021     /*
0022      * Add actions, establish connections.
0023      */
0024     QObject::connect(m_ui.buttonOk, &QPushButton::clicked, this, &QDialog::close);
0025 
0026     /*
0027      * Additional setup.
0028      */
0029     setAttribute(Qt::WA_DeleteOnClose);
0030     setWindowTitle(title);
0031     show();
0032     activateWindow();
0033 }
0034 
0035 SvnProgressDialog::~SvnProgressDialog()
0036 {
0037     disconnectFromProcess();
0038 }
0039 
0040 void SvnProgressDialog::connectToProcess(QProcess *process)
0041 {
0042     disconnectFromProcess();
0043 
0044     m_svnTerminated = false;
0045 
0046     m_conCancel = connect(m_ui.buttonCancel, &QPushButton::clicked, this, [this, process] () {
0047         process->terminate();
0048         m_svnTerminated = true;
0049     } );
0050     m_conCompeted = connect(process, &QProcess::finished, this, &SvnProgressDialog::operationCompeleted);
0051     m_conProcessError = connect(process, &QProcess::errorOccurred, this, [this, process] (QProcess::ProcessError) {
0052         const QString commandLine = process->program() + process->arguments().join(QLatin1Char(' '));
0053         appendErrorText(i18nc("@info:status", "Error starting: %1", commandLine));
0054         operationCompeleted();
0055     } );
0056     m_conStdOut = connect(process, &QProcess::readyReadStandardOutput, this, [this, process] () {
0057         appendInfoText( QString::fromLocal8Bit(process->readAllStandardOutput()) );
0058     } );
0059     m_conStrErr = connect(process, &QProcess::readyReadStandardError, this, [this, process] () {
0060         appendErrorText( QString::fromLocal8Bit(process->readAllStandardError()) );
0061     } );
0062 }
0063 
0064 void SvnProgressDialog::disconnectFromProcess()
0065 {
0066     QObject::disconnect(m_conCancel);
0067     QObject::disconnect(m_conCompeted);
0068     QObject::disconnect(m_conProcessError);
0069     QObject::disconnect(m_conStdOut);
0070     QObject::disconnect(m_conStrErr);
0071 }
0072 
0073 void SvnProgressDialog::appendInfoText(const QString& text)
0074 {
0075     const QTextCursor pos = m_ui.texteditMessage->textCursor();
0076 
0077     m_ui.texteditMessage->moveCursor(QTextCursor::End);
0078     m_ui.texteditMessage->insertPlainText(text);
0079     m_ui.texteditMessage->setTextCursor(pos);
0080 }
0081 
0082 void SvnProgressDialog::appendErrorText(const QString& text)
0083 {
0084     static const QString htmlBegin = QStringLiteral("<font color=\"Red\">");
0085     static const QString htmlEnd = QStringLiteral("</font><br>");
0086 
0087     QString message = QString(text).replace(QLatin1Char('\n'), QLatin1String("<br>"));
0088     // Remove last <br> as it will be in htmlEnd.
0089     if (message.endsWith(QLatin1String("<br>"))) {
0090         message.chop(4);
0091     }
0092 
0093     m_ui.texteditMessage->appendHtml(htmlBegin + message + htmlEnd);
0094 }
0095 
0096 void SvnProgressDialog::operationCompeleted()
0097 {
0098     disconnectFromProcess();
0099 
0100     if (m_svnTerminated && !m_workingDir.isEmpty()) {
0101         const CommandResult result = SvnCommands::cleanup(m_workingDir);
0102         if (!result.success) {
0103             qWarning() << QStringLiteral("'svn cleanup' failed for %1").arg(m_workingDir);
0104             qWarning() << result.stdErr;
0105         }
0106         m_svnTerminated = false;
0107     }
0108 
0109     m_ui.buttonOk->setEnabled(true);
0110     m_ui.buttonCancel->setEnabled(false);
0111 }
0112 
0113 void SvnProgressDialog::reject()
0114 {
0115     if (m_ui.buttonOk->isEnabled()) {
0116         QDialog::reject();
0117     } else {
0118         Q_EMIT m_ui.buttonCancel->clicked();
0119     }
0120 }
0121 
0122 #include "moc_svnprogressdialog.cpp"