]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Update the size of the menu toolbar-item
[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 clearStatusBar();
895
896 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
897 urlNavigator->goBack();
898
899 if (urlNavigator->locationState().isEmpty()) {
900 // An empty location state indicates a redirection URL,
901 // which must be skipped too
902 urlNavigator->goBack();
903 }
904 }
905
906 void DolphinMainWindow::goForward()
907 {
908 clearStatusBar();
909 m_activeViewContainer->urlNavigator()->goForward();
910 }
911
912 void DolphinMainWindow::goUp()
913 {
914 clearStatusBar();
915 m_activeViewContainer->urlNavigator()->goUp();
916 }
917
918 void DolphinMainWindow::goBack(Qt::MouseButtons buttons)
919 {
920 // The default case (left button pressed) is handled in goBack().
921 if (buttons == Qt::MidButton) {
922 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
923 const int index = urlNavigator->historyIndex() + 1;
924 openNewTab(urlNavigator->locationUrl(index));
925 }
926 }
927
928 void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
929 {
930 // The default case (left button pressed) is handled in goForward().
931 if (buttons == Qt::MidButton) {
932 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
933 const int index = urlNavigator->historyIndex() - 1;
934 openNewTab(urlNavigator->locationUrl(index));
935 }
936 }
937
938 void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
939 {
940 // The default case (left button pressed) is handled in goUp().
941 if (buttons == Qt::MidButton) {
942 openNewTab(activeViewContainer()->url().upUrl());
943 }
944 }
945
946 void DolphinMainWindow::compareFiles()
947 {
948 // The method is only invoked if exactly 2 files have
949 // been selected. The selected files may be:
950 // - both in the primary view
951 // - both in the secondary view
952 // - one in the primary view and the other in the secondary
953 // view
954 Q_ASSERT(m_viewTab[m_tabIndex].primaryView);
955
956 KUrl urlA;
957 KUrl urlB;
958
959 KFileItemList items = m_viewTab[m_tabIndex].primaryView->view()->selectedItems();
960
961 switch (items.count()) {
962 case 0: {
963 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView);
964 items = m_viewTab[m_tabIndex].secondaryView->view()->selectedItems();
965 Q_ASSERT(items.count() == 2);
966 urlA = items[0].url();
967 urlB = items[1].url();
968 break;
969 }
970
971 case 1: {
972 urlA = items[0].url();
973 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView);
974 items = m_viewTab[m_tabIndex].secondaryView->view()->selectedItems();
975 Q_ASSERT(items.count() == 1);
976 urlB = items[0].url();
977 break;
978 }
979
980 case 2: {
981 urlA = items[0].url();
982 urlB = items[1].url();
983 break;
984 }
985
986 default: {
987 // may not happen: compareFiles may only get invoked if 2
988 // files are selected
989 Q_ASSERT(false);
990 }
991 }
992
993 QString command("kompare -c \"");
994 command.append(urlA.pathOrUrl());
995 command.append("\" \"");
996 command.append(urlB.pathOrUrl());
997 command.append('\"');
998 KRun::runCommand(command, "Kompare", "kompare", this);
999 }
1000
1001 void DolphinMainWindow::toggleShowMenuBar()
1002 {
1003 const bool visible = menuBar()->isVisible();
1004 menuBar()->setVisible(!visible);
1005 if (visible) {
1006 createToolBarMenuButton();
1007 } else {
1008 deleteToolBarMenuButton();
1009 }
1010 }
1011
1012 void DolphinMainWindow::openTerminal()
1013 {
1014 QString dir(QDir::homePath());
1015
1016 // If the given directory is not local, it can still be the URL of an
1017 // ioslave using UDS_LOCAL_PATH which to be converted first.
1018 KUrl url = KIO::NetAccess::mostLocalUrl(m_activeViewContainer->url(), this);
1019
1020 //If the URL is local after the above conversion, set the directory.
1021 if (url.isLocalFile()) {
1022 dir = url.toLocalFile();
1023 }
1024
1025 KToolInvocation::invokeTerminal(QString(), dir);
1026 }
1027
1028 void DolphinMainWindow::editSettings()
1029 {
1030 if (!m_settingsDialog) {
1031 const KUrl url = activeViewContainer()->url();
1032 m_settingsDialog = new DolphinSettingsDialog(url, this);
1033 m_settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
1034 m_settingsDialog->show();
1035 } else {
1036 m_settingsDialog->raise();
1037 }
1038 }
1039
1040 void DolphinMainWindow::setActiveTab(int index)
1041 {
1042 Q_ASSERT(index >= 0);
1043 Q_ASSERT(index < m_viewTab.count());
1044 if (index == m_tabIndex) {
1045 return;
1046 }
1047
1048 // hide current tab content
1049 ViewTab& hiddenTab = m_viewTab[m_tabIndex];
1050 hiddenTab.isPrimaryViewActive = hiddenTab.primaryView->isActive();
1051 hiddenTab.primaryView->setActive(false);
1052 if (hiddenTab.secondaryView) {
1053 hiddenTab.secondaryView->setActive(false);
1054 }
1055 QSplitter* splitter = m_viewTab[m_tabIndex].splitter;
1056 splitter->hide();
1057 m_centralWidgetLayout->removeWidget(splitter);
1058
1059 // show active tab content
1060 m_tabIndex = index;
1061
1062 ViewTab& viewTab = m_viewTab[index];
1063 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
1064 viewTab.primaryView->show();
1065 if (viewTab.secondaryView) {
1066 viewTab.secondaryView->show();
1067 }
1068 viewTab.splitter->show();
1069
1070 setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
1071 viewTab.secondaryView);
1072 }
1073
1074 void DolphinMainWindow::closeTab()
1075 {
1076 closeTab(m_tabBar->currentIndex());
1077 }
1078
1079 void DolphinMainWindow::closeTab(int index)
1080 {
1081 Q_ASSERT(index >= 0);
1082 Q_ASSERT(index < m_viewTab.count());
1083 if (m_viewTab.count() == 1) {
1084 // the last tab may never get closed
1085 return;
1086 }
1087
1088 if (index == m_tabIndex) {
1089 // The tab that should be closed is the active tab. Activate the
1090 // previous tab before closing the tab.
1091 m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
1092 }
1093 rememberClosedTab(index);
1094
1095 // delete tab
1096 m_viewTab[index].primaryView->deleteLater();
1097 if (m_viewTab[index].secondaryView) {
1098 m_viewTab[index].secondaryView->deleteLater();
1099 }
1100 m_viewTab[index].splitter->deleteLater();
1101 m_viewTab.erase(m_viewTab.begin() + index);
1102
1103 m_tabBar->blockSignals(true);
1104 m_tabBar->removeTab(index);
1105
1106 if (m_tabIndex > index) {
1107 m_tabIndex--;
1108 Q_ASSERT(m_tabIndex >= 0);
1109 }
1110
1111 // if only one tab is left, also remove the tab entry so that
1112 // closing the last tab is not possible
1113 if (m_viewTab.count() == 1) {
1114 m_tabBar->removeTab(0);
1115 actionCollection()->action("close_tab")->setEnabled(false);
1116 } else {
1117 m_tabBar->blockSignals(false);
1118 }
1119 }
1120
1121 void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
1122 {
1123 KMenu menu(this);
1124
1125 QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
1126 newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
1127
1128 QAction* detachTabAction = menu.addAction(KIcon("tab-detach"), i18nc("@action:inmenu", "Detach Tab"));
1129
1130 QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
1131
1132 QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
1133 closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
1134 QAction* selectedAction = menu.exec(pos);
1135 if (selectedAction == newTabAction) {
1136 const ViewTab& tab = m_viewTab[index];
1137 Q_ASSERT(tab.primaryView);
1138 const KUrl url = tab.secondaryView && tab.secondaryView->isActive() ?
1139 tab.secondaryView->url() : tab.primaryView->url();
1140 openNewTab(url);
1141 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
1142 } else if (selectedAction == detachTabAction) {
1143 const ViewTab& tab = m_viewTab[index];
1144 Q_ASSERT(tab.primaryView);
1145 const KUrl primaryUrl = tab.primaryView->url();
1146 DolphinMainWindow* window = DolphinApplication::app()->createMainWindow();
1147 window->changeUrl(primaryUrl);
1148
1149 if (tab.secondaryView) {
1150 const KUrl secondaryUrl = tab.secondaryView->url();
1151 if (!window->m_viewTab[0].secondaryView) {
1152 window->toggleSplitView();
1153 }
1154 window->m_viewTab[0].secondaryView->setUrl(secondaryUrl);
1155 if (tab.primaryView->isActive()) {
1156 window->m_viewTab[0].primaryView->setActive(true);
1157 } else {
1158 window->m_viewTab[0].secondaryView->setActive(true);
1159 }
1160 }
1161 window->show();
1162 closeTab(index);
1163 } else if (selectedAction == closeOtherTabsAction) {
1164 const int count = m_tabBar->count();
1165 for (int i = 0; i < index; ++i) {
1166 closeTab(0);
1167 }
1168 for (int i = index + 1; i < count; ++i) {
1169 closeTab(1);
1170 }
1171 } else if (selectedAction == closeTabAction) {
1172 closeTab(index);
1173 }
1174 }
1175
1176 void DolphinMainWindow::slotTabMoved(int from, int to)
1177 {
1178 m_viewTab.move(from, to);
1179 m_tabIndex = m_tabBar->currentIndex();
1180 }
1181
1182 void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
1183 {
1184 if (buttons & Qt::MidButton) {
1185 openNewTab(url);
1186 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
1187 } else {
1188 changeUrl(url);
1189 }
1190 }
1191
1192 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
1193 {
1194 canDecode = KUrl::List::canDecode(event->mimeData());
1195 }
1196
1197 void DolphinMainWindow::handleUrl(const KUrl& url)
1198 {
1199 delete m_lastHandleUrlStatJob;
1200 m_lastHandleUrlStatJob = 0;
1201
1202 if (url.isLocalFile() && QFileInfo(url.toLocalFile()).isDir()) {
1203 activeViewContainer()->setUrl(url);
1204 } else if (KProtocolManager::supportsListing(url)) {
1205 // stat the URL to see if it is a dir or not
1206 m_lastHandleUrlStatJob = KIO::stat(url, KIO::HideProgressInfo);
1207 connect(m_lastHandleUrlStatJob, SIGNAL(result(KJob*)),
1208 this, SLOT(slotHandleUrlStatFinished(KJob*)));
1209
1210 } else {
1211 new KRun(url, this);
1212 }
1213 }
1214
1215 void DolphinMainWindow::slotHandleUrlStatFinished(KJob* job)
1216 {
1217 m_lastHandleUrlStatJob = 0;
1218 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
1219 const KUrl url = static_cast<KIO::StatJob*>(job)->url();
1220 if (entry.isDir()) {
1221 activeViewContainer()->setUrl(url);
1222 } else {
1223 new KRun(url, this);
1224 }
1225 }
1226
1227 void DolphinMainWindow::tabDropEvent(int tab, QDropEvent* event)
1228 {
1229 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
1230 if (!urls.isEmpty() && tab != -1) {
1231 const ViewTab& viewTab = m_viewTab[tab];
1232 const KUrl destPath = viewTab.isPrimaryViewActive ? viewTab.primaryView->url() : viewTab.secondaryView->url();
1233 DragAndDropHelper::instance().dropUrls(KFileItem(), destPath, event, m_tabBar);
1234 }
1235 }
1236
1237 void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
1238 {
1239 newFileMenu()->setEnabled(isFolderWritable);
1240 }
1241
1242 void DolphinMainWindow::slotSearchModeChanged(bool enabled)
1243 {
1244 #ifdef HAVE_NEPOMUK
1245 const KUrl url = m_activeViewContainer->url();
1246 const DolphinSearchInformation& searchInfo = DolphinSearchInformation::instance();
1247 if (!searchInfo.isIndexingEnabled() || !searchInfo.isPathIndexed(url)) {
1248 return;
1249 }
1250
1251 QDockWidget* searchDock = findChild<QDockWidget*>("searchDock");
1252 if (!searchDock) {
1253 return;
1254 }
1255
1256 if (enabled) {
1257 if (!searchDock->isVisible()) {
1258 m_searchDockIsTemporaryVisible = true;
1259 }
1260 searchDock->show();
1261 } else {
1262 if (searchDock->isVisible() && m_searchDockIsTemporaryVisible) {
1263 searchDock->hide();
1264 }
1265 m_searchDockIsTemporaryVisible = false;
1266 }
1267
1268 SearchPanel* searchPanel = qobject_cast<SearchPanel*>(searchDock->widget());
1269 if (searchPanel) {
1270 // Per default any search-operation triggered by the Search Panel is done
1271 // "Everywhere".
1272 SearchPanel::SearchMode searchMode = SearchPanel::Everywhere;
1273
1274 if (enabled && (SearchSettings::location() == QLatin1String("FromHere"))) {
1275 // Only if the search-mode is enabled it is visible for the user whether
1276 // a searching is done "Everywhere" or "From Here" (= current directory).
1277 searchMode = SearchPanel::FromCurrentDir;
1278 }
1279 searchPanel->setSearchMode(searchMode);
1280 }
1281 #else
1282 Q_UNUSED(enabled);
1283 #endif
1284 }
1285
1286 void DolphinMainWindow::openContextMenu(const KFileItem& item,
1287 const KUrl& url,
1288 const QList<QAction*>& customActions)
1289 {
1290 QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, item, url);
1291 contextMenu->setCustomActions(customActions);
1292 const DolphinContextMenu::Command command = contextMenu->open();
1293
1294 switch (command) {
1295 case DolphinContextMenu::OpenParentFolderInNewWindow: {
1296 DolphinMainWindow* window = DolphinApplication::app()->createMainWindow();
1297 window->changeUrl(item.url().upUrl());
1298 window->show();
1299 break;
1300 }
1301
1302 case DolphinContextMenu::OpenParentFolderInNewTab:
1303 openNewTab(item.url().upUrl());
1304 break;
1305
1306 case DolphinContextMenu::None:
1307 default:
1308 break;
1309 }
1310
1311 delete contextMenu;
1312 }
1313
1314 void DolphinMainWindow::openToolBarMenu()
1315 {
1316 const int height = m_openToolBarMenuButton->height();
1317 const QPoint pos = m_openToolBarMenuButton->mapToGlobal(QPoint(0, height));
1318
1319 m_toolBarMenu = new KMenu(m_openToolBarMenuButton);
1320 m_toolBarMenu.data()->setAttribute(Qt::WA_DeleteOnClose);
1321 connect(m_toolBarMenu.data(), SIGNAL(aboutToShow()), this, SLOT(updateToolBarMenu()));
1322
1323 m_toolBarMenu.data()->exec(pos);
1324 }
1325
1326 void DolphinMainWindow::updateToolBarMenu()
1327 {
1328 KMenu* menu = m_toolBarMenu.data();
1329 if (!menu) {
1330 return;
1331 }
1332
1333 const GeneralSettings* generalSettings = DolphinSettings::instance().generalSettings();
1334
1335 KActionCollection* ac = actionCollection();
1336
1337 // Add "Edit" actions
1338 bool added = addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Undo)), menu) |
1339 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Find)), menu) |
1340 addActionToMenu(ac->action("select_all"), menu) |
1341 addActionToMenu(ac->action("invert_selection"), menu);
1342
1343 if (added) {
1344 menu->addSeparator();
1345 }
1346
1347 // Add "View" actions
1348 if (!generalSettings->showZoomSlider()) {
1349 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomIn)), menu);
1350 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ZoomOut)), menu);
1351 menu->addSeparator();
1352 }
1353
1354 added = addActionToMenu(ac->action("view_mode"), menu) |
1355 addActionToMenu(ac->action("sort"), menu) |
1356 addActionToMenu(ac->action("additional_info"), menu) |
1357 addActionToMenu(ac->action("show_preview"), menu) |
1358 addActionToMenu(ac->action("show_in_groups"), menu) |
1359 addActionToMenu(ac->action("show_hidden_files"), menu);
1360
1361 if (added) {
1362 menu->addSeparator();
1363 }
1364
1365 added = addActionToMenu(ac->action("split_view"), menu) |
1366 addActionToMenu(ac->action("reload"), menu) |
1367 addActionToMenu(ac->action("view_properties"), menu);
1368 if (added) {
1369 menu->addSeparator();
1370 }
1371
1372 addActionToMenu(ac->action("panels"), menu);
1373 KMenu* locationBarMenu = new KMenu(i18nc("@action:inmenu", "Location Bar"), menu);
1374 locationBarMenu->addAction(ac->action("editable_location"));
1375 locationBarMenu->addAction(ac->action("replace_location"));
1376 menu->addMenu(locationBarMenu);
1377
1378 menu->addSeparator();
1379
1380 // Add "Go" menu
1381 KMenu* goMenu = new KMenu(i18nc("@action:inmenu", "Go"), menu);
1382 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Back)));
1383 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Forward)));
1384 goMenu->addAction(ac->action(KStandardAction::name(KStandardAction::Up)));
1385 goMenu->addAction(ac->action("closed_tabs"));
1386 menu->addMenu(goMenu);
1387
1388 // Add "Tool" menu
1389 KMenu* toolsMenu = new KMenu(i18nc("@action:inmenu", "Tools"), menu);
1390 toolsMenu->addAction(ac->action("show_filter_bar"));
1391 toolsMenu->addAction(ac->action("compare_files"));
1392 toolsMenu->addAction(ac->action("open_terminal"));
1393 toolsMenu->addAction(ac->action("change_remote_encoding"));
1394 menu->addMenu(toolsMenu);
1395
1396 // Add "Settings" menu entries
1397 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::KeyBindings)), menu);
1398 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ConfigureToolbars)), menu);
1399 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::Preferences)), menu);
1400
1401 // Add "Help" menu
1402 KMenu* helpMenu = new KMenu(i18nc("@action:inmenu", "Help"), menu);
1403 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::HelpContents)));
1404 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::WhatsThis)));
1405 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutApp)));
1406 helpMenu->addAction(ac->action(KStandardAction::name(KStandardAction::AboutKDE)));
1407 menu->addMenu(helpMenu);
1408
1409 menu->addSeparator();
1410 addActionToMenu(ac->action(KStandardAction::name(KStandardAction::ShowMenubar)), menu);
1411 }
1412
1413 void DolphinMainWindow::updateToolBar()
1414 {
1415 if (!menuBar()->isVisible()) {
1416 createToolBarMenuButton();
1417 }
1418 }
1419
1420 void DolphinMainWindow::slotToolBarSpacerDeleted()
1421 {
1422 m_toolBarSpacer = 0;
1423 m_updateToolBarTimer->start();
1424 }
1425
1426 void DolphinMainWindow::slotToolBarMenuButtonDeleted()
1427 {
1428 m_openToolBarMenuButton = 0;
1429 m_updateToolBarTimer->start();
1430 }
1431
1432 void DolphinMainWindow::slotToolBarIconSizeChanged(const QSize& iconSize)
1433 {
1434 if (m_openToolBarMenuButton) {
1435 m_openToolBarMenuButton->setIconSize(iconSize);
1436 }
1437 }
1438
1439 void DolphinMainWindow::init()
1440 {
1441 DolphinSettings& settings = DolphinSettings::instance();
1442
1443 // Check whether Dolphin runs the first time. If yes then
1444 // a proper default window size is given at the end of DolphinMainWindow::init().
1445 GeneralSettings* generalSettings = settings.generalSettings();
1446 const bool firstRun = generalSettings->firstRun();
1447 if (firstRun) {
1448 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1449 }
1450
1451 setAcceptDrops(true);
1452
1453 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1454 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1455
1456 setupActions();
1457
1458 const KUrl homeUrl(generalSettings->homeUrl());
1459 setUrlAsCaption(homeUrl);
1460 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1461 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1462 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1463
1464 m_viewTab[m_tabIndex].primaryView = createViewContainer(homeUrl, m_viewTab[m_tabIndex].splitter);
1465
1466 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1467 connectViewSignals(m_activeViewContainer);
1468 DolphinView* view = m_activeViewContainer->view();
1469 m_activeViewContainer->show();
1470 m_actionHandler->setCurrentView(view);
1471
1472 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1473 connect(this, SIGNAL(urlChanged(const KUrl&)),
1474 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1475
1476 m_tabBar = new KTabBar(this);
1477 m_tabBar->setMovable(true);
1478 m_tabBar->setTabsClosable(true);
1479 connect(m_tabBar, SIGNAL(currentChanged(int)),
1480 this, SLOT(setActiveTab(int)));
1481 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1482 this, SLOT(closeTab(int)));
1483 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1484 this, SLOT(openTabContextMenu(int, const QPoint&)));
1485 connect(m_tabBar, SIGNAL(newTabRequest()),
1486 this, SLOT(openNewTab()));
1487 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1488 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1489 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1490 this, SLOT(closeTab(int)));
1491 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1492 this, SLOT(slotTabMoved(int, int)));
1493 connect(m_tabBar, SIGNAL(receivedDropEvent(int, QDropEvent*)),
1494 this, SLOT(tabDropEvent(int, QDropEvent*)));
1495
1496 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1497
1498 QWidget* centralWidget = new QWidget(this);
1499 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1500 m_centralWidgetLayout->setSpacing(0);
1501 m_centralWidgetLayout->setMargin(0);
1502 m_centralWidgetLayout->addWidget(m_tabBar);
1503 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1504
1505 setCentralWidget(centralWidget);
1506 setupDockWidgets();
1507 emit urlChanged(homeUrl);
1508
1509 setupGUI(Keys | Save | Create | ToolBar);
1510 stateChanged("new_file");
1511
1512 QClipboard* clipboard = QApplication::clipboard();
1513 connect(clipboard, SIGNAL(dataChanged()),
1514 this, SLOT(updatePasteAction()));
1515
1516 if (generalSettings->splitView()) {
1517 toggleSplitView();
1518 }
1519 updateEditActions();
1520 updateViewActions();
1521 updateGoActions();
1522
1523 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1524 showFilterBarAction->setChecked(generalSettings->filterBar());
1525
1526 if (firstRun) {
1527 menuBar()->setVisible(false);
1528 // Assure a proper default size if Dolphin runs the first time
1529 resize(750, 500);
1530 }
1531
1532 const bool showMenu = !menuBar()->isHidden();
1533 QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
1534 showMenuBarAction->setChecked(showMenu); // workaround for bug #171080
1535 if (!showMenu) {
1536 createToolBarMenuButton();
1537 }
1538 }
1539
1540 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1541 {
1542 Q_ASSERT(viewContainer);
1543 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1544 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1545 if (m_activeViewContainer == viewContainer) {
1546 return;
1547 }
1548
1549 m_activeViewContainer->setActive(false);
1550 m_activeViewContainer = viewContainer;
1551
1552 // Activating the view container might trigger a recursive setActiveViewContainer() call
1553 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1554 // disconnect the activated() signal in this case:
1555 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1556 m_activeViewContainer->setActive(true);
1557 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1558
1559 m_actionHandler->setCurrentView(viewContainer->view());
1560
1561 updateHistory();
1562 updateEditActions();
1563 updateViewActions();
1564 updateGoActions();
1565
1566 const KUrl url = m_activeViewContainer->url();
1567 setUrlAsCaption(url);
1568 if (m_viewTab.count() > 1) {
1569 m_tabBar->setTabText(m_tabIndex, tabName(url));
1570 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1571 }
1572
1573 emit urlChanged(url);
1574 }
1575
1576 DolphinViewContainer* DolphinMainWindow::createViewContainer(const KUrl& url, QWidget* parent)
1577 {
1578 DolphinViewContainer* container = new DolphinViewContainer(url, parent);
1579
1580 // The places-selector from the URL navigator should only be shown
1581 // if the places dock is invisible
1582 QDockWidget* placesDock = findChild<QDockWidget*>("placesDock");
1583 container->urlNavigator()->setPlacesSelectorVisible(!placesDock || !placesDock->isVisible());
1584
1585 return container;
1586 }
1587
1588 void DolphinMainWindow::setupActions()
1589 {
1590 // setup 'File' menu
1591 m_newFileMenu = new DolphinNewFileMenu(this);
1592 KMenu* menu = m_newFileMenu->menu();
1593 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1594 menu->setIcon(KIcon("document-new"));
1595 connect(menu, SIGNAL(aboutToShow()),
1596 this, SLOT(updateNewMenu()));
1597
1598 KAction* newWindow = actionCollection()->addAction("new_window");
1599 newWindow->setIcon(KIcon("window-new"));
1600 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1601 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1602 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1603
1604 KAction* newTab = actionCollection()->addAction("new_tab");
1605 newTab->setIcon(KIcon("tab-new"));
1606 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1607 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1608 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1609
1610 KAction* closeTab = actionCollection()->addAction("close_tab");
1611 closeTab->setIcon(KIcon("tab-close"));
1612 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1613 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1614 closeTab->setEnabled(false);
1615 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1616
1617 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1618
1619 // setup 'Edit' menu
1620 KStandardAction::undo(this,
1621 SLOT(undo()),
1622 actionCollection());
1623
1624 // need to remove shift+del from cut action, else the shortcut for deletejob
1625 // doesn't work
1626 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1627 KShortcut cutShortcut = cut->shortcut();
1628 cutShortcut.remove(Qt::SHIFT | Qt::Key_Delete, KShortcut::KeepEmpty);
1629 cut->setShortcut(cutShortcut);
1630 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1631 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1632 // The text of the paste-action is modified dynamically by Dolphin
1633 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1634 // due to the long text, the text "Paste" is used:
1635 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1636
1637 KStandardAction::find(this, SLOT(find()), actionCollection());
1638
1639 KAction* selectAll = actionCollection()->addAction("select_all");
1640 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1641 selectAll->setShortcut(Qt::CTRL | Qt::Key_A);
1642 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1643
1644 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1645 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1646 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1647 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1648
1649 // setup 'View' menu
1650 // (note that most of it is set up in DolphinViewActionHandler)
1651
1652 KAction* split = actionCollection()->addAction("split_view");
1653 split->setShortcut(Qt::Key_F3);
1654 updateSplitAction();
1655 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1656
1657 KAction* reload = actionCollection()->addAction("reload");
1658 reload->setText(i18nc("@action:inmenu View", "Reload"));
1659 reload->setShortcut(Qt::Key_F5);
1660 reload->setIcon(KIcon("view-refresh"));
1661 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1662
1663 KAction* stop = actionCollection()->addAction("stop");
1664 stop->setText(i18nc("@action:inmenu View", "Stop"));
1665 stop->setToolTip(i18nc("@info", "Stop loading"));
1666 stop->setIcon(KIcon("process-stop"));
1667 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1668
1669 KToggleAction* editableLocation = actionCollection()->add<KToggleAction>("editable_location");
1670 editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1671 editableLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1672 connect(editableLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1673
1674 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1675 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1676 replaceLocation->setShortcut(Qt::Key_F6);
1677 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1678
1679 // setup 'Go' menu
1680 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1681 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1682 KShortcut backShortcut = backAction->shortcut();
1683 backShortcut.setAlternate(Qt::Key_Backspace);
1684 backAction->setShortcut(backShortcut);
1685
1686 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1687 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1688 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1689 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1690 this, SLOT(restoreClosedTab(QAction *)));
1691
1692 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1693 action->setIcon(KIcon("edit-clear-list"));
1694 action->setData(QVariant::fromValue(true));
1695 m_recentTabsMenu->addAction(action);
1696 m_recentTabsMenu->addSeparator();
1697 m_recentTabsMenu->setEnabled(false);
1698
1699 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1700 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1701
1702 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1703 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1704
1705 // setup 'Tools' menu
1706 KAction* showFilterBar = actionCollection()->addAction("show_filter_bar");
1707 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1708 showFilterBar->setIcon(KIcon("view-filter"));
1709 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1710 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1711
1712 KAction* compareFiles = actionCollection()->addAction("compare_files");
1713 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1714 compareFiles->setIcon(KIcon("kompare"));
1715 compareFiles->setEnabled(false);
1716 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1717
1718 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1719 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1720 openTerminal->setIcon(KIcon("utilities-terminal"));
1721 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1722 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1723
1724 // setup 'Settings' menu
1725 KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1726 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1727
1728 // not in menu actions
1729 QList<QKeySequence> nextTabKeys;
1730 nextTabKeys.append(KStandardShortcut::tabNext().primary());
1731 nextTabKeys.append(QKeySequence(Qt::CTRL | Qt::Key_Tab));
1732
1733 QList<QKeySequence> prevTabKeys;
1734 prevTabKeys.append(KStandardShortcut::tabPrev().primary());
1735 prevTabKeys.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
1736
1737 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1738 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1739 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1740 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? prevTabKeys : nextTabKeys);
1741
1742 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1743 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1744 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1745 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? nextTabKeys : prevTabKeys);
1746
1747 // for context menu
1748 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1749 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1750 openInNewTab->setIcon(KIcon("tab-new"));
1751 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1752
1753 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1754 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1755 openInNewWindow->setIcon(KIcon("window-new"));
1756 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1757 }
1758
1759 void DolphinMainWindow::setupDockWidgets()
1760 {
1761 const bool lock = DolphinSettings::instance().generalSettings()->lockPanels();
1762
1763 KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>("lock_panels");
1764 lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1765 lockLayoutAction->setActiveIcon(KIcon("object-unlocked"));
1766 lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1767 lockLayoutAction->setInactiveIcon(KIcon("object-locked"));
1768 lockLayoutAction->setActive(lock);
1769 connect(lockLayoutAction, SIGNAL(triggered()), this, SLOT(togglePanelLockState()));
1770
1771 // Setup "Information"
1772 DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1773 infoDock->setLocked(lock);
1774 infoDock->setObjectName("infoDock");
1775 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1776 Panel* infoPanel = new InformationPanel(infoDock);
1777 infoPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1778 connect(infoPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1779 infoDock->setWidget(infoPanel);
1780
1781 QAction* infoAction = infoDock->toggleViewAction();
1782 infoAction->setIcon(KIcon("dialog-information"));
1783 infoAction->setShortcut(Qt::Key_F11);
1784 addActionCloneToCollection(infoAction, "show_information_panel");
1785
1786 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1787 connect(this, SIGNAL(urlChanged(KUrl)),
1788 infoPanel, SLOT(setUrl(KUrl)));
1789 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1790 infoPanel, SLOT(setSelection(KFileItemList)));
1791 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1792 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1793
1794 // Setup "Folders"
1795 DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1796 foldersDock->setLocked(lock);
1797 foldersDock->setObjectName("foldersDock");
1798 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1799 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1800 foldersPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1801 foldersDock->setWidget(foldersPanel);
1802
1803 QAction* foldersAction = foldersDock->toggleViewAction();
1804 foldersAction->setShortcut(Qt::Key_F7);
1805 foldersAction->setIcon(KIcon("folder"));
1806 addActionCloneToCollection(foldersAction, "show_folders_panel");
1807
1808 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1809 connect(this, SIGNAL(urlChanged(KUrl)),
1810 foldersPanel, SLOT(setUrl(KUrl)));
1811 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1812 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1813
1814 // Setup "Terminal"
1815 #ifndef Q_OS_WIN
1816 DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1817 terminalDock->setLocked(lock);
1818 terminalDock->setObjectName("terminalDock");
1819 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1820 Panel* terminalPanel = new TerminalPanel(terminalDock);
1821 terminalPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1822 terminalDock->setWidget(terminalPanel);
1823
1824 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1825
1826 QAction* terminalAction = terminalDock->toggleViewAction();
1827 terminalAction->setShortcut(Qt::Key_F4);
1828 terminalAction->setIcon(KIcon("utilities-terminal"));
1829 addActionCloneToCollection(terminalAction, "show_terminal_panel");
1830
1831 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1832 connect(this, SIGNAL(urlChanged(KUrl)),
1833 terminalPanel, SLOT(setUrl(KUrl)));
1834 #endif
1835
1836 // Setup "Search"
1837 #ifdef HAVE_NEPOMUK
1838 DolphinDockWidget* searchDock = new DolphinDockWidget(i18nc("@title:window", "Search"));
1839 searchDock->setLocked(lock);
1840 searchDock->setObjectName("searchDock");
1841 searchDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1842 Panel* searchPanel = new SearchPanel(searchDock);
1843 searchPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1844 connect(searchPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1845 searchDock->setWidget(searchPanel);
1846
1847 QAction* searchAction = searchDock->toggleViewAction();
1848 searchAction->setShortcut(Qt::Key_F12);
1849 searchAction->setIcon(KIcon("system-search"));
1850 addActionCloneToCollection(searchAction, "show_search_panel");
1851 addDockWidget(Qt::RightDockWidgetArea, searchDock);
1852 connect(this, SIGNAL(urlChanged(KUrl)),
1853 searchPanel, SLOT(setUrl(KUrl)));
1854 #endif
1855
1856 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1857 if (firstRun) {
1858 infoDock->hide();
1859 foldersDock->hide();
1860 #ifndef Q_OS_WIN
1861 terminalDock->hide();
1862 #endif
1863 #ifdef HAVE_NEPOMUK
1864 searchDock->hide();
1865 #endif
1866 }
1867
1868 // Setup "Places"
1869 DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1870 placesDock->setLocked(lock);
1871 placesDock->setObjectName("placesDock");
1872 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1873
1874 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1875 QAction* separator = new QAction(placesPanel);
1876 separator->setSeparator(true);
1877 QList<QAction*> placesActions;
1878 placesActions.append(separator);
1879 placesActions.append(lockLayoutAction);
1880 placesPanel->addActions(placesActions);
1881 placesPanel->setModel(DolphinSettings::instance().placesModel());
1882 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1883 placesDock->setWidget(placesPanel);
1884
1885 QAction* placesAction = placesDock->toggleViewAction();
1886 placesAction->setShortcut(Qt::Key_F9);
1887 placesAction->setIcon(KIcon("bookmarks"));
1888 addActionCloneToCollection(placesAction, "show_places_panel");
1889
1890 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1891 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1892 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1893 connect(this, SIGNAL(urlChanged(KUrl)),
1894 placesPanel, SLOT(setUrl(KUrl)));
1895 connect(placesDock, SIGNAL(visibilityChanged(bool)),
1896 this, SLOT(slotPlacesPanelVisibilityChanged(bool)));
1897
1898 // Add actions into the "Panels" menu
1899 KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
1900 actionCollection()->addAction("panels", panelsMenu);
1901 panelsMenu->setDelayed(false);
1902 panelsMenu->addAction(placesAction);
1903 panelsMenu->addAction(infoAction);
1904 panelsMenu->addAction(foldersAction);
1905 #ifndef Q_OS_WIN
1906 panelsMenu->addAction(terminalAction);
1907 #endif
1908 #ifdef HAVE_NEPOMUK
1909 panelsMenu->addAction(searchAction);
1910 #endif
1911 panelsMenu->addSeparator();
1912 panelsMenu->addAction(lockLayoutAction);
1913 }
1914
1915 void DolphinMainWindow::updateEditActions()
1916 {
1917 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1918 if (list.isEmpty()) {
1919 stateChanged("has_no_selection");
1920 } else {
1921 stateChanged("has_selection");
1922
1923 KActionCollection* col = actionCollection();
1924 QAction* renameAction = col->action("rename");
1925 QAction* moveToTrashAction = col->action("move_to_trash");
1926 QAction* deleteAction = col->action("delete");
1927 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1928 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1929
1930 KFileItemListProperties capabilities(list);
1931 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1932
1933 renameAction->setEnabled(capabilities.supportsMoving());
1934 moveToTrashAction->setEnabled(enableMoveToTrash);
1935 deleteAction->setEnabled(capabilities.supportsDeleting());
1936 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1937 cutAction->setEnabled(capabilities.supportsMoving());
1938 }
1939 updatePasteAction();
1940 }
1941
1942 void DolphinMainWindow::updateViewActions()
1943 {
1944 m_actionHandler->updateViewActions();
1945
1946 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1947 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1948
1949 updateSplitAction();
1950
1951 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1952 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1953 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1954 }
1955
1956 void DolphinMainWindow::updateGoActions()
1957 {
1958 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1959 const KUrl currentUrl = m_activeViewContainer->url();
1960 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1961 }
1962
1963 void DolphinMainWindow::createToolBarMenuButton()
1964 {
1965 if (m_toolBarSpacer && m_openToolBarMenuButton) {
1966 return;
1967 }
1968 Q_ASSERT(!m_toolBarSpacer);
1969 Q_ASSERT(!m_openToolBarMenuButton);
1970
1971 m_toolBarSpacer = new QWidget(this);
1972 m_toolBarSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
1973
1974 m_openToolBarMenuButton = new QToolButton(this);
1975 m_openToolBarMenuButton->setIcon(KIcon("configure"));
1976
1977 // Instead of using QPushButton::setMenu() the opening of the menu is done manually
1978 // to prevent the "clutter" of the down-arrow drawn by the style.
1979 connect(m_openToolBarMenuButton, SIGNAL(clicked()), this, SLOT(openToolBarMenu()));
1980
1981 toolBar()->addWidget(m_toolBarSpacer);
1982 toolBar()->addWidget(m_openToolBarMenuButton);
1983 connect(toolBar(), SIGNAL(iconSizeChanged(QSize)), this, SLOT(slotToolBarIconSizeChanged(QSize)));
1984
1985 // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
1986 // gets edited. In this case we must add them again. The adding is done asynchronously by
1987 // m_updateToolBarTimer.
1988 connect(m_toolBarSpacer, SIGNAL(destroyed()), this, SLOT(slotToolBarSpacerDeleted()));
1989 connect(m_openToolBarMenuButton, SIGNAL(destroyed()), this, SLOT(slotToolBarMenuButtonDeleted()));
1990 m_updateToolBarTimer = new QTimer(this);
1991 m_updateToolBarTimer->setInterval(500);
1992 connect(m_updateToolBarTimer, SIGNAL(timeout()), this, SLOT(updateToolBar()));
1993 }
1994
1995 void DolphinMainWindow::deleteToolBarMenuButton()
1996 {
1997 delete m_toolBarSpacer;
1998 m_toolBarSpacer = 0;
1999
2000 delete m_openToolBarMenuButton;
2001 m_openToolBarMenuButton = 0;
2002
2003 delete m_updateToolBarTimer;
2004 m_updateToolBarTimer = 0;
2005 }
2006
2007 bool DolphinMainWindow::addActionToMenu(QAction* action, KMenu* menu)
2008 {
2009 Q_ASSERT(action);
2010 Q_ASSERT(menu);
2011
2012 const KToolBar* toolBarWidget = toolBar();
2013 foreach (const QWidget* widget, action->associatedWidgets()) {
2014 if (widget == toolBarWidget) {
2015 return false;
2016 }
2017 }
2018
2019 menu->addAction(action);
2020 return true;
2021 }
2022
2023 void DolphinMainWindow::rememberClosedTab(int index)
2024 {
2025 KMenu* tabsMenu = m_recentTabsMenu->menu();
2026
2027 const QString primaryPath = m_viewTab[index].primaryView->url().path();
2028 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
2029
2030 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
2031
2032 ClosedTab closedTab;
2033 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
2034
2035 if (m_viewTab[index].secondaryView) {
2036 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
2037 closedTab.isSplit = true;
2038 } else {
2039 closedTab.isSplit = false;
2040 }
2041
2042 action->setData(QVariant::fromValue(closedTab));
2043 action->setIcon(KIcon(iconName));
2044
2045 // add the closed tab menu entry after the separator and
2046 // "Empty Recently Closed Tabs" entry
2047 if (tabsMenu->actions().size() == 2) {
2048 tabsMenu->addAction(action);
2049 } else {
2050 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
2051 }
2052
2053 // assure that only up to 8 closed tabs are shown in the menu
2054 if (tabsMenu->actions().size() > 8) {
2055 tabsMenu->removeAction(tabsMenu->actions().last());
2056 }
2057 actionCollection()->action("closed_tabs")->setEnabled(true);
2058 KAcceleratorManager::manage(tabsMenu);
2059 }
2060
2061 void DolphinMainWindow::clearStatusBar()
2062 {
2063 m_activeViewContainer->statusBar()->clear();
2064 }
2065
2066 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
2067 {
2068 connect(container, SIGNAL(showFilterBarChanged(bool)),
2069 this, SLOT(updateFilterBarAction(bool)));
2070 connect(container, SIGNAL(writeStateChanged(bool)),
2071 this, SLOT(slotWriteStateChanged(bool)));
2072 connect(container, SIGNAL(searchModeChanged(bool)),
2073 this, SLOT(slotSearchModeChanged(bool)));
2074
2075 DolphinView* view = container->view();
2076 connect(view, SIGNAL(selectionChanged(KFileItemList)),
2077 this, SLOT(slotSelectionChanged(KFileItemList)));
2078 connect(view, SIGNAL(requestItemInfo(KFileItem)),
2079 this, SLOT(slotRequestItemInfo(KFileItem)));
2080 connect(view, SIGNAL(activated()),
2081 this, SLOT(toggleActiveView()));
2082 connect(view, SIGNAL(tabRequested(const KUrl&)),
2083 this, SLOT(openNewTab(const KUrl&)));
2084 connect(view, SIGNAL(requestContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)),
2085 this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)));
2086 connect(view, SIGNAL(startedPathLoading(KUrl)),
2087 this, SLOT(enableStopAction()));
2088 connect(view, SIGNAL(finishedPathLoading(KUrl)),
2089 this, SLOT(disableStopAction()));
2090
2091 const KUrlNavigator* navigator = container->urlNavigator();
2092 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
2093 this, SLOT(changeUrl(const KUrl&)));
2094 connect(navigator, SIGNAL(historyChanged()),
2095 this, SLOT(updateHistory()));
2096 connect(navigator, SIGNAL(editableStateChanged(bool)),
2097 this, SLOT(slotEditableStateChanged(bool)));
2098 connect(navigator, SIGNAL(tabRequested(const KUrl&)),
2099 this, SLOT(openNewTab(KUrl)));
2100 }
2101
2102 void DolphinMainWindow::updateSplitAction()
2103 {
2104 QAction* splitAction = actionCollection()->action("split_view");
2105 if (m_viewTab[m_tabIndex].secondaryView) {
2106 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
2107 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
2108 splitAction->setToolTip(i18nc("@info", "Close right view"));
2109 splitAction->setIcon(KIcon("view-right-close"));
2110 } else {
2111 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
2112 splitAction->setToolTip(i18nc("@info", "Close left view"));
2113 splitAction->setIcon(KIcon("view-left-close"));
2114 }
2115 } else {
2116 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
2117 splitAction->setToolTip(i18nc("@info", "Split view"));
2118 splitAction->setIcon(KIcon("view-right-new"));
2119 }
2120 }
2121
2122 QString DolphinMainWindow::tabName(const KUrl& url) const
2123 {
2124 QString name;
2125 if (url.equals(KUrl("file:///"))) {
2126 name = '/';
2127 } else {
2128 name = url.fileName();
2129 if (name.isEmpty()) {
2130 name = url.protocol();
2131 } else {
2132 // Make sure that a '&' inside the directory name is displayed correctly
2133 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
2134 name.replace('&', "&&");
2135 }
2136 }
2137 return name;
2138 }
2139
2140 bool DolphinMainWindow::isKompareInstalled() const
2141 {
2142 static bool initialized = false;
2143 static bool installed = false;
2144 if (!initialized) {
2145 // TODO: maybe replace this approach later by using a menu
2146 // plugin like kdiff3plugin.cpp
2147 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
2148 initialized = true;
2149 }
2150 return installed;
2151 }
2152
2153 void DolphinMainWindow::createSecondaryView(int tabIndex)
2154 {
2155 QSplitter* splitter = m_viewTab[tabIndex].splitter;
2156 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
2157
2158 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
2159 m_viewTab[tabIndex].secondaryView = createViewContainer(view->rootUrl(), 0);
2160 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
2161 splitter->setSizes(QList<int>() << newWidth << newWidth);
2162 connectViewSignals(m_viewTab[tabIndex].secondaryView);
2163 m_viewTab[tabIndex].secondaryView->setActive(false);
2164 m_viewTab[tabIndex].secondaryView->show();
2165 }
2166
2167 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
2168 {
2169 return "Tab " + QString::number(tabIndex) + ' ' + property;
2170 }
2171
2172 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
2173 {
2174 QString caption;
2175 if (!url.isLocalFile()) {
2176 caption.append(url.protocol() + " - ");
2177 if (url.hasHost()) {
2178 caption.append(url.host() + " - ");
2179 }
2180 }
2181
2182 const QString fileName = url.fileName().isEmpty() ? "/" : url.fileName();
2183 caption.append(fileName);
2184
2185 setCaption(caption);
2186 }
2187
2188 QString DolphinMainWindow::squeezedText(const QString& text) const
2189 {
2190 const QFontMetrics fm = fontMetrics();
2191 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
2192 }
2193
2194 void DolphinMainWindow::addActionCloneToCollection(QAction* action, const QString& actionName)
2195 {
2196 KAction* actionClone = actionCollection()->addAction(actionName);
2197 actionClone->setText(action->text());
2198 actionClone->setIcon(action->icon());
2199 connect(actionClone, SIGNAL(triggered()), action, SLOT(trigger()));
2200 }
2201
2202 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
2203 KIO::FileUndoManager::UiInterface()
2204 {
2205 }
2206
2207 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
2208 {
2209 }
2210
2211 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
2212 {
2213 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
2214 if (mainWin) {
2215 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
2216 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
2217 } else {
2218 KIO::FileUndoManager::UiInterface::jobError(job);
2219 }
2220 }
2221
2222 #include "dolphinmainwindow.moc"