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