Warning, file /frameworks/kjobwidgets/src/kwidgetjobtracker.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2000 Matej Koss <koss@miesto.sk>
0004     SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
0005     SPDX-FileCopyrightText: 2007 Rafael Fernández López <ereslibre@kde.org>
0006     SPDX-FileCopyrightText: 2009 Shaun Reich <shaun.reich@kdemail.net>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 */
0010 
0011 #include "kwidgetjobtracker.h"
0012 #include "debug.h"
0013 #include "kjobtrackerformatters_p.h"
0014 #include "kwidgetjobtracker_p.h"
0015 
0016 #include <QCoreApplication>
0017 #include <QDir>
0018 #include <QEvent>
0019 #include <QGridLayout>
0020 #include <QLabel>
0021 #include <QProcess>
0022 #include <QProgressBar>
0023 #include <QPushButton>
0024 #include <QStandardPaths>
0025 #include <QStyle>
0026 #include <QTimer>
0027 #include <QVBoxLayout>
0028 
0029 #include <KSeparator>
0030 #include <KSqueezedTextLabel>
0031 
0032 void KWidgetJobTrackerPrivate::_k_showProgressWidget()
0033 {
0034     Q_Q(KWidgetJobTracker);
0035 
0036     if (progressWidgetsToBeShown.isEmpty()) {
0037         return;
0038     }
0039 
0040     KJob *job = progressWidgetsToBeShown.dequeue();
0041 
0042     // If the job has been unregistered before reaching this point, widget will
0043     // return 0.
0044     QWidget *widget = q->widget(job);
0045 
0046     if (widget) {
0047         // Don't steal the focus from the current widget (e. g. Kate)
0048         widget->setAttribute(Qt::WA_ShowWithoutActivating);
0049         widget->show();
0050     }
0051 }
0052 
0053 KWidgetJobTracker::KWidgetJobTracker(QWidget *parent)
0054     : KAbstractWidgetJobTracker(*new KWidgetJobTrackerPrivate(parent, this), parent)
0055 {
0056 }
0057 
0058 KWidgetJobTracker::~KWidgetJobTracker() = default;
0059 
0060 QWidget *KWidgetJobTracker::widget(KJob *job)
0061 {
0062     Q_D(KWidgetJobTracker);
0063 
0064     return d->progressWidget.value(job, nullptr);
0065 }
0066 
0067 void KWidgetJobTracker::registerJob(KJob *job)
0068 {
0069     Q_D(KWidgetJobTracker);
0070 
0071     auto *vi = new KWidgetJobTrackerPrivate::ProgressWidget(job, this, d->parent);
0072     vi->jobRegistered = true;
0073     vi->setAttribute(Qt::WA_DeleteOnClose);
0074     d->progressWidget.insert(job, vi);
0075     d->progressWidgetsToBeShown.enqueue(job);
0076 
0077     KAbstractWidgetJobTracker::registerJob(job);
0078 
0079     QTimer::singleShot(500, this, SLOT(_k_showProgressWidget()));
0080 }
0081 
0082 void KWidgetJobTracker::unregisterJob(KJob *job)
0083 {
0084     Q_D(KWidgetJobTracker);
0085 
0086     KAbstractWidgetJobTracker::unregisterJob(job);
0087 
0088     d->progressWidgetsToBeShown.removeAll(job);
0089     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0090     if (!pWidget) {
0091         return;
0092     }
0093 
0094     pWidget->jobRegistered = false;
0095     pWidget->deref();
0096 }
0097 
0098 bool KWidgetJobTracker::keepOpen(KJob *job) const
0099 {
0100     Q_D(const KWidgetJobTracker);
0101 
0102     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0103     if (!pWidget) {
0104         return false;
0105     }
0106 
0107     return pWidget->keepOpenCheck->isChecked();
0108 }
0109 
0110 void KWidgetJobTracker::infoMessage(KJob *job, const QString &plain, const QString &rich)
0111 {
0112     Q_D(KWidgetJobTracker);
0113 
0114     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0115     if (!pWidget) {
0116         return;
0117     }
0118 
0119     pWidget->infoMessage(plain, rich);
0120 }
0121 
0122 void KWidgetJobTracker::description(KJob *job, const QString &title, const QPair<QString, QString> &field1, const QPair<QString, QString> &field2)
0123 {
0124     Q_D(KWidgetJobTracker);
0125 
0126     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0127     if (!pWidget) {
0128         return;
0129     }
0130 
0131     pWidget->description(title, field1, field2);
0132 }
0133 
0134 void KWidgetJobTracker::totalAmount(KJob *job, KJob::Unit unit, qulonglong amount)
0135 {
0136     Q_D(KWidgetJobTracker);
0137 
0138     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0139     if (!pWidget) {
0140         return;
0141     }
0142 
0143     pWidget->totalAmount(unit, amount);
0144 }
0145 
0146 void KWidgetJobTracker::processedAmount(KJob *job, KJob::Unit unit, qulonglong amount)
0147 {
0148     Q_D(KWidgetJobTracker);
0149 
0150     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0151     if (!pWidget) {
0152         return;
0153     }
0154 
0155     pWidget->processedAmount(unit, amount);
0156 }
0157 
0158 void KWidgetJobTracker::percent(KJob *job, unsigned long percent)
0159 {
0160     Q_D(KWidgetJobTracker);
0161 
0162     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0163     if (!pWidget) {
0164         return;
0165     }
0166 
0167     pWidget->percent(percent);
0168 }
0169 
0170 void KWidgetJobTracker::speed(KJob *job, unsigned long value)
0171 {
0172     Q_D(KWidgetJobTracker);
0173 
0174     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0175     if (!pWidget) {
0176         return;
0177     }
0178 
0179     pWidget->speed(value);
0180 }
0181 
0182 void KWidgetJobTracker::slotClean(KJob *job)
0183 {
0184     Q_D(KWidgetJobTracker);
0185 
0186     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0187     if (!pWidget) {
0188         return;
0189     }
0190 
0191     pWidget->slotClean();
0192 }
0193 
0194 void KWidgetJobTracker::suspended(KJob *job)
0195 {
0196     Q_D(KWidgetJobTracker);
0197 
0198     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0199     if (!pWidget) {
0200         return;
0201     }
0202 
0203     pWidget->suspended();
0204 }
0205 
0206 void KWidgetJobTracker::resumed(KJob *job)
0207 {
0208     Q_D(KWidgetJobTracker);
0209 
0210     KWidgetJobTrackerPrivate::ProgressWidget *pWidget = d->progressWidget.value(job, nullptr);
0211     if (!pWidget) {
0212         return;
0213     }
0214 
0215     pWidget->resumed();
0216 }
0217 
0218 void KWidgetJobTrackerPrivate::ProgressWidget::ref()
0219 {
0220     ++refCount;
0221 }
0222 
0223 void KWidgetJobTrackerPrivate::ProgressWidget::deref()
0224 {
0225     if (refCount) {
0226         --refCount;
0227     }
0228 
0229     if (!refCount) {
0230         if (!keepOpenCheck->isChecked()) {
0231             closeNow();
0232         } else {
0233             slotClean();
0234         }
0235     }
0236 }
0237 
0238 void KWidgetJobTrackerPrivate::ProgressWidget::closeNow()
0239 {
0240     close();
0241 
0242     // It might happen the next scenario:
0243     // - Start a job which opens a progress widget. Keep it open. Address job is 0xdeadbeef
0244     // - Start a new job, which is given address 0xdeadbeef. A new window is opened.
0245     //   This one will take much longer to complete. The key 0xdeadbeef on the widget map now
0246     //   stores the new widget address.
0247     // - Close the first progress widget that was opened (and has already finished) while the
0248     //   last one is still running. We remove its reference on the map. Wrong.
0249     // For that reason we have to check if the map stores the widget as the current one.
0250     // ereslibre
0251     if (tracker->d_func()->progressWidget[job] == this) {
0252         tracker->d_func()->progressWidget.remove(job);
0253         tracker->d_func()->progressWidgetsToBeShown.removeAll(job);
0254     }
0255 }
0256 
0257 bool KWidgetJobTrackerPrivate::ProgressWidget::eventFilter(QObject *watched, QEvent *event)
0258 {
0259     // Handle context menu events for the source/dest labels here, so that we are ref()ed while the
0260     // menu is exec()ed, to avoid a crash if the job finishes meanwhile. #159621.
0261     if ((watched == sourceEdit || watched == destEdit) && event->type() == QEvent::ContextMenu) {
0262         ref();
0263         watched->event(event);
0264         deref();
0265         return true;
0266     }
0267 
0268     return QWidget::eventFilter(watched, event);
0269 }
0270 
0271 void KWidgetJobTrackerPrivate::ProgressWidget::infoMessage(const QString &plain, const QString & /*rich*/)
0272 {
0273     speedLabel->setText(plain);
0274     speedLabel->setAlignment(speedLabel->alignment() & ~Qt::TextWordWrap);
0275 }
0276 
0277 void KWidgetJobTrackerPrivate::ProgressWidget::description(const QString &title, const QPair<QString, QString> &field1, const QPair<QString, QString> &field2)
0278 {
0279     setWindowTitle(title);
0280     caption = title;
0281     sourceInvite->setText(QCoreApplication::translate("KWidgetJobTracker", "%1:", "%1 is the label, we add a ':' to it").arg(field1.first));
0282     sourceEdit->setText(field1.second);
0283 
0284     if (field2.first.isEmpty()) {
0285         setDestVisible(false);
0286     } else {
0287         setDestVisible(true);
0288         checkDestination(QUrl::fromUserInput(field2.second)); // path or URL
0289         destInvite->setText(QCoreApplication::translate("KWidgetJobTracker", "%1:", "%1 is the label, we add a ':' to it").arg(field2.first));
0290         destEdit->setText(field2.second);
0291     }
0292 }
0293 
0294 void KWidgetJobTrackerPrivate::ProgressWidget::totalAmount(KJob::Unit unit, qulonglong amount)
0295 {
0296     switch (unit) {
0297     case KJob::Bytes:
0298         totalSizeKnown = true;
0299         // size is measured in bytes
0300         if (totalSize == amount) {
0301             return;
0302         }
0303         totalSize = amount;
0304         if (!startTime.isValid()) {
0305             startTime.start();
0306         }
0307         break;
0308 
0309     case KJob::Files:
0310         if (totalFiles == amount) {
0311             return;
0312         }
0313         totalFiles = amount;
0314         showTotals();
0315         break;
0316 
0317     case KJob::Directories:
0318         if (totalDirs == amount) {
0319             return;
0320         }
0321         totalDirs = amount;
0322         showTotals();
0323         break;
0324 
0325     case KJob::Items:
0326         if (totalItems == amount) {
0327             return;
0328         }
0329         totalItems = amount;
0330         showTotals();
0331         break;
0332 
0333     case KJob::UnitsCount:
0334         Q_UNREACHABLE();
0335         break;
0336     }
0337 }
0338 
0339 void KWidgetJobTrackerPrivate::ProgressWidget::processedAmount(KJob::Unit unit, qulonglong amount)
0340 {
0341     QString tmp;
0342 
0343     switch (unit) {
0344     case KJob::Bytes:
0345         if (processedSize == amount) {
0346             return;
0347         }
0348         processedSize = amount;
0349 
0350         if (totalSizeKnown) {
0351             //~ singular %1 of %2 complete
0352             //~ plural %1 of %2 complete
0353             tmp = QCoreApplication::translate("KWidgetJobTracker", "%1 of %2 complete", "", amount)
0354                       .arg(KJobTrackerFormatters::byteSize(amount), KJobTrackerFormatters::byteSize(totalSize));
0355         } else {
0356             tmp = KJobTrackerFormatters::byteSize(amount);
0357         }
0358         sizeLabel->setText(tmp);
0359         if (!totalSizeKnown) { // update jumping progressbar
0360             progressBar->setValue(amount);
0361         }
0362         break;
0363 
0364     case KJob::Directories:
0365         if (processedDirs == amount) {
0366             return;
0367         }
0368         processedDirs = amount;
0369 
0370         //~ singular %1 / %n folder
0371         //~ plural %1 / %n folders
0372         tmp = QCoreApplication::translate("KWidgetJobTracker", "%1 / %n folder(s)", "", totalDirs).arg(processedDirs);
0373         tmp += QLatin1String("   ");
0374         //~ singular %1 / %n file
0375         //~ plural %1 / %n files
0376         tmp += QCoreApplication::translate("KWidgetJobTracker", "%1 / %n file(s)", "", totalFiles).arg(processedFiles);
0377         progressLabel->setText(tmp);
0378         break;
0379 
0380     case KJob::Files:
0381         if (processedFiles == amount) {
0382             return;
0383         }
0384         processedFiles = amount;
0385 
0386         if (totalDirs > 1) {
0387             //~ singular %1 / %n folder
0388             //~ plural %1 / %n folders
0389             tmp = QCoreApplication::translate("KWidgetJobTracker", "%1 / %n folder(s)", "", totalDirs).arg(processedDirs);
0390             tmp += QLatin1String("   ");
0391         }
0392         //~ singular %1 / %n file
0393         //~ plural %1 / %n files
0394         tmp += QCoreApplication::translate("KWidgetJobTracker", "%1 / %n file(s)", "", totalFiles).arg(processedFiles);
0395         progressLabel->setText(tmp);
0396         break;
0397 
0398     case KJob::Items:
0399         if (processedItems == amount) {
0400             return;
0401         }
0402         processedItems = amount;
0403         //~ singular %1 / %n item
0404         //~ plural %1 / %n items
0405         tmp = QCoreApplication::translate("KWidgetJobTracker", "%1 / %n item(s)", "", totalItems).arg(processedItems);
0406         progressLabel->setText(tmp);
0407         break;
0408 
0409     case KJob::UnitsCount:
0410         Q_UNREACHABLE();
0411         break;
0412     }
0413 }
0414 
0415 void KWidgetJobTrackerPrivate::ProgressWidget::percent(unsigned long percent)
0416 {
0417     QString title = caption + QLatin1String(" (");
0418 
0419     if (totalSizeKnown) {
0420         title += QCoreApplication::translate("KWidgetJobTracker", "%1% of %2").arg(percent).arg(KJobTrackerFormatters::byteSize(totalSize));
0421     } else if (totalFiles) {
0422         //~ singular %1% of %n file
0423         //~ plural %1% of %n files
0424         title += QCoreApplication::translate("KWidgetJobTracker", "%1% of %n file(s)", "", totalFiles).arg(percent);
0425     } else {
0426         title += QCoreApplication::translate("KWidgetJobTracker", "%1%").arg(percent);
0427     }
0428 
0429     title += QLatin1Char(')');
0430 
0431     progressBar->setMaximum(100);
0432     progressBar->setValue(percent);
0433     setWindowTitle(title);
0434 }
0435 
0436 void KWidgetJobTrackerPrivate::ProgressWidget::speed(unsigned long value)
0437 {
0438     if (value == 0) {
0439         speedLabel->setText(QCoreApplication::translate("KWidgetJobTracker", "Stalled"));
0440     } else {
0441         const QString speedStr = KJobTrackerFormatters::byteSize(value);
0442         if (totalSizeKnown) {
0443             const int remaining = 1000 * (totalSize - processedSize) / value;
0444             //~ singular %1/s (%2 remaining)
0445             //~ plural %1/s (%2 remaining)
0446             speedLabel->setText(QCoreApplication::translate("KWidgetJobTracker", "%1/s (%2 remaining)", "", remaining)
0447                                     .arg(speedStr, KJobTrackerFormatters::duration(remaining)));
0448         } else { // total size is not known (#24228)
0449             speedLabel->setText(QCoreApplication::translate("KWidgetJobTracker", "%1/s", "speed in bytes per second").arg(speedStr));
0450         }
0451     }
0452 }
0453 
0454 void KWidgetJobTrackerPrivate::ProgressWidget::slotClean()
0455 {
0456     percent(100);
0457     cancelClose->setText(QCoreApplication::translate("KWidgetJobTracker", "&Close"));
0458     cancelClose->setIcon(QIcon::fromTheme(QStringLiteral("window-close")));
0459     cancelClose->setToolTip(QCoreApplication::translate("KWidgetJobTracker", "Close the current window or document"));
0460     openFile->setEnabled(true);
0461     if (!totalSizeKnown || totalSize < processedSize) {
0462         totalSize = processedSize;
0463     }
0464     processedAmount(KJob::Bytes, totalSize);
0465     keepOpenCheck->setEnabled(false);
0466     pauseButton->setEnabled(false);
0467     if (startTime.isValid()) {
0468         int s = startTime.elapsed();
0469         if (!s) {
0470             s = 1;
0471         }
0472         speedLabel->setText(QCoreApplication::translate("KWidgetJobTracker", "%1/s (done)").arg(KJobTrackerFormatters::byteSize(1000 * totalSize / s)));
0473     }
0474 }
0475 
0476 void KWidgetJobTrackerPrivate::ProgressWidget::suspended()
0477 {
0478     pauseButton->setText(QCoreApplication::translate("KWidgetJobTracker", "&Resume"));
0479     suspendedProperty = true;
0480 }
0481 
0482 void KWidgetJobTrackerPrivate::ProgressWidget::resumed()
0483 {
0484     pauseButton->setText(QCoreApplication::translate("KWidgetJobTracker", "&Pause"));
0485     suspendedProperty = false;
0486 }
0487 
0488 void KWidgetJobTrackerPrivate::ProgressWidget::closeEvent(QCloseEvent *event)
0489 {
0490     if (jobRegistered && tracker->stopOnClose(job)) {
0491         tracker->slotStop(job);
0492     }
0493 
0494     QWidget::closeEvent(event);
0495 }
0496 
0497 void KWidgetJobTrackerPrivate::ProgressWidget::init()
0498 {
0499     setWindowIcon(QIcon::fromTheme(QStringLiteral("document-save"), windowIcon()));
0500 
0501     QVBoxLayout *topLayout = new QVBoxLayout(this);
0502 
0503     QGridLayout *grid = new QGridLayout();
0504     topLayout->addLayout(grid);
0505     const int horizontalSpacing = style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing);
0506     grid->addItem(new QSpacerItem(horizontalSpacing, 0), 0, 1);
0507     // filenames or action name
0508     sourceInvite = new QLabel(QCoreApplication::translate("KWidgetJobTracker", "Source:", "The source url of a job"), this);
0509     grid->addWidget(sourceInvite, 0, 0);
0510 
0511     sourceEdit = new KSqueezedTextLabel(this);
0512     sourceEdit->setTextInteractionFlags(Qt::TextSelectableByMouse);
0513     sourceEdit->installEventFilter(this);
0514     grid->addWidget(sourceEdit, 0, 2);
0515 
0516     destInvite = new QLabel(QCoreApplication::translate("KWidgetJobTracker", "Destination:", "The destination url of a job"), this);
0517     grid->addWidget(destInvite, 1, 0);
0518 
0519     destEdit = new KSqueezedTextLabel(this);
0520     destEdit->setTextInteractionFlags(Qt::TextSelectableByMouse);
0521     destEdit->installEventFilter(this);
0522     grid->addWidget(destEdit, 1, 2);
0523 
0524     QHBoxLayout *progressHBox = new QHBoxLayout();
0525     topLayout->addLayout(progressHBox);
0526 
0527     progressBar = new QProgressBar(this);
0528     progressBar->setMaximum(0); // want a jumping progress bar if percent is not emitted
0529     progressHBox->addWidget(progressBar);
0530 
0531     suspendedProperty = false;
0532 
0533     // processed info
0534     QHBoxLayout *hBox = new QHBoxLayout();
0535     topLayout->addLayout(hBox);
0536 
0537     arrowButton = new QPushButton(this);
0538     arrowButton->setMaximumSize(QSize(32, 25));
0539     arrowButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
0540     arrowButton->setToolTip(QCoreApplication::translate("KWidgetJobTracker", "Click this to expand the dialog, to show details"));
0541     arrowState = Qt::DownArrow;
0542     connect(arrowButton, &QPushButton::clicked, this, &KWidgetJobTrackerPrivate::ProgressWidget::arrowClicked);
0543     hBox->addWidget(arrowButton);
0544     hBox->addStretch(1);
0545 
0546     KSeparator *separator1 = new KSeparator(Qt::Horizontal, this);
0547     topLayout->addWidget(separator1);
0548 
0549     sizeLabel = new QLabel(this);
0550     hBox->addWidget(sizeLabel, 0, Qt::AlignLeft);
0551 
0552     resumeLabel = new QLabel(this);
0553     hBox->addWidget(resumeLabel);
0554 
0555     pauseButton = new QPushButton(QCoreApplication::translate("KWidgetJobTracker", "&Pause"), this);
0556     pauseButton->setVisible(job && (job->capabilities() & KJob::Suspendable));
0557     connect(pauseButton, &QPushButton::clicked, this, &KWidgetJobTrackerPrivate::ProgressWidget::pauseResumeClicked);
0558     hBox->addWidget(pauseButton);
0559 
0560     hBox = new QHBoxLayout();
0561     topLayout->addLayout(hBox);
0562 
0563     speedLabel = new QLabel(this);
0564     hBox->addWidget(speedLabel, 1);
0565     speedLabel->hide();
0566 
0567     hBox = new QHBoxLayout();
0568     topLayout->addLayout(hBox);
0569 
0570     progressLabel = new QLabel(this);
0571     progressLabel->setAlignment(Qt::AlignLeft);
0572     hBox->addWidget(progressLabel);
0573     progressLabel->hide();
0574 
0575     keepOpenCheck = new QCheckBox(QCoreApplication::translate("KWidgetJobTracker", "&Keep this window open after transfer is complete"), this);
0576     connect(keepOpenCheck, &QCheckBox::toggled, this, &KWidgetJobTrackerPrivate::ProgressWidget::keepOpenToggled);
0577     topLayout->addWidget(keepOpenCheck);
0578     keepOpenCheck->hide();
0579 
0580     hBox = new QHBoxLayout();
0581     topLayout->addLayout(hBox);
0582 
0583     openFile = new QPushButton(QCoreApplication::translate("KWidgetJobTracker", "Open &File"), this);
0584     connect(openFile, &QPushButton::clicked, this, &KWidgetJobTrackerPrivate::ProgressWidget::openFileClicked);
0585     hBox->addWidget(openFile);
0586     openFile->setEnabled(false);
0587     openFile->hide();
0588 
0589     openLocation = new QPushButton(QCoreApplication::translate("KWidgetJobTracker", "Open &Destination"), this);
0590     connect(openLocation, &QPushButton::clicked, this, &KWidgetJobTrackerPrivate::ProgressWidget::openLocationClicked);
0591     hBox->addWidget(openLocation);
0592     openLocation->hide();
0593 
0594     hBox->addStretch(1);
0595 
0596     cancelClose = new QPushButton(this);
0597     cancelClose->setText(QCoreApplication::translate("KWidgetJobTracker", "&Cancel"));
0598     cancelClose->setIcon(QIcon::fromTheme(QStringLiteral("dialog-cancel")));
0599     connect(cancelClose, &QPushButton::clicked, this, &KWidgetJobTrackerPrivate::ProgressWidget::cancelClicked);
0600     hBox->addWidget(cancelClose);
0601 
0602     resize(sizeHint());
0603     setMaximumHeight(sizeHint().height());
0604 
0605     setWindowTitle(QCoreApplication::translate("KWidgetJobTracker", "Progress Dialog")); // show something better than kuiserver
0606 }
0607 
0608 void KWidgetJobTrackerPrivate::ProgressWidget::showTotals()
0609 {
0610     // Show the totals in the progress label, if we still haven't
0611     // processed anything. This is useful when the stat'ing phase
0612     // of CopyJob takes a long time (e.g. over networks).
0613     if (processedFiles == 0 && processedDirs == 0 && processedItems == 0) {
0614         QString total;
0615         if (totalItems > 1) {
0616             //~ singular %n item
0617             //~ plural %n items
0618             total = QCoreApplication::translate("KWidgetJobTracker", "%n item(s)", "", totalItems);
0619             progressLabel->setText(total);
0620         } else {
0621             if (totalDirs > 1) {
0622                 //~ singular %n folder
0623                 //~ plural %n folders
0624                 total = QCoreApplication::translate("KWidgetJobTracker", "%n folder(s)", "", totalDirs) + QLatin1String("   ");
0625             }
0626             //~ singular %n file
0627             //~ plural %n files
0628             total += QCoreApplication::translate("KWidgetJobTracker", "%n file(s)", "", totalFiles);
0629             progressLabel->setText(total);
0630         }
0631     }
0632 }
0633 
0634 void KWidgetJobTrackerPrivate::ProgressWidget::setDestVisible(bool visible)
0635 {
0636     // We can't hide the destInvite/destEdit labels,
0637     // because it screws up the QGridLayout.
0638     if (visible) {
0639         destInvite->show();
0640         destEdit->show();
0641     } else {
0642         destInvite->hide();
0643         destEdit->hide();
0644         destInvite->setText(QString());
0645         destEdit->setText(QString());
0646     }
0647     setMaximumHeight(sizeHint().height());
0648 }
0649 
0650 void KWidgetJobTrackerPrivate::ProgressWidget::checkDestination(const QUrl &dest)
0651 {
0652     bool ok = true;
0653 
0654     if (dest.isLocalFile()) {
0655         const QString path = dest.toLocalFile();
0656         if (path.contains(QDir::tempPath())) {
0657             ok = false; // it's in the tmp directory
0658         }
0659     }
0660 
0661     if (ok) {
0662         openFile->show();
0663         openLocation->show();
0664         keepOpenCheck->show();
0665         setMaximumHeight(sizeHint().height());
0666         location = dest;
0667     }
0668 }
0669 
0670 void KWidgetJobTrackerPrivate::ProgressWidget::keepOpenToggled(bool keepOpen)
0671 {
0672     if (keepOpen) {
0673         Q_ASSERT(!tracker->d_func()->eventLoopLocker);
0674         tracker->d_func()->eventLoopLocker = new QEventLoopLocker;
0675     } else {
0676         delete tracker->d_func()->eventLoopLocker;
0677         tracker->d_func()->eventLoopLocker = nullptr;
0678     }
0679 }
0680 
0681 static QString findKdeOpen()
0682 {
0683     const QString exec = QStandardPaths::findExecutable(QStringLiteral("kde-open"));
0684     if (exec.isEmpty()) {
0685         qCWarning(KJOBWIDGETS) << "Could not find kde-open executable in PATH";
0686     }
0687     return exec;
0688 }
0689 
0690 void KWidgetJobTrackerPrivate::ProgressWidget::openFileClicked()
0691 {
0692     const QString exec = findKdeOpen();
0693     if (!exec.isEmpty()) {
0694         QProcess::startDetached(exec, QStringList() << location.toDisplayString());
0695     }
0696 }
0697 
0698 void KWidgetJobTrackerPrivate::ProgressWidget::openLocationClicked()
0699 {
0700     const QString exec = findKdeOpen();
0701     if (!exec.isEmpty()) {
0702         QProcess::startDetached(exec, QStringList() << location.adjusted(QUrl::RemoveFilename).toString());
0703     }
0704 }
0705 
0706 void KWidgetJobTrackerPrivate::ProgressWidget::pauseResumeClicked()
0707 {
0708     if (jobRegistered && !suspendedProperty) {
0709         tracker->slotSuspend(job);
0710     } else if (jobRegistered) {
0711         tracker->slotResume(job);
0712     }
0713 }
0714 
0715 void KWidgetJobTrackerPrivate::ProgressWidget::cancelClicked()
0716 {
0717     if (jobRegistered) {
0718         tracker->slotStop(job);
0719     }
0720     closeNow();
0721 }
0722 
0723 void KWidgetJobTrackerPrivate::ProgressWidget::arrowClicked()
0724 {
0725     if (arrowState == Qt::DownArrow) {
0726         // The arrow is in the down position, dialog is collapsed, expand it and change icon.
0727         progressLabel->show();
0728         speedLabel->show();
0729         arrowButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up")));
0730         arrowButton->setToolTip(QCoreApplication::translate("KWidgetJobTracker", "Click this to collapse the dialog, to hide details"));
0731         arrowState = Qt::UpArrow;
0732     } else {
0733         // Collapse the dialog
0734         progressLabel->hide();
0735         speedLabel->hide();
0736         arrowButton->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
0737         arrowButton->setToolTip(QCoreApplication::translate("KWidgetJobTracker", "Click this to expand the dialog, to show details"));
0738         arrowState = Qt::DownArrow;
0739     }
0740     setMaximumHeight(sizeHint().height());
0741 }
0742 
0743 #include "moc_kwidgetjobtracker.cpp"
0744 #include "moc_kwidgetjobtracker_p.cpp"