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