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