summaryrefslogtreecommitdiffstats
path: root/konq-plugins/fsview/fsview_part.cpp
blob: 3995a47339f358ad592682c9ee98a72bf5646721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/* This file is part of FSView.
   Copyright (C) 2002, 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation, version 2.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * The KPart embedding the FSView widget
 */

#include <tqclipboard.h>
#include <tqtimer.h>
#include <tqwhatsthis.h>

#include <kinstance.h>
#include <kfiledialog.h>
#include <kfileitem.h>
#include <kparts/genericfactory.h>
#include <kapplication.h>
#include <kaboutdata.h>
#include <klocale.h>
#include <kaction.h>
#include <kpopupmenu.h>
#include <kglobalsettings.h>
#include <kprotocolinfo.h>
#include <kio/job.h>
#include <kmessagebox.h>

// from tdebase/libkonq...
#include <konq_operations.h>
#include <konq_drag.h>

#include "fsview_part.h"




typedef KParts::GenericFactory<FSViewPart> FSViewPartFactory;
K_EXPORT_COMPONENT_FACTORY( libfsviewpart, FSViewPartFactory )


// FSJob, for progress

FSJob::FSJob(FSView* v)
  : KIO::Job(false)
{
  _view = v;
  TQObject::connect(v, TQT_SIGNAL(progress(int,int,const TQString&)),
                   this, TQT_SLOT(progressSlot(int,int,const TQString&)));
}

void FSJob::kill(bool quietly)
{
  _view->stop();

  Job::kill(quietly);
}

void FSJob::progressSlot(int percent, int dirs, const TQString& cDir)
{
  if (percent<100) {
    emitPercent(percent, 100);
    slotInfoMessage(this, i18n("Read 1 folder, in %1",
                               "Read %n folders, in %1",
                               dirs ).arg(cDir));
  }
  else
    slotInfoMessage(this, i18n("1 folder", "%n folders",  dirs));
}


// FSViewPart

KAboutData* FSViewPart::createAboutData()
{
  KAboutData* aboutData;
  aboutData = new KAboutData("fsview", I18N_NOOP("FSView"), "0.1.1",
                             I18N_NOOP("Filesystem Utilization Viewer"),
                             KAboutData::License_GPL,
                             I18N_NOOP("(c) 2003-2005, Josef Weidendorfer"));
  return aboutData;
}

FSViewPart::FSViewPart(TQWidget *parentWidget, const char *widgetName,
                       TQObject *parent, const char *name,
                       const TQStringList& /* args */)
    : KParts::ReadOnlyPart(parent, name)
{
    // we need an instance
    setInstance( FSViewPartFactory::instance() );

    _view = new FSView(new Inode(), parentWidget, widgetName);
    TQWhatsThis::add(_view, i18n("<p>This is the FSView plugin, a graphical "
				"browsing mode showing filesystem utilization "
				"by using a tree map visualization.</p>"
				"<p>Note that in this mode, automatic updating "
				"when filesystem changes are made "
				"is intentionally <b>not</b> done.</p>"
				"<p>For details on usage and options available, "
				"see the online help under "
				"menu 'Help/FSView Manual'.</p>"));

    _view->show();
    setWidget(_view);

    _ext = new FSViewBrowserExtension(this);
    _job = 0;

    _areaMenu = new KActionMenu (i18n("Stop at Area"),
				 actionCollection(), "treemap_areadir");
    _depthMenu = new KActionMenu (i18n("Stop at Depth"),
				 actionCollection(), "treemap_depthdir");
    _visMenu = new KActionMenu (i18n("Visualization"),
				 actionCollection(), "treemap_visdir");
    _colorMenu = new KActionMenu (i18n("Color Mode"),
				  actionCollection(), "treemap_colordir");

    KAction* action;
    action = new KAction( i18n( "&FSView Manual" ), "fsview",
			KShortcut(), this, TQT_SLOT(showHelp()),
                        actionCollection(), "help_fsview" );
    action->setToolTip(i18n("Show FSView manual"));
    action->setWhatsThis(i18n("Opens the help browser with the "
			      "FSView documentation"));

    TQObject::connect (_visMenu->popupMenu(), TQT_SIGNAL (aboutToShow()),
		      TQT_SLOT (slotShowVisMenu()));
    TQObject::connect (_areaMenu->popupMenu(), TQT_SIGNAL (aboutToShow()),
		      TQT_SLOT (slotShowAreaMenu()));
    TQObject::connect (_depthMenu->popupMenu(), TQT_SIGNAL (aboutToShow()),
		      TQT_SLOT (slotShowDepthMenu()));
    TQObject::connect (_colorMenu->popupMenu(), TQT_SIGNAL (aboutToShow()),
		      TQT_SLOT (slotShowColorMenu()));

    slotSettingsChanged(KApplication::SETTINGS_MOUSE);
    if (kapp)
      connect( kapp, TQT_SIGNAL( settingsChanged(int) ),
	       TQT_SLOT( slotSettingsChanged(int) ) );

    TQObject::connect(_view,TQT_SIGNAL(returnPressed(TreeMapItem*)),
                     _ext,TQT_SLOT(selected(TreeMapItem*)));
    TQObject::connect(_view,TQT_SIGNAL(selectionChanged()),
                     _ext,TQT_SLOT(updateActions()));
    TQObject::connect(_view,
                     TQT_SIGNAL(contextMenuRequested(TreeMapItem*,const TQPoint&)),
                     _ext,
                     TQT_SLOT(contextMenu(TreeMapItem*, const TQPoint&)));

    TQObject::connect(_view, TQT_SIGNAL(started()), this, TQT_SLOT(startedSlot()));
    TQObject::connect(_view, TQT_SIGNAL(completed(int)),
		     this, TQT_SLOT(completedSlot(int)));

    TQTimer::singleShot(1, this, TQT_SLOT(showInfo()));

    setXMLFile( "fsview_part.rc" );
}


FSViewPart::~FSViewPart()
{
  kdDebug(90100) << "FSViewPart Destructor" << endl;

  delete _job;
  _view->saveFSOptions();
}

void FSViewPart::slotSettingsChanged(int category)
{
  if (category != KApplication::SETTINGS_MOUSE) return;

  TQObject::disconnect(_view,TQT_SIGNAL(clicked(TreeMapItem*)),
		      _ext,TQT_SLOT(selected(TreeMapItem*)));
  TQObject::disconnect(_view,TQT_SIGNAL(doubleClicked(TreeMapItem*)),
		      _ext,TQT_SLOT(selected(TreeMapItem*)));
  
  if (KGlobalSettings::singleClick())
    TQObject::connect(_view,TQT_SIGNAL(clicked(TreeMapItem*)),
		     _ext,TQT_SLOT(selected(TreeMapItem*)));
  else
    TQObject::connect(_view,TQT_SIGNAL(doubleClicked(TreeMapItem*)),
		     _ext,TQT_SLOT(selected(TreeMapItem*)));
}

void FSViewPart::showInfo()
{
    TQString info;
    info = i18n("FSView intentionally does not support automatic updates "
		"when changes are made to files or directories, "
		"currently visible in FSView, from the outside.\n"
		"For details, see the 'Help/FSView Manual'.");

    KMessageBox::information( _view, info, TQString(), "ShowFSViewInfo");
}

void FSViewPart::showHelp()
{
    KApplication::startServiceByDesktopName("khelpcenter",
					    TQString("help:/konq-plugins/fsview/index.html"));
}

void FSViewPart::startedSlot()
{
  _job = new FSJob(_view);
  emit started(_job);
}

void FSViewPart::completedSlot(int dirs)
{
  if (_job) {
    _job->progressSlot(100, dirs, TQString());
    delete _job;
    _job = 0;
  }

  KConfigGroup cconfig(_view->config(), TQCString("MetricCache"));
  _view->saveMetric(&cconfig);

  emit completed();
}

void FSViewPart::slotShowVisMenu()
{
  _visMenu->popupMenu()->clear();
  _view->addVisualizationItems(_visMenu->popupMenu(), 1301);
}

void FSViewPart::slotShowAreaMenu()
{
  _areaMenu->popupMenu()->clear();
  _view->addAreaStopItems(_areaMenu->popupMenu(), 1001, 0);
}

void FSViewPart::slotShowDepthMenu()
{
  _depthMenu->popupMenu()->clear();
  _view->addDepthStopItems(_depthMenu->popupMenu(), 1501, 0);
}

void FSViewPart::slotShowColorMenu()
{
  _colorMenu->popupMenu()->clear();
  _view->addColorItems(_colorMenu->popupMenu(), 1401);
}

bool FSViewPart::openFile() // never called since openURL is reimplemented
{
  kdDebug(90100) << "FSViewPart::openFile " << m_file << endl;
  _view->setPath(m_file);

  return true;
}

bool FSViewPart::openURL(const KURL &url)
{
  kdDebug(90100) << "FSViewPart::openURL " << url.path() << endl;

  if (!url.isValid()) return false;
  if (!url.isLocalFile()) return false;

  m_url = url;
  emit setWindowCaption( m_url.prettyURL() );

  _view->setPath(url.path());

  return true;
}

bool FSViewPart::closeURL()
{
  kdDebug(90100) << "FSViewPart::closeURL " << endl;

  _view->stop();

  return true;
}

// FSViewBrowserExtension

FSViewBrowserExtension::FSViewBrowserExtension(FSViewPart* viewPart,
                                               const char *name)
  :KParts::BrowserExtension(viewPart, name)
{
  _view = viewPart->view();
}

FSViewBrowserExtension::~FSViewBrowserExtension()
{}

void FSViewBrowserExtension::updateActions()
{
  TreeMapItemList s = _view->selection();
  TreeMapItem* i;
  int canDel = 0, canCopy = 0;
  KURL::List urls;

  for(i=s.first();i;i=s.next()) {
    KURL u;
    u.setPath( ((Inode*)i)->path() );
    urls.append(u);
    canCopy++;
    if (  KProtocolInfo::supportsDeleting(  u ) ) canDel++;
  }
  emit enableAction( "copy", canCopy > 0 );
  emit enableAction( "cut", canDel > 0 );
  emit enableAction( "trash", canDel > 0);
  emit enableAction( "del", canDel > 0 );
  emit enableAction( "editMimeType", ( s.count() == 1 ) );

  emit selectionInfo(urls);

  kdDebug(90100) << "FSViewPart::updateActions, deletable " << canDel << endl;
}


void FSViewBrowserExtension::del()
{
  KonqOperations::del(_view, KonqOperations::DEL, _view->selectedUrls());
  // How to get notified of end of delete operation?
  // - search for the KonqOperations child of _view (name "KonqOperations")
  // - connect to destroyed signal
  KonqOperations* o = (KonqOperations*) _view->child("KonqOperations");
  if (o) connect(o, TQT_SIGNAL(destroyed()), TQT_SLOT(refresh()));
}

void FSViewBrowserExtension::trash()
{
  KonqOperations::del(_view, KonqOperations::TRASH, _view->selectedUrls());
  KonqOperations* o = (KonqOperations*) _view->child("KonqOperations");
  if (o) connect(o, TQT_SIGNAL(destroyed()), TQT_SLOT(refresh()));
}

void FSViewBrowserExtension::copySelection( bool move )
{
  KonqDrag *urlData = KonqDrag::newDrag( _view->selectedUrls(), move );
  TQApplication::clipboard()->setData( urlData );
}

void FSViewBrowserExtension::editMimeType()
{
  Inode* i = (Inode*) _view->selection().first();
  if (i)
    KonqOperations::editMimeType( i->mimeType()->name() );
}


// refresh treemap at end of KIO jobs
void FSViewBrowserExtension::refresh()
{
  // only need to refresh common ancestor for all selected items
  TreeMapItemList s = _view->selection();
  TreeMapItem *i, *commonParent = s.first();
  if (!commonParent) return;
  while( (i=s.next()) )
    commonParent = commonParent->commonParent(i);

  /* if commonParent is a file, update parent directory */
  if ( !((Inode*)commonParent)->isDir() ) {
    commonParent = commonParent->parent();
    if (!commonParent) return;
  }

  kdDebug(90100) << "FSViewPart::refreshing "
	    << ((Inode*)commonParent)->path() << endl;

  _view->requestUpdate( (Inode*)commonParent );
}

void FSViewBrowserExtension::selected(TreeMapItem* i)
{
  if (!i) return;

  KURL url;
  url.setPath( ((Inode*)i)->path() );
  emit openURLRequest(url);
}

void FSViewBrowserExtension::contextMenu(TreeMapItem* /*item*/,const TQPoint& p)
{
    TreeMapItemList s = _view->selection();
    TreeMapItem* i;
    KFileItemList items;
    items.setAutoDelete(true);

    for(i=s.first();i;i=s.next()) {
	KURL u;
	u.setPath( ((Inode*)i)->path() );
	TQString mimetype = ((Inode*)i)->mimeType()->name();
	const TQFileInfo& info = ((Inode*)i)->fileInfo();
	mode_t mode =
	    info.isFile() ? S_IFREG :
	    info.isDir() ? S_IFDIR :
	    info.isSymLink() ? S_IFLNK : (mode_t)-1;
	items.append(new KFileItem(u, mimetype, mode));
    }

    if (items.count()>0)
      emit popupMenu(_view->mapToGlobal(p), items);
}


#include "fsview_part.moc"