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