File indexing completed on 2024-05-12 17:16:14

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht                             *
0003  *   ral@alwins-world.de                                                   *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "stopdlg.h"
0021 #include "ccontextlistener.h"
0022 #include "settings/kdesvnsettings.h"
0023 #include "helpers/stringhelper.h"
0024 
0025 #include <KLocalizedString>
0026 
0027 #include <QApplication>
0028 #include <QDialogButtonBox>
0029 #include <QTimer>
0030 #include <QLabel>
0031 #include <QTextBrowser>
0032 #include <QWidget>
0033 #include <QVBoxLayout>
0034 #include <QProgressBar>
0035 
0036 StopDlg::StopDlg(CContextListener *listener, QWidget *parent, const QString &caption, const QString &text)
0037     : QDialog(parent)
0038     , m_MinDuration(1000)
0039     , mCancelled(false)
0040     , mShown(false)
0041     , m_BarShown(false)
0042     , m_netBarShown(false)
0043     , cstack(nullptr)
0044     , m_bBox(new QDialogButtonBox(QDialogButtonBox::Cancel, this))
0045 {
0046     setWindowTitle(caption);
0047 
0048     m_lastLogLines = 0;
0049     m_lastLog.clear();
0050 
0051     mShowTimer = new QTimer(this);
0052     m_StopTick.start();
0053 
0054     mainLayout = new QVBoxLayout(this);
0055     layout = new QVBoxLayout;
0056     mainLayout->addLayout(layout);
0057     mainLayout->addWidget(m_bBox);
0058     mLabel = new QLabel(text, this);
0059     layout->addWidget(mLabel);
0060     m_ProgressBar = new QProgressBar(this);
0061     m_ProgressBar->setRange(0, 15);
0062     m_ProgressBar->setTextVisible(false);
0063     layout->addWidget(m_ProgressBar);
0064     m_NetBar = new QProgressBar(this);
0065     m_NetBar->setRange(0, 15);
0066     layout->addWidget(m_NetBar);
0067 
0068     mWait = false;
0069     m_LogWindow = nullptr;
0070 
0071     connect(mShowTimer, &QTimer::timeout, this, &StopDlg::slotAutoShow);
0072     connect(m_bBox, &QDialogButtonBox::rejected, this, &StopDlg::slotCancel);
0073     if (listener) {
0074         connect(listener, &CContextListener::tickProgress, this, &StopDlg::slotTick);
0075         connect(listener, &CContextListener::waitShow, this, &StopDlg::slotWait);
0076         connect(listener, &CContextListener::netProgress,
0077                 this, &StopDlg::slotNetProgres);
0078         connect(this, &StopDlg::sigCancel, listener, &CContextListener::setCanceled);
0079     }
0080     mShowTimer->setSingleShot(true);
0081     mShowTimer->start(m_MinDuration);
0082     setMinimumSize(280, 160);
0083     adjustSize();
0084 }
0085 
0086 void StopDlg::showEvent(QShowEvent *e)
0087 {
0088     if (!cstack) {
0089         cstack = new CursorStack(Qt::BusyCursor);
0090     }
0091     QDialog::showEvent(e);
0092 }
0093 
0094 void StopDlg::hideEvent(QHideEvent *e)
0095 {
0096     delete cstack;
0097     cstack = nullptr;
0098     QDialog::hideEvent(e);
0099 }
0100 
0101 void StopDlg::slotWait(bool how)
0102 {
0103     mWait = how;
0104     if (mShown && mWait) {
0105         hide();
0106         mShown = false;
0107     }
0108 }
0109 
0110 StopDlg::~StopDlg()
0111 {
0112     delete cstack;
0113 }
0114 
0115 void StopDlg::slotAutoShow()
0116 {
0117     bool hasDialogs = false;
0118     QWidget *w = QApplication::activeModalWidget();
0119     if (w && w != this && w != parentWidget()) {
0120         hasDialogs = true;
0121     }
0122     if (hasDialogs) {
0123         hide();
0124     }
0125     if (mShown || mWait || hasDialogs) {
0126         mShowTimer->setSingleShot(true);
0127         if (mWait) {
0128             //qCDebug(KDESVN_LOG) << "Waiting for show"<<endl;
0129             mShowTimer->start(m_MinDuration);
0130         }
0131         mShowTimer->start(m_MinDuration);
0132         return;
0133     }
0134     m_ProgressBar->hide();
0135     m_NetBar->hide();
0136     m_BarShown = false;
0137     m_netBarShown = false;
0138     show();
0139     QCoreApplication::processEvents();
0140     mShown = true;
0141     mShowTimer->setSingleShot(true);
0142     mShowTimer->start(m_MinDuration);
0143 }
0144 
0145 void StopDlg::slotCancel()
0146 {
0147     mCancelled = true;
0148     emit sigCancel(true);
0149 }
0150 
0151 void StopDlg::slotTick()
0152 {
0153     if (m_StopTick.elapsed() > 500) {
0154         if (!m_BarShown) {
0155             m_ProgressBar->show();
0156             m_BarShown = true;
0157         }
0158         if (m_ProgressBar->value() == 15) {
0159             m_ProgressBar->reset();
0160         } else {
0161             m_ProgressBar->setValue(m_ProgressBar->value() + 1);
0162         }
0163         m_StopTick.restart();
0164         QCoreApplication::processEvents();
0165     }
0166 }
0167 
0168 void StopDlg::slotExtraMessage(const QString &msg)
0169 {
0170     ++m_lastLogLines;
0171     if (!m_LogWindow) {
0172         m_LogWindow = new QTextBrowser(this);
0173         layout->addWidget(m_LogWindow);
0174         m_LogWindow->show();
0175         resize(QSize(500, 400).expandedTo(minimumSizeHint()));
0176     }
0177     if (m_lastLogLines >= Kdesvnsettings::self()->cmdline_log_minline() &&
0178             isHidden()) {
0179         slotAutoShow();
0180     }
0181     m_LogWindow->append(msg);
0182     QCoreApplication::processEvents();
0183 }
0184 
0185 void StopDlg::slotNetProgres(long long int current, long long int max)
0186 {
0187     if (m_StopTick.elapsed() > 300 || (m_BarShown && !m_netBarShown)) {
0188         if (!m_netBarShown) {
0189             m_NetBar->show();
0190             m_netBarShown = true;
0191         }
0192         QString s1 = helpers::ByteToString(current);
0193         if (max > -1 && max != m_NetBar->maximum()) {
0194             QString s2 = helpers::ByteToString(max);
0195             m_NetBar->setFormat(i18n("%p% of %1", s2));
0196             m_NetBar->setRange(0, max);
0197         }
0198         if (max == -1) {
0199             if (m_NetBar->maximum() == -1 || m_NetBar->maximum() < current) {
0200                 m_NetBar->setFormat(i18n("%1 transferred.", s1));
0201                 m_NetBar->setRange(0, current + 1);
0202             } else {
0203                 m_NetBar->setFormat(i18n("%1 of %2", s1, helpers::ByteToString(m_NetBar->maximum())));
0204             }
0205         }
0206         m_NetBar->setValue(current);
0207         m_StopTick.restart();
0208         QCoreApplication::processEvents();
0209     }
0210 }