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