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