]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinpart.cpp
allow to disable the preview of the Information Panel
[dolphin.git] / src / dolphinpart.cpp
1 /* This file is part of the KDE project
2 Copyright (c) 2007 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "dolphinpart.h"
21 #include <kdebug.h>
22 #include "dolphinviewactionhandler.h"
23 #include "dolphinsortfilterproxymodel.h"
24 #include "dolphinview.h"
25 #include "dolphinmodel.h"
26
27 #include <konq_fileitemcapabilities.h>
28 #include <konq_operations.h>
29
30 #include <kaboutdata.h>
31 #include <kactioncollection.h>
32 #include <kconfiggroup.h>
33 #include <kdirlister.h>
34 #include <kglobalsettings.h>
35 #include <kiconloader.h>
36 #include <klocale.h>
37 #include <kmessagebox.h>
38 #include <kpluginfactory.h>
39 #include <ktoggleaction.h>
40 #include <kio/netaccess.h>
41 #include <ktoolinvocation.h>
42 #include <kauthorized.h>
43 #include <knewmenu.h>
44 #include <kmenu.h>
45
46 #include <QActionGroup>
47 #include <QApplication>
48 #include <QClipboard>
49
50 K_PLUGIN_FACTORY(DolphinPartFactory, registerPlugin<DolphinPart>();)
51 K_EXPORT_PLUGIN(DolphinPartFactory("dolphinpart", "dolphin"))
52
53 DolphinPart::DolphinPart(QWidget* parentWidget, QObject* parent, const QVariantList& args)
54 : KParts::ReadOnlyPart(parent)
55 {
56 Q_UNUSED(args)
57 setComponentData(DolphinPartFactory::componentData(), false);
58 m_extension = new DolphinPartBrowserExtension(this);
59
60 // make sure that other apps using this part find Dolphin's view-file-columns icons
61 KIconLoader::global()->addAppDir("dolphin");
62
63 m_dirLister = new KDirLister;
64 m_dirLister->setAutoUpdate(true);
65 if (parentWidget) {
66 m_dirLister->setMainWindow(parentWidget->window());
67 }
68 m_dirLister->setDelayedMimeTypes(true);
69
70 //connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotStarted()));
71 connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted(KUrl)));
72 connect(m_dirLister, SIGNAL(canceled(KUrl)), this, SLOT(slotCanceled(KUrl)));
73 connect(m_dirLister, SIGNAL(percent(int)), this, SLOT(updateProgress(int)));
74
75 m_dolphinModel = new DolphinModel(this);
76 m_dolphinModel->setDirLister(m_dirLister);
77
78 m_proxyModel = new DolphinSortFilterProxyModel(this);
79 m_proxyModel->setSourceModel(m_dolphinModel);
80
81 m_view = new DolphinView(parentWidget,
82 KUrl(),
83 m_dirLister,
84 m_dolphinModel,
85 m_proxyModel);
86 m_view->setTabsForFilesEnabled(true);
87 setWidget(m_view);
88
89 setXMLFile("dolphinpart.rc");
90
91 connect(m_view, SIGNAL(infoMessage(QString)),
92 this, SLOT(slotInfoMessage(QString)));
93 connect(m_view, SIGNAL(errorMessage(QString)),
94 this, SLOT(slotErrorMessage(QString)));
95 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
96 this, SLOT(slotItemTriggered(KFileItem)));
97 connect(m_view, SIGNAL(tabRequested(KUrl)),
98 this, SLOT(createNewWindow(KUrl)));
99 connect(m_view, SIGNAL(requestContextMenu(KFileItem,KUrl,QList<QAction*>)),
100 this, SLOT(slotOpenContextMenu(KFileItem,KUrl,QList<QAction*>)));
101 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
102 m_extension, SIGNAL(selectionInfo(KFileItemList)));
103 connect(m_view, SIGNAL(selectionChanged(KFileItemList)),
104 this, SLOT(slotSelectionChanged(KFileItemList)));
105 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
106 this, SLOT(slotRequestItemInfo(KFileItem)));
107 connect(m_view, SIGNAL(requestUrlChange(KUrl)),
108 this, SLOT(slotRequestUrlChange(KUrl)));
109 connect(m_view, SIGNAL(modeChanged()),
110 this, SIGNAL(viewModeChanged())); // relay signal
111 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
112 this, SLOT(slotRedirection(KUrl, KUrl)));
113
114 // Watch for changes that should result in updates to the
115 // status bar text.
116 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
117 this, SLOT(updateStatusBar()));
118 connect(m_dirLister, SIGNAL(clear()),
119 this, SLOT(updateStatusBar()));
120 connect(m_view, SIGNAL(selectionChanged(const KFileItemList)),
121 this, SLOT(updateStatusBar()));
122
123 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
124 m_actionHandler->setCurrentView(m_view);
125
126 QClipboard* clipboard = QApplication::clipboard();
127 connect(clipboard, SIGNAL(dataChanged()),
128 this, SLOT(updatePasteAction()));
129
130 createActions();
131 m_actionHandler->updateViewActions();
132 slotSelectionChanged(KFileItemList()); // initially disable selection-dependent actions
133
134 // TODO there was a "always open a new window" (when clicking on a directory) setting in konqueror
135 // (sort of spacial navigation)
136
137 loadPlugins(this, this, componentData());
138 }
139
140 DolphinPart::~DolphinPart()
141 {
142 delete m_dirLister;
143 }
144
145 void DolphinPart::createActions()
146 {
147 // Edit menu
148
149 m_newMenu = new KNewMenu(actionCollection(), widget(), "new_menu");
150 connect(m_newMenu->menu(), SIGNAL(aboutToShow()),
151 this, SLOT(updateNewMenu()));
152
153 KAction *editMimeTypeAction = actionCollection()->addAction( "editMimeType" );
154 editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) );
155 connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType()));
156
157 // View menu: all done by DolphinViewActionHandler
158
159 // Go menu
160
161 QActionGroup* goActionGroup = new QActionGroup(this);
162 connect(goActionGroup, SIGNAL(triggered(QAction*)),
163 this, SLOT(slotGoTriggered(QAction*)));
164
165 createGoAction("go_applications", "start-here-kde",
166 i18nc("@action:inmenu Go", "App&lications"), QString("programs:/"),
167 goActionGroup);
168 createGoAction("go_network_folders", "folder-remote",
169 i18nc("@action:inmenu Go", "&Network Folders"), QString("remote:/"),
170 goActionGroup);
171 createGoAction("go_settings", "preferences-system",
172 i18nc("@action:inmenu Go", "Sett&ings"), QString("settings:/"),
173 goActionGroup);
174 createGoAction("go_trash", "user-trash",
175 i18nc("@action:inmenu Go", "Trash"), QString("trash:/"),
176 goActionGroup);
177 createGoAction("go_autostart", "",
178 i18nc("@action:inmenu Go", "Autostart"), KGlobalSettings::autostartPath(),
179 goActionGroup);
180
181 // Tools menu
182 if (KAuthorized::authorizeKAction("shell_access")) {
183 KAction* action = actionCollection()->addAction("open_terminal");
184 action->setIcon(KIcon("utilities-terminal"));
185 action->setText(i18nc("@action:inmenu Tools", "Open &Terminal"));
186 connect(action, SIGNAL(triggered()), SLOT(slotOpenTerminal()));
187 action->setShortcut(Qt::Key_F4);
188 }
189
190 }
191
192 void DolphinPart::createGoAction(const char* name, const char* iconName,
193 const QString& text, const QString& url,
194 QActionGroup* actionGroup)
195 {
196 KAction* action = actionCollection()->addAction(name);
197 action->setIcon(KIcon(iconName));
198 action->setText(text);
199 action->setData(url);
200 action->setActionGroup(actionGroup);
201 }
202
203 void DolphinPart::slotGoTriggered(QAction* action)
204 {
205 const QString url = action->data().toString();
206 emit m_extension->openUrlRequest(KUrl(url));
207 }
208
209 void DolphinPart::slotSelectionChanged(const KFileItemList& selection)
210 {
211 const bool hasSelection = !selection.isEmpty();
212
213 QAction* renameAction = actionCollection()->action("rename");
214 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
215 QAction* deleteAction = actionCollection()->action("delete");
216 QAction* editMimeTypeAction = actionCollection()->action("editMimeType");
217 QAction* propertiesAction = actionCollection()->action("properties");
218 QAction* deleteWithTrashShortcut = actionCollection()->action("delete_shortcut"); // see DolphinViewActionHandler
219
220 if (!hasSelection) {
221 stateChanged("has_no_selection");
222
223 emit m_extension->enableAction("cut", false);
224 emit m_extension->enableAction("copy", false);
225 deleteWithTrashShortcut->setEnabled(false);
226 editMimeTypeAction->setEnabled(false);
227 } else {
228 stateChanged("has_selection");
229
230 // TODO share this code with DolphinMainWindow::updateEditActions (and the desktop code)
231 // in libkonq
232 KonqFileItemCapabilities capabilities(selection);
233 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
234
235 renameAction->setEnabled(capabilities.supportsMoving());
236 moveToTrashAction->setEnabled(enableMoveToTrash);
237 deleteAction->setEnabled(capabilities.supportsDeleting());
238 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
239 editMimeTypeAction->setEnabled(true);
240 propertiesAction->setEnabled(true);
241 emit m_extension->enableAction("cut", capabilities.supportsMoving());
242 emit m_extension->enableAction("copy", true);
243 }
244 }
245
246 void DolphinPart::updatePasteAction()
247 {
248 QPair<bool, QString> pasteInfo = m_view->pasteInfo();
249 emit m_extension->enableAction( "paste", pasteInfo.first );
250 emit m_extension->setActionText( "paste", pasteInfo.second );
251 }
252
253 KAboutData* DolphinPart::createAboutData()
254 {
255 return new KAboutData("dolphinpart", "dolphin", ki18nc("@title", "Dolphin Part"), "0.1");
256 }
257
258 bool DolphinPart::openUrl(const KUrl& url)
259 {
260 bool reload = arguments().reload();
261 // A bit of a workaround so that changing the namefilter works: force reload.
262 // Otherwise DolphinView wouldn't relist the URL, so nothing would happen.
263 if (m_nameFilter != m_dirLister->nameFilter())
264 reload = true;
265 if (m_view->url() == url && !reload) { // DolphinView won't do anything in that case, so don't emit started
266 return true;
267 }
268 setUrl(url); // remember it at the KParts level
269 KUrl visibleUrl(url);
270 if (!m_nameFilter.isEmpty()) {
271 visibleUrl.addPath(m_nameFilter);
272 }
273 QString prettyUrl = visibleUrl.pathOrUrl();
274 emit setWindowCaption(prettyUrl);
275 emit m_extension->setLocationBarUrl(prettyUrl);
276 emit started(0); // get the wheel to spin
277 m_dirLister->setNameFilter(m_nameFilter);
278 m_view->setUrl(url);
279 updatePasteAction();
280 emit aboutToOpenURL();
281 if (reload)
282 m_view->reload();
283 return true;
284 }
285
286 void DolphinPart::slotCompleted(const KUrl& url)
287 {
288 Q_UNUSED(url)
289 emit completed();
290 }
291
292 void DolphinPart::slotCanceled(const KUrl& url)
293 {
294 slotCompleted(url);
295 }
296
297 void DolphinPart::slotInfoMessage(const QString& msg)
298 {
299 emit setStatusBarText(msg);
300 }
301
302 void DolphinPart::slotErrorMessage(const QString& msg)
303 {
304 KMessageBox::error(m_view, msg);
305 }
306
307 void DolphinPart::slotRequestItemInfo(const KFileItem& item)
308 {
309 emit m_extension->mouseOverInfo(item);
310 if (item.isNull()) {
311 updateStatusBar();
312 } else {
313 ReadOnlyPart::setStatusBarText(item.getStatusBarInfo());
314 }
315 }
316
317 void DolphinPart::slotItemTriggered(const KFileItem& item)
318 {
319 KParts::OpenUrlArguments args;
320 args.setMimeType(item.mimetype());
321
322 // Ideally, konqueror should be changed to not require trustedSource for directory views,
323 // since the idea was not to need BrowserArguments for non-browser stuff...
324 KParts::BrowserArguments browserArgs;
325 browserArgs.trustedSource = true;
326 emit m_extension->openUrlRequest(item.targetUrl(), args, browserArgs);
327 }
328
329 void DolphinPart::createNewWindow(const KUrl& url)
330 {
331 // TODO: Check issue N176832 for the missing QAIV signal; task 177399 - maybe this code
332 // should be moved into DolphinPart::slotItemTriggered()
333 KFileItem item(S_IFDIR, (mode_t)-1, url);
334 KParts::OpenUrlArguments args;
335 args.setMimeType(item.mimetype());
336 emit m_extension->createNewWindow(url, args);
337 }
338
339 void DolphinPart::slotOpenContextMenu(const KFileItem& _item,
340 const KUrl&,
341 const QList<QAction*>& customActions)
342 {
343 KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
344 | KParts::BrowserExtension::ShowProperties
345 | KParts::BrowserExtension::ShowUrlOperations;
346
347 KFileItem item(_item);
348
349 if (item.isNull()) { // viewport context menu
350 popupFlags |= KParts::BrowserExtension::ShowNavigationItems | KParts::BrowserExtension::ShowUp;
351 item = m_dirLister->rootItem();
352 if (item.isNull())
353 item = KFileItem( S_IFDIR, (mode_t)-1, url() );
354 }
355
356 KParts::BrowserExtension::ActionGroupMap actionGroups;
357 QList<QAction *> editActions;
358
359 editActions += customActions;
360
361 if (!_item.isNull()) { // only for context menu on one or more items
362 bool sDeleting = true;
363 bool sMoving = true;
364
365 // If the parent directory of the selected item is writable, moving
366 // and deleting are possible.
367 KFileItem parentDir = m_dirLister->rootItem();
368 if (!parentDir.isNull() && !parentDir.isWritable()) {
369 popupFlags |= KParts::BrowserExtension::NoDeletion;
370 sDeleting = false;
371 sMoving = false;
372 }
373
374 if ( sMoving )
375 editActions.append(actionCollection()->action("rename"));
376
377 bool addTrash = false;
378 bool addDel = false;
379
380 bool isIntoTrash = _item.url().protocol() == "trash";
381
382 if ( sMoving && !isIntoTrash && item.isLocalFile() )
383 addTrash = true;
384
385 if ( sDeleting ) {
386 if ( !item.isLocalFile() )
387 addDel = true;
388 else if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
389 addTrash = false;
390 addDel = true;
391 }
392 else {
393 KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::IncludeGlobals);
394 KConfigGroup configGroup(globalConfig, "KDE");
395 if ( configGroup.readEntry("ShowDeleteCommand", false) )
396 addDel = true;
397 }
398 }
399
400 if (addTrash)
401 editActions.append(actionCollection()->action("move_to_trash"));
402 if (addDel)
403 editActions.append(actionCollection()->action("delete"));
404
405 // Normally KonqPopupMenu only shows the "Create new" subdir in the current view
406 // since otherwise the created file would not be visible.
407 // But in treeview mode we should allow it.
408 if (m_view->itemsExpandable())
409 popupFlags |= KParts::BrowserExtension::ShowCreateDirectory;
410
411 }
412
413 actionGroups.insert("editactions", editActions);
414
415 // TODO: We should change the signature of the slots (and signals) for being able
416 // to tell for which items we want a popup.
417 KFileItemList items = (m_view->selectedItems().count() ? m_view->selectedItems()
418 : KFileItemList() << item);
419 emit m_extension->popupMenu(QCursor::pos(),
420 items,
421 KParts::OpenUrlArguments(),
422 KParts::BrowserArguments(),
423 popupFlags,
424 actionGroups);
425 }
426
427 void DolphinPart::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
428 {
429 //kDebug() << oldUrl << newUrl << "currentUrl=" << url();
430 if (oldUrl == url()) {
431 KParts::ReadOnlyPart::setUrl(newUrl);
432 const QString prettyUrl = newUrl.pathOrUrl();
433 emit m_extension->setLocationBarUrl(prettyUrl);
434 }
435 }
436
437 void DolphinPart::slotRequestUrlChange(const KUrl& url)
438 {
439 if (m_view->url() != url) {
440 // If the view URL is not equal to 'url', then an inner URL change has
441 // been done (e. g. by activating an existing column in the column view).
442 openUrl(url);
443 emit m_extension->openUrlNotify();
444 }
445 }
446
447 ////
448
449 void DolphinPartBrowserExtension::cut()
450 {
451 m_part->view()->cutSelectedItems();
452 }
453
454 void DolphinPartBrowserExtension::copy()
455 {
456 m_part->view()->copySelectedItems();
457 }
458
459 void DolphinPartBrowserExtension::paste()
460 {
461 m_part->view()->paste();
462 }
463
464 void DolphinPartBrowserExtension::reparseConfiguration()
465 {
466 m_part->view()->refresh();
467 }
468
469 ////
470
471 void DolphinPart::slotEditMimeType()
472 {
473 const KFileItemList items = m_view->selectedItems();
474 if (!items.isEmpty()) {
475 KonqOperations::editMimeType(items.first().mimetype(), m_view);
476 }
477 }
478
479 void DolphinPart::setCurrentViewMode(const QString& viewModeName)
480 {
481 QAction* action = actionCollection()->action(viewModeName);
482 Q_ASSERT(action);
483 action->trigger();
484 }
485
486 QString DolphinPart::currentViewMode() const
487 {
488 return m_actionHandler->currentViewModeActionName();
489 }
490
491 void DolphinPart::setNameFilter(const QString& nameFilter)
492 {
493 // This is the "/home/dfaure/*.diff" kind of name filter (KDirLister::setNameFilter)
494 // which is unrelated to DolphinView::setNameFilter which is substring filtering in a proxy.
495 m_nameFilter = nameFilter;
496 // TODO save/restore name filter in saveState/restoreState like KonqDirPart did in kde3?
497 }
498
499 void DolphinPart::slotOpenTerminal()
500 {
501 QString dir(QDir::homePath());
502
503 KUrl u(url());
504
505 // If the given directory is not local, it can still be the URL of an
506 // ioslave using UDS_LOCAL_PATH which to be converted first.
507 u = KIO::NetAccess::mostLocalUrl(u, widget());
508
509 //If the URL is local after the above conversion, set the directory.
510 if (u.isLocalFile()) {
511 dir = u.path();
512 }
513
514 KToolInvocation::invokeTerminal(QString(), dir);
515 }
516
517 void DolphinPart::updateNewMenu()
518 {
519 // As requested by KNewMenu :
520 m_newMenu->slotCheckUpToDate();
521 // And set the files that the menu apply on :
522 m_newMenu->setPopupFiles(url());
523 }
524
525 void DolphinPart::updateStatusBar()
526 {
527 emit ReadOnlyPart::setStatusBarText(m_view->statusBarText());
528 }
529
530 void DolphinPart::updateProgress(int percent)
531 {
532 m_extension->loadingProgress(percent);
533 }
534
535 #include "dolphinpart.moc"