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