]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropsprogressinfo.cpp
show busy indicator inside the progress bar when counting the directories
[dolphin.git] / src / 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "viewpropsprogressinfo.h"
22 #include "applyviewpropsjob.h"
23 #include "viewproperties.h"
24
25 #include <QLabel>
26 #include <QProgressBar>
27 #include <QTimer>
28 #include <QVBoxLayout>
29
30 #include <assert.h>
31 #include <klocale.h>
32 #include <kurl.h>
33 #include <kio/jobclasses.h>
34
35 ViewPropsProgressInfo::ViewPropsProgressInfo(QWidget* parent,
36 const KUrl& dir,
37 const ViewProperties& viewProps) :
38 KDialog(parent),
39 m_dir(dir),
40 m_viewProps(0),
41 m_label(0),
42 m_progressBar(0),
43 m_dirSizeJob(0),
44 m_applyViewPropsJob(0),
45 m_timer(0)
46 {
47 setCaption(i18n("Applying view properties"));
48 setButtons(KDialog::Cancel);
49
50 m_viewProps = new ViewProperties(dir);
51 m_viewProps->setViewMode(viewProps.viewMode());
52 m_viewProps->setShowHiddenFilesEnabled(viewProps.isShowHiddenFilesEnabled());
53 m_viewProps->setSorting(viewProps.sorting());
54 m_viewProps->setSortOrder(viewProps.sortOrder());
55
56 // the view properties are stored by the ViewPropsApplierJob, so prevent
57 // that the view properties are saved twice:
58 m_viewProps->setAutoSaveEnabled(false);
59
60 QWidget* main = new QWidget();
61 QVBoxLayout* topLayout = new QVBoxLayout();
62
63 m_label = new QLabel(i18n("Counting folders: %1", 0), main);
64 m_progressBar = new QProgressBar(main);
65 m_progressBar->setMinimum(0);
66 m_progressBar->setMaximum(0);
67 m_progressBar->setValue(0);
68
69 topLayout->addWidget(m_label);
70 topLayout->addWidget(m_progressBar);
71
72 main->setLayout(topLayout);
73 setMainWidget(main);
74
75 // Use the directory size job to count the number of directories first. This
76 // allows to give a progress indication for the user when applying the view
77 // properties later.
78 m_dirSizeJob = KIO::directorySize(dir);
79 connect(m_dirSizeJob, SIGNAL(result(KJob*)),
80 this, SLOT(applyViewProperties()));
81
82 // The directory size job cannot emit any progress signal, as it is not aware
83 // about the total number of directories. Therefor a timer is triggered, which
84 // periodically updates the current directory count.
85 m_timer = new QTimer(this);
86 connect(m_timer, SIGNAL(timeout()),
87 this, SLOT(updateProgress()));
88 m_timer->start(300);
89
90 connect(this, SIGNAL(cancelClicked()), this, SLOT(cancelApplying()));
91 }
92
93 ViewPropsProgressInfo::~ViewPropsProgressInfo()
94 {
95 delete m_viewProps;
96 m_viewProps = 0;
97 }
98
99 void ViewPropsProgressInfo::closeEvent(QCloseEvent* event)
100 {
101 m_timer->stop();
102 m_applyViewPropsJob = 0;
103 KDialog::closeEvent(event);
104 }
105
106 void ViewPropsProgressInfo::updateProgress()
107 {
108 if (m_dirSizeJob != 0) {
109 const int subdirs = m_dirSizeJob->totalSubdirs();
110 m_label->setText(i18n("Counting folders: %1", subdirs));
111 }
112
113 if (m_applyViewPropsJob != 0) {
114 const int progress = m_applyViewPropsJob->progress();
115 m_progressBar->setValue(progress);
116 }
117 }
118
119 void ViewPropsProgressInfo::applyViewProperties()
120 {
121 if (m_dirSizeJob->error()) {
122 return;
123 }
124
125 const int subdirs = m_dirSizeJob->totalSubdirs();
126 m_label->setText(i18n("Folders: %1", subdirs));
127 m_progressBar->setMaximum(subdirs);
128
129 m_dirSizeJob = 0;
130
131 m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps);
132 connect(m_applyViewPropsJob, SIGNAL(result(KJob*)),
133 this, SLOT(close()));
134 }
135
136 void ViewPropsProgressInfo::cancelApplying()
137 {
138 if (m_dirSizeJob != 0) {
139 m_dirSizeJob->doKill();
140 }
141
142 if (m_applyViewPropsJob != 0) {
143 m_applyViewPropsJob->doKill();
144 }
145 }
146
147 #include "viewpropsprogressinfo.moc"