• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

filelight

  • sources
  • kde-4.12
  • kdeutils
  • filelight
  • src
  • part
part.cpp
Go to the documentation of this file.
1 /***********************************************************************
2 * Copyright 2003-2004 Max Howell <max.howell@methylblue.com>
3 * Copyright 2008-2013 Martin Sandsmark <martin.sandsmark@kde.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License or (at your option) version 3 or any later version
9 * accepted by the membership of KDE e.V. (or its successor approved
10 * by the membership of KDE e.V.), which shall act as a proxy
11 * defined in Section 14 of version 3 of the license.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 
22 #include "part.h"
23 
24 #include "Config.h"
25 #include "define.h"
26 #include "fileTree.h"
27 #include "progressBox.h"
28 #include "radialMap/widget.h"
29 #include "scan.h"
30 #include "settingsDialog.h"
31 #include "summaryWidget.h"
32 
33 #include <KAboutData> //::createAboutData()
34 #include <KAction>
35 #include <KActionCollection>
36 #include <KLocale>
37 #include <KMessageBox> //::start()
38 #include <KStandardAction>
39 #include <KStatusBar>
40 #include <KPluginFactory>
41 
42 #include <QtCore/QFile> //encodeName()
43 #include <QtCore/QTimer> //postInit() hack
44 #include <QtCore/QByteArray>
45 #include <QtCore/QDir>
46 #include <QtGui/QScrollArea>
47 
48 #include <unistd.h> //access()
49 #include <iostream>
50 
51 namespace Filelight {
52 
53 K_PLUGIN_FACTORY(filelightPartFactory, registerPlugin<Part>();) // produce a factory
54 K_EXPORT_PLUGIN(filelightPartFactory(KAboutData(
55  "filelightpart",
56  0,
57  ki18n(APP_PRETTYNAME),
58  APP_VERSION,
59  ki18n("Displays file usage in an easy to understand way."),
60  KAboutData::License_GPL,
61  ki18n("(c) 2002-2004 Max Howell\n\
62  (c) 2008-2013 Martin T. Sandsmark"),
63  KLocalizedString(),
64  "http://utils.kde.org/projects/filelight",
65  "martin.sandsmark@kde.org").
66  setProgramIconName(QLatin1String(APP_NAME)).
67  setCatalogName(APP_NAME)))
68 
69 BrowserExtension::BrowserExtension(Part *parent)
70  : KParts::BrowserExtension(parent)
71 {}
72 
73 
74 Part::Part(QWidget *parentWidget, QObject *parent, const QList<QVariant>&)
75  : ReadOnlyPart(parent)
76  , m_summary(0)
77  , m_ext(new BrowserExtension(this))
78  , m_statusbar(new StatusBarExtension(this))
79  , m_map(0)
80  , m_started(false)
81 {
82  Config::read();
83  setComponentData(filelightPartFactory::componentData());
84  setXMLFile(QLatin1String( "filelightpartui.rc" ));
85 
86  QScrollArea *scrollArea = new QScrollArea(parentWidget);
87  scrollArea->setWidgetResizable(true);
88  setWidget(scrollArea);
89 
90  QWidget *partWidget = new QWidget(scrollArea);
91  scrollArea->setWidget(partWidget);
92 
93  partWidget->setBackgroundRole(QPalette::Base);
94  partWidget->setAutoFillBackground(true);
95 
96  m_layout = new QGridLayout();
97  partWidget->setLayout(m_layout);
98 
99  m_manager = new ScanManager(partWidget);
100 
101  m_map = new RadialMap::Widget(partWidget);
102  m_layout->addWidget(m_map);
103 
104  m_stateWidget = new ProgressBox(statusBar(), this, m_manager);
105  m_layout->addWidget(m_stateWidget);
106  m_stateWidget->hide();
107 
108  m_numberOfFiles = new QLabel();
109  m_statusbar->addStatusBarItem(m_numberOfFiles, 0, true);
110 
111  KStandardAction::zoomIn(m_map, SLOT(zoomIn()), actionCollection());
112  KStandardAction::zoomOut(m_map, SLOT(zoomOut()), actionCollection());
113  KAction *action = actionCollection()->addAction(QLatin1String("configure_filelight"));
114  action->setText(i18n("Configure Filelight..."));
115  action->setIcon(KIcon(QLatin1String("configure")));
116  connect(action, SIGNAL(triggered()), this, SLOT(configFilelight()));
117 
118  connect(m_map, SIGNAL(created(const Folder*)), SIGNAL(completed()));
119  connect(m_map, SIGNAL(created(const Folder*)), SLOT(mapChanged(const Folder*)));
120  connect(m_map, SIGNAL(activated(KUrl)), SLOT(updateURL(KUrl)));
121 
122  // TODO make better system
123  connect(m_map, SIGNAL(giveMeTreeFor(KUrl)), SLOT(updateURL(KUrl)));
124  connect(m_map, SIGNAL(giveMeTreeFor(KUrl)), SLOT(openUrl(KUrl)));
125 
126  connect(m_manager, SIGNAL(completed(Folder*)), SLOT(scanCompleted(Folder*)));
127  connect(m_manager, SIGNAL(aboutToEmptyCache()), m_map, SLOT(invalidate()));
128 
129  QTimer::singleShot(0, this, SLOT(postInit()));
130 }
131 
132 void
133 Part::postInit()
134 {
135  if (url().isEmpty()) //if url is not empty openUrl() has been called immediately after ctor, which happens
136  {
137  m_map->hide();
138  showSummary();
139 
140  //FIXME KXMLGUI is b0rked, it should allow us to set this
141  //BEFORE createGUI is called but it doesn't
142  stateChanged(QLatin1String( "scan_failed" ));
143  }
144 }
145 
146 bool
147 Part::openUrl(const KUrl &u)
148 {
149 
150  //TODO everyone hates dialogs, instead render the text in big fonts on the Map
151  //TODO should have an empty KUrl until scan is confirmed successful
152  //TODO probably should set caption to QString::null while map is unusable
153 
154 #define KMSG(s) KMessageBox::information(widget(), s)
155 
156  KUrl uri = u;
157  uri.cleanPath(KUrl::SimplifyDirSeparators);
158  const QString path = uri.path(KUrl::AddTrailingSlash);
159  const QByteArray path8bit = QFile::encodeName(path);
160  const bool isLocal = uri.protocol() == QLatin1String( "file" );
161 
162  if (uri.isEmpty())
163  {
164  //do nothing, chances are the user accidently pressed ENTER
165  }
166  else if (!uri.isValid())
167  {
168  KMSG(i18n("The entered URL cannot be parsed; it is invalid."));
169  }
170  else if ((!isLocal && path[0] != QLatin1Char( '/' )) || (isLocal && !QDir::isAbsolutePath(path)))
171  {
172  KMSG(i18n("Filelight only accepts absolute paths, eg. /%1", path));
173  }
174  else if (isLocal && access(path8bit, F_OK) != 0) //stat(path, &statbuf) == 0
175  {
176  KMSG(i18n("Folder not found: %1", path));
177  }
178  else if (isLocal && !QDir(path).isReadable()) //access(path8bit, R_OK | X_OK) != 0 doesn't work on win32
179  {
180  KMSG(i18n("Unable to enter: %1\nYou do not have access rights to this location.", path));
181  }
182  else
183  {
184  //we don't want to be using the summary screen anymore
185  if (m_summary != 0)
186  m_summary->hide();
187 
188  m_stateWidget->show();
189  m_layout->addWidget(m_stateWidget);
190 
191  return start(uri);
192  }
193 
194  return false;
195 }
196 
197 bool
198 Part::closeUrl()
199 {
200  if (m_manager->abort())
201  statusBar()->showMessage(i18n("Aborting Scan..."));
202 
203  m_map->hide();
204  m_stateWidget->hide();
205 
206  showSummary();
207 
208  return ReadOnlyPart::closeUrl();
209 }
210 
211 void
212 Part::updateURL(const KUrl &u)
213 {
214  //the map has changed internally, update the interface to reflect this
215  emit m_ext->openUrlNotify(); //must be done first
216  emit m_ext->setLocationBarUrl(u.prettyUrl());
217 
218  if (m_manager->running())
219  m_manager->abort();
220 
221  if (u == url())
222  m_manager->emptyCache(); //same as rescan()
223 
224  //do this last, or it breaks Konqi location bar
225  setUrl(u);
226 }
227 
228 void
229 Part::configFilelight()
230 {
231  QWidget *dialog = new SettingsDialog(widget());
232 
233  connect(dialog, SIGNAL(canvasIsDirty(int)), m_map, SLOT(refresh(int)));
234  connect(dialog, SIGNAL(mapIsInvalid()), m_manager, SLOT(emptyCache()));
235 
236  dialog->show(); //deletes itself
237 }
238 
239 bool
240 Part::start(const KUrl &url)
241 {
242  if (!m_started) {
243  connect(m_map, SIGNAL(mouseHover(QString)), statusBar(), SLOT(message(QString)));
244  connect(m_map, SIGNAL(created(const Folder*)), statusBar(), SLOT(clear()));
245  m_started = true;
246  }
247 
248  if (m_manager->running())
249  m_manager->abort();
250 
251  m_numberOfFiles->setText(QString());
252 
253  if (m_manager->start(url)) {
254  setUrl(url);
255 
256  const QString s = i18n("Scanning: %1", prettyUrl());
257  stateChanged(QLatin1String( "scan_started" ));
258  emit started(0); //as a Part, we have to do this
259  emit setWindowCaption(s);
260  statusBar()->showMessage(s);
261  m_map->hide();
262  m_map->invalidate(); //to maintain ui consistency
263 
264 
265  return true;
266  }
267 
268  return false;
269 }
270 
271 void
272 Part::rescan()
273 {
274  if (m_summary && !m_summary->isHidden()) {
275  delete m_summary;
276  m_summary = 0;
277  showSummary();
278  return;
279  }
280 
281  //FIXME we have to empty the cache because otherwise rescan picks up the old tree..
282  m_manager->emptyCache(); //causes canvas to invalidate
283  m_map->hide();
284  m_stateWidget->show();
285  start(url());
286 }
287 
288 void
289 Part::scanCompleted(Folder *tree)
290 {
291  if (tree) {
292  statusBar()->showMessage(i18n("Scan completed, generating map..."));
293 
294  m_stateWidget->hide();
295  m_map->show();
296  m_map->create(tree);
297 
298  stateChanged(QLatin1String( "scan_complete" ));
299  }
300  else {
301  stateChanged(QLatin1String( "scan_failed" ));
302  emit canceled(i18n("Scan failed: %1", prettyUrl()));
303  emit setWindowCaption(QString());
304 
305  statusBar()->clearMessage();
306 
307  setUrl(KUrl());
308  }
309 }
310 
311 void
312 Part::mapChanged(const Folder *tree)
313 {
314  //IMPORTANT -> url() has already been set
315 
316  emit setWindowCaption(prettyUrl());
317 
318  const int fileCount = tree->children();
319  const QString text = ( fileCount == 0 ) ?
320  i18n("No files.") :
321  i18np("1 file", "%1 files",fileCount);
322 
323  m_numberOfFiles->setText(text);
324 }
325 
326 void
327 Part::showSummary()
328 {
329  if (m_summary == 0) {
330  m_summary = new SummaryWidget(widget());
331  m_summary->setObjectName(QLatin1String( "summaryWidget" ));
332  connect(m_summary, SIGNAL(activated(KUrl)), SLOT(openUrl(KUrl)));
333  m_summary->show();
334  m_layout->addWidget(m_summary);
335  }
336  else m_summary->show();
337 }
338 
339 } //namespace Filelight
340 
341 #include "part.moc"
progressBox.h
Filelight::Part::rescan
void rescan()
Definition: part.cpp:272
summaryWidget.h
RadialMap::Widget::invalidate
void invalidate()
Definition: widget.cpp:75
ProgressBox
Definition: progressBox.h:32
SummaryWidget
Definition: summaryWidget.h:29
QWidget
Filelight::m_ext
http QObject const QList< QVariant > m_ext(new BrowserExtension(this))
RadialMap::Widget
Definition: widget.h:50
Filelight::Config::read
static void read()
Definition: Config.cpp:43
QObject
Config.h
Filelight::Part::closeUrl
virtual bool closeUrl()
Definition: part.cpp:198
Filelight::K_PLUGIN_FACTORY
K_PLUGIN_FACTORY(filelightPartFactory, registerPlugin< Part >();) K_EXPORT_PLUGIN(filelightPartFactory(KAboutData("filelightpart"
Filelight::ScanManager::running
bool running() const
Definition: scan.cpp:61
Filelight::Part::Part
Part(QWidget *, QObject *, const QList< QVariant > &)
Filelight::Part::prettyUrl
QString prettyUrl() const
Definition: part.h:62
Filelight::ScanManager::emptyCache
void emptyCache()
Definition: scan.cpp:188
Filelight::KLocalizedString
KLocalizedString()
settingsDialog.h
Folder
Definition: fileTree.h:271
Filelight::ScanManager::start
bool start(const KUrl &)
Definition: scan.cpp:66
fileTree.h
Filelight::Part::openUrl
virtual bool openUrl(const KUrl &)
Definition: part.cpp:147
Filelight::BrowserExtension
Definition: part.h:43
Filelight::APP_VERSION
APP_VERSION
Definition: part.cpp:58
Filelight::m_summary
http QObject const QList< QVariant > m_summary(0)
SettingsDialog
Definition: settingsDialog.h:31
RadialMap::Widget::create
void create(const Folder *)
Definition: widget.cpp:103
APP_NAME
#define APP_NAME
Definition: define.h:32
Filelight::parent
http QObject * parent
Definition: part.cpp:74
KMSG
#define KMSG(s)
Folder::children
uint children() const
Definition: fileTree.h:276
widget.h
Filelight::ki18n
ki18n(APP_PRETTYNAME)
scan.h
Filelight::m_statusbar
http QObject const QList< QVariant > m_statusbar(new StatusBarExtension(this))
APP_PRETTYNAME
#define APP_PRETTYNAME
Definition: define.h:34
Filelight::ScanManager::abort
bool abort()
Definition: scan.cpp:178
Filelight::Part::configFilelight
void configFilelight()
Definition: part.cpp:229
KAction
Filelight::Part
Definition: part.h:50
Filelight::ScanManager
Definition: scan.h:35
Filelight::m_started
http QObject const QList< QVariant > m_started(false)
Definition: part.cpp:80
part.h
Filelight::m_map
http QObject const QList< QVariant > m_map(0)
define.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:08:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

filelight

Skip menu "filelight"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal