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