]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Readd the "go home" action
[dolphin.git] / src / dolphinmainwindow.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
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
24 #include <config-nepomuk.h>
25
26 #include "dolphinapplication.h"
27 #include "dolphindockwidget.h"
28 #include "dolphincontextmenu.h"
29 #include "dolphinnewfilemenu.h"
30 #include "dolphinviewcontainer.h"
31 #include "mainwindowadaptor.h"
32 #ifdef HAVE_NEPOMUK
33 #include "panels/search/searchpanel.h"
34 #include <Nepomuk/ResourceManager>
35 #endif
36 #include "panels/folders/folderspanel.h"
37 #include "panels/places/placespanel.h"
38 #include "panels/information/informationpanel.h"
39 #include "search/dolphinsearchinformation.h"
40 #include "settings/dolphinsettings.h"
41 #include "settings/dolphinsettingsdialog.h"
42 #include "statusbar/dolphinstatusbar.h"
43 #include "views/dolphinviewactionhandler.h"
44 #include "views/dolphinremoteencoding.h"
45 #include "views/draganddrophelper.h"
46 #include "views/viewproperties.h"
47
48 #ifndef Q_OS_WIN
49 #include "panels/terminal/terminalpanel.h"
50 #endif
51
52 #include "dolphin_generalsettings.h"
53 #include "dolphin_iconsmodesettings.h"
54 #include "dolphin_searchsettings.h"
55
56 #include <KAcceleratorManager>
57 #include <KAction>
58 #include <KActionCollection>
59 #include <KActionMenu>
60 #include <KConfig>
61 #include <KDesktopFile>
62 #include <kdeversion.h>
63 #include <kdualaction.h>
64 #include <KFileDialog>
65 #include <KFilePlacesModel>
66 #include <KGlobal>
67 #include <KLineEdit>
68 #include <KToolBar>
69 #include <KIcon>
70 #include <KIconLoader>
71 #include <KIO/NetAccess>
72 #include <KInputDialog>
73 #include <KLocale>
74 #include <KProtocolManager>
75 #include <KMenu>
76 #include <KMenuBar>
77 #include <KMessageBox>
78 #include <KFileItemListProperties>
79 #include <konqmimedata.h>
80 #include <KProtocolInfo>
81 #include <KRun>
82 #include <KShell>
83 #include <KStandardDirs>
84 #include <kstatusbar.h>
85 #include <KStandardAction>
86 #include <ktabbar.h>
87 #include <KToggleAction>
88 #include <KUrlNavigator>
89 #include <KUrl>
90 #include <KUrlComboBox>
91 #include <KToolInvocation>
92
93 #include <QDBusMessage>
94 #include <QKeyEvent>
95 #include <QClipboard>
96 #include <QToolButton>
97 #include <QSplitter>
98
99 /*
100 * Remembers the tab configuration if a tab has been closed.
101 * Each closed tab can be restored by the menu
102 * "Go -> Recently Closed Tabs".
103 */
104 struct ClosedTab
105 {
106 KUrl primaryUrl;
107 KUrl secondaryUrl;
108 bool isSplit;
109 };
110 Q_DECLARE_METATYPE(ClosedTab)
111
112 DolphinMainWindow::DolphinMainWindow(int id) :
113 KXmlGuiWindow(0),
114 m_newFileMenu(0),
115 m_tabBar(0),
116 m_activeViewContainer(0),
117 m_centralWidgetLayout(0),
118 m_id(id),
119 m_tabIndex(0),
120 m_viewTab(),
121 m_actionHandler(0),
122 m_remoteEncoding(0),
123 m_settingsDialog(0),
124 m_toolBarSpacer(0),
125 m_openToolBarMenuButton(0),
126 m_toolBarMenu(),
127 m_updateToolBarTimer(0),
128 m_lastHandleUrlStatJob(0),
129 m_searchDockIsTemporaryVisible(false)
130 {
131 // Workaround for a X11-issue in combination with KModifierInfo
132 // (see DolphinContextMenu::initializeModifierKeyInfo() for
133 // more information):
134 DolphinContextMenu::initializeModifierKeyInfo();
135
136 setObjectName("Dolphin#");
137
138 m_viewTab.append(ViewTab());
139
140 new MainWindowAdaptor(this);
141 QDBusConnection::sessionBus().registerObject(QString("/dolphin/MainWindow%1").arg(m_id), this);
142
143 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
144 undoManager->setUiInterface(new UndoUiInterface());
145
146 connect(undoManager, SIGNAL(undoAvailable(bool)),
147 this, SLOT(slotUndoAvailable(bool)));
148 connect(undoManager, SIGNAL(undoTextChanged(const QString&)),
149 this, SLOT(slotUndoTextChanged(const QString&)));
150 connect(undoManager, SIGNAL(jobRecordingStarted(CommandType)),
151 this, SLOT(clearStatusBar()));
152 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
153 this, SLOT(showCommand(CommandType)));
154 connect(DolphinSettings::instance().placesModel(), SIGNAL(errorMessage(const QString&)),
155 this, SLOT(showErrorMessage(const QString&)));
156 connect(&DragAndDropHelper::instance(), SIGNAL(errorMessage(const QString&)),
157 this, SLOT(showErrorMessage(const QString&)));
158 }
159
160 DolphinMainWindow::~DolphinMainWindow()
161 {
162 DolphinApplication::app()->removeMainWindow(this);
163 }
164
165 void DolphinMainWindow::openDirectories(const QList<KUrl>& dirs)
166 {
167 if (dirs.isEmpty()) {
168 return;
169 }
170
171 if (dirs.count() == 1) {
172 m_activeViewContainer->setUrl(dirs.first());
173 return;
174 }
175
176 const int oldOpenTabsCount = m_viewTab.count();
177
178 const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
179 const bool hasSplitView = generalSettings->splitView();
180
181 // Open each directory inside a new tab. If the "split view" option has been enabled,
182 // always show two directories within one tab.
183 QList<KUrl>::const_iterator it = dirs.begin();
184 while (it != dirs.end()) {
185 openNewTab(*it);
186 ++it;
187
188 if (hasSplitView && (it != dirs.end())) {
189 const int tabIndex = m_viewTab.count() - 1;
190 m_viewTab[tabIndex].secondaryView->setUrl(*it);
191 ++it;
192 }
193 }
194
195 // remove the previously opened tabs
196 for (int i = 0; i < oldOpenTabsCount; ++i) {
197 closeTab(0);
198 }
199 }
200
201 void DolphinMainWindow::openFiles(const QList<KUrl>& files)
202 {
203 if (files.isEmpty()) {
204 return;
205 }
206
207 // Get all distinct directories from 'files' and open a tab
208 // for each directory. If the "split view" option is enabled, two
209 // directories are shown inside one tab (see openDirectories()).
210 QList<KUrl> dirs;
211 foreach (const KUrl& url, files) {
212 const KUrl dir(url.directory());
213 if (!dirs.contains(dir)) {
214 dirs.append(dir);
215 }
216 }
217
218 openDirectories(dirs);
219
220 // Select the files. Although the files can be split between several
221 // tabs, there is no need to split 'files' accordingly, as
222 // the DolphinView will just ignore invalid selections.
223 const int tabCount = m_viewTab.count();
224 for (int i = 0; i < tabCount; ++i) {
225 m_viewTab[i].primaryView->view()->markUrlsAsSelected(files);
226 if (m_viewTab[i].secondaryView) {
227 m_viewTab[i].secondaryView->view()->markUrlsAsSelected(files);
228 }
229 }
230 }
231
232 void DolphinMainWindow::showCommand(CommandType command)
233 {
234 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
235 switch (command) {
236 case KIO::FileUndoManager::Copy:
237 statusBar->setMessage(i18nc("@info:status", "Successfully copied."),
238 DolphinStatusBar::OperationCompleted);
239 break;
240 case KIO::FileUndoManager::Move:
241 statusBar->setMessage(i18nc("@info:status", "Successfully moved."),
242 DolphinStatusBar::OperationCompleted);
243 break;
244 case KIO::FileUndoManager::Link:
245 statusBar->setMessage(i18nc("@info:status", "Successfully linked."),
246 DolphinStatusBar::OperationCompleted);
247 break;
248 case KIO::FileUndoManager::Trash:
249 statusBar->setMessage(i18nc("@info:status", "Successfully moved to trash."),
250 DolphinStatusBar::OperationCompleted);
251 break;
252 case KIO::FileUndoManager::Rename:
253 statusBar->setMessage(i18nc("@info:status", "Successfully renamed."),
254 DolphinStatusBar::OperationCompleted);
255 break;
256
257 case KIO::FileUndoManager::Mkdir:
258 statusBar->setMessage(i18nc("@info:status", "Created folder."),
259 DolphinStatusBar::OperationCompleted);
260 break;
261
262 default:
263 break;
264 }
265 }
266
267 void DolphinMainWindow::refreshViews()
268 {
269 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
270
271 // remember the current active view, as because of
272 // the refreshing the active view might change to
273 // the secondary view
274 DolphinViewContainer* activeViewContainer = m_activeViewContainer;
275
276 const int tabCount = m_viewTab.count();
277 for (int i = 0; i < tabCount; ++i) {
278 m_viewTab[i].primaryView->refresh();
279 if (m_viewTab[i].secondaryView) {
280 m_viewTab[i].secondaryView->refresh();
281 }
282 }
283
284 setActiveViewContainer(activeViewContainer);
285
286 const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
287 if (generalSettings->modifiedStartupSettings()) {
288 // The startup settings have been changed by the user (see bug #254947).
289 // Synchronize the split-view setting with the active view:
290 const bool splitView = generalSettings->splitView();
291 const ViewTab& activeTab = m_viewTab[m_tabIndex];
292 const bool toggle = ( splitView && !activeTab.secondaryView)
293 || (!splitView && activeTab.secondaryView);
294 if (toggle) {
295 toggleSplitView();
296 }
297 }
298 }
299
300 void DolphinMainWindow::pasteIntoFolder()
301 {
302 m_activeViewContainer->view()->pasteIntoFolder();
303 }
304
305 void DolphinMainWindow::changeUrl(const KUrl& url)
306 {
307 if (!KProtocolManager::supportsListing(url)) {
308 // The URL navigator only checks for validity, not
309 // if the URL can be listed. An error message is
310 // shown due to DolphinViewContainer::restoreView().
311 return;
312 }
313
314 DolphinViewContainer* view = activeViewContainer();
315 if (view) {
316 view->setUrl(url);
317 updateEditActions();
318 updateViewActions();
319 updateGoActions();
320 setUrlAsCaption(url);
321 if (m_viewTab.count() > 1) {
322 m_tabBar->setTabText(m_tabIndex, squeezedText(tabName(m_activeViewContainer->url())));
323 }
324 const QString iconName = KMimeType::iconNameForUrl(url);
325 m_tabBar->setTabIcon(m_tabIndex, KIcon(iconName));
326 emit urlChanged(url);
327 }
328 }
329
330 void DolphinMainWindow::slotEditableStateChanged(bool editable)
331 {
332 KToggleAction* editableLocationAction =
333 static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
334 editableLocationAction->setChecked(editable);
335 }
336
337 void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
338 {
339 updateEditActions();
340
341 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
342 int selectedUrlsCount = m_viewTab[m_tabIndex].primaryView->view()->selectedItemsCount();
343 if (m_viewTab[m_tabIndex].secondaryView) {
344 selectedUrlsCount += m_viewTab[m_tabIndex].secondaryView->view()->selectedItemsCount();
345 }
346
347 QAction* compareFilesAction = actionCollection()->action("compare_files");
348 if (selectedUrlsCount == 2) {
349 compareFilesAction->setEnabled(isKompareInstalled());
350 } else {
351 compareFilesAction->setEnabled(false);
352 }
353
354 emit selectionChanged(selection);
355 }
356
357 void DolphinMainWindow::slotRequestItemInfo(const KFileItem& item)
358 {
359 emit requestItemInfo(item);
360 }
361
362 void DolphinMainWindow::updateHistory()
363 {
364 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
365 const int index = urlNavigator->historyIndex();
366
367 QAction* backAction = actionCollection()->action("go_back");
368 backAction->setToolTip(i18nc("@info", "Go back"));
369 if (backAction) {
370 backAction->setEnabled(index < urlNavigator->historySize() - 1);
371 }
372
373 QAction* forwardAction = actionCollection()->action("go_forward");
374 forwardAction->setToolTip(i18nc("@info", "Go forward"));
375 if (forwardAction) {
376 forwardAction->setEnabled(index > 0);
377 }
378 }
379
380 void DolphinMainWindow::updateFilterBarAction(bool show)
381 {
382 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
383 showFilterBarAction->setChecked(show);
384 }
385
386 void DolphinMainWindow::openNewMainWindow()
387 {
388 DolphinApplication::app()->createMainWindow()->show();
389 }
390
391 void DolphinMainWindow::openNewTab()
392 {
393 const bool isUrlEditable = m_activeViewContainer->urlNavigator()->isUrlEditable();
394
395 openNewTab(m_activeViewContainer->url());
396 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
397
398 // The URL navigator of the new tab should have the same editable state
399 // as the current tab
400 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
401 navigator->setUrlEditable(isUrlEditable);
402
403 if (isUrlEditable) {
404 // If a new tab is opened and the URL is editable, assure that
405 // the user can edit the URL without manually setting the focus
406 navigator->setFocus();
407 }
408 }
409
410 void DolphinMainWindow::openNewTab(const KUrl& url)
411 {
412 QWidget* focusWidget = QApplication::focusWidget();
413
414 if (m_viewTab.count() == 1) {
415 // Only one view is open currently and hence no tab is shown at
416 // all. Before creating a tab for 'url', provide a tab for the current URL.
417 const KUrl currentUrl = m_activeViewContainer->url();
418 m_tabBar->addTab(KIcon(KMimeType::iconNameForUrl(currentUrl)),
419 squeezedText(tabName(currentUrl)));
420 m_tabBar->blockSignals(false);
421 }
422
423 m_tabBar->addTab(KIcon(KMimeType::iconNameForUrl(url)),
424 squeezedText(tabName(url)));
425
426 ViewTab viewTab;
427 viewTab.splitter = new QSplitter(this);
428 viewTab.splitter->setChildrenCollapsible(false);
429 viewTab.primaryView = createViewContainer(url, viewTab.splitter);
430 viewTab.primaryView->setActive(false);
431 connectViewSignals(viewTab.primaryView);
432
433 m_viewTab.append(viewTab);
434
435 actionCollection()->action("close_tab")->setEnabled(true);
436
437 // provide a split view, if the startup settings are set this way
438 const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
439 if (generalSettings->splitView()) {
440 const int tabIndex = m_viewTab.count() - 1;
441 createSecondaryView(tabIndex);
442 m_viewTab[tabIndex].secondaryView->setActive(true);
443 m_viewTab[tabIndex].isPrimaryViewActive = false;
444 }
445
446 if (focusWidget) {
447 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
448 // in background, assure that the previous focused widget gets the focus back.
449 focusWidget->setFocus();
450 }
451 }
452
453 void DolphinMainWindow::activateNextTab()
454 {
455 if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
456 return;
457 }
458
459 const int tabIndex = (m_tabBar->currentIndex() + 1) % m_tabBar->count();
460 m_tabBar->setCurrentIndex(tabIndex);
461 }
462
463 void DolphinMainWindow::activatePrevTab()
464 {
465 if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
466 return;
467 }
468
469 int tabIndex = m_tabBar->currentIndex() - 1;
470 if (tabIndex == -1) {
471 tabIndex = m_tabBar->count() - 1;
472 }
473 m_tabBar->setCurrentIndex(tabIndex);
474 }
475
476 void DolphinMainWindow::openInNewTab()
477 {
478 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
479 if (list.isEmpty()) {
480 openNewTab(m_activeViewContainer->url());
481 } else if ((list.count() == 1) && list[0].isDir()) {
482 openNewTab(list[0].url());
483 }
484 }
485
486 void DolphinMainWindow::openInNewWindow()
487 {
488 KUrl newWindowUrl;
489
490 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
491 if (list.isEmpty()) {
492 newWindowUrl = m_activeViewContainer->url();
493 } else if ((list.count() == 1) && list[0].isDir()) {
494 newWindowUrl = list[0].url();
495 }
496
497 if (!newWindowUrl.isEmpty()) {
498 DolphinMainWindow* window = DolphinApplication::app()->createMainWindow();
499 window->changeUrl(newWindowUrl);
500 window->show();
501 }
502 }
503
504 void DolphinMainWindow::toggleActiveView()
505 {
506 if (!m_viewTab[m_tabIndex].secondaryView) {
507 // only one view is available
508 return;
509 }
510
511 Q_ASSERT(m_activeViewContainer);
512 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
513
514 DolphinViewContainer* left = m_viewTab[m_tabIndex].primaryView;
515 DolphinViewContainer* right = m_viewTab[m_tabIndex].secondaryView;
516 setActiveViewContainer(m_activeViewContainer == right ? left : right);
517 }
518
519 void DolphinMainWindow::showEvent(QShowEvent* event)
520 {
521 KXmlGuiWindow::showEvent(event);
522 if (!event->spontaneous()) {
523 m_activeViewContainer->view()->setFocus();
524 }
525 }
526
527 void DolphinMainWindow::closeEvent(QCloseEvent* event)
528 {
529 DolphinSettings& settings = DolphinSettings::instance();
530 GeneralSettings* generalSettings = settings.generalSettings();
531
532 // Find out if Dolphin is closed directly by the user or
533 // by the session manager because the session is closed
534 bool closedByUser = true;
535 DolphinApplication *application = qobject_cast<DolphinApplication*>(qApp);
536 if (application && application->sessionSaving()) {
537 closedByUser = false;
538 }
539
540 if ((m_viewTab.count() > 1) && generalSettings->confirmClosingMultipleTabs() && closedByUser) {
541 // Ask the user if he really wants to quit and close all tabs.
542 // Open a confirmation dialog with 3 buttons:
543 // KDialog::Yes -> Quit
544 // KDialog::No -> Close only the current tab
545 // KDialog::Cancel -> do nothing
546 KDialog *dialog = new KDialog(this, Qt::Dialog);
547 dialog->setCaption(i18nc("@title:window", "Confirmation"));
548 dialog->setButtons(KDialog::Yes | KDialog::No | KDialog::Cancel);
549 dialog->setModal(true);
550 dialog->setButtonGuiItem(KDialog::Yes, KStandardGuiItem::quit());
551 dialog->setButtonGuiItem(KDialog::No, KGuiItem(i18n("C&lose Current Tab"), KIcon("tab-close")));
552 dialog->setButtonGuiItem(KDialog::Cancel, KStandardGuiItem::cancel());
553 dialog->setDefaultButton(KDialog::Yes);
554
555 bool doNotAskAgainCheckboxResult = false;
556
557 const int result = KMessageBox::createKMessageBox(dialog,
558 QMessageBox::Warning,
559 i18n("You have multiple tabs open in this window, are you sure you want to quit?"),
560 QStringList(),
561 i18n("Do not ask again"),
562 &doNotAskAgainCheckboxResult,
563 KMessageBox::Notify);
564
565 if (doNotAskAgainCheckboxResult) {
566 generalSettings->setConfirmClosingMultipleTabs(false);
567 }
568
569 switch (result) {
570 case KDialog::Yes:
571 // Quit
572 break;
573 case KDialog::No:
574 // Close only the current tab
575 closeTab();
576 default:
577 event->ignore();
578 return;
579 }
580 }
581
582 generalSettings->setFirstRun(false);
583
584 settings.save();
585
586 if (m_searchDockIsTemporaryVisible) {
587 QDockWidget* searchDock = findChild<QDockWidget*>("searchDock");
588 if (searchDock) {
589 searchDock->hide();
590 }
591 m_searchDockIsTemporaryVisible = false;
592 }
593
594 KXmlGuiWindow::closeEvent(event);
595 }
596
597 void DolphinMainWindow::saveProperties(KConfigGroup& group)
598 {
599 const int tabCount = m_viewTab.count();
600 group.writeEntry("Tab Count", tabCount);
601 group.writeEntry("Active Tab Index", m_tabBar->currentIndex());
602
603 for (int i = 0; i < tabCount; ++i) {
604 const DolphinViewContainer* cont = m_viewTab[i].primaryView;
605 group.writeEntry(tabProperty("Primary URL", i), cont->url().url());
606 group.writeEntry(tabProperty("Primary Editable", i),
607 cont->urlNavigator()->isUrlEditable());
608
609 cont = m_viewTab[i].secondaryView;
610 if (cont) {
611 group.writeEntry(tabProperty("Secondary URL", i), cont->url().url());
612 group.writeEntry(tabProperty("Secondary Editable", i),
613 cont->urlNavigator()->isUrlEditable());
614 }
615 }
616 }
617
618 void DolphinMainWindow::readProperties(const KConfigGroup& group)
619 {
620 const int tabCount = group.readEntry("Tab Count", 1);
621 for (int i = 0; i < tabCount; ++i) {
622 DolphinViewContainer* cont = m_viewTab[i].primaryView;
623
624 cont->setUrl(group.readEntry(tabProperty("Primary URL", i)));
625 const bool editable = group.readEntry(tabProperty("Primary Editable", i), false);
626 cont->urlNavigator()->setUrlEditable(editable);
627
628 cont = m_viewTab[i].secondaryView;
629 const QString secondaryUrl = group.readEntry(tabProperty("Secondary URL", i));
630 if (!secondaryUrl.isEmpty()) {
631 if (!cont) {
632 // a secondary view should be shown, but no one is available
633 // currently -> create a new view
634 toggleSplitView();
635 cont = m_viewTab[i].secondaryView;
636 Q_ASSERT(cont);
637 }
638
639 cont->setUrl(secondaryUrl);
640 const bool editable = group.readEntry(tabProperty("Secondary Editable", i), false);
641 cont->urlNavigator()->setUrlEditable(editable);
642 } else if (cont) {
643 // no secondary view should be shown, but the default setting shows
644 // one already -> close the view
645 toggleSplitView();
646 }
647
648 // openNewTab() needs to be called only tabCount - 1 times
649 if (i != tabCount - 1) {
650 openNewTab();
651 }
652 }
653
654 const int index = group.readEntry("Active Tab Index", 0);
655 m_tabBar->setCurrentIndex(index);
656 }
657
658 void DolphinMainWindow::updateNewMenu()
659 {
660 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->showHiddenFiles());
661 m_newFileMenu->checkUpToDate();
662 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
663 }
664
665 void DolphinMainWindow::createDirectory()
666 {
667 m_newFileMenu->setViewShowsHiddenFiles(activeViewContainer()->view()->showHiddenFiles());
668 m_newFileMenu->setPopupFiles(activeViewContainer()->url());
669 m_newFileMenu->createDirectory();
670 }
671
672 void DolphinMainWindow::quit()
673 {
674 close();
675 }
676
677 void DolphinMainWindow::showErrorMessage(const QString& message)
678 {
679 if (!message.isEmpty()) {
680 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
681 statusBar->setMessage(message, DolphinStatusBar::Error);
682 }
683 }
684
685 void DolphinMainWindow::slotUndoAvailable(bool available)
686 {
687 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
688 if (undoAction) {
689 undoAction->setEnabled(available);
690 }
691 }
692
693 void DolphinMainWindow::restoreClosedTab(QAction* action)
694 {
695 if (action->data().toBool()) {
696 // clear all actions except the "Empty Recently Closed Tabs"
697 // action and the separator
698 QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
699 const int count = actions.size();
700 for (int i = 2; i < count; ++i) {
701 m_recentTabsMenu->menu()->removeAction(actions.at(i));
702 }
703 } else {
704 const ClosedTab closedTab = action->data().value<ClosedTab>();
705 openNewTab(closedTab.primaryUrl);
706 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
707
708 if (closedTab.isSplit) {
709 // create secondary view
710 toggleSplitView();
711 m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
712 }
713
714 m_recentTabsMenu->removeAction(action);
715 }
716
717 if (m_recentTabsMenu->menu()->actions().count() == 2) {
718 m_recentTabsMenu->setEnabled(false);
719 }
720 }
721
722 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
723 {
724 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
725 if (undoAction) {
726 undoAction->setText(text);
727 }
728 }
729
730 void DolphinMainWindow::undo()
731 {
732 clearStatusBar();
733 KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
734 KIO::FileUndoManager::self()->undo();
735 }
736
737 void DolphinMainWindow::cut()
738 {
739 m_activeViewContainer->view()->cutSelectedItems();
740 }
741
742 void DolphinMainWindow::copy()
743 {
744 m_activeViewContainer->view()->copySelectedItems();
745 }
746
747 void DolphinMainWindow::paste()
748 {
749 m_activeViewContainer->view()->paste();
750 }
751
752 void DolphinMainWindow::find()
753 {
754 m_activeViewContainer->setSearchModeEnabled(true);
755 }
756
757 void DolphinMainWindow::updatePasteAction()
758 {
759 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
760 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
761 pasteAction->setEnabled(pasteInfo.first);
762 pasteAction->setText(pasteInfo.second);
763 }
764
765 void DolphinMainWindow::selectAll()
766 {
767 clearStatusBar();
768
769 // if the URL navigator is editable and focused, select the whole
770 // URL instead of all items of the view
771
772 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
773 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit(); // krazy:exclude=qclasses
774 const bool selectUrl = urlNavigator->isUrlEditable() &&
775 lineEdit->hasFocus();
776 if (selectUrl) {
777 lineEdit->selectAll();
778 } else {
779 m_activeViewContainer->view()->selectAll();
780 }
781 }
782
783 void DolphinMainWindow::invertSelection()
784 {
785 clearStatusBar();
786 m_activeViewContainer->view()->invertSelection();
787 }
788
789 void DolphinMainWindow::toggleSplitView()
790 {
791 if (!m_viewTab[m_tabIndex].secondaryView) {
792 createSecondaryView(m_tabIndex);
793 setActiveViewContainer(m_viewTab[m_tabIndex].secondaryView);
794 } else if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
795 // remove secondary view
796 m_viewTab[m_tabIndex].secondaryView->close();
797 m_viewTab[m_tabIndex].secondaryView->deleteLater();
798 m_viewTab[m_tabIndex].secondaryView = 0;
799
800 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
801 } else {
802 // The primary view is active and should be closed. Hence from a users point of view
803 // the content of the secondary view should be moved to the primary view.
804 // From an implementation point of view it is more efficient to close
805 // the primary view and exchange the internal pointers afterwards.
806
807 m_viewTab[m_tabIndex].primaryView->close();
808 m_viewTab[m_tabIndex].primaryView->deleteLater();
809 m_viewTab[m_tabIndex].primaryView = m_viewTab[m_tabIndex].secondaryView;
810 m_viewTab[m_tabIndex].secondaryView = 0;
811
812 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
813 }
814
815 updateViewActions();
816 }
817
818 void DolphinMainWindow::reloadView()
819 {
820 clearStatusBar();
821 m_activeViewContainer->view()->reload();
822 }
823
824 void DolphinMainWindow::stopLoading()
825 {
826 m_activeViewContainer->view()->stopLoading();
827 }
828
829 void DolphinMainWindow::enableStopAction()
830 {
831 actionCollection()->action("stop")->setEnabled(true);
832 }
833
834 void DolphinMainWindow::disableStopAction()
835 {
836 actionCollection()->action("stop")->setEnabled(false);
837 }
838
839 void DolphinMainWindow::showFilterBar()
840 {
841 m_activeViewContainer->setFilterBarVisible(true);
842 }
843
844 void DolphinMainWindow::toggleEditLocation()
845 {
846 clearStatusBar();
847
848 QAction* action = actionCollection()->action("editable_location");
849 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
850 urlNavigator->setUrlEditable(action->isChecked());
851 }
852
853 void DolphinMainWindow::replaceLocation()
854 {
855 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
856 navigator->setUrlEditable(true);
857 navigator->setFocus();
858
859 // select the whole text of the combo box editor
860 QLineEdit* lineEdit = navigator->editor()->lineEdit(); // krazy:exclude=qclasses
861 lineEdit->selectAll();
862 }
863
864 void DolphinMainWindow::togglePanelLockState()
865 {
866 GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
867
868 const bool newLockState = !generalSettings->lockPanels();
869 foreach (QObject* child, children()) {
870 DolphinDockWidget* dock = qobject_cast<DolphinDockWidget*>(child);
871 if (dock) {
872 dock->setLocked(newLockState);
873 }
874 }
875
876 generalSettings->setLockPanels(newLockState);
877 }
878
879 void DolphinMainWindow::slotPlacesPanelVisibilityChanged(bool visible)
880 {
881 const int tabCount = m_viewTab.count();
882 for (int i = 0; i < tabCount; ++i) {
883 ViewTab& tab = m_viewTab[i];
884 Q_ASSERT(tab.primaryView);
885 tab.primaryView->urlNavigator()->setPlacesSelectorVisible(!visible);
886 if (tab.secondaryView) {
887 tab.secondaryView->urlNavigator()->setPlacesSelectorVisible(!visible);
888 }
889 }
890 }
891
892 void DolphinMainWindow::goBack()
893 {
894 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
895 urlNavigator->goBack();
896
897 if (urlNavigator->locationState().isEmpty()) {
898 // An empty location state indicates a redirection URL,
899 // which must be skipped too
900 urlNavigator->goBack();
901 }
902 }
903
904 void DolphinMainWindow::goForward()
905 {
906 m_activeViewContainer->urlNavigator()->goForward();
907 }
908
909 void DolphinMainWindow::goUp()
910 {
911 m_activeViewContainer->urlNavigator()->goUp();
912 }
913
914 void DolphinMainWindow::goHome()
915 {
916 m_activeViewContainer->urlNavigator()->goHome();
917 }
918
919 void DolphinMainWindow::goBack(Qt::MouseButtons buttons)
920 {
921 // The default case (left button pressed) is handled in goBack().
922 if (buttons == Qt::MidButton) {
923 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
924 const int index = urlNavigator->historyIndex() + 1;
925 openNewTab(urlNavigator->locationUrl(index));
926 }
927 }
928
929 void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
930 {
931 // The default case (left button pressed) is handled in goForward().
932 if (buttons == Qt::MidButton) {
933 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
934 const int index = urlNavigator->historyIndex() - 1;
935 openNewTab(urlNavigator->locationUrl(index));
936 }
937 }
938
939 void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
940 {
941 // The default case (left button pressed) is handled in goUp().
942 if (buttons == Qt::MidButton) {
943 openNewTab(activeViewContainer()->url().upUrl());
944 }
945 }
946
947 void DolphinMainWindow::compareFiles()
948 {
949 // The method is only invoked if exactly 2 files have
950 // been selected. The selected files may be:
951 // - both in the primary view
952 // - both in the secondary view
953 // - one in the primary view and the other in the secondary
954 // view
955 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
956
957 KUrl urlA;
958 KUrl urlB;
959
960 KFileItemList items = m_viewTab[m_tabIndex].primaryView->view()->selectedItems();
961
962 switch (items.count()) {
963 case 0: {
964 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView);
965 items = m_viewTab[m_tabIndex].secondaryView->view()->selectedItems();
966 Q_ASSERT(items.count() == 2);
967 urlA = items[0].url();
968 urlB = items[1].url();
969 break;
970 }
971
972 case 1: {
973 urlA = items[0].url();
974 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView);
975 items = m_viewTab[m_tabIndex].secondaryView->view()->selectedItems();
976 Q_ASSERT(items.count() == 1);
977 urlB = items[0].url();
978 break;
979 }
980
981 case 2: {
982 urlA = items[0].url();
983 urlB = items[1].url();
984 break;
985 }
986
987 default: {
988 // may not happen: compareFiles may only get invoked if 2
989 // files are selected
990 Q_ASSERT(false);
991 }
992 }
993
994 QString command("kompare -c \"");
995 command.append(urlA.pathOrUrl());
996 command.append("\" \"");
997 command.append(urlB.pathOrUrl());
998 command.append('\"');
999 KRun::runCommand(command, "Kompare", "kompare", this);
1000 }
1001
1002 void DolphinMainWindow::toggleShowMenuBar()
1003 {
1004 const bool visible = menuBar()->isVisible();
1005 menuBar()->setVisible(!visible);
1006 if (visible) {
1007 createToolBarMenuButton();
1008 } else {
1009 deleteToolBarMenuButton();
1010 }
1011 }
1012
1013 void DolphinMainWindow::openTerminal()
1014 {
1015 QString dir(QDir::homePath());
1016
1017 // If the given directory is not local, it can still be the URL of an
1018 // ioslave using UDS_LOCAL_PATH which to be converted first.
1019 KUrl url = KIO::NetAccess::mostLocalUrl(m_activeViewContainer->url(), this);
1020
1021 //If the URL is local after the above conversion, set the directory.
1022 if (url.isLocalFile()) {
1023 dir = url.toLocalFile();
1024 }
1025
1026 KToolInvocation::invokeTerminal(QString(), dir);
1027 }
1028
1029 void DolphinMainWindow::editSettings()
1030 {
1031 if (!m_settingsDialog) {
1032 const KUrl url = activeViewContainer()->url();
1033 m_settingsDialog = new DolphinSettingsDialog(url, this);
1034 m_settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
1035 m_settingsDialog->show();
1036 } else {
1037 m_settingsDialog->raise();
1038 }
1039 }
1040
1041 void DolphinMainWindow::setActiveTab(int index)
1042 {
1043 Q_ASSERT(index >= 0);
1044 Q_ASSERT(index < m_viewTab.count());
1045 if (index == m_tabIndex) {
1046 return;
1047 }
1048
1049 // hide current tab content
1050 ViewTab& hiddenTab = m_viewTab[m_tabIndex];
1051 hiddenTab.isPrimaryViewActive = hiddenTab.primaryView->isActive();
1052 hiddenTab.primaryView->setActive(false);
1053 if (hiddenTab.secondaryView) {
1054 hiddenTab.secondaryView->setActive(false);
1055 }
1056 QSplitter* splitter = m_viewTab[m_tabIndex].splitter;
1057 splitter->hide();
1058 m_centralWidgetLayout->removeWidget(splitter);
1059
1060 // show active tab content
1061 m_tabIndex = index;
1062
1063 ViewTab& viewTab = m_viewTab[index];
1064 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
1065 viewTab.primaryView->show();
1066 if (viewTab.secondaryView) {
1067 viewTab.secondaryView->show();
1068 }
1069 viewTab.splitter->show();
1070
1071 setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
1072 viewTab.secondaryView);
1073 }
1074
1075 void DolphinMainWindow::closeTab()
1076 {
1077 closeTab(m_tabBar->currentIndex());
1078 }
1079
1080 void DolphinMainWindow::closeTab(int index)
1081 {
1082 Q_ASSERT(index >= 0);
1083 Q_ASSERT(index < m_viewTab.count());
1084 if (m_viewTab.count() == 1) {
1085 // the last tab may never get closed
1086 return;
1087 }
1088
1089 if (index == m_tabIndex) {
1090 // The tab that should be closed is the active tab. Activate the
1091 // previous tab before closing the tab.
1092 m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
1093 }
1094 rememberClosedTab(index);
1095
1096 // delete tab
1097 m_viewTab[index].primaryView->deleteLater();
1098 if (m_viewTab[index].secondaryView) {
1099 m_viewTab[index].secondaryView->deleteLater();
1100 }
1101 m_viewTab[index].splitter->deleteLater();
1102 m_viewTab.erase(m_viewTab.begin() + index);
1103
1104 m_tabBar->blockSignals(true);
1105 m_tabBar->removeTab(index);
1106
1107 if (m_tabIndex > index) {
1108 m_tabIndex--;
1109 Q_ASSERT(m_tabIndex >= 0);
1110 }
1111
1112 // if only one tab is left, also remove the tab entry so that
1113 // closing the last tab is not possible
1114 if (m_viewTab.count() == 1) {
1115 m_tabBar->removeTab(0);
1116 actionCollection()->action("close_tab")->setEnabled(false);
1117 } else {
1118 m_tabBar->blockSignals(false);
1119 }
1120 }
1121
1122 void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
1123 {
1124 KMenu menu(this);
1125
1126 QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
1127 newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
1128
1129 QAction* detachTabAction = menu.addAction(KIcon("tab-detach"), i18nc("@action:inmenu", "Detach Tab"));
1130
1131 QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
1132
1133 QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
1134 closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
1135 QAction* selectedAction = menu.exec(pos);
1136 if (selectedAction == newTabAction) {
1137 const ViewTab& tab = m_viewTab[index];
1138 Q_ASSERT(tab.primaryView);
1139 const KUrl url = tab.secondaryView && tab.secondaryView->isActive() ?
1140 tab.secondaryView->url() : tab.primaryView->url();
1141 openNewTab(url);
1142 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
1143 } else if (selectedAction == detachTabAction) {
1144 const ViewTab& tab = m_viewTab[index];
1145 Q_ASSERT(tab.primaryView);
1146 const KUrl primaryUrl = tab.primaryView->url();
1147 DolphinMainWindow* window = DolphinApplication::app()->createMainWindow();
1148 window->changeUrl(primaryUrl);
1149
1150 if (tab.secondaryView) {
1151 const KUrl secondaryUrl = tab.secondaryView->url();
1152 if (!window->m_viewTab[0].secondaryView) {
1153 window->toggleSplitView();
1154 }
1155 window->m_viewTab[0].secondaryView->setUrl(secondaryUrl);
1156 if (tab.primaryView->isActive()) {
1157 window->m_viewTab[0].primaryView->setActive(true);
1158 } else {
1159 window->m_viewTab[0].secondaryView->setActive(true);
1160 }
1161 }
1162 window->show();
1163 closeTab(index);
1164 } else if (selectedAction == closeOtherTabsAction) {
1165 const int count = m_tabBar->count();
1166 for (int i = 0; i < index; ++i) {
1167 closeTab(0);
1168 }
1169 for (int i = index + 1; i < count; ++i) {
1170 closeTab(1);
1171 }
1172 } else if (selectedAction == closeTabAction) {
1173 closeTab(index);
1174 }
1175 }
1176
1177 void DolphinMainWindow::slotTabMoved(int from, int to)
1178 {
1179 m_viewTab.move(from, to);
1180 m_tabIndex = m_tabBar->currentIndex();
1181 }
1182
1183 void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
1184 {
1185 if (buttons & Qt::MidButton) {
1186 openNewTab(url);
1187 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
1188 } else {
1189 changeUrl(url);
1190 }
1191 }
1192
1193 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
1194 {
1195 canDecode = KUrl::List::canDecode(event->mimeData());
1196 }
1197
1198 void DolphinMainWindow::handleUrl(const KUrl& url)
1199 {
1200 delete m_lastHandleUrlStatJob;
1201 m_lastHandleUrlStatJob = 0;
1202
1203 if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir()) {
1204 activeViewContainer()->setUrl(url);
1205 } else if (KProtocolManager::supportsListing(url)) {
1206 // stat the URL to see if it is a dir or not
1207 m_lastHandleUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
1208 connect(m_lastHandleUrlStatJob, SIGNAL(result(KJob*)),
1209 this, SLOT(slotHandleUrlStatFinished(KJob*)));
1210
1211 } else {
1212 new KRun(url, this);
1213 }
1214 }
1215
1216 void DolphinMainWindow::slotHandleUrlStatFinished(KJob* job)
1217 {
1218 m_lastHandleUrlStatJob = 0;
1219 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
1220 const KUrl url = static_cast<KIO::StatJob*>(job)->url();
1221 if (entry.isDir()) {
1222 activeViewContainer()->setUrl(url);
1223 } else {
1224 new KRun(url, this);
1225 }
1226 }
1227
1228 void DolphinMainWindow::tabDropEvent(int tab, QDropEvent* event)
1229 {
1230 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
1231 if (!urls.isEmpty() && tab != -1) {
1232 const ViewTab& viewTab = m_viewTab[tab];
1233 const KUrl destPath = viewTab.isPrimaryViewActive ? viewTab.primaryView->url() : viewTab.secondaryView->url();
1234 DragAndDropHelper::instance().dropUrls(KFileItem(), destPath, event, m_tabBar);
1235 }
1236 }
1237
1238 void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
1239 {
1240 newFileMenu()->setEnabled(isFolderWritable);
1241 }
1242
1243 void DolphinMainWindow::slotSearchModeChanged(bool enabled)
1244 {
1245 #ifdef HAVE_NEPOMUK
1246 const KUrl url = m_activeViewContainer->url();
1247 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
1248 if (!searchInfo.isIndexingEnabled() || !searchInfo.isPathIndexed(url)) {
1249 return;
1250 }
1251
1252 QDockWidget* searchDock = findChild<QDockWidget*>("searchDock");
1253 if (!searchDock) {
1254 return;
1255 }
1256
1257 if (enabled) {
1258 if (!searchDock->isVisible()) {
1259 m_searchDockIsTemporaryVisible = true;
1260 }
1261 searchDock->show();
1262 } else {
1263 if (searchDock->isVisible() && m_searchDockIsTemporaryVisible) {
1264 searchDock->hide();
1265 }
1266 m_searchDockIsTemporaryVisible = false;
1267 }
1268
1269 SearchPanel* searchPanel = qobject_cast<SearchPanel*>(searchDock->widget());
1270 if (searchPanel) {
1271 // Per default any search-operation triggered by the Search Panel is done
1272 // "Everywhere".
1273 SearchPanel::SearchMode searchMode = SearchPanel::Everywhere;
1274
1275 if (enabled && (SearchSettings::location() == QLatin1String("FromHere"))) {
1276 // Only if the search-mode is enabled it is visible for the user whether
1277 // a searching is done "Everywhere" or "From Here" (= current directory).
1278 searchMode = SearchPanel::FromCurrentDir;
1279 }
1280 searchPanel->setSearchMode(searchMode);
1281 }
1282 #else
1283 Q_UNUSED(enabled);
1284 #endif
1285 }
1286
1287 void DolphinMainWindow::openContextMenu(const KFileItem& item,
1288 const KUrl& url,
1289 const QList<QAction*>& customActions)
1290 {
1291 QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, item, url);
1292 contextMenu->setCustomActions(customActions);
1293 const DolphinContextMenu::Command command = contextMenu->open();
1294
1295 switch (command) {
1296 case DolphinContextMenu::OpenParentFolderInNewWindow: {
1297 DolphinMainWindow* window = DolphinApplication::app()->createMainWindow();
1298 window->changeUrl(item.url().upUrl());
1299 window->show();
1300 break;
1301 }
1302
1303 case DolphinContextMenu::OpenParentFolderInNewTab:
1304 openNewTab(item.url().upUrl());
1305 break;
1306
1307 case DolphinContextMenu::None:
1308 default:
1309 break;
1310 }
1311
1312 delete contextMenu;
1313 }
1314
1315 void DolphinMainWindow::openToolBarMenu()
1316 {
1317 const int height = m_openToolBarMenuButton->height();
1318 const QPoint pos = m_openToolBarMenuButton->mapToGlobal(QPoint(0, height));
1319
1320 m_toolBarMenu = new KMenu(m_openToolBarMenuButton);
1321 m_toolBarMenu.data()->setAttribute(Qt::WA_DeleteOnClose);
1322 connect(m_toolBarMenu.data(), SIGNAL(aboutToShow()), this, SLOT(updateToolBarMenu()));
1323
1324 m_toolBarMenu.data()->exec(pos);
1325 }
1326
1327 void DolphinMainWindow::updateToolBarMenu()
1328 {
1329 KMenu* menu = m_toolBarMenu.data();
1330 if (!menu) {
1331 return;
1332 }
1333
1334 const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
1335
1336 KActionCollection* ac = actionCollection();
1337
1338 // Add "Edit" actions
1339 bool added = addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Undo)), menu) |
1340 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Find)), menu) |
1341 addActionToMenu(ac->action("select_all"), menu) |
1342 addActionToMenu(ac->action("invert_selection"), menu);
1343
1344 if (added) {
1345 menu->addSeparator();
1346 }
1347
1348 // Add "View" actions
1349 if (!generalSettings->showZoomSlider()) {
1350 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomIn)), menu);
1351 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomOut)), menu);
1352 menu->addSeparator();
1353 }
1354
1355 added = addActionToMenu(ac->action("view_mode"), menu) |
1356 addActionToMenu(ac->action("sort"), menu) |
1357 addActionToMenu(ac->action("additional_info"), menu) |
1358 addActionToMenu(ac->action("show_preview"), menu) |
1359 addActionToMenu(ac->action("show_in_groups"), menu) |
1360 addActionToMenu(ac->action("show_hidden_files"), menu);
1361
1362 if (added) {
1363 menu->addSeparator();
1364 }
1365
1366 added = addActionToMenu(ac->action("split_view"), menu) |
1367 addActionToMenu(ac->action("reload"), menu) |
1368 addActionToMenu(ac->action("view_properties"), menu);
1369 if (added) {
1370 menu->addSeparator();
1371 }
1372
1373 addActionToMenu(ac->action("panels"), menu);
1374 KMenu* locationBarMenu = new KMenu(i18nc("@action:inmenu", "Location Bar"), menu);
1375 locationBarMenu->addAction(ac->action("editable_location"));
1376 locationBarMenu->addAction(ac->action("replace_location"));
1377 menu->addMenu(locationBarMenu);
1378
1379 menu->addSeparator();
1380
1381 // Add "Go" menu
1382 KMenu* goMenu = new KMenu(i18nc("@action:inmenu", "Go"), menu);
1383 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Back)));
1384 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Forward)));
1385 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Up)));
1386 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Home)));
1387 goMenu->addAction(ac->action("closed_tabs"));
1388 menu->addMenu(goMenu);
1389
1390 // Add "Tool" menu
1391 KMenu* toolsMenu = new KMenu(i18nc("@action:inmenu", "Tools"), menu);
1392 toolsMenu->addAction(ac->action("show_filter_bar"));
1393 toolsMenu->addAction(ac->action("compare_files"));
1394 toolsMenu->addAction(ac->action("open_terminal"));
1395 toolsMenu->addAction(ac->action("change_remote_encoding"));
1396 menu->addMenu(toolsMenu);
1397
1398 // Add "Settings" menu entries
1399 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::KeyBindings)), menu);
1400 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ConfigureToolbars)), menu);
1401 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Preferences)), menu);
1402
1403 // Add "Help" menu
1404 KMenu* helpMenu = new KMenu(i18nc("@action:inmenu", "Help"), menu);
1405 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::HelpContents)));
1406 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::WhatsThis)));
1407 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutApp)));
1408 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutKDE)));
1409 menu->addMenu(helpMenu);
1410
1411 menu->addSeparator();
1412 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ShowMenubar)), menu);
1413 }
1414
1415 void DolphinMainWindow::updateToolBar()
1416 {
1417 if (!menuBar()->isVisible()) {
1418 createToolBarMenuButton();
1419 }
1420 }
1421
1422 void DolphinMainWindow::slotToolBarSpacerDeleted()
1423 {
1424 m_toolBarSpacer = 0;
1425 m_updateToolBarTimer->start();
1426 }
1427
1428 void DolphinMainWindow::slotToolBarMenuButtonDeleted()
1429 {
1430 m_openToolBarMenuButton = 0;
1431 m_updateToolBarTimer->start();
1432 }
1433
1434 void DolphinMainWindow::slotToolBarIconSizeChanged(const QSize& iconSize)
1435 {
1436 if (m_openToolBarMenuButton) {
1437 m_openToolBarMenuButton->setIconSize(iconSize);
1438 }
1439 }
1440
1441 void DolphinMainWindow::init()
1442 {
1443 DolphinSettings& settings = DolphinSettings::instance();
1444
1445 // Check whether Dolphin runs the first time. If yes then
1446 // a proper default window size is given at the end of DolphinMainWindow::init().
1447 GeneralSettings* generalSettings = settings.generalSettings();
1448 const bool firstRun = generalSettings->firstRun();
1449 if (firstRun) {
1450 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1451 }
1452
1453 setAcceptDrops(true);
1454
1455 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1456 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1457
1458 setupActions();
1459
1460 const KUrl homeUrl(generalSettings->homeUrl());
1461 setUrlAsCaption(homeUrl);
1462 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1463 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1464 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1465
1466 m_viewTab[m_tabIndex].primaryView = createViewContainer(homeUrl, m_viewTab[m_tabIndex].splitter);
1467
1468 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1469 connectViewSignals(m_activeViewContainer);
1470 DolphinView* view = m_activeViewContainer->view();
1471 m_activeViewContainer->show();
1472 m_actionHandler->setCurrentView(view);
1473
1474 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1475 connect(this, SIGNAL(urlChanged(const KUrl&)),
1476 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1477
1478 m_tabBar = new KTabBar(this);
1479 m_tabBar->setMovable(true);
1480 m_tabBar->setTabsClosable(true);
1481 connect(m_tabBar, SIGNAL(currentChanged(int)),
1482 this, SLOT(setActiveTab(int)));
1483 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1484 this, SLOT(closeTab(int)));
1485 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1486 this, SLOT(openTabContextMenu(int, const QPoint&)));
1487 connect(m_tabBar, SIGNAL(newTabRequest()),
1488 this, SLOT(openNewTab()));
1489 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1490 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1491 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1492 this, SLOT(closeTab(int)));
1493 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1494 this, SLOT(slotTabMoved(int, int)));
1495 connect(m_tabBar, SIGNAL(receivedDropEvent(int, QDropEvent*)),
1496 this, SLOT(tabDropEvent(int, QDropEvent*)));
1497
1498 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1499
1500 QWidget* centralWidget = new QWidget(this);
1501 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1502 m_centralWidgetLayout->setSpacing(0);
1503 m_centralWidgetLayout->setMargin(0);
1504 m_centralWidgetLayout->addWidget(m_tabBar);
1505 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1506
1507 setCentralWidget(centralWidget);
1508 setupDockWidgets();
1509 emit urlChanged(homeUrl);
1510
1511 setupGUI(Keys | Save | Create | ToolBar);
1512 stateChanged("new_file");
1513
1514 QClipboard* clipboard = QApplication::clipboard();
1515 connect(clipboard, SIGNAL(dataChanged()),
1516 this, SLOT(updatePasteAction()));
1517
1518 if (generalSettings->splitView()) {
1519 toggleSplitView();
1520 }
1521 updateEditActions();
1522 updateViewActions();
1523 updateGoActions();
1524
1525 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1526 showFilterBarAction->setChecked(generalSettings->filterBar());
1527
1528 if (firstRun) {
1529 menuBar()->setVisible(false);
1530 // Assure a proper default size if Dolphin runs the first time
1531 resize(750, 500);
1532 }
1533
1534 const bool showMenu = !menuBar()->isHidden();
1535 QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
1536 showMenuBarAction->setChecked(showMenu); // workaround for bug #171080
1537 if (!showMenu) {
1538 createToolBarMenuButton();
1539 }
1540 }
1541
1542 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1543 {
1544 Q_ASSERT(viewContainer);
1545 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1546 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1547 if (m_activeViewContainer == viewContainer) {
1548 return;
1549 }
1550
1551 m_activeViewContainer->setActive(false);
1552 m_activeViewContainer = viewContainer;
1553
1554 // Activating the view container might trigger a recursive setActiveViewContainer() call
1555 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1556 // disconnect the activated() signal in this case:
1557 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1558 m_activeViewContainer->setActive(true);
1559 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1560
1561 m_actionHandler->setCurrentView(viewContainer->view());
1562
1563 updateHistory();
1564 updateEditActions();
1565 updateViewActions();
1566 updateGoActions();
1567
1568 const KUrl url = m_activeViewContainer->url();
1569 setUrlAsCaption(url);
1570 if (m_viewTab.count() > 1) {
1571 m_tabBar->setTabText(m_tabIndex, tabName(url));
1572 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1573 }
1574
1575 emit urlChanged(url);
1576 }
1577
1578 DolphinViewContainer* DolphinMainWindow::createViewContainer(const KUrl& url, QWidget* parent)
1579 {
1580 DolphinViewContainer* container = new DolphinViewContainer(url, parent);
1581
1582 // The places-selector from the URL navigator should only be shown
1583 // if the places dock is invisible
1584 QDockWidget* placesDock = findChild<QDockWidget*>("placesDock");
1585 container->urlNavigator()->setPlacesSelectorVisible(!placesDock || !placesDock->isVisible());
1586
1587 return container;
1588 }
1589
1590 void DolphinMainWindow::setupActions()
1591 {
1592 // setup 'File' menu
1593 m_newFileMenu = new DolphinNewFileMenu(this);
1594 KMenu* menu = m_newFileMenu->menu();
1595 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1596 menu->setIcon(KIcon("document-new"));
1597 connect(menu, SIGNAL(aboutToShow()),
1598 this, SLOT(updateNewMenu()));
1599
1600 KAction* newWindow = actionCollection()->addAction("new_window");
1601 newWindow->setIcon(KIcon("window-new"));
1602 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1603 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1604 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1605
1606 KAction* newTab = actionCollection()->addAction("new_tab");
1607 newTab->setIcon(KIcon("tab-new"));
1608 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1609 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1610 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1611
1612 KAction* closeTab = actionCollection()->addAction("close_tab");
1613 closeTab->setIcon(KIcon("tab-close"));
1614 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1615 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1616 closeTab->setEnabled(false);
1617 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1618
1619 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1620
1621 // setup 'Edit' menu
1622 KStandardAction::undo(this,
1623 SLOT(undo()),
1624 actionCollection());
1625
1626 // need to remove shift+del from cut action, else the shortcut for deletejob
1627 // doesn't work
1628 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1629 KShortcut cutShortcut = cut->shortcut();
1630 cutShortcut.remove(Qt::SHIFT | Qt::Key_Delete, KShortcut::KeepEmpty);
1631 cut->setShortcut(cutShortcut);
1632 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1633 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1634 // The text of the paste-action is modified dynamically by Dolphin
1635 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1636 // due to the long text, the text "Paste" is used:
1637 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1638
1639 KStandardAction::find(this, SLOT(find()), actionCollection());
1640
1641 KAction* selectAll = actionCollection()->addAction("select_all");
1642 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1643 selectAll->setShortcut(Qt::CTRL | Qt::Key_A);
1644 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1645
1646 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1647 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1648 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1649 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1650
1651 // setup 'View' menu
1652 // (note that most of it is set up in DolphinViewActionHandler)
1653
1654 KAction* split = actionCollection()->addAction("split_view");
1655 split->setShortcut(Qt::Key_F3);
1656 updateSplitAction();
1657 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1658
1659 KAction* reload = actionCollection()->addAction("reload");
1660 reload->setText(i18nc("@action:inmenu View", "Reload"));
1661 reload->setShortcut(Qt::Key_F5);
1662 reload->setIcon(KIcon("view-refresh"));
1663 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1664
1665 KAction* stop = actionCollection()->addAction("stop");
1666 stop->setText(i18nc("@action:inmenu View", "Stop"));
1667 stop->setToolTip(i18nc("@info", "Stop loading"));
1668 stop->setIcon(KIcon("process-stop"));
1669 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1670
1671 KToggleAction* editableLocation = actionCollection()->add<KToggleAction>("editable_location");
1672 editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1673 editableLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1674 connect(editableLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1675
1676 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1677 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1678 replaceLocation->setShortcut(Qt::Key_F6);
1679 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1680
1681 // setup 'Go' menu
1682 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1683 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1684 KShortcut backShortcut = backAction->shortcut();
1685 backShortcut.setAlternate(Qt::Key_Backspace);
1686 backAction->setShortcut(backShortcut);
1687
1688 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1689 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1690 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1691 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1692 this, SLOT(restoreClosedTab(QAction *)));
1693
1694 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1695 action->setIcon(KIcon("edit-clear-list"));
1696 action->setData(QVariant::fromValue(true));
1697 m_recentTabsMenu->addAction(action);
1698 m_recentTabsMenu->addSeparator();
1699 m_recentTabsMenu->setEnabled(false);
1700
1701 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1702 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1703
1704 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1705 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1706
1707 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1708
1709 // setup 'Tools' menu
1710 KAction* showFilterBar = actionCollection()->addAction("show_filter_bar");
1711 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1712 showFilterBar->setIcon(KIcon("view-filter"));
1713 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1714 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1715
1716 KAction* compareFiles = actionCollection()->addAction("compare_files");
1717 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1718 compareFiles->setIcon(KIcon("kompare"));
1719 compareFiles->setEnabled(false);
1720 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1721
1722 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1723 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1724 openTerminal->setIcon(KIcon("utilities-terminal"));
1725 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1726 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1727
1728 // setup 'Settings' menu
1729 KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1730 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1731
1732 // not in menu actions
1733 QList<QKeySequence> nextTabKeys;
1734 nextTabKeys.append(KStandardShortcut::tabNext().primary());
1735 nextTabKeys.append(QKeySequence(Qt::CTRL | Qt::Key_Tab));
1736
1737 QList<QKeySequence> prevTabKeys;
1738 prevTabKeys.append(KStandardShortcut::tabPrev().primary());
1739 prevTabKeys.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
1740
1741 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1742 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1743 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1744 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? prevTabKeys : nextTabKeys);
1745
1746 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1747 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1748 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1749 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? nextTabKeys : prevTabKeys);
1750
1751 // for context menu
1752 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1753 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1754 openInNewTab->setIcon(KIcon("tab-new"));
1755 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1756
1757 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1758 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1759 openInNewWindow->setIcon(KIcon("window-new"));
1760 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1761 }
1762
1763 void DolphinMainWindow::setupDockWidgets()
1764 {
1765 const bool lock = DolphinSettings::instance().generalSettings()->lockPanels();
1766
1767 KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>("lock_panels");
1768 lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1769 lockLayoutAction->setActiveIcon(KIcon("object-unlocked"));
1770 lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1771 lockLayoutAction->setInactiveIcon(KIcon("object-locked"));
1772 lockLayoutAction->setActive(lock);
1773 connect(lockLayoutAction, SIGNAL(triggered()), this, SLOT(togglePanelLockState()));
1774
1775 // Setup "Information"
1776 DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1777 infoDock->setLocked(lock);
1778 infoDock->setObjectName("infoDock");
1779 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1780 Panel* infoPanel = new InformationPanel(infoDock);
1781 infoPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1782 connect(infoPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1783 infoDock->setWidget(infoPanel);
1784
1785 QAction* infoAction = infoDock->toggleViewAction();
1786 infoAction->setIcon(KIcon("dialog-information"));
1787 infoAction->setShortcut(Qt::Key_F11);
1788 addActionCloneToCollection(infoAction, "show_information_panel");
1789
1790 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1791 connect(this, SIGNAL(urlChanged(KUrl)),
1792 infoPanel, SLOT(setUrl(KUrl)));
1793 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1794 infoPanel, SLOT(setSelection(KFileItemList)));
1795 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1796 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1797
1798 // Setup "Folders"
1799 DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1800 foldersDock->setLocked(lock);
1801 foldersDock->setObjectName("foldersDock");
1802 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1803 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1804 foldersPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1805 foldersDock->setWidget(foldersPanel);
1806
1807 QAction* foldersAction = foldersDock->toggleViewAction();
1808 foldersAction->setShortcut(Qt::Key_F7);
1809 foldersAction->setIcon(KIcon("folder"));
1810 addActionCloneToCollection(foldersAction, "show_folders_panel");
1811
1812 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1813 connect(this, SIGNAL(urlChanged(KUrl)),
1814 foldersPanel, SLOT(setUrl(KUrl)));
1815 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1816 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1817
1818 // Setup "Terminal"
1819 #ifndef Q_OS_WIN
1820 DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1821 terminalDock->setLocked(lock);
1822 terminalDock->setObjectName("terminalDock");
1823 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1824 Panel* terminalPanel = new TerminalPanel(terminalDock);
1825 terminalPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1826 terminalDock->setWidget(terminalPanel);
1827
1828 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1829
1830 QAction* terminalAction = terminalDock->toggleViewAction();
1831 terminalAction->setShortcut(Qt::Key_F4);
1832 terminalAction->setIcon(KIcon("utilities-terminal"));
1833 addActionCloneToCollection(terminalAction, "show_terminal_panel");
1834
1835 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1836 connect(this, SIGNAL(urlChanged(KUrl)),
1837 terminalPanel, SLOT(setUrl(KUrl)));
1838 #endif
1839
1840 // Setup "Search"
1841 #ifdef HAVE_NEPOMUK
1842 DolphinDockWidget* searchDock = new DolphinDockWidget(i18nc("@title:window", "Search"));
1843 searchDock->setLocked(lock);
1844 searchDock->setObjectName("searchDock");
1845 searchDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1846 Panel* searchPanel = new SearchPanel(searchDock);
1847 searchPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1848 connect(searchPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1849 searchDock->setWidget(searchPanel);
1850
1851 QAction* searchAction = searchDock->toggleViewAction();
1852 searchAction->setShortcut(Qt::Key_F12);
1853 searchAction->setIcon(KIcon("system-search"));
1854 addActionCloneToCollection(searchAction, "show_search_panel");
1855 addDockWidget(Qt::RightDockWidgetArea, searchDock);
1856 connect(this, SIGNAL(urlChanged(KUrl)),
1857 searchPanel, SLOT(setUrl(KUrl)));
1858 #endif
1859
1860 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1861 if (firstRun) {
1862 infoDock->hide();
1863 foldersDock->hide();
1864 #ifndef Q_OS_WIN
1865 terminalDock->hide();
1866 #endif
1867 #ifdef HAVE_NEPOMUK
1868 searchDock->hide();
1869 #endif
1870 }
1871
1872 // Setup "Places"
1873 DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1874 placesDock->setLocked(lock);
1875 placesDock->setObjectName("placesDock");
1876 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1877
1878 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1879 QAction* separator = new QAction(placesPanel);
1880 separator->setSeparator(true);
1881 QList<QAction*> placesActions;
1882 placesActions.append(separator);
1883 placesActions.append(lockLayoutAction);
1884 placesPanel->addActions(placesActions);
1885 placesPanel->setModel(DolphinSettings::instance().placesModel());
1886 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1887 placesDock->setWidget(placesPanel);
1888
1889 QAction* placesAction = placesDock->toggleViewAction();
1890 placesAction->setShortcut(Qt::Key_F9);
1891 placesAction->setIcon(KIcon("bookmarks"));
1892 addActionCloneToCollection(placesAction, "show_places_panel");
1893
1894 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1895 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1896 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1897 connect(this, SIGNAL(urlChanged(KUrl)),
1898 placesPanel, SLOT(setUrl(KUrl)));
1899 connect(placesDock, SIGNAL(visibilityChanged(bool)),
1900 this, SLOT(slotPlacesPanelVisibilityChanged(bool)));
1901
1902 // Add actions into the "Panels" menu
1903 KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
1904 actionCollection()->addAction("panels", panelsMenu);
1905 panelsMenu->setDelayed(false);
1906 panelsMenu->addAction(placesAction);
1907 panelsMenu->addAction(infoAction);
1908 panelsMenu->addAction(foldersAction);
1909 #ifndef Q_OS_WIN
1910 panelsMenu->addAction(terminalAction);
1911 #endif
1912 #ifdef HAVE_NEPOMUK
1913 panelsMenu->addAction(searchAction);
1914 #endif
1915 panelsMenu->addSeparator();
1916 panelsMenu->addAction(lockLayoutAction);
1917 }
1918
1919 void DolphinMainWindow::updateEditActions()
1920 {
1921 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1922 if (list.isEmpty()) {
1923 stateChanged("has_no_selection");
1924 } else {
1925 stateChanged("has_selection");
1926
1927 KActionCollection* col = actionCollection();
1928 QAction* renameAction = col->action("rename");
1929 QAction* moveToTrashAction = col->action("move_to_trash");
1930 QAction* deleteAction = col->action("delete");
1931 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1932 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1933
1934 KFileItemListProperties capabilities(list);
1935 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1936
1937 renameAction->setEnabled(capabilities.supportsMoving());
1938 moveToTrashAction->setEnabled(enableMoveToTrash);
1939 deleteAction->setEnabled(capabilities.supportsDeleting());
1940 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1941 cutAction->setEnabled(capabilities.supportsMoving());
1942 }
1943 updatePasteAction();
1944 }
1945
1946 void DolphinMainWindow::updateViewActions()
1947 {
1948 m_actionHandler->updateViewActions();
1949
1950 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1951 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1952
1953 updateSplitAction();
1954
1955 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1956 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1957 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1958 }
1959
1960 void DolphinMainWindow::updateGoActions()
1961 {
1962 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1963 const KUrl currentUrl = m_activeViewContainer->url();
1964 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1965 }
1966
1967 void DolphinMainWindow::createToolBarMenuButton()
1968 {
1969 if (m_toolBarSpacer && m_openToolBarMenuButton) {
1970 return;
1971 }
1972 Q_ASSERT(!m_toolBarSpacer);
1973 Q_ASSERT(!m_openToolBarMenuButton);
1974
1975 m_toolBarSpacer = new QWidget(this);
1976 m_toolBarSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
1977
1978 m_openToolBarMenuButton = new QToolButton(this);
1979 m_openToolBarMenuButton->setIcon(KIcon("configure"));
1980
1981 // Instead of using QPushButton::setMenu() the opening of the menu is done manually
1982 // to prevent the "clutter" of the down-arrow drawn by the style.
1983 connect(m_openToolBarMenuButton, SIGNAL(clicked()), this, SLOT(openToolBarMenu()));
1984
1985 toolBar()->addWidget(m_toolBarSpacer);
1986 toolBar()->addWidget(m_openToolBarMenuButton);
1987 connect(toolBar(), SIGNAL(iconSizeChanged(QSize)), this, SLOT(slotToolBarIconSizeChanged(QSize)));
1988
1989 // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
1990 // gets edited. In this case we must add them again. The adding is done asynchronously by
1991 // m_updateToolBarTimer.
1992 connect(m_toolBarSpacer, SIGNAL(destroyed()), this, SLOT(slotToolBarSpacerDeleted()));
1993 connect(m_openToolBarMenuButton, SIGNAL(destroyed()), this, SLOT(slotToolBarMenuButtonDeleted()));
1994 m_updateToolBarTimer = new QTimer(this);
1995 m_updateToolBarTimer->setInterval(500);
1996 connect(m_updateToolBarTimer, SIGNAL(timeout()), this, SLOT(updateToolBar()));
1997 }
1998
1999 void DolphinMainWindow::deleteToolBarMenuButton()
2000 {
2001 delete m_toolBarSpacer;
2002 m_toolBarSpacer = 0;
2003
2004 delete m_openToolBarMenuButton;
2005 m_openToolBarMenuButton = 0;
2006
2007 delete m_updateToolBarTimer;
2008 m_updateToolBarTimer = 0;
2009 }
2010
2011 bool DolphinMainWindow::addActionToMenu(QAction* action, KMenu* menu)
2012 {
2013 Q_ASSERT(action);
2014 Q_ASSERT(menu);
2015
2016 const KToolBar* toolBarWidget = toolBar();
2017 foreach (const QWidget* widget, action->associatedWidgets()) {
2018 if (widget == toolBarWidget) {
2019 return false;
2020 }
2021 }
2022
2023 menu->addAction(action);
2024 return true;
2025 }
2026
2027 void DolphinMainWindow::rememberClosedTab(int index)
2028 {
2029 KMenu* tabsMenu = m_recentTabsMenu->menu();
2030
2031 const QString primaryPath = m_viewTab[index].primaryView->url().path();
2032 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
2033
2034 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
2035
2036 ClosedTab closedTab;
2037 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
2038
2039 if (m_viewTab[index].secondaryView) {
2040 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
2041 closedTab.isSplit = true;
2042 } else {
2043 closedTab.isSplit = false;
2044 }
2045
2046 action->setData(QVariant::fromValue(closedTab));
2047 action->setIcon(KIcon(iconName));
2048
2049 // add the closed tab menu entry after the separator and
2050 // "Empty Recently Closed Tabs" entry
2051 if (tabsMenu->actions().size() == 2) {
2052 tabsMenu->addAction(action);
2053 } else {
2054 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
2055 }
2056
2057 // assure that only up to 8 closed tabs are shown in the menu
2058 if (tabsMenu->actions().size() > 8) {
2059 tabsMenu->removeAction(tabsMenu->actions().last());
2060 }
2061 actionCollection()->action("closed_tabs")->setEnabled(true);
2062 KAcceleratorManager::manage(tabsMenu);
2063 }
2064
2065 void DolphinMainWindow::clearStatusBar()
2066 {
2067 m_activeViewContainer->statusBar()->clear();
2068 }
2069
2070 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
2071 {
2072 connect(container, SIGNAL(showFilterBarChanged(bool)),
2073 this, SLOT(updateFilterBarAction(bool)));
2074 connect(container, SIGNAL(writeStateChanged(bool)),
2075 this, SLOT(slotWriteStateChanged(bool)));
2076 connect(container, SIGNAL(searchModeChanged(bool)),
2077 this, SLOT(slotSearchModeChanged(bool)));
2078
2079 DolphinView* view = container->view();
2080 connect(view, SIGNAL(selectionChanged(KFileItemList)),
2081 this, SLOT(slotSelectionChanged(KFileItemList)));
2082 connect(view, SIGNAL(requestItemInfo(KFileItem)),
2083 this, SLOT(slotRequestItemInfo(KFileItem)));
2084 connect(view, SIGNAL(activated()),
2085 this, SLOT(toggleActiveView()));
2086 connect(view, SIGNAL(tabRequested(const KUrl&)),
2087 this, SLOT(openNewTab(const KUrl&)));
2088 connect(view, SIGNAL(requestContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)),
2089 this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)));
2090 connect(view, SIGNAL(startedPathLoading(KUrl)),
2091 this, SLOT(enableStopAction()));
2092 connect(view, SIGNAL(finishedPathLoading(KUrl)),
2093 this, SLOT(disableStopAction()));
2094
2095 const KUrlNavigator* navigator = container->urlNavigator();
2096 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
2097 this, SLOT(changeUrl(const KUrl&)));
2098 connect(navigator, SIGNAL(historyChanged()),
2099 this, SLOT(updateHistory()));
2100 connect(navigator, SIGNAL(editableStateChanged(bool)),
2101 this, SLOT(slotEditableStateChanged(bool)));
2102 connect(navigator, SIGNAL(tabRequested(const KUrl&)),
2103 this, SLOT(openNewTab(KUrl)));
2104 }
2105
2106 void DolphinMainWindow::updateSplitAction()
2107 {
2108 QAction* splitAction = actionCollection()->action("split_view");
2109 if (m_viewTab[m_tabIndex].secondaryView) {
2110 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
2111 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
2112 splitAction->setToolTip(i18nc("@info", "Close right view"));
2113 splitAction->setIcon(KIcon("view-right-close"));
2114 } else {
2115 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
2116 splitAction->setToolTip(i18nc("@info", "Close left view"));
2117 splitAction->setIcon(KIcon("view-left-close"));
2118 }
2119 } else {
2120 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
2121 splitAction->setToolTip(i18nc("@info", "Split view"));
2122 splitAction->setIcon(KIcon("view-right-new"));
2123 }
2124 }
2125
2126 QString DolphinMainWindow::tabName(const KUrl& url) const
2127 {
2128 QString name;
2129 if (url.equals(KUrl("file:///"))) {
2130 name = '/';
2131 } else {
2132 name = url.fileName();
2133 if (name.isEmpty()) {
2134 name = url.protocol();
2135 } else {
2136 // Make sure that a '&' inside the directory name is displayed correctly
2137 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
2138 name.replace('&', "&&");
2139 }
2140 }
2141 return name;
2142 }
2143
2144 bool DolphinMainWindow::isKompareInstalled() const
2145 {
2146 static bool initialized = false;
2147 static bool installed = false;
2148 if (!initialized) {
2149 // TODO: maybe replace this approach later by using a menu
2150 // plugin like kdiff3plugin.cpp
2151 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
2152 initialized = true;
2153 }
2154 return installed;
2155 }
2156
2157 void DolphinMainWindow::createSecondaryView(int tabIndex)
2158 {
2159 QSplitter* splitter = m_viewTab[tabIndex].splitter;
2160 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
2161
2162 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
2163 m_viewTab[tabIndex].secondaryView = createViewContainer(view->rootUrl(), 0);
2164 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
2165 splitter->setSizes(QList<int>() << newWidth << newWidth);
2166 connectViewSignals(m_viewTab[tabIndex].secondaryView);
2167 m_viewTab[tabIndex].secondaryView->setActive(false);
2168 m_viewTab[tabIndex].secondaryView->show();
2169 }
2170
2171 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
2172 {
2173 return "Tab " + QString::number(tabIndex) + ' ' + property;
2174 }
2175
2176 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
2177 {
2178 QString caption;
2179 if (!url.isLocalFile()) {
2180 caption.append(url.protocol() + " - ");
2181 if (url.hasHost()) {
2182 caption.append(url.host() + " - ");
2183 }
2184 }
2185
2186 const QString fileName = url.fileName().isEmpty() ? "/" : url.fileName();
2187 caption.append(fileName);
2188
2189 setCaption(caption);
2190 }
2191
2192 QString DolphinMainWindow::squeezedText(const QString& text) const
2193 {
2194 const QFontMetrics fm = fontMetrics();
2195 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
2196 }
2197
2198 void DolphinMainWindow::addActionCloneToCollection(QAction* action, const QString& actionName)
2199 {
2200 KAction* actionClone = actionCollection()->addAction(actionName);
2201 actionClone->setText(action->text());
2202 actionClone->setIcon(action->icon());
2203 connect(actionClone, SIGNAL(triggered()), action, SLOT(trigger()));
2204 }
2205
2206 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
2207 KIO::FileUndoManager::UiInterface()
2208 {
2209 }
2210
2211 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
2212 {
2213 }
2214
2215 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
2216 {
2217 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
2218 if (mainWin) {
2219 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
2220 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
2221 } else {
2222 KIO::FileUndoManager::UiInterface::jobError(job);
2223 }
2224 }
2225
2226 #include "dolphinmainwindow.moc"