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