]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Don't use a submenu for the settings
[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 <KAction>
57 #include <KActionCollection>
58 #include <KActionMenu>
59 #include <KConfig>
60 #include <KDesktopFile>
61 #include <kdeversion.h>
62 #include <kdualaction.h>
63 #include <KFileDialog>
64 #include <KFilePlacesModel>
65 #include <KGlobal>
66 #include <KLineEdit>
67 #include <KToolBar>
68 #include <KIcon>
69 #include <KIconLoader>
70 #include <KIO/NetAccess>
71 #include <KInputDialog>
72 #include <KLocale>
73 #include <KProtocolManager>
74 #include <KPushButton>
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 <QSplitter>
97 #include <kacceleratormanager.h>
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::init()
1433 {
1434 DolphinSettings& settings = DolphinSettings::instance();
1435
1436 // Check whether Dolphin runs the first time. If yes then
1437 // a proper default window size is given at the end of DolphinMainWindow::init().
1438 GeneralSettings* generalSettings = settings.generalSettings();
1439 const bool firstRun = generalSettings->firstRun();
1440 if (firstRun) {
1441 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1442 }
1443
1444 setAcceptDrops(true);
1445
1446 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1447 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1448
1449 setupActions();
1450
1451 const KUrl homeUrl(generalSettings->homeUrl());
1452 setUrlAsCaption(homeUrl);
1453 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1454 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1455 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1456
1457 m_viewTab[m_tabIndex].primaryView = createViewContainer(homeUrl, m_viewTab[m_tabIndex].splitter);
1458
1459 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1460 connectViewSignals(m_activeViewContainer);
1461 DolphinView* view = m_activeViewContainer->view();
1462 m_activeViewContainer->show();
1463 m_actionHandler->setCurrentView(view);
1464
1465 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1466 connect(this, SIGNAL(urlChanged(const KUrl&)),
1467 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1468
1469 m_tabBar = new KTabBar(this);
1470 m_tabBar->setMovable(true);
1471 m_tabBar->setTabsClosable(true);
1472 connect(m_tabBar, SIGNAL(currentChanged(int)),
1473 this, SLOT(setActiveTab(int)));
1474 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1475 this, SLOT(closeTab(int)));
1476 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1477 this, SLOT(openTabContextMenu(int, const QPoint&)));
1478 connect(m_tabBar, SIGNAL(newTabRequest()),
1479 this, SLOT(openNewTab()));
1480 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1481 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1482 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1483 this, SLOT(closeTab(int)));
1484 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1485 this, SLOT(slotTabMoved(int, int)));
1486 connect(m_tabBar, SIGNAL(receivedDropEvent(int, QDropEvent*)),
1487 this, SLOT(tabDropEvent(int, QDropEvent*)));
1488
1489 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1490
1491 QWidget* centralWidget = new QWidget(this);
1492 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1493 m_centralWidgetLayout->setSpacing(0);
1494 m_centralWidgetLayout->setMargin(0);
1495 m_centralWidgetLayout->addWidget(m_tabBar);
1496 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1497
1498 setCentralWidget(centralWidget);
1499 setupDockWidgets();
1500 emit urlChanged(homeUrl);
1501
1502 setupGUI(Keys | Save | Create | ToolBar);
1503 stateChanged("new_file");
1504
1505 QClipboard* clipboard = QApplication::clipboard();
1506 connect(clipboard, SIGNAL(dataChanged()),
1507 this, SLOT(updatePasteAction()));
1508
1509 if (generalSettings->splitView()) {
1510 toggleSplitView();
1511 }
1512 updateEditActions();
1513 updateViewActions();
1514 updateGoActions();
1515
1516 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1517 showFilterBarAction->setChecked(generalSettings->filterBar());
1518
1519 if (firstRun) {
1520 menuBar()->setVisible(false);
1521 // Assure a proper default size if Dolphin runs the first time
1522 resize(750, 500);
1523 }
1524
1525 const bool showMenu = !menuBar()->isHidden();
1526 QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
1527 showMenuBarAction->setChecked(showMenu); // workaround for bug #171080
1528 if (!showMenu) {
1529 createToolBarMenuButton();
1530 }
1531 }
1532
1533 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1534 {
1535 Q_ASSERT(viewContainer);
1536 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1537 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1538 if (m_activeViewContainer == viewContainer) {
1539 return;
1540 }
1541
1542 m_activeViewContainer->setActive(false);
1543 m_activeViewContainer = viewContainer;
1544
1545 // Activating the view container might trigger a recursive setActiveViewContainer() call
1546 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1547 // disconnect the activated() signal in this case:
1548 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1549 m_activeViewContainer->setActive(true);
1550 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1551
1552 m_actionHandler->setCurrentView(viewContainer->view());
1553
1554 updateHistory();
1555 updateEditActions();
1556 updateViewActions();
1557 updateGoActions();
1558
1559 const KUrl url = m_activeViewContainer->url();
1560 setUrlAsCaption(url);
1561 if (m_viewTab.count() > 1) {
1562 m_tabBar->setTabText(m_tabIndex, tabName(url));
1563 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1564 }
1565
1566 emit urlChanged(url);
1567 }
1568
1569 DolphinViewContainer* DolphinMainWindow::createViewContainer(const KUrl& url, QWidget* parent)
1570 {
1571 DolphinViewContainer* container = new DolphinViewContainer(url, parent);
1572
1573 // The places-selector from the URL navigator should only be shown
1574 // if the places dock is invisible
1575 QDockWidget* placesDock = findChild<QDockWidget*>("placesDock");
1576 container->urlNavigator()->setPlacesSelectorVisible(!placesDock || !placesDock->isVisible());
1577
1578 return container;
1579 }
1580
1581 void DolphinMainWindow::setupActions()
1582 {
1583 // setup 'File' menu
1584 m_newFileMenu = new DolphinNewFileMenu(this);
1585 KMenu* menu = m_newFileMenu->menu();
1586 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1587 menu->setIcon(KIcon("document-new"));
1588 connect(menu, SIGNAL(aboutToShow()),
1589 this, SLOT(updateNewMenu()));
1590
1591 KAction* newWindow = actionCollection()->addAction("new_window");
1592 newWindow->setIcon(KIcon("window-new"));
1593 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1594 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1595 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1596
1597 KAction* newTab = actionCollection()->addAction("new_tab");
1598 newTab->setIcon(KIcon("tab-new"));
1599 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1600 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1601 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1602
1603 KAction* closeTab = actionCollection()->addAction("close_tab");
1604 closeTab->setIcon(KIcon("tab-close"));
1605 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1606 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1607 closeTab->setEnabled(false);
1608 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1609
1610 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1611
1612 // setup 'Edit' menu
1613 KStandardAction::undo(this,
1614 SLOT(undo()),
1615 actionCollection());
1616
1617 // need to remove shift+del from cut action, else the shortcut for deletejob
1618 // doesn't work
1619 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1620 KShortcut cutShortcut = cut->shortcut();
1621 cutShortcut.remove(Qt::SHIFT | Qt::Key_Delete, KShortcut::KeepEmpty);
1622 cut->setShortcut(cutShortcut);
1623 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1624 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1625 // The text of the paste-action is modified dynamically by Dolphin
1626 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1627 // due to the long text, the text "Paste" is used:
1628 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1629
1630 KStandardAction::find(this, SLOT(find()), actionCollection());
1631
1632 KAction* selectAll = actionCollection()->addAction("select_all");
1633 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1634 selectAll->setShortcut(Qt::CTRL | Qt::Key_A);
1635 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1636
1637 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1638 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1639 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1640 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1641
1642 // setup 'View' menu
1643 // (note that most of it is set up in DolphinViewActionHandler)
1644
1645 KAction* split = actionCollection()->addAction("split_view");
1646 split->setShortcut(Qt::Key_F3);
1647 updateSplitAction();
1648 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1649
1650 KAction* reload = actionCollection()->addAction("reload");
1651 reload->setText(i18nc("@action:inmenu View", "Reload"));
1652 reload->setShortcut(Qt::Key_F5);
1653 reload->setIcon(KIcon("view-refresh"));
1654 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1655
1656 KAction* stop = actionCollection()->addAction("stop");
1657 stop->setText(i18nc("@action:inmenu View", "Stop"));
1658 stop->setToolTip(i18nc("@info", "Stop loading"));
1659 stop->setIcon(KIcon("process-stop"));
1660 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1661
1662 KToggleAction* editableLocation = actionCollection()->add<KToggleAction>("editable_location");
1663 editableLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1664 editableLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1665 connect(editableLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1666
1667 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1668 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1669 replaceLocation->setShortcut(Qt::Key_F6);
1670 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1671
1672 // setup 'Go' menu
1673 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1674 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1675 KShortcut backShortcut = backAction->shortcut();
1676 backShortcut.setAlternate(Qt::Key_Backspace);
1677 backAction->setShortcut(backShortcut);
1678
1679 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1680 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1681 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1682 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1683 this, SLOT(restoreClosedTab(QAction *)));
1684
1685 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1686 action->setIcon(KIcon("edit-clear-list"));
1687 action->setData(QVariant::fromValue(true));
1688 m_recentTabsMenu->addAction(action);
1689 m_recentTabsMenu->addSeparator();
1690 m_recentTabsMenu->setEnabled(false);
1691
1692 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1693 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1694
1695 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1696 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1697
1698 // setup 'Tools' menu
1699 KAction* showFilterBar = actionCollection()->addAction("show_filter_bar");
1700 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1701 showFilterBar->setIcon(KIcon("view-filter"));
1702 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1703 connect(showFilterBar, SIGNAL(triggered()), this, SLOT(showFilterBar()));
1704
1705 KAction* compareFiles = actionCollection()->addAction("compare_files");
1706 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1707 compareFiles->setIcon(KIcon("kompare"));
1708 compareFiles->setEnabled(false);
1709 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1710
1711 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1712 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1713 openTerminal->setIcon(KIcon("utilities-terminal"));
1714 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1715 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1716
1717 // setup 'Settings' menu
1718 KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1719 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1720
1721 // not in menu actions
1722 QList<QKeySequence> nextTabKeys;
1723 nextTabKeys.append(KStandardShortcut::tabNext().primary());
1724 nextTabKeys.append(QKeySequence(Qt::CTRL | Qt::Key_Tab));
1725
1726 QList<QKeySequence> prevTabKeys;
1727 prevTabKeys.append(KStandardShortcut::tabPrev().primary());
1728 prevTabKeys.append(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
1729
1730 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1731 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1732 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1733 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? prevTabKeys : nextTabKeys);
1734
1735 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1736 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1737 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1738 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? nextTabKeys : prevTabKeys);
1739
1740 // for context menu
1741 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1742 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1743 openInNewTab->setIcon(KIcon("tab-new"));
1744 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1745
1746 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1747 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1748 openInNewWindow->setIcon(KIcon("window-new"));
1749 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1750 }
1751
1752 void DolphinMainWindow::setupDockWidgets()
1753 {
1754 const bool lock = DolphinSettings::instance().generalSettings()->lockPanels();
1755
1756 KDualAction* lockLayoutAction = actionCollection()->add<KDualAction>("lock_panels");
1757 lockLayoutAction->setActiveText(i18nc("@action:inmenu Panels", "Unlock Panels"));
1758 lockLayoutAction->setActiveIcon(KIcon("object-unlocked"));
1759 lockLayoutAction->setInactiveText(i18nc("@action:inmenu Panels", "Lock Panels"));
1760 lockLayoutAction->setInactiveIcon(KIcon("object-locked"));
1761 lockLayoutAction->setActive(lock);
1762 connect(lockLayoutAction, SIGNAL(triggered()), this, SLOT(togglePanelLockState()));
1763
1764 // Setup "Information"
1765 DolphinDockWidget* infoDock = new DolphinDockWidget(i18nc("@title:window", "Information"));
1766 infoDock->setLocked(lock);
1767 infoDock->setObjectName("infoDock");
1768 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1769 Panel* infoPanel = new InformationPanel(infoDock);
1770 infoPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1771 connect(infoPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1772 infoDock->setWidget(infoPanel);
1773
1774 QAction* infoAction = infoDock->toggleViewAction();
1775 infoAction->setIcon(KIcon("dialog-information"));
1776 infoAction->setShortcut(Qt::Key_F11);
1777 addActionCloneToCollection(infoAction, "show_information_panel");
1778
1779 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1780 connect(this, SIGNAL(urlChanged(KUrl)),
1781 infoPanel, SLOT(setUrl(KUrl)));
1782 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1783 infoPanel, SLOT(setSelection(KFileItemList)));
1784 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1785 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1786
1787 // Setup "Folders"
1788 DolphinDockWidget* foldersDock = new DolphinDockWidget(i18nc("@title:window", "Folders"));
1789 foldersDock->setLocked(lock);
1790 foldersDock->setObjectName("foldersDock");
1791 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1792 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1793 foldersPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1794 foldersDock->setWidget(foldersPanel);
1795
1796 QAction* foldersAction = foldersDock->toggleViewAction();
1797 foldersAction->setShortcut(Qt::Key_F7);
1798 foldersAction->setIcon(KIcon("folder"));
1799 addActionCloneToCollection(foldersAction, "show_folders_panel");
1800
1801 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1802 connect(this, SIGNAL(urlChanged(KUrl)),
1803 foldersPanel, SLOT(setUrl(KUrl)));
1804 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1805 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1806
1807 // Setup "Terminal"
1808 #ifndef Q_OS_WIN
1809 DolphinDockWidget* terminalDock = new DolphinDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1810 terminalDock->setLocked(lock);
1811 terminalDock->setObjectName("terminalDock");
1812 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1813 Panel* terminalPanel = new TerminalPanel(terminalDock);
1814 terminalPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1815 terminalDock->setWidget(terminalPanel);
1816
1817 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1818
1819 QAction* terminalAction = terminalDock->toggleViewAction();
1820 terminalAction->setShortcut(Qt::Key_F4);
1821 terminalAction->setIcon(KIcon("utilities-terminal"));
1822 addActionCloneToCollection(terminalAction, "show_terminal_panel");
1823
1824 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1825 connect(this, SIGNAL(urlChanged(KUrl)),
1826 terminalPanel, SLOT(setUrl(KUrl)));
1827 #endif
1828
1829 // Setup "Search"
1830 #ifdef HAVE_NEPOMUK
1831 DolphinDockWidget* searchDock = new DolphinDockWidget(i18nc("@title:window", "Search"));
1832 searchDock->setLocked(lock);
1833 searchDock->setObjectName("searchDock");
1834 searchDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1835 Panel* searchPanel = new SearchPanel(searchDock);
1836 searchPanel->setCustomContextMenuActions(QList<QAction*>() << lockLayoutAction);
1837 connect(searchPanel, SIGNAL(urlActivated(KUrl)), this, SLOT(handleUrl(KUrl)));
1838 searchDock->setWidget(searchPanel);
1839
1840 QAction* searchAction = searchDock->toggleViewAction();
1841 searchAction->setShortcut(Qt::Key_F12);
1842 searchAction->setIcon(KIcon("system-search"));
1843 addActionCloneToCollection(searchAction, "show_search_panel");
1844 addDockWidget(Qt::RightDockWidgetArea, searchDock);
1845 connect(this, SIGNAL(urlChanged(KUrl)),
1846 searchPanel, SLOT(setUrl(KUrl)));
1847 #endif
1848
1849 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1850 if (firstRun) {
1851 infoDock->hide();
1852 foldersDock->hide();
1853 #ifndef Q_OS_WIN
1854 terminalDock->hide();
1855 #endif
1856 #ifdef HAVE_NEPOMUK
1857 searchDock->hide();
1858 #endif
1859 }
1860
1861 // Setup "Places"
1862 DolphinDockWidget* placesDock = new DolphinDockWidget(i18nc("@title:window", "Places"));
1863 placesDock->setLocked(lock);
1864 placesDock->setObjectName("placesDock");
1865 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1866
1867 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1868 QAction* separator = new QAction(placesPanel);
1869 separator->setSeparator(true);
1870 QList<QAction*> placesActions;
1871 placesActions.append(separator);
1872 placesActions.append(lockLayoutAction);
1873 placesPanel->addActions(placesActions);
1874 placesPanel->setModel(DolphinSettings::instance().placesModel());
1875 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1876 placesDock->setWidget(placesPanel);
1877
1878 QAction* placesAction = placesDock->toggleViewAction();
1879 placesAction->setShortcut(Qt::Key_F9);
1880 placesAction->setIcon(KIcon("bookmarks"));
1881 addActionCloneToCollection(placesAction, "show_places_panel");
1882
1883 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1884 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1885 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1886 connect(this, SIGNAL(urlChanged(KUrl)),
1887 placesPanel, SLOT(setUrl(KUrl)));
1888 connect(placesDock, SIGNAL(visibilityChanged(bool)),
1889 this, SLOT(slotPlacesPanelVisibilityChanged(bool)));
1890
1891 // Add actions into the "Panels" menu
1892 KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
1893 actionCollection()->addAction("panels", panelsMenu);
1894 panelsMenu->setDelayed(false);
1895 panelsMenu->addAction(placesAction);
1896 panelsMenu->addAction(infoAction);
1897 panelsMenu->addAction(foldersAction);
1898 #ifndef Q_OS_WIN
1899 panelsMenu->addAction(terminalAction);
1900 #endif
1901 #ifdef HAVE_NEPOMUK
1902 panelsMenu->addAction(searchAction);
1903 #endif
1904 panelsMenu->addSeparator();
1905 panelsMenu->addAction(lockLayoutAction);
1906 }
1907
1908 void DolphinMainWindow::updateEditActions()
1909 {
1910 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1911 if (list.isEmpty()) {
1912 stateChanged("has_no_selection");
1913 } else {
1914 stateChanged("has_selection");
1915
1916 KActionCollection* col = actionCollection();
1917 QAction* renameAction = col->action("rename");
1918 QAction* moveToTrashAction = col->action("move_to_trash");
1919 QAction* deleteAction = col->action("delete");
1920 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1921 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1922
1923 KFileItemListProperties capabilities(list);
1924 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1925
1926 renameAction->setEnabled(capabilities.supportsMoving());
1927 moveToTrashAction->setEnabled(enableMoveToTrash);
1928 deleteAction->setEnabled(capabilities.supportsDeleting());
1929 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1930 cutAction->setEnabled(capabilities.supportsMoving());
1931 }
1932 updatePasteAction();
1933 }
1934
1935 void DolphinMainWindow::updateViewActions()
1936 {
1937 m_actionHandler->updateViewActions();
1938
1939 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1940 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1941
1942 updateSplitAction();
1943
1944 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1945 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1946 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1947 }
1948
1949 void DolphinMainWindow::updateGoActions()
1950 {
1951 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1952 const KUrl currentUrl = m_activeViewContainer->url();
1953 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1954 }
1955
1956 void DolphinMainWindow::createToolBarMenuButton()
1957 {
1958 if (m_toolBarSpacer && m_openToolBarMenuButton) {
1959 return;
1960 }
1961 Q_ASSERT(!m_toolBarSpacer);
1962 Q_ASSERT(!m_openToolBarMenuButton);
1963
1964 m_toolBarSpacer = new QWidget(this);
1965 m_toolBarSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
1966
1967 m_openToolBarMenuButton = new KPushButton(this);
1968 m_openToolBarMenuButton->setFlat(true);
1969 m_openToolBarMenuButton->setIcon(KIcon("configure"));
1970 m_openToolBarMenuButton->setMaximumWidth(m_openToolBarMenuButton->sizeHint().height());
1971
1972 // Instead of using QPushButton::setMenu() the opening of the menu is done manually
1973 // to prevent the "clutter" of the down-arrow drawn by the style.
1974 connect(m_openToolBarMenuButton, SIGNAL(clicked()), this, SLOT(openToolBarMenu()));
1975
1976 toolBar()->addWidget(m_toolBarSpacer);
1977 toolBar()->addWidget(m_openToolBarMenuButton);
1978
1979 // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
1980 // gets edited. In this case we must add them again. The adding is done asynchronously by
1981 // m_updateToolBarTimer.
1982 connect(m_toolBarSpacer, SIGNAL(destroyed()), this, SLOT(slotToolBarSpacerDeleted()));
1983 connect(m_openToolBarMenuButton, SIGNAL(destroyed()), this, SLOT(slotToolBarMenuButtonDeleted()));
1984 m_updateToolBarTimer = new QTimer(this);
1985 m_updateToolBarTimer->setInterval(500);
1986 connect(m_updateToolBarTimer, SIGNAL(timeout()), this, SLOT(updateToolBar()));
1987 }
1988
1989 void DolphinMainWindow::deleteToolBarMenuButton()
1990 {
1991 delete m_toolBarSpacer;
1992 m_toolBarSpacer = 0;
1993
1994 delete m_openToolBarMenuButton;
1995 m_openToolBarMenuButton = 0;
1996
1997 delete m_updateToolBarTimer;
1998 m_updateToolBarTimer = 0;
1999 }
2000
2001 bool DolphinMainWindow::addActionToMenu(QAction* action, KMenu* menu)
2002 {
2003 Q_ASSERT(action);
2004 Q_ASSERT(menu);
2005
2006 const KToolBar* toolBarWidget = toolBar();
2007 foreach (const QWidget* widget, action->associatedWidgets()) {
2008 if (widget == toolBarWidget) {
2009 return false;
2010 }
2011 }
2012
2013 menu->addAction(action);
2014 return true;
2015 }
2016
2017 void DolphinMainWindow::rememberClosedTab(int index)
2018 {
2019 KMenu* tabsMenu = m_recentTabsMenu->menu();
2020
2021 const QString primaryPath = m_viewTab[index].primaryView->url().path();
2022 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
2023
2024 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
2025
2026 ClosedTab closedTab;
2027 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
2028
2029 if (m_viewTab[index].secondaryView) {
2030 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
2031 closedTab.isSplit = true;
2032 } else {
2033 closedTab.isSplit = false;
2034 }
2035
2036 action->setData(QVariant::fromValue(closedTab));
2037 action->setIcon(KIcon(iconName));
2038
2039 // add the closed tab menu entry after the separator and
2040 // "Empty Recently Closed Tabs" entry
2041 if (tabsMenu->actions().size() == 2) {
2042 tabsMenu->addAction(action);
2043 } else {
2044 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
2045 }
2046
2047 // assure that only up to 8 closed tabs are shown in the menu
2048 if (tabsMenu->actions().size() > 8) {
2049 tabsMenu->removeAction(tabsMenu->actions().last());
2050 }
2051 actionCollection()->action("closed_tabs")->setEnabled(true);
2052 KAcceleratorManager::manage(tabsMenu);
2053 }
2054
2055 void DolphinMainWindow::clearStatusBar()
2056 {
2057 m_activeViewContainer->statusBar()->clear();
2058 }
2059
2060 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
2061 {
2062 connect(container, SIGNAL(showFilterBarChanged(bool)),
2063 this, SLOT(updateFilterBarAction(bool)));
2064 connect(container, SIGNAL(writeStateChanged(bool)),
2065 this, SLOT(slotWriteStateChanged(bool)));
2066 connect(container, SIGNAL(searchModeChanged(bool)),
2067 this, SLOT(slotSearchModeChanged(bool)));
2068
2069 DolphinView* view = container->view();
2070 connect(view, SIGNAL(selectionChanged(KFileItemList)),
2071 this, SLOT(slotSelectionChanged(KFileItemList)));
2072 connect(view, SIGNAL(requestItemInfo(KFileItem)),
2073 this, SLOT(slotRequestItemInfo(KFileItem)));
2074 connect(view, SIGNAL(activated()),
2075 this, SLOT(toggleActiveView()));
2076 connect(view, SIGNAL(tabRequested(const KUrl&)),
2077 this, SLOT(openNewTab(const KUrl&)));
2078 connect(view, SIGNAL(requestContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)),
2079 this, SLOT(openContextMenu(KFileItem, const KUrl&, const QList<QAction*>&)));
2080 connect(view, SIGNAL(startedPathLoading(KUrl)),
2081 this, SLOT(enableStopAction()));
2082 connect(view, SIGNAL(finishedPathLoading(KUrl)),
2083 this, SLOT(disableStopAction()));
2084
2085 const KUrlNavigator* navigator = container->urlNavigator();
2086 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
2087 this, SLOT(changeUrl(const KUrl&)));
2088 connect(navigator, SIGNAL(historyChanged()),
2089 this, SLOT(updateHistory()));
2090 connect(navigator, SIGNAL(editableStateChanged(bool)),
2091 this, SLOT(slotEditableStateChanged(bool)));
2092 connect(navigator, SIGNAL(tabRequested(const KUrl&)),
2093 this, SLOT(openNewTab(KUrl)));
2094 }
2095
2096 void DolphinMainWindow::updateSplitAction()
2097 {
2098 QAction* splitAction = actionCollection()->action("split_view");
2099 if (m_viewTab[m_tabIndex].secondaryView) {
2100 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
2101 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
2102 splitAction->setToolTip(i18nc("@info", "Close right view"));
2103 splitAction->setIcon(KIcon("view-right-close"));
2104 } else {
2105 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
2106 splitAction->setToolTip(i18nc("@info", "Close left view"));
2107 splitAction->setIcon(KIcon("view-left-close"));
2108 }
2109 } else {
2110 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
2111 splitAction->setToolTip(i18nc("@info", "Split view"));
2112 splitAction->setIcon(KIcon("view-right-new"));
2113 }
2114 }
2115
2116 QString DolphinMainWindow::tabName(const KUrl& url) const
2117 {
2118 QString name;
2119 if (url.equals(KUrl("file:///"))) {
2120 name = '/';
2121 } else {
2122 name = url.fileName();
2123 if (name.isEmpty()) {
2124 name = url.protocol();
2125 } else {
2126 // Make sure that a '&' inside the directory name is displayed correctly
2127 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
2128 name.replace('&', "&&");
2129 }
2130 }
2131 return name;
2132 }
2133
2134 bool DolphinMainWindow::isKompareInstalled() const
2135 {
2136 static bool initialized = false;
2137 static bool installed = false;
2138 if (!initialized) {
2139 // TODO: maybe replace this approach later by using a menu
2140 // plugin like kdiff3plugin.cpp
2141 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
2142 initialized = true;
2143 }
2144 return installed;
2145 }
2146
2147 void DolphinMainWindow::createSecondaryView(int tabIndex)
2148 {
2149 QSplitter* splitter = m_viewTab[tabIndex].splitter;
2150 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
2151
2152 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
2153 m_viewTab[tabIndex].secondaryView = createViewContainer(view->rootUrl(), 0);
2154 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
2155 splitter->setSizes(QList<int>() << newWidth << newWidth);
2156 connectViewSignals(m_viewTab[tabIndex].secondaryView);
2157 m_viewTab[tabIndex].secondaryView->setActive(false);
2158 m_viewTab[tabIndex].secondaryView->show();
2159 }
2160
2161 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
2162 {
2163 return "Tab " + QString::number(tabIndex) + ' ' + property;
2164 }
2165
2166 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
2167 {
2168 QString caption;
2169 if (!url.isLocalFile()) {
2170 caption.append(url.protocol() + " - ");
2171 if (url.hasHost()) {
2172 caption.append(url.host() + " - ");
2173 }
2174 }
2175
2176 const QString fileName = url.fileName().isEmpty() ? "/" : url.fileName();
2177 caption.append(fileName);
2178
2179 setCaption(caption);
2180 }
2181
2182 QString DolphinMainWindow::squeezedText(const QString& text) const
2183 {
2184 const QFontMetrics fm = fontMetrics();
2185 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
2186 }
2187
2188 void DolphinMainWindow::addActionCloneToCollection(QAction* action, const QString& actionName)
2189 {
2190 KAction* actionClone = actionCollection()->addAction(actionName);
2191 actionClone->setText(action->text());
2192 actionClone->setIcon(action->icon());
2193 connect(actionClone, SIGNAL(triggered()), action, SLOT(trigger()));
2194 }
2195
2196 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
2197 KIO::FileUndoManager::UiInterface()
2198 {
2199 }
2200
2201 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
2202 {
2203 }
2204
2205 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
2206 {
2207 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
2208 if (mainWin) {
2209 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
2210 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
2211 } else {
2212 KIO::FileUndoManager::UiInterface::jobError(job);
2213 }
2214 }
2215
2216 #include "dolphinmainwindow.moc"