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