]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropsprogressinfo.cpp
Merge branch 'release/20.08'
[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 setLayout(layout);
48
49 m_label = new QLabel(i18nc("@info:progress", "Counting folders: %1", 0), this);
50 layout->addWidget(m_label);
51
52 m_progressBar = new QProgressBar(this);
53 m_progressBar->setMinimum(0);
54 m_progressBar->setMaximum(0);
55 m_progressBar->setValue(0);
56 layout->addWidget(m_progressBar);
57
58 layout->addStretch();
59
60 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
61 connect(buttonBox, &QDialogButtonBox::accepted, this, &ViewPropsProgressInfo::accept);
62 connect(buttonBox, &QDialogButtonBox::rejected, this, &ViewPropsProgressInfo::reject);
63 layout->addWidget(buttonBox);
64
65 // Use the directory size job to count the number of directories first. This
66 // allows to give a progress indication for the user when applying the view
67 // properties later.
68 m_dirSizeJob = KIO::directorySize(dir);
69 connect(m_dirSizeJob, &KIO::DirectorySizeJob::result,
70 this, &ViewPropsProgressInfo::applyViewProperties);
71
72 // The directory size job cannot emit any progress signal, as it is not aware
73 // about the total number of directories. Therefor a timer is triggered, which
74 // periodically updates the current directory count.
75 m_timer = new QTimer(this);
76 connect(m_timer, &QTimer::timeout,
77 this, &ViewPropsProgressInfo::updateProgress);
78 m_timer->start(300);
79 }
80
81 ViewPropsProgressInfo::~ViewPropsProgressInfo()
82 {
83 delete m_viewProps;
84 m_viewProps = nullptr;
85 }
86
87 void ViewPropsProgressInfo::closeEvent(QCloseEvent* event)
88 {
89 m_timer->stop();
90 m_applyViewPropsJob = nullptr;
91 QDialog::closeEvent(event);
92 }
93
94 void ViewPropsProgressInfo::reject()
95 {
96 if (m_dirSizeJob) {
97 m_dirSizeJob->kill();
98 m_dirSizeJob = nullptr;
99 }
100
101 if (m_applyViewPropsJob) {
102 m_applyViewPropsJob->kill();
103 m_applyViewPropsJob = nullptr;
104 }
105
106 QDialog::reject();
107 }
108
109 void ViewPropsProgressInfo::updateProgress()
110 {
111 if (m_dirSizeJob) {
112 const int subdirs = m_dirSizeJob->totalSubdirs();
113 m_label->setText(i18nc("@info:progress", "Counting folders: %1", subdirs));
114 }
115
116 if (m_applyViewPropsJob) {
117 const int progress = m_applyViewPropsJob->progress();
118 m_progressBar->setValue(progress);
119 }
120 }
121
122 void ViewPropsProgressInfo::applyViewProperties()
123 {
124 if (m_dirSizeJob->error()) {
125 return;
126 }
127
128 const int subdirs = m_dirSizeJob->totalSubdirs();
129 m_label->setText(i18nc("@info:progress", "Folders: %1", subdirs));
130 m_progressBar->setMaximum(subdirs);
131
132 m_dirSizeJob = nullptr;
133
134 m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps);
135 connect(m_applyViewPropsJob, &ApplyViewPropsJob::result,
136 this, &ViewPropsProgressInfo::close);
137 }
138