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