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