]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropsprogressinfo.cpp
Merge branch 'release/20.12'
[dolphin.git] / src / settings / viewpropsprogressinfo.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "viewpropsprogressinfo.h"
8
9 #include "applyviewpropsjob.h"
10 #include "views/viewproperties.h"
11
12 #include <KConfigGroup>
13 #include <KLocalizedString>
14
15 #include <QDialogButtonBox>
16 #include <QLabel>
17 #include <QProgressBar>
18 #include <QPushButton>
19 #include <QTimer>
20 #include <QVBoxLayout>
21
22 ViewPropsProgressInfo::ViewPropsProgressInfo(QWidget* parent,
23 const QUrl& dir,
24 const ViewProperties& viewProps) :
25 QDialog(parent),
26 m_dir(dir),
27 m_viewProps(nullptr),
28 m_label(nullptr),
29 m_progressBar(nullptr),
30 m_dirSizeJob(nullptr),
31 m_applyViewPropsJob(nullptr),
32 m_timer(nullptr)
33 {
34 const QSize minSize = minimumSize();
35 setMinimumSize(QSize(320, minSize.height()));
36 setWindowTitle(i18nc("@title:window", "Applying View Properties"));
37 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
38
39 m_viewProps = new ViewProperties(dir);
40 m_viewProps->setDirProperties(viewProps);
41
42 // the view properties are stored by the ApplyViewPropsJob, so prevent
43 // that the view properties are saved twice:
44 m_viewProps->setAutoSaveEnabled(false);
45
46 auto layout = new QVBoxLayout(this);
47
48 m_label = new QLabel(i18nc("@info:progress", "Counting folders: %1", 0), this);
49 layout->addWidget(m_label);
50
51 m_progressBar = new QProgressBar(this);
52 m_progressBar->setMinimum(0);
53 m_progressBar->setMaximum(0);
54 m_progressBar->setValue(0);
55 layout->addWidget(m_progressBar);
56
57 layout->addStretch();
58
59 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
60 connect(buttonBox, &QDialogButtonBox::accepted, this, &ViewPropsProgressInfo::accept);
61 connect(buttonBox, &QDialogButtonBox::rejected, this, &ViewPropsProgressInfo::reject);
62 layout->addWidget(buttonBox);
63
64 // Use the directory size job to count the number of directories first. This
65 // allows to give a progress indication for the user when applying the view
66 // properties later.
67 m_dirSizeJob = KIO::directorySize(dir);
68 connect(m_dirSizeJob, &KIO::DirectorySizeJob::result,
69 this, &ViewPropsProgressInfo::applyViewProperties);
70
71 // The directory size job cannot emit any progress signal, as it is not aware
72 // about the total number of directories. Therefor a timer is triggered, which
73 // periodically updates the current directory count.
74 m_timer = new QTimer(this);
75 connect(m_timer, &QTimer::timeout,
76 this, &ViewPropsProgressInfo::updateProgress);
77 m_timer->start(300);
78 }
79
80 ViewPropsProgressInfo::~ViewPropsProgressInfo()
81 {
82 delete m_viewProps;
83 m_viewProps = nullptr;
84 }
85
86 void ViewPropsProgressInfo::closeEvent(QCloseEvent* event)
87 {
88 m_timer->stop();
89 m_applyViewPropsJob = nullptr;
90 QDialog::closeEvent(event);
91 }
92
93 void ViewPropsProgressInfo::reject()
94 {
95 if (m_dirSizeJob) {
96 m_dirSizeJob->kill();
97 m_dirSizeJob = nullptr;
98 }
99
100 if (m_applyViewPropsJob) {
101 m_applyViewPropsJob->kill();
102 m_applyViewPropsJob = nullptr;
103 }
104
105 QDialog::reject();
106 }
107
108 void ViewPropsProgressInfo::updateProgress()
109 {
110 if (m_dirSizeJob) {
111 const int subdirs = m_dirSizeJob->totalSubdirs();
112 m_label->setText(i18nc("@info:progress", "Counting folders: %1", subdirs));
113 }
114
115 if (m_applyViewPropsJob) {
116 const int progress = m_applyViewPropsJob->progress();
117 m_progressBar->setValue(progress);
118 }
119 }
120
121 void ViewPropsProgressInfo::applyViewProperties()
122 {
123 if (m_dirSizeJob->error()) {
124 return;
125 }
126
127 const int subdirs = m_dirSizeJob->totalSubdirs();
128 m_label->setText(i18nc("@info:progress", "Folders: %1", subdirs));
129 m_progressBar->setMaximum(subdirs);
130
131 m_dirSizeJob = nullptr;
132
133 m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps);
134 connect(m_applyViewPropsJob, &ApplyViewPropsJob::result,
135 this, &ViewPropsProgressInfo::close);
136 }
137