]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
allow to restrict the search results by the current folder (including all sub folders)
[dolphin.git] / src / dolphinmainwindow.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Stefan Monov <logixoul@gmail.com> *
4 * Copyright (C) 2006 by Cvetoslav Ludmiloff <ludmiloff@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #include "dolphinmainwindow.h"
23 #include "dolphinviewactionhandler.h"
24 #include "dolphinremoteencoding.h"
25
26 #include <config-nepomuk.h>
27 #ifdef HAVE_NEPOMUK
28 #include "search/dolphinsearchoptionsconfigurator.h"
29 #endif
30
31 #include "dolphinapplication.h"
32 #include "dolphinnewmenu.h"
33 #include "search/dolphinsearchbox.h"
34 #include "search/dolphinsearchoptionsconfigurator.h"
35 #include "settings/dolphinsettings.h"
36 #include "settings/dolphinsettingsdialog.h"
37 #include "dolphinviewcontainer.h"
38 #include "panels/folders/folderspanel.h"
39 #include "panels/places/placespanel.h"
40 #include "panels/information/informationpanel.h"
41 #include "mainwindowadaptor.h"
42 #include "statusbar/dolphinstatusbar.h"
43 #include "viewproperties.h"
44
45 #ifndef Q_OS_WIN
46 #include "panels/terminal/terminalpanel.h"
47 #endif
48
49 #include "dolphin_generalsettings.h"
50 #include "dolphin_iconsmodesettings.h"
51 #include "draganddrophelper.h"
52
53 #include <kaction.h>
54 #include <kactioncollection.h>
55 #include <kconfig.h>
56 #include <kdesktopfile.h>
57 #include <kdeversion.h>
58 #include <kfiledialog.h>
59 #include <kfileplacesmodel.h>
60 #include <kglobal.h>
61 #include <klineedit.h>
62 #include <ktoolbar.h>
63 #include <kicon.h>
64 #include <kiconloader.h>
65 #include <kio/netaccess.h>
66 #include <kinputdialog.h>
67 #include <klocale.h>
68 #include <kprotocolmanager.h>
69 #include <kmenu.h>
70 #include <kmenubar.h>
71 #include <kmessagebox.h>
72 #include <kfileitemlistproperties.h>
73 #include <konqmimedata.h>
74 #include <kprotocolinfo.h>
75 #include <krun.h>
76 #include <kshell.h>
77 #include <kstandarddirs.h>
78 #include <kstatusbar.h>
79 #include <kstandardaction.h>
80 #include <ktabbar.h>
81 #include <ktoggleaction.h>
82 #include <kurlnavigator.h>
83 #include <kurl.h>
84 #include <kurlcombobox.h>
85 #include <ktoolinvocation.h>
86
87 #include <QDBusMessage>
88 #include <QKeyEvent>
89 #include <QClipboard>
90 #include <QSplitter>
91 #include <QDockWidget>
92 #include <kacceleratormanager.h>
93
94 /*
95 * Remembers the tab configuration if a tab has been closed.
96 * Each closed tab can be restored by the menu
97 * "Go -> Recently Closed Tabs".
98 */
99 struct ClosedTab
100 {
101 KUrl primaryUrl;
102 KUrl secondaryUrl;
103 bool isSplit;
104 };
105 Q_DECLARE_METATYPE(ClosedTab)
106
107 DolphinMainWindow::DolphinMainWindow(int id) :
108 KXmlGuiWindow(0),
109 m_newMenu(0),
110 m_showMenuBar(0),
111 m_tabBar(0),
112 m_activeViewContainer(0),
113 m_centralWidgetLayout(0),
114 m_searchBox(0),
115 #ifdef HAVE_NEPOMUK
116 m_searchOptionsConfigurator(0),
117 #endif
118 m_id(id),
119 m_tabIndex(0),
120 m_viewTab(),
121 m_actionHandler(0),
122 m_remoteEncoding(0),
123 m_settingsDialog(0)
124 {
125 setObjectName("Dolphin#");
126
127 m_viewTab.append(ViewTab());
128
129 new MainWindowAdaptor(this);
130 QDBusConnection::sessionBus().registerObject(QString("/dolphin/MainWindow%1").arg(m_id), this);
131
132 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
133 undoManager->setUiInterface(new UndoUiInterface());
134
135 connect(undoManager, SIGNAL(undoAvailable(bool)),
136 this, SLOT(slotUndoAvailable(bool)));
137 connect(undoManager, SIGNAL(undoTextChanged(const QString&)),
138 this, SLOT(slotUndoTextChanged(const QString&)));
139 connect(undoManager, SIGNAL(jobRecordingStarted(CommandType)),
140 this, SLOT(clearStatusBar()));
141 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
142 this, SLOT(showCommand(CommandType)));
143 connect(DolphinSettings::instance().placesModel(), SIGNAL(errorMessage(const QString&)),
144 this, SLOT(showErrorMessage(const QString&)));
145 connect(&DragAndDropHelper::instance(), SIGNAL(errorMessage(const QString&)),
146 this, SLOT(showErrorMessage(const QString&)));
147 }
148
149 DolphinMainWindow::~DolphinMainWindow()
150 {
151 DolphinApplication::app()->removeMainWindow(this);
152 }
153
154 void DolphinMainWindow::toggleViews()
155 {
156 if (m_viewTab[m_tabIndex].primaryView == 0) {
157 return;
158 }
159
160 // move secondary view from the last position of the splitter
161 // to the first position
162 m_viewTab[m_tabIndex].splitter->insertWidget(0, m_viewTab[m_tabIndex].secondaryView);
163
164 DolphinViewContainer* container = m_viewTab[m_tabIndex].primaryView;
165 m_viewTab[m_tabIndex].primaryView = m_viewTab[m_tabIndex].secondaryView;
166 m_viewTab[m_tabIndex].secondaryView = container;
167 }
168
169 void DolphinMainWindow::showCommand(CommandType command)
170 {
171 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
172 switch (command) {
173 case KIO::FileUndoManager::Copy:
174 statusBar->setMessage(i18nc("@info:status", "Successfully copied."),
175 DolphinStatusBar::OperationCompleted);
176 break;
177 case KIO::FileUndoManager::Move:
178 statusBar->setMessage(i18nc("@info:status", "Successfully moved."),
179 DolphinStatusBar::OperationCompleted);
180 break;
181 case KIO::FileUndoManager::Link:
182 statusBar->setMessage(i18nc("@info:status", "Successfully linked."),
183 DolphinStatusBar::OperationCompleted);
184 break;
185 case KIO::FileUndoManager::Trash:
186 statusBar->setMessage(i18nc("@info:status", "Successfully moved to trash."),
187 DolphinStatusBar::OperationCompleted);
188 break;
189 case KIO::FileUndoManager::Rename:
190 statusBar->setMessage(i18nc("@info:status", "Successfully renamed."),
191 DolphinStatusBar::OperationCompleted);
192 break;
193
194 case KIO::FileUndoManager::Mkdir:
195 statusBar->setMessage(i18nc("@info:status", "Created folder."),
196 DolphinStatusBar::OperationCompleted);
197 break;
198
199 default:
200 break;
201 }
202 }
203
204 void DolphinMainWindow::refreshViews()
205 {
206 Q_ASSERT(m_viewTab[m_tabIndex].primaryView != 0);
207
208 // remember the current active view, as because of
209 // the refreshing the active view might change to
210 // the secondary view
211 DolphinViewContainer* activeViewContainer = m_activeViewContainer;
212
213 const int tabCount = m_viewTab.count();
214 for (int i = 0; i < tabCount; ++i) {
215 m_viewTab[i].primaryView->refresh();
216 if (m_viewTab[i].secondaryView != 0) {
217 m_viewTab[i].secondaryView->refresh();
218 }
219 }
220
221 setActiveViewContainer(activeViewContainer);
222 }
223
224 void DolphinMainWindow::pasteIntoFolder()
225 {
226 m_activeViewContainer->view()->pasteIntoFolder();
227 }
228
229 void DolphinMainWindow::changeUrl(const KUrl& url)
230 {
231 if (!KProtocolManager::supportsListing(url)) {
232 // The URL navigator only checks for validity, not
233 // if the URL can be listed. An error message is
234 // shown due to DolphinViewContainer::restoreView().
235 return;
236 }
237
238 DolphinViewContainer* view = activeViewContainer();
239 if (view != 0) {
240 view->setUrl(url);
241 updateEditActions();
242 updateViewActions();
243 updateGoActions();
244 setUrlAsCaption(url);
245 if (m_viewTab.count() > 1) {
246 m_tabBar->setTabText(m_tabIndex, squeezedText(tabName(m_activeViewContainer->url())));
247 }
248 const QString iconName = KMimeType::iconNameForUrl(url);
249 m_tabBar->setTabIcon(m_tabIndex, KIcon(iconName));
250 emit urlChanged(url);
251 }
252 }
253
254 void DolphinMainWindow::changeSelection(const KFileItemList& selection)
255 {
256 activeViewContainer()->view()->changeSelection(selection);
257 }
258
259 void DolphinMainWindow::slotEditableStateChanged(bool editable)
260 {
261 KToggleAction* editableLocationAction =
262 static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
263 editableLocationAction->setChecked(editable);
264 }
265
266 void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
267 {
268 updateEditActions();
269
270 Q_ASSERT(m_viewTab[m_tabIndex].primaryView != 0);
271 int selectedUrlsCount = m_viewTab[m_tabIndex].primaryView->view()->selectedItemsCount();
272 if (m_viewTab[m_tabIndex].secondaryView != 0) {
273 selectedUrlsCount += m_viewTab[m_tabIndex].secondaryView->view()->selectedItemsCount();
274 }
275
276 QAction* compareFilesAction = actionCollection()->action("compare_files");
277 if (selectedUrlsCount == 2) {
278 compareFilesAction->setEnabled(isKompareInstalled());
279 } else {
280 compareFilesAction->setEnabled(false);
281 }
282
283 emit selectionChanged(selection);
284 }
285
286 void DolphinMainWindow::slotWheelMoved(int wheelDelta)
287 {
288 if (wheelDelta > 0) {
289 activatePrevTab();
290 } else {
291 activateNextTab();
292 }
293 }
294
295 void DolphinMainWindow::slotRequestItemInfo(const KFileItem& item)
296 {
297 emit requestItemInfo(item);
298 }
299
300 void DolphinMainWindow::updateHistory()
301 {
302 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
303 const int index = urlNavigator->historyIndex();
304
305 QAction* backAction = actionCollection()->action("go_back");
306 backAction->setToolTip(i18nc("@info", "Go back"));
307 if (backAction != 0) {
308 backAction->setEnabled(index < urlNavigator->historySize() - 1);
309 }
310
311 QAction* forwardAction = actionCollection()->action("go_forward");
312 forwardAction->setToolTip(i18nc("@info", "Go forward"));
313 if (forwardAction != 0) {
314 forwardAction->setEnabled(index > 0);
315 }
316 }
317
318 void DolphinMainWindow::updateFilterBarAction(bool show)
319 {
320 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
321 showFilterBarAction->setChecked(show);
322 }
323
324 void DolphinMainWindow::openNewMainWindow()
325 {
326 DolphinApplication::app()->createMainWindow()->show();
327 }
328
329 void DolphinMainWindow::openNewTab()
330 {
331 openNewTab(m_activeViewContainer->url());
332 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
333
334 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
335 if (navigator->isUrlEditable()) {
336 // if a new tab is opened and the URL is editable, assure that
337 // the user can edit the URL without manually setting the focus
338 navigator->setFocus();
339 }
340 }
341
342 void DolphinMainWindow::openNewTab(const KUrl& url)
343 {
344 const KIcon icon = KIcon(KMimeType::iconNameForUrl(m_activeViewContainer->url()));
345 if (m_viewTab.count() == 1) {
346 // Only one view is open currently and hence no tab is shown at
347 // all. Before creating a tab for 'url', provide a tab for the current URL.
348 m_tabBar->addTab(icon, squeezedText(tabName(m_activeViewContainer->url())));
349 m_tabBar->blockSignals(false);
350 }
351
352 m_tabBar->addTab(icon, squeezedText(tabName(url)));
353
354 ViewTab viewTab;
355 viewTab.splitter = new QSplitter(this);
356 viewTab.splitter->setChildrenCollapsible(false);
357 viewTab.primaryView = new DolphinViewContainer(this, viewTab.splitter, url);
358 viewTab.primaryView->setActive(false);
359 connectViewSignals(viewTab.primaryView);
360 viewTab.primaryView->view()->reload();
361
362 m_viewTab.append(viewTab);
363
364 actionCollection()->action("close_tab")->setEnabled(true);
365
366 // provide a split view, if the startup settings are set this way
367 const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
368 if (generalSettings->splitView()) {
369 const int tabIndex = m_viewTab.count() - 1;
370 createSecondaryView(tabIndex);
371 m_viewTab[tabIndex].secondaryView->setActive(true);
372 m_viewTab[tabIndex].isPrimaryViewActive = false;
373 }
374 }
375
376 void DolphinMainWindow::activateNextTab()
377 {
378 if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
379 return;
380 }
381
382 const int tabIndex = (m_tabBar->currentIndex() + 1) % m_tabBar->count();
383 m_tabBar->setCurrentIndex(tabIndex);
384 }
385
386 void DolphinMainWindow::activatePrevTab()
387 {
388 if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
389 return;
390 }
391
392 int tabIndex = m_tabBar->currentIndex() - 1;
393 if (tabIndex == -1) {
394 tabIndex = m_tabBar->count() - 1;
395 }
396 m_tabBar->setCurrentIndex(tabIndex);
397 }
398
399 void DolphinMainWindow::openInNewTab()
400 {
401 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
402 if ((list.count() == 1) && list[0].isDir()) {
403 openNewTab(m_activeViewContainer->view()->selectedUrls()[0]);
404 }
405 }
406
407 void DolphinMainWindow::openInNewWindow()
408 {
409 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
410 if ((list.count() == 1) && list[0].isDir()) {
411 DolphinMainWindow* window = DolphinApplication::app()->createMainWindow();
412 window->changeUrl(m_activeViewContainer->view()->selectedUrls()[0]);
413 window->show();
414 }
415 }
416
417 void DolphinMainWindow::toggleActiveView()
418 {
419 if (m_viewTab[m_tabIndex].secondaryView == 0) {
420 // only one view is available
421 return;
422 }
423
424 Q_ASSERT(m_activeViewContainer != 0);
425 Q_ASSERT(m_viewTab[m_tabIndex].primaryView != 0);
426
427 DolphinViewContainer* left = m_viewTab[m_tabIndex].primaryView;
428 DolphinViewContainer* right = m_viewTab[m_tabIndex].secondaryView;
429 setActiveViewContainer(m_activeViewContainer == right ? left : right);
430 }
431
432 void DolphinMainWindow::closeEvent(QCloseEvent* event)
433 {
434 DolphinSettings& settings = DolphinSettings::instance();
435 GeneralSettings* generalSettings = settings.generalSettings();
436
437 // Find out if Dolphin is closed directly by the user or
438 // by the session manager because the session is closed
439 bool closedByUser = true;
440 DolphinApplication *application = qobject_cast<DolphinApplication*>(qApp);
441 if (application && application->sessionSaving()) {
442 closedByUser = false;
443 }
444
445 if ((m_viewTab.count() > 1) && generalSettings->confirmClosingMultipleTabs() && closedByUser) {
446 // Ask the user if he really wants to quit and close all tabs.
447 // Open a confirmation dialog with 3 buttons:
448 // KDialog::Yes -> Quit
449 // KDialog::No -> Close only the current tab
450 // KDialog::Cancel -> do nothing
451 KDialog *dialog = new KDialog(this, Qt::Dialog);
452 dialog->setCaption(i18nc("@title:window", "Confirmation"));
453 dialog->setButtons(KDialog::Yes | KDialog::No | KDialog::Cancel);
454 dialog->setModal(true);
455 dialog->showButtonSeparator(true);
456 dialog->setButtonGuiItem(KDialog::Yes, KStandardGuiItem::quit());
457 dialog->setButtonGuiItem(KDialog::No, KGuiItem(i18n("C&lose Current Tab"), KIcon("tab-close")));
458 dialog->setButtonGuiItem(KDialog::Cancel, KStandardGuiItem::cancel());
459 dialog->setDefaultButton(KDialog::Yes);
460
461 bool doNotAskAgainCheckboxResult = false;
462
463 const int result = KMessageBox::createKMessageBox(dialog,
464 QMessageBox::Warning,
465 i18n("You have multiple tabs open in this window, are you sure you want to quit?"),
466 QStringList(),
467 i18n("Do not ask again"),
468 &doNotAskAgainCheckboxResult,
469 KMessageBox::Notify);
470
471 if (doNotAskAgainCheckboxResult) {
472 generalSettings->setConfirmClosingMultipleTabs(false);
473 }
474
475 switch (result) {
476 case KDialog::Yes:
477 // Quit
478 break;
479 case KDialog::No:
480 // Close only the current tab
481 closeTab();
482 default:
483 event->ignore();
484 return;
485 }
486 }
487
488 generalSettings->setFirstRun(false);
489
490 settings.save();
491
492 KXmlGuiWindow::closeEvent(event);
493 }
494
495 void DolphinMainWindow::saveProperties(KConfigGroup& group)
496 {
497 const int tabCount = m_viewTab.count();
498 group.writeEntry("Tab Count", tabCount);
499 group.writeEntry("Active Tab Index", m_tabBar->currentIndex());
500
501 for (int i = 0; i < tabCount; ++i) {
502 const DolphinViewContainer* cont = m_viewTab[i].primaryView;
503 group.writeEntry(tabProperty("Primary URL", i), cont->url().url());
504 group.writeEntry(tabProperty("Primary Editable", i), cont->isUrlEditable());
505
506 cont = m_viewTab[i].secondaryView;
507 if (cont != 0) {
508 group.writeEntry(tabProperty("Secondary URL", i), cont->url().url());
509 group.writeEntry(tabProperty("Secondary Editable", i), cont->isUrlEditable());
510 }
511 }
512 }
513
514 void DolphinMainWindow::readProperties(const KConfigGroup& group)
515 {
516 const int tabCount = group.readEntry("Tab Count", 1);
517 for (int i = 0; i < tabCount; ++i) {
518 DolphinViewContainer* cont = m_viewTab[i].primaryView;
519
520 cont->setUrl(group.readEntry(tabProperty("Primary URL", i)));
521 const bool editable = group.readEntry(tabProperty("Primary Editable", i), false);
522 cont->urlNavigator()->setUrlEditable(editable);
523
524 cont = m_viewTab[i].secondaryView;
525 const QString secondaryUrl = group.readEntry(tabProperty("Secondary URL", i));
526 if (!secondaryUrl.isEmpty()) {
527 if (cont == 0) {
528 // a secondary view should be shown, but no one is available
529 // currently -> create a new view
530 toggleSplitView();
531 cont = m_viewTab[i].secondaryView;
532 Q_ASSERT(cont != 0);
533 }
534
535 cont->setUrl(secondaryUrl);
536 const bool editable = group.readEntry(tabProperty("Secondary Editable", i), false);
537 cont->urlNavigator()->setUrlEditable(editable);
538 } else if (cont != 0) {
539 // no secondary view should be shown, but the default setting shows
540 // one already -> close the view
541 toggleSplitView();
542 }
543
544 // openNewTab() needs to be called only tabCount - 1 times
545 if (i != tabCount - 1) {
546 openNewTab();
547 }
548 }
549
550 const int index = group.readEntry("Active Tab Index", 0);
551 m_tabBar->setCurrentIndex(index);
552 }
553
554 void DolphinMainWindow::updateNewMenu()
555 {
556 m_newMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->showHiddenFiles());
557 m_newMenu->slotCheckUpToDate();
558 m_newMenu->setPopupFiles(activeViewContainer()->url());
559 }
560
561 void DolphinMainWindow::createDirectory()
562 {
563 m_newMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->showHiddenFiles());
564 m_newMenu->setPopupFiles(activeViewContainer()->url());
565 m_newMenu->createDirectory();
566 }
567
568 void DolphinMainWindow::quit()
569 {
570 close();
571 }
572
573 void DolphinMainWindow::showErrorMessage(const QString& message)
574 {
575 if (!message.isEmpty()) {
576 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
577 statusBar->setMessage(message, DolphinStatusBar::Error);
578 }
579 }
580
581 void DolphinMainWindow::slotUndoAvailable(bool available)
582 {
583 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
584 if (undoAction != 0) {
585 undoAction->setEnabled(available);
586 }
587 }
588
589 void DolphinMainWindow::restoreClosedTab(QAction* action)
590 {
591 if (action->data().toBool()) {
592 // clear all actions except the "Empty Recently Closed Tabs"
593 // action and the separator
594 QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
595 const int count = actions.size();
596 for (int i = 2; i < count; ++i) {
597 m_recentTabsMenu->menu()->removeAction(actions.at(i));
598 }
599 } else {
600 const ClosedTab closedTab = action->data().value<ClosedTab>();
601 openNewTab(closedTab.primaryUrl);
602 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
603
604 if (closedTab.isSplit) {
605 // create secondary view
606 toggleSplitView();
607 m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
608 }
609
610 m_recentTabsMenu->removeAction(action);
611 }
612
613 if (m_recentTabsMenu->menu()->actions().count() == 2) {
614 m_recentTabsMenu->setEnabled(false);
615 }
616 }
617
618 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
619 {
620 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
621 if (undoAction != 0) {
622 undoAction->setText(text);
623 }
624 }
625
626 void DolphinMainWindow::undo()
627 {
628 clearStatusBar();
629 KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
630 KIO::FileUndoManager::self()->undo();
631 }
632
633 void DolphinMainWindow::cut()
634 {
635 m_activeViewContainer->view()->cutSelectedItems();
636 }
637
638 void DolphinMainWindow::copy()
639 {
640 m_activeViewContainer->view()->copySelectedItems();
641 }
642
643 void DolphinMainWindow::paste()
644 {
645 m_activeViewContainer->view()->paste();
646 }
647
648 void DolphinMainWindow::updatePasteAction()
649 {
650 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
651 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
652 pasteAction->setEnabled(pasteInfo.first);
653 pasteAction->setText(pasteInfo.second);
654 }
655
656 void DolphinMainWindow::selectAll()
657 {
658 clearStatusBar();
659
660 // if the URL navigator is editable and focused, select the whole
661 // URL instead of all items of the view
662
663 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
664 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit(); // krazy:exclude=qclasses
665 const bool selectUrl = urlNavigator->isUrlEditable() &&
666 lineEdit->hasFocus();
667 if (selectUrl) {
668 lineEdit->selectAll();
669 } else {
670 m_activeViewContainer->view()->selectAll();
671 }
672 }
673
674 void DolphinMainWindow::invertSelection()
675 {
676 clearStatusBar();
677 m_activeViewContainer->view()->invertSelection();
678 }
679
680 void DolphinMainWindow::toggleSplitView()
681 {
682 if (m_viewTab[m_tabIndex].secondaryView == 0) {
683 createSecondaryView(m_tabIndex);
684 setActiveViewContainer(m_viewTab[m_tabIndex].secondaryView);
685 } else if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
686 // remove secondary view
687 m_viewTab[m_tabIndex].secondaryView->close();
688 m_viewTab[m_tabIndex].secondaryView->deleteLater();
689 m_viewTab[m_tabIndex].secondaryView = 0;
690
691 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
692 } else {
693 // The primary view is active and should be closed. Hence from a users point of view
694 // the content of the secondary view should be moved to the primary view.
695 // From an implementation point of view it is more efficient to close
696 // the primary view and exchange the internal pointers afterwards.
697
698 m_viewTab[m_tabIndex].primaryView->close();
699 m_viewTab[m_tabIndex].primaryView->deleteLater();
700 m_viewTab[m_tabIndex].primaryView = m_viewTab[m_tabIndex].secondaryView;
701 m_viewTab[m_tabIndex].secondaryView = 0;
702
703 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
704 }
705
706 updateViewActions();
707 }
708
709 void DolphinMainWindow::reloadView()
710 {
711 clearStatusBar();
712 m_activeViewContainer->view()->reload();
713 }
714
715 void DolphinMainWindow::stopLoading()
716 {
717 }
718
719 void DolphinMainWindow::toggleFilterBarVisibility(bool show)
720 {
721 m_activeViewContainer->showFilterBar(show);
722 }
723
724 void DolphinMainWindow::toggleEditLocation()
725 {
726 clearStatusBar();
727
728 QAction* action = actionCollection()->action("editable_location");
729 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
730 urlNavigator->setUrlEditable(action->isChecked());
731 }
732
733 void DolphinMainWindow::replaceLocation()
734 {
735 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
736 navigator->setUrlEditable(true);
737 navigator->setFocus();
738
739 // select the whole text of the combo box editor
740 QLineEdit* lineEdit = navigator->editor()->lineEdit(); // krazy:exclude=qclasses
741 const QString text = lineEdit->text();
742 lineEdit->setSelection(0, text.length());
743 }
744
745 void DolphinMainWindow::goBack()
746 {
747 clearStatusBar();
748 m_activeViewContainer->urlNavigator()->goBack();
749 }
750
751 void DolphinMainWindow::goForward()
752 {
753 clearStatusBar();
754 m_activeViewContainer->urlNavigator()->goForward();
755 }
756
757 void DolphinMainWindow::goUp()
758 {
759 clearStatusBar();
760 m_activeViewContainer->urlNavigator()->goUp();
761 }
762
763 void DolphinMainWindow::goBack(Qt::MouseButtons buttons)
764 {
765 // The default case (left button pressed) is handled in goBack().
766 if (buttons == Qt::MidButton) {
767 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
768 openNewTab(urlNavigator->historyUrl(urlNavigator->historyIndex() + 1));
769 }
770 }
771
772 void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
773 {
774 // The default case (left button pressed) is handled in goForward().
775 if (buttons == Qt::MidButton) {
776 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
777 openNewTab(urlNavigator->historyUrl(urlNavigator->historyIndex() - 1));
778 }
779 }
780
781 void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
782 {
783 // The default case (left button pressed) is handled in goUp().
784 if (buttons == Qt::MidButton) {
785 openNewTab(activeViewContainer()->url().upUrl());
786 }
787 }
788
789 void DolphinMainWindow::goHome()
790 {
791 clearStatusBar();
792 m_activeViewContainer->urlNavigator()->goHome();
793 }
794
795 void DolphinMainWindow::compareFiles()
796 {
797 // The method is only invoked if exactly 2 files have
798 // been selected. The selected files may be:
799 // - both in the primary view
800 // - both in the secondary view
801 // - one in the primary view and the other in the secondary
802 // view
803 Q_ASSERT(m_viewTab[m_tabIndex].primaryView != 0);
804
805 KUrl urlA;
806 KUrl urlB;
807 KUrl::List urls = m_viewTab[m_tabIndex].primaryView->view()->selectedUrls();
808
809 switch (urls.count()) {
810 case 0: {
811 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView != 0);
812 urls = m_viewTab[m_tabIndex].secondaryView->view()->selectedUrls();
813 Q_ASSERT(urls.count() == 2);
814 urlA = urls[0];
815 urlB = urls[1];
816 break;
817 }
818
819 case 1: {
820 urlA = urls[0];
821 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView != 0);
822 urls = m_viewTab[m_tabIndex].secondaryView->view()->selectedUrls();
823 Q_ASSERT(urls.count() == 1);
824 urlB = urls[0];
825 break;
826 }
827
828 case 2: {
829 urlA = urls[0];
830 urlB = urls[1];
831 break;
832 }
833
834 default: {
835 // may not happen: compareFiles may only get invoked if 2
836 // files are selected
837 Q_ASSERT(false);
838 }
839 }
840
841 QString command("kompare -c \"");
842 command.append(urlA.pathOrUrl());
843 command.append("\" \"");
844 command.append(urlB.pathOrUrl());
845 command.append('\"');
846 KRun::runCommand(command, "Kompare", "kompare", this);
847 }
848
849 void DolphinMainWindow::toggleShowMenuBar()
850 {
851 const bool visible = menuBar()->isVisible();
852 menuBar()->setVisible(!visible);
853 }
854
855 void DolphinMainWindow::openTerminal()
856 {
857 QString dir(QDir::homePath());
858
859 // If the given directory is not local, it can still be the URL of an
860 // ioslave using UDS_LOCAL_PATH which to be converted first.
861 KUrl url = KIO::NetAccess::mostLocalUrl(m_activeViewContainer->url(), this);
862
863 //If the URL is local after the above conversion, set the directory.
864 if (url.isLocalFile()) {
865 dir = url.toLocalFile();
866 }
867
868 KToolInvocation::invokeTerminal(QString(), dir);
869 }
870
871 void DolphinMainWindow::editSettings()
872 {
873 if (m_settingsDialog == 0) {
874 const KUrl& url = activeViewContainer()->url();
875 m_settingsDialog = new DolphinSettingsDialog(url, this);
876 m_settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
877 m_settingsDialog->show();
878 } else {
879 m_settingsDialog->raise();
880 }
881 }
882
883 void DolphinMainWindow::setActiveTab(int index)
884 {
885 Q_ASSERT(index >= 0);
886 Q_ASSERT(index < m_viewTab.count());
887 if (index == m_tabIndex) {
888 return;
889 }
890
891 // hide current tab content
892 ViewTab& hiddenTab = m_viewTab[m_tabIndex];
893 hiddenTab.isPrimaryViewActive = hiddenTab.primaryView->isActive();
894 hiddenTab.primaryView->setActive(false);
895 if (hiddenTab.secondaryView != 0) {
896 hiddenTab.secondaryView->setActive(false);
897 }
898 QSplitter* splitter = m_viewTab[m_tabIndex].splitter;
899 splitter->hide();
900 m_centralWidgetLayout->removeWidget(splitter);
901
902 // show active tab content
903 m_tabIndex = index;
904
905 ViewTab& viewTab = m_viewTab[index];
906 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
907 viewTab.primaryView->show();
908 if (viewTab.secondaryView != 0) {
909 viewTab.secondaryView->show();
910 }
911 viewTab.splitter->show();
912
913 setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
914 viewTab.secondaryView);
915 }
916
917 void DolphinMainWindow::closeTab()
918 {
919 closeTab(m_tabBar->currentIndex());
920 }
921
922 void DolphinMainWindow::closeTab(int index)
923 {
924 Q_ASSERT(index >= 0);
925 Q_ASSERT(index < m_viewTab.count());
926 if (m_viewTab.count() == 1) {
927 // the last tab may never get closed
928 return;
929 }
930
931 if (index == m_tabIndex) {
932 // The tab that should be closed is the active tab. Activate the
933 // previous tab before closing the tab.
934 m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
935 }
936 rememberClosedTab(index);
937
938 // delete tab
939 m_viewTab[index].primaryView->deleteLater();
940 if (m_viewTab[index].secondaryView != 0) {
941 m_viewTab[index].secondaryView->deleteLater();
942 }
943 m_viewTab[index].splitter->deleteLater();
944 m_viewTab.erase(m_viewTab.begin() + index);
945
946 m_tabBar->blockSignals(true);
947 m_tabBar->removeTab(index);
948
949 if (m_tabIndex > index) {
950 m_tabIndex--;
951 Q_ASSERT(m_tabIndex >= 0);
952 }
953
954 // if only one tab is left, also remove the tab entry so that
955 // closing the last tab is not possible
956 if (m_viewTab.count() == 1) {
957 m_tabBar->removeTab(0);
958 actionCollection()->action("close_tab")->setEnabled(false);
959 } else {
960 m_tabBar->blockSignals(false);
961 }
962 }
963
964 void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
965 {
966 KMenu menu(this);
967
968 QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
969 newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
970
971 QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
972
973 QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
974 closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
975 QAction* selectedAction = menu.exec(pos);
976 if (selectedAction == newTabAction) {
977 const ViewTab& tab = m_viewTab[index];
978 Q_ASSERT(tab.primaryView != 0);
979 const KUrl url = (tab.secondaryView != 0) && tab.secondaryView->isActive() ?
980 tab.secondaryView->url() : tab.primaryView->url();
981 openNewTab(url);
982 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
983 } else if (selectedAction == closeOtherTabsAction) {
984 const int count = m_tabBar->count();
985 for (int i = 0; i < index; ++i) {
986 closeTab(0);
987 }
988 for (int i = index + 1; i < count; ++i) {
989 closeTab(1);
990 }
991 } else if (selectedAction == closeTabAction) {
992 closeTab(index);
993 }
994 }
995
996 void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
997 {
998 if (buttons & Qt::MidButton) {
999 openNewTab(url);
1000 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
1001 } else {
1002 changeUrl(url);
1003 }
1004 }
1005
1006 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
1007 {
1008 canDecode = KUrl::List::canDecode(event->mimeData());
1009 }
1010
1011 void DolphinMainWindow::searchItems()
1012 {
1013 #ifdef HAVE_NEPOMUK
1014 const KUrl nepomukSearchUrl = m_searchOptionsConfigurator->nepomukSearchUrl();
1015 m_activeViewContainer->setUrl(nepomukSearchUrl);
1016 #endif
1017 }
1018
1019 void DolphinMainWindow::slotTabMoved(int from, int to)
1020 {
1021 m_viewTab.move(from, to);
1022 m_tabIndex = m_tabBar->currentIndex();
1023 }
1024
1025 void DolphinMainWindow::showSearchOptions()
1026 {
1027 #ifdef HAVE_NEPOMUK
1028 m_searchOptionsConfigurator->show();
1029 #endif
1030 }
1031
1032 void DolphinMainWindow::init()
1033 {
1034 DolphinSettings& settings = DolphinSettings::instance();
1035
1036 // Check whether Dolphin runs the first time. If yes then
1037 // a proper default window size is given at the end of DolphinMainWindow::init().
1038 GeneralSettings* generalSettings = settings.generalSettings();
1039 const bool firstRun = generalSettings->firstRun();
1040 if (firstRun) {
1041 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1042 }
1043
1044 setAcceptDrops(true);
1045
1046 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1047 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1048
1049 setupActions();
1050
1051 const KUrl& homeUrl = generalSettings->homeUrl();
1052 setUrlAsCaption(homeUrl);
1053 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1054 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1055 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1056 ViewProperties props(homeUrl);
1057 m_viewTab[m_tabIndex].primaryView = new DolphinViewContainer(this,
1058 m_viewTab[m_tabIndex].splitter,
1059 homeUrl);
1060
1061 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1062 connectViewSignals(m_activeViewContainer);
1063 DolphinView* view = m_activeViewContainer->view();
1064 view->reload();
1065 m_activeViewContainer->show();
1066 m_actionHandler->setCurrentView(view);
1067
1068 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1069 connect(this, SIGNAL(urlChanged(const KUrl&)),
1070 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1071
1072 #ifdef HAVE_NEPOMUK
1073 m_searchOptionsConfigurator = new DolphinSearchOptionsConfigurator(this);
1074 m_searchOptionsConfigurator->hide();
1075 connect(m_searchOptionsConfigurator, SIGNAL(searchOptionsChanged()),
1076 this, SLOT(searchItems()));
1077 connect(this, SIGNAL(urlChanged(KUrl)), m_searchOptionsConfigurator, SLOT(setDirectory(KUrl)));
1078 #endif
1079
1080 m_tabBar = new KTabBar(this);
1081 m_tabBar->setMovable(true);
1082 m_tabBar->setTabsClosable(true);
1083 connect(m_tabBar, SIGNAL(currentChanged(int)),
1084 this, SLOT(setActiveTab(int)));
1085 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1086 this, SLOT(closeTab(int)));
1087 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1088 this, SLOT(openTabContextMenu(int, const QPoint&)));
1089 connect(m_tabBar, SIGNAL(newTabRequest()),
1090 this, SLOT(openNewTab()));
1091 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1092 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1093 connect(m_tabBar, SIGNAL(wheelDelta(int)),
1094 this, SLOT(slotWheelMoved(int)));
1095 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1096 this, SLOT(closeTab(int)));
1097 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1098 this, SLOT(slotTabMoved(int, int)));
1099
1100 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1101
1102 QWidget* centralWidget = new QWidget(this);
1103 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1104 m_centralWidgetLayout->setSpacing(0);
1105 m_centralWidgetLayout->setMargin(0);
1106 #ifdef HAVE_NEPOMUK
1107 m_centralWidgetLayout->addWidget(m_searchOptionsConfigurator);
1108 #endif
1109 m_centralWidgetLayout->addWidget(m_tabBar);
1110 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1111
1112 setCentralWidget(centralWidget);
1113 setupDockWidgets();
1114 emit urlChanged(homeUrl);
1115
1116 setupGUI(Keys | Save | Create | ToolBar);
1117
1118 m_searchBox->setParent(toolBar("searchToolBar"));
1119 m_searchBox->show();
1120 connect(m_searchBox, SIGNAL(requestSearchOptions()),
1121 this, SLOT(showSearchOptions()));
1122 #ifdef HAVE_NEPOMUK
1123 connect(m_searchBox, SIGNAL(searchTextChanged(QString)),
1124 m_searchOptionsConfigurator, SLOT(setCustomSearchQuery(QString)));
1125 #endif
1126
1127 stateChanged("new_file");
1128
1129 QClipboard* clipboard = QApplication::clipboard();
1130 connect(clipboard, SIGNAL(dataChanged()),
1131 this, SLOT(updatePasteAction()));
1132 updatePasteAction();
1133 updateGoActions();
1134
1135 if (generalSettings->splitView()) {
1136 toggleSplitView();
1137 }
1138 updateViewActions();
1139
1140 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1141 showFilterBarAction->setChecked(generalSettings->filterBar());
1142
1143 if (firstRun) {
1144 // assure a proper default size if Dolphin runs the first time
1145 resize(750, 500);
1146 }
1147
1148 m_showMenuBar->setChecked(!menuBar()->isHidden()); // workaround for bug #171080
1149 }
1150
1151 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1152 {
1153 Q_ASSERT(viewContainer != 0);
1154 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1155 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1156 if (m_activeViewContainer == viewContainer) {
1157 return;
1158 }
1159
1160 m_activeViewContainer->setActive(false);
1161 m_activeViewContainer = viewContainer;
1162
1163 // Activating the view container might trigger a recursive setActiveViewContainer() call
1164 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1165 // disconnect the activated() signal in this case:
1166 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1167 m_activeViewContainer->setActive(true);
1168 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1169
1170 m_actionHandler->setCurrentView(viewContainer->view());
1171
1172 updateHistory();
1173 updateEditActions();
1174 updateViewActions();
1175 updateGoActions();
1176
1177 const KUrl& url = m_activeViewContainer->url();
1178 setUrlAsCaption(url);
1179 if (m_viewTab.count() > 1 && m_viewTab[m_tabIndex].secondaryView != 0) {
1180 m_tabBar->setTabText(m_tabIndex, tabName(url));
1181 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1182 }
1183
1184 emit urlChanged(url);
1185 }
1186
1187 void DolphinMainWindow::setupActions()
1188 {
1189 // setup 'File' menu
1190 m_newMenu = new DolphinNewMenu(this, this);
1191 KMenu* menu = m_newMenu->menu();
1192 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1193 menu->setIcon(KIcon("document-new"));
1194 connect(menu, SIGNAL(aboutToShow()),
1195 this, SLOT(updateNewMenu()));
1196
1197 KAction* newWindow = actionCollection()->addAction("new_window");
1198 newWindow->setIcon(KIcon("window-new"));
1199 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1200 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1201 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1202
1203 KAction* newTab = actionCollection()->addAction("new_tab");
1204 newTab->setIcon(KIcon("tab-new"));
1205 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1206 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1207 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1208
1209 KAction* closeTab = actionCollection()->addAction("close_tab");
1210 closeTab->setIcon(KIcon("tab-close"));
1211 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1212 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1213 closeTab->setEnabled(false);
1214 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1215
1216 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1217
1218 // setup 'Edit' menu
1219 KStandardAction::undo(this,
1220 SLOT(undo()),
1221 actionCollection());
1222
1223 // need to remove shift+del from cut action, else the shortcut for deletejob
1224 // doesn't work
1225 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1226 KShortcut cutShortcut = cut->shortcut();
1227 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
1228 cut->setShortcut(cutShortcut);
1229 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1230 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1231 // The text of the paste-action is modified dynamically by Dolphin
1232 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1233 // due to the long text, the text "Paste" is used:
1234 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1235
1236 KAction* selectAll = actionCollection()->addAction("select_all");
1237 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1238 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1239 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1240
1241 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1242 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1243 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1244 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1245
1246 // setup 'View' menu
1247 // (note that most of it is set up in DolphinViewActionHandler)
1248
1249 KAction* split = actionCollection()->addAction("split_view");
1250 split->setShortcut(Qt::Key_F3);
1251 updateSplitAction();
1252 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1253
1254 KAction* reload = actionCollection()->addAction("reload");
1255 reload->setText(i18nc("@action:inmenu View", "Reload"));
1256 reload->setShortcut(Qt::Key_F5);
1257 reload->setIcon(KIcon("view-refresh"));
1258 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1259
1260 KAction* stop = actionCollection()->addAction("stop");
1261 stop->setText(i18nc("@action:inmenu View", "Stop"));
1262 stop->setToolTip(i18nc("@info", "Stop loading"));
1263 stop->setIcon(KIcon("process-stop"));
1264 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1265
1266 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1267 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1268 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1269 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1270
1271 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1272 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1273 replaceLocation->setShortcut(Qt::Key_F6);
1274 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1275
1276 // setup 'Go' menu
1277 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1278 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1279 KShortcut backShortcut = backAction->shortcut();
1280 backShortcut.setAlternate(Qt::Key_Backspace);
1281 backAction->setShortcut(backShortcut);
1282
1283 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1284 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1285 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1286 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1287 this, SLOT(restoreClosedTab(QAction *)));
1288
1289 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1290 action->setIcon(KIcon("edit-clear-list"));
1291 action->setData(QVariant::fromValue(true));
1292 m_recentTabsMenu->addAction(action);
1293 m_recentTabsMenu->addSeparator();
1294 m_recentTabsMenu->setEnabled(false);
1295
1296 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1297 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1298
1299 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1300 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1301
1302 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1303
1304 // setup 'Tools' menu
1305 KToggleAction* showSearchBar = actionCollection()->add<KToggleAction>("show_search_bar");
1306 showSearchBar->setText(i18nc("@action:inmenu Tools", "Show Search Bar"));
1307 showSearchBar->setShortcut(Qt::CTRL | Qt::Key_S);
1308 connect(showSearchBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1309
1310 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1311 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1312 showFilterBar->setIcon(KIcon("view-filter"));
1313 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1314 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1315
1316 KAction* compareFiles = actionCollection()->addAction("compare_files");
1317 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1318 compareFiles->setIcon(KIcon("kompare"));
1319 compareFiles->setEnabled(false);
1320 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1321
1322 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1323 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1324 openTerminal->setIcon(KIcon("utilities-terminal"));
1325 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1326 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1327
1328 // setup 'Settings' menu
1329 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1330 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1331
1332 // not in menu actions
1333 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1334 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1335 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1336 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() :
1337 KStandardShortcut::tabNext());
1338
1339 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1340 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1341 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1342 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() :
1343 KStandardShortcut::tabPrev());
1344
1345 // for context menu
1346 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1347 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1348 openInNewTab->setIcon(KIcon("tab-new"));
1349 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1350
1351 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1352 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1353 openInNewWindow->setIcon(KIcon("window-new"));
1354 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1355
1356 // 'Search' toolbar
1357 m_searchBox = new DolphinSearchBox(this);
1358 connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(searchItems()));
1359
1360 KAction* search = new KAction(this);
1361 actionCollection()->addAction("search_bar", search);
1362 search->setText(i18nc("@action:inmenu", "Search Bar"));
1363 search->setDefaultWidget(m_searchBox);
1364 search->setShortcutConfigurable(false);
1365 }
1366
1367 void DolphinMainWindow::setupDockWidgets()
1368 {
1369 // setup "Information"
1370 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
1371 infoDock->setObjectName("infoDock");
1372 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1373 Panel* infoPanel = new InformationPanel(infoDock);
1374 connect(infoPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1375 infoDock->setWidget(infoPanel);
1376
1377 QAction* infoAction = infoDock->toggleViewAction();
1378 infoAction->setText(i18nc("@title:window", "Information"));
1379 infoAction->setShortcut(Qt::Key_F11);
1380 infoAction->setIcon(KIcon("dialog-information"));
1381 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1382
1383 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1384 connect(this, SIGNAL(urlChanged(KUrl)),
1385 infoPanel, SLOT(setUrl(KUrl)));
1386 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1387 infoPanel, SLOT(setSelection(KFileItemList)));
1388 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1389 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1390
1391 // setup "Folders"
1392 QDockWidget* foldersDock = new QDockWidget(i18nc("@title:window", "Folders"));
1393 foldersDock->setObjectName("foldersDock");
1394 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1395 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1396 foldersDock->setWidget(foldersPanel);
1397
1398 QAction* foldersAction = foldersDock->toggleViewAction();
1399 foldersAction->setText(i18nc("@title:window", "Folders"));
1400 foldersAction->setShortcut(Qt::Key_F7);
1401 foldersAction->setIcon(KIcon("folder"));
1402 actionCollection()->addAction("show_folders_panel", foldersDock->toggleViewAction());
1403
1404 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1405 connect(this, SIGNAL(urlChanged(KUrl)),
1406 foldersPanel, SLOT(setUrl(KUrl)));
1407 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1408 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1409 connect(foldersPanel, SIGNAL(changeSelection(KFileItemList)),
1410 this, SLOT(changeSelection(KFileItemList)));
1411
1412 // setup "Terminal"
1413 #ifndef Q_OS_WIN
1414 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1415 terminalDock->setObjectName("terminalDock");
1416 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1417 Panel* terminalPanel = new TerminalPanel(terminalDock);
1418 terminalDock->setWidget(terminalPanel);
1419
1420 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1421
1422 QAction* terminalAction = terminalDock->toggleViewAction();
1423 terminalAction->setText(i18nc("@title:window Shell terminal", "Terminal"));
1424 terminalAction->setShortcut(Qt::Key_F4);
1425 terminalAction->setIcon(KIcon("utilities-terminal"));
1426 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
1427
1428 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1429 connect(this, SIGNAL(urlChanged(KUrl)),
1430 terminalPanel, SLOT(setUrl(KUrl)));
1431 #endif
1432
1433 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1434 if (firstRun) {
1435 foldersDock->hide();
1436 #ifndef Q_OS_WIN
1437 terminalDock->hide();
1438 #endif
1439 }
1440
1441 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
1442 placesDock->setObjectName("placesDock");
1443 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1444
1445 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1446 placesDock->setWidget(placesPanel);
1447 placesPanel->setModel(DolphinSettings::instance().placesModel());
1448 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1449
1450 QAction* placesAction = placesDock->toggleViewAction();
1451 placesAction->setText(i18nc("@title:window", "Places"));
1452 placesAction->setShortcut(Qt::Key_F9);
1453 placesAction->setIcon(KIcon("bookmarks"));
1454 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
1455
1456 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1457 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1458 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1459 connect(this, SIGNAL(urlChanged(KUrl)),
1460 placesPanel, SLOT(setUrl(KUrl)));
1461 }
1462
1463 void DolphinMainWindow::updateEditActions()
1464 {
1465 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1466 if (list.isEmpty()) {
1467 stateChanged("has_no_selection");
1468 } else {
1469 stateChanged("has_selection");
1470
1471 KActionCollection* col = actionCollection();
1472 QAction* renameAction = col->action("rename");
1473 QAction* moveToTrashAction = col->action("move_to_trash");
1474 QAction* deleteAction = col->action("delete");
1475 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1476 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1477
1478 KFileItemListProperties capabilities(list);
1479 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1480
1481 renameAction->setEnabled(capabilities.supportsMoving());
1482 moveToTrashAction->setEnabled(enableMoveToTrash);
1483 deleteAction->setEnabled(capabilities.supportsDeleting());
1484 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1485 cutAction->setEnabled(capabilities.supportsMoving());
1486 }
1487 updatePasteAction();
1488 }
1489
1490 void DolphinMainWindow::updateViewActions()
1491 {
1492 m_actionHandler->updateViewActions();
1493
1494 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1495 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1496
1497 updateSplitAction();
1498
1499 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1500 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1501 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1502 }
1503
1504 void DolphinMainWindow::updateGoActions()
1505 {
1506 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1507 const KUrl& currentUrl = m_activeViewContainer->url();
1508 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1509 }
1510
1511 void DolphinMainWindow::rememberClosedTab(int index)
1512 {
1513 KMenu* tabsMenu = m_recentTabsMenu->menu();
1514
1515 const QString primaryPath = m_viewTab[index].primaryView->url().path();
1516 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
1517
1518 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
1519
1520 ClosedTab closedTab;
1521 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
1522
1523 if (m_viewTab[index].secondaryView != 0) {
1524 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
1525 closedTab.isSplit = true;
1526 } else {
1527 closedTab.isSplit = false;
1528 }
1529
1530 action->setData(QVariant::fromValue(closedTab));
1531 action->setIcon(KIcon(iconName));
1532
1533 // add the closed tab menu entry after the separator and
1534 // "Empty Recently Closed Tabs" entry
1535 if (tabsMenu->actions().size() == 2) {
1536 tabsMenu->addAction(action);
1537 } else {
1538 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
1539 }
1540
1541 // assure that only up to 8 closed tabs are shown in the menu
1542 if (tabsMenu->actions().size() > 8) {
1543 tabsMenu->removeAction(tabsMenu->actions().last());
1544 }
1545 actionCollection()->action("closed_tabs")->setEnabled(true);
1546 KAcceleratorManager::manage(tabsMenu);
1547 }
1548
1549 void DolphinMainWindow::clearStatusBar()
1550 {
1551 m_activeViewContainer->statusBar()->clear();
1552 }
1553
1554 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1555 {
1556 connect(container, SIGNAL(showFilterBarChanged(bool)),
1557 this, SLOT(updateFilterBarAction(bool)));
1558
1559 DolphinView* view = container->view();
1560 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1561 this, SLOT(slotSelectionChanged(KFileItemList)));
1562 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1563 this, SLOT(slotRequestItemInfo(KFileItem)));
1564 connect(view, SIGNAL(activated()),
1565 this, SLOT(toggleActiveView()));
1566 connect(view, SIGNAL(tabRequested(const KUrl&)),
1567 this, SLOT(openNewTab(const KUrl&)));
1568
1569 const KUrlNavigator* navigator = container->urlNavigator();
1570 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1571 this, SLOT(changeUrl(const KUrl&)));
1572 connect(navigator, SIGNAL(historyChanged()),
1573 this, SLOT(updateHistory()));
1574 connect(navigator, SIGNAL(editableStateChanged(bool)),
1575 this, SLOT(slotEditableStateChanged(bool)));
1576 }
1577
1578 void DolphinMainWindow::updateSplitAction()
1579 {
1580 QAction* splitAction = actionCollection()->action("split_view");
1581 if (m_viewTab[m_tabIndex].secondaryView != 0) {
1582 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
1583 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1584 splitAction->setToolTip(i18nc("@info", "Close right view"));
1585 splitAction->setIcon(KIcon("view-right-close"));
1586 } else {
1587 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1588 splitAction->setToolTip(i18nc("@info", "Close left view"));
1589 splitAction->setIcon(KIcon("view-left-close"));
1590 }
1591 } else {
1592 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1593 splitAction->setToolTip(i18nc("@info", "Split view"));
1594 splitAction->setIcon(KIcon("view-right-new"));
1595 }
1596 }
1597
1598 QString DolphinMainWindow::tabName(const KUrl& url) const
1599 {
1600 QString name;
1601 if (url.equals(KUrl("file:///"))) {
1602 name = '/';
1603 } else {
1604 name = url.fileName();
1605 if (name.isEmpty()) {
1606 name = url.protocol();
1607 } else {
1608 // Make sure that a '&' inside the directory name is displayed correctly
1609 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
1610 name.replace('&', "&&");
1611 }
1612 }
1613 return name;
1614 }
1615
1616 bool DolphinMainWindow::isKompareInstalled() const
1617 {
1618 static bool initialized = false;
1619 static bool installed = false;
1620 if (!initialized) {
1621 // TODO: maybe replace this approach later by using a menu
1622 // plugin like kdiff3plugin.cpp
1623 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
1624 initialized = true;
1625 }
1626 return installed;
1627 }
1628
1629 void DolphinMainWindow::createSecondaryView(int tabIndex)
1630 {
1631 QSplitter* splitter = m_viewTab[tabIndex].splitter;
1632 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
1633
1634 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
1635 m_viewTab[tabIndex].secondaryView = new DolphinViewContainer(this, 0, view->rootUrl());
1636 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
1637 splitter->setSizes(QList<int>() << newWidth << newWidth);
1638 connectViewSignals(m_viewTab[tabIndex].secondaryView);
1639 m_viewTab[tabIndex].secondaryView->view()->reload();
1640 m_viewTab[tabIndex].secondaryView->setActive(false);
1641 m_viewTab[tabIndex].secondaryView->show();
1642 }
1643
1644 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
1645 {
1646 return "Tab " + QString::number(tabIndex) + ' ' + property;
1647 }
1648
1649 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
1650 {
1651 QString caption;
1652 if (url.equals(KUrl("file:///"))) {
1653 caption = '/';
1654 } else {
1655 caption = url.fileName();
1656 if (caption.isEmpty()) {
1657 caption = url.protocol();
1658 }
1659 }
1660
1661 setCaption(caption);
1662 }
1663
1664 void DolphinMainWindow::handleUrl(const KUrl& url)
1665 {
1666 if (KProtocolManager::supportsListing(url)) {
1667 activeViewContainer()->setUrl(url);
1668 }
1669 else {
1670 new KRun(url, this);
1671 }
1672 }
1673
1674 QString DolphinMainWindow::squeezedText(const QString& text) const
1675 {
1676 const QFontMetrics fm = fontMetrics();
1677 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
1678 }
1679
1680 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1681 KIO::FileUndoManager::UiInterface()
1682 {
1683 }
1684
1685 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1686 {
1687 }
1688
1689 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1690 {
1691 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1692 if (mainWin) {
1693 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
1694 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1695 } else {
1696 KIO::FileUndoManager::UiInterface::jobError(job);
1697 }
1698 }
1699
1700 #include "dolphinmainwindow.moc"