File indexing completed on 2024-04-21 16:30:03
0001 /* 0002 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "viewpropsprogressinfo.h" 0008 0009 #include "applyviewpropsjob.h" 0010 #include "views/viewproperties.h" 0011 0012 #include <KLocalizedString> 0013 0014 #include <QDialogButtonBox> 0015 #include <QLabel> 0016 #include <QProgressBar> 0017 #include <QPushButton> 0018 #include <QTimer> 0019 #include <QVBoxLayout> 0020 0021 ViewPropsProgressInfo::ViewPropsProgressInfo(QWidget *parent, const QUrl &dir, const ViewProperties &viewProps) 0022 : QDialog(parent) 0023 , m_dir(dir) 0024 , m_viewProps(nullptr) 0025 , m_label(nullptr) 0026 , m_progressBar(nullptr) 0027 , m_dirSizeJob(nullptr) 0028 , m_applyViewPropsJob(nullptr) 0029 , m_timer(nullptr) 0030 { 0031 const QSize minSize = minimumSize(); 0032 setMinimumSize(QSize(320, minSize.height())); 0033 setWindowTitle(i18nc("@title:window", "Applying View Properties")); 0034 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); 0035 0036 m_viewProps = new ViewProperties(dir); 0037 m_viewProps->setDirProperties(viewProps); 0038 0039 // the view properties are stored by the ApplyViewPropsJob, so prevent 0040 // that the view properties are saved twice: 0041 m_viewProps->setAutoSaveEnabled(false); 0042 0043 auto layout = new QVBoxLayout(this); 0044 0045 m_label = new QLabel(i18nc("@info:progress", "Counting folders: %1", 0), this); 0046 layout->addWidget(m_label); 0047 0048 m_progressBar = new QProgressBar(this); 0049 m_progressBar->setMinimum(0); 0050 m_progressBar->setMaximum(0); 0051 m_progressBar->setValue(0); 0052 layout->addWidget(m_progressBar); 0053 0054 layout->addStretch(); 0055 0056 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this); 0057 connect(buttonBox, &QDialogButtonBox::accepted, this, &ViewPropsProgressInfo::accept); 0058 connect(buttonBox, &QDialogButtonBox::rejected, this, &ViewPropsProgressInfo::reject); 0059 layout->addWidget(buttonBox); 0060 0061 // Use the directory size job to count the number of directories first. This 0062 // allows to give a progress indication for the user when applying the view 0063 // properties later. 0064 m_dirSizeJob = KIO::directorySize(dir); 0065 connect(m_dirSizeJob, &KIO::DirectorySizeJob::result, this, &ViewPropsProgressInfo::applyViewProperties); 0066 0067 // The directory size job cannot emit any progress signal, as it is not aware 0068 // about the total number of directories. Therefor a timer is triggered, which 0069 // periodically updates the current directory count. 0070 m_timer = new QTimer(this); 0071 connect(m_timer, &QTimer::timeout, this, &ViewPropsProgressInfo::updateProgress); 0072 m_timer->start(300); 0073 } 0074 0075 ViewPropsProgressInfo::~ViewPropsProgressInfo() 0076 { 0077 delete m_viewProps; 0078 m_viewProps = nullptr; 0079 } 0080 0081 void ViewPropsProgressInfo::closeEvent(QCloseEvent *event) 0082 { 0083 m_timer->stop(); 0084 m_applyViewPropsJob = nullptr; 0085 QDialog::closeEvent(event); 0086 } 0087 0088 void ViewPropsProgressInfo::reject() 0089 { 0090 if (m_dirSizeJob) { 0091 m_dirSizeJob->kill(); 0092 m_dirSizeJob = nullptr; 0093 } 0094 0095 if (m_applyViewPropsJob) { 0096 m_applyViewPropsJob->kill(); 0097 m_applyViewPropsJob = nullptr; 0098 } 0099 0100 QDialog::reject(); 0101 } 0102 0103 void ViewPropsProgressInfo::updateProgress() 0104 { 0105 if (m_dirSizeJob) { 0106 const int subdirs = m_dirSizeJob->totalSubdirs(); 0107 m_label->setText(i18nc("@info:progress", "Counting folders: %1", subdirs)); 0108 } 0109 0110 if (m_applyViewPropsJob) { 0111 const int progress = m_applyViewPropsJob->progress(); 0112 m_progressBar->setValue(progress); 0113 } 0114 } 0115 0116 void ViewPropsProgressInfo::applyViewProperties() 0117 { 0118 if (m_dirSizeJob->error()) { 0119 return; 0120 } 0121 0122 const int subdirs = m_dirSizeJob->totalSubdirs(); 0123 m_label->setText(i18nc("@info:progress", "Folders: %1", subdirs)); 0124 m_progressBar->setMaximum(subdirs); 0125 0126 m_dirSizeJob = nullptr; 0127 0128 m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps); 0129 connect(m_applyViewPropsJob, &ApplyViewPropsJob::result, this, &ViewPropsProgressInfo::close); 0130 } 0131 0132 #include "moc_viewpropsprogressinfo.cpp"