]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewpropsprogressinfo.cpp
Use a KIO Job for applying the view properties recursively to sub 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(viewProps),
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 QWidget* main = new QWidget();
51 QVBoxLayout* topLayout = new QVBoxLayout();
52
53 m_label = new QLabel(i18n("Counting folders: %1", 0), main);
54 m_progressBar = new QProgressBar(main);
55
56 topLayout->addWidget(m_label);
57 topLayout->addWidget(m_progressBar);
58
59 main->setLayout(topLayout);
60 setMainWidget(main);
61
62 // Use the directory size job to count the number of directories first. This
63 // allows to give a progress indication for the user when applying the view
64 // properties later.
65 m_dirSizeJob = KIO::directorySize(dir);
66 connect(m_dirSizeJob, SIGNAL(result(KJob*)),
67 this, SLOT(applyViewProperties()));
68
69 // The directory size job cannot emit any progress signal, as it is not aware
70 // about the total number of directories. Therefor a timer is triggered, which
71 // periodically updates the current directory count.
72 m_timer = new QTimer(this);
73 connect(m_timer, SIGNAL(timeout()),
74 this, SLOT(updateProgress()));
75 m_timer->start(300);
76
77 connect(this, SIGNAL(cancelClicked()), this, SLOT(cancelApplying()));
78 }
79
80 ViewPropsProgressInfo::~ViewPropsProgressInfo()
81 {
82 m_timer->stop();
83 }
84
85 void ViewPropsProgressInfo::updateProgress()
86 {
87 if (m_dirSizeJob != 0) {
88 const int subdirs = m_dirSizeJob->totalSubdirs();
89 m_label->setText(i18n("Counting folders: %1", subdirs));
90 }
91 else {
92 assert(m_applyViewPropsJob != 0);
93 const int progress = m_applyViewPropsJob->progress();
94 m_progressBar->setValue(progress);
95 }
96 }
97
98 void ViewPropsProgressInfo::applyViewProperties()
99 {
100 if (m_dirSizeJob->error()) {
101 return;
102 }
103
104 const int subdirs = m_dirSizeJob->totalSubdirs();
105 m_label->setText(i18n("Folders: %1", subdirs));
106 m_progressBar->setMaximum(subdirs);
107
108 m_dirSizeJob = 0;
109
110 m_applyViewPropsJob = new ApplyViewPropsJob(m_dir, *m_viewProps);
111 connect(m_applyViewPropsJob, SIGNAL(result(KJob*)),
112 this, SLOT(close()));
113 }
114
115 void ViewPropsProgressInfo::cancelApplying()
116 {
117 if (m_dirSizeJob != 0) {
118 m_dirSizeJob->doKill();
119 }
120 else {
121 assert(m_applyViewPropsJob != 0);
122 m_applyViewPropsJob->doKill();
123 }
124 }
125
126 #include "viewpropsprogressinfo.moc"