]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/viewpropsprogressinfo.cpp
489cc1d8c5b2bbb33b0087aa3759022bf23f1e80
[dolphin.git] / src / settings / viewpropsprogressinfo.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "viewpropsprogressinfo.h"
22 #include "applyviewpropsjob.h"
23
24 #include <QLabel>
25 #include <QProgressBar>
26 #include <QTimer>
27 #include <QVBoxLayout>
28 #include <QDialogButtonBox>
29 #include <QPushButton>
30
31 #include <KConfigGroup>
32 #include <KLocalizedString>
33 #include <KIO/JobClasses>
34
35 #include <views/viewproperties.h>
36
37 ViewPropsProgressInfo::ViewPropsProgressInfo(QWidget* parent,
38 const QUrl& dir,
39 const ViewProperties& viewProps) :
40 QDialog(parent),
41 m_dir(dir),
42 m_viewProps(0),
43 m_label(0),
44 m_progressBar(0),
45 m_dirSizeJob(0),
46 m_applyViewPropsJob(0),
47 m_timer(0)
48 {
49 const QSize minSize = minimumSize();
50 setMinimumSize(QSize(320, minSize.height()));
51 setWindowTitle(i18nc("@title:window", "Applying View Properties"));
52 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
53
54 m_viewProps = new ViewProperties(dir);
55 m_viewProps->setDirProperties(viewProps);
56
57 // the view properties are stored by the ViewPropsApplierJob, so prevent
58 // that the view properties are saved twice:
59 m_viewProps->setAutoSaveEnabled(false);
60
61 auto layout = new QVBoxLayout(this);
62 setLayout(layout);
63
64 m_label = new QLabel(i18nc("@info:progress", "Counting folders: %1", 0), this);
65 layout->addWidget(m_label);
66
67 m_progressBar = new QProgressBar(this);
68 m_progressBar->setMinimum(0);
69 m_progressBar->setMaximum(0);
70 m_progressBar->setValue(0);
71 layout->addWidget(m_progressBar);
72
73 layout->addStretch();
74
75 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
76 connect(buttonBox, &QDialogButtonBox::accepted, this, &ViewPropsProgressInfo::accept);
77 connect(buttonBox, &QDialogButtonBox::rejected, this, &ViewPropsProgressInfo::reject);
78 layout->addWidget(buttonBox);
79
80 // Use the directory size job to count the number of directories first. This
81 // allows to give a progress indication for the user when applying the view
82 // properties later.
83 m_dirSizeJob = KIO::directorySize(dir);
84 connect(m_dirSizeJob, &KIO::DirectorySizeJob::result,
85 this, &ViewPropsProgressInfo::applyViewProperties);
86
87 // The directory size job cannot emit any progress signal, as it is not aware
88 // about the total number of directories. Therefor a timer is triggered, which
89 // periodically updates the current directory count.
90 m_timer = new QTimer(this);
91 connect(m_timer, &QTimer::timeout,
92 this, &ViewPropsProgressInfo::updateProgress);
93 m_timer->start(300);
94 }
95
96 ViewPropsProgressInfo::~ViewPropsProgressInfo()
97 {
98 delete m_viewProps;
99 m_viewProps = 0;
100 }
101
102 void ViewPropsProgressInfo::closeEvent(QCloseEvent* event)
103 {
104 m_timer->stop();
105 m_applyViewPropsJob = 0;
106 QDialog::closeEvent(event);
107 }
108
109 void ViewPropsProgressInfo::reject()
110 {
111 if (m_dirSizeJob) {
112 m_dirSizeJob->kill();
113 m_dirSizeJob = 0;
114 }
115
116 if (m_applyViewPropsJob) {
117 m_applyViewPropsJob->kill();
118 m_applyViewPropsJob = 0;
119 }
120
121 QDialog::reject();
122 }
123
124 void ViewPropsProgressInfo::updateProgress()
125 {
126 if (m_dirSizeJob) {
127 const int subdirs = m_dirSizeJob->totalSubdirs();
128 m_label->setText(i18nc("@info:progress", "Counting folders: %1", subdirs));
129 }
130
131 if (m_applyViewPropsJob) {
132 const int progress = m_applyViewPropsJob->progress();
133 m_progressBar->setValue(progress);
134 }
135 }
136
137 void ViewPropsProgressInfo::applyViewProperties()
138 {
139 if (m_dirSizeJob->error()) {
140 return;
141 }
142
143 const int subdirs = m_dirSizeJob->totalSubdirs();
144 m_label->setText(i18nc("@info:progress", "Folders: %1", subdirs));
145 m_progressBar->setMaximum(subdirs);
146
147 m_dirSizeJob = 0;
148
149 m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps);
150 connect(m_applyViewPropsJob, &ApplyViewPropsJob::result,
151 this, &ViewPropsProgressInfo::close);
152 }
153