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