]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
No need to set the accelerator manually, it's done automatically anyways(or it will...
[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 QAction* selectedAction = menu.exec(pos);
856 if (selectedAction == newTabAction) {
857 const ViewTab& tab = m_viewTab[index];
858 Q_ASSERT(tab.primaryView != 0);
859 const KUrl url = (tab.secondaryView != 0) && tab.secondaryView->isActive() ?
860 tab.secondaryView->url() : tab.primaryView->url();
861 openNewTab(url);
862 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
863 } else if (selectedAction == closeOtherTabsAction) {
864 const int count = m_tabBar->count();
865 for (int i = 0; i < index; ++i) {
866 closeTab(0);
867 }
868 for (int i = index + 1; i < count; ++i) {
869 closeTab(1);
870 }
871 } else if (selectedAction == closeTabAction) {
872 closeTab(index);
873 }
874 }
875
876 void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
877 {
878 if (buttons & Qt::MidButton) {
879 openNewTab(url);
880 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
881 } else {
882 changeUrl(url);
883 }
884 }
885
886 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
887 {
888 canDecode = KUrl::List::canDecode(event->mimeData());
889 }
890
891 void DolphinMainWindow::searchItems(const KUrl& url)
892 {
893 m_activeViewContainer->setUrl(url);
894 }
895
896 void DolphinMainWindow::init()
897 {
898 DolphinSettings& settings = DolphinSettings::instance();
899
900 // Check whether Dolphin runs the first time. If yes then
901 // a proper default window size is given at the end of DolphinMainWindow::init().
902 GeneralSettings* generalSettings = settings.generalSettings();
903 const bool firstRun = generalSettings->firstRun();
904 if (firstRun) {
905 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
906 }
907
908 setAcceptDrops(true);
909
910 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
911
912 setupActions();
913
914 const KUrl& homeUrl = generalSettings->homeUrl();
915 setCaption(homeUrl.fileName());
916 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
917 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
918 ViewProperties props(homeUrl);
919 m_viewTab[m_tabIndex].primaryView = new DolphinViewContainer(this,
920 m_viewTab[m_tabIndex].splitter,
921 homeUrl);
922
923 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
924 connectViewSignals(m_activeViewContainer);
925 DolphinView* view = m_activeViewContainer->view();
926 view->reload();
927 m_activeViewContainer->show();
928 m_actionHandler->setCurrentView(view);
929
930 m_tabBar = new KTabBar(this);
931 m_tabBar->setMovable(true);
932 m_tabBar->setTabsClosable(true);
933 connect(m_tabBar, SIGNAL(currentChanged(int)),
934 this, SLOT(setActiveTab(int)));
935 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
936 this, SLOT(closeTab(int)));
937 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
938 this, SLOT(openTabContextMenu(int, const QPoint&)));
939 connect(m_tabBar, SIGNAL(newTabRequest()),
940 this, SLOT(openNewTab()));
941 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
942 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
943 connect(m_tabBar, SIGNAL(wheelDelta(int)),
944 this, SLOT(slotWheelMoved(int)));
945 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
946 this, SLOT(closeTab(int)));
947
948 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
949
950 QWidget* centralWidget = new QWidget(this);
951 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
952 m_centralWidgetLayout->setSpacing(0);
953 m_centralWidgetLayout->setMargin(0);
954 m_centralWidgetLayout->addWidget(m_tabBar);
955 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter);
956
957 setCentralWidget(centralWidget);
958 setupDockWidgets();
959 emit urlChanged(homeUrl);
960
961 setupGUI(Keys | Save | Create | ToolBar);
962
963 m_searchBox->setParent(toolBar("searchToolBar"));
964 m_searchBox->show();
965
966 stateChanged("new_file");
967
968 QClipboard* clipboard = QApplication::clipboard();
969 connect(clipboard, SIGNAL(dataChanged()),
970 this, SLOT(updatePasteAction()));
971 updatePasteAction();
972 updateGoActions();
973
974 if (generalSettings->splitView()) {
975 toggleSplitView();
976 }
977 updateViewActions();
978
979 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
980 showFilterBarAction->setChecked(generalSettings->filterBar());
981
982 if (firstRun) {
983 // assure a proper default size if Dolphin runs the first time
984 resize(750, 500);
985 }
986
987 m_showMenuBar->setChecked(!menuBar()->isHidden()); // workaround for bug #171080
988 }
989
990 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
991 {
992 Q_ASSERT(viewContainer != 0);
993 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
994 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
995 if (m_activeViewContainer == viewContainer) {
996 return;
997 }
998
999 m_activeViewContainer->setActive(false);
1000 m_activeViewContainer = viewContainer;
1001
1002 // Activating the view container might trigger a recursive setActiveViewContainer() call
1003 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1004 // disconnect the activated() signal in this case:
1005 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1006 m_activeViewContainer->setActive(true);
1007 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1008
1009 m_actionHandler->setCurrentView(viewContainer->view());
1010
1011 updateHistory();
1012 updateEditActions();
1013 updateViewActions();
1014 updateGoActions();
1015
1016 const KUrl& url = m_activeViewContainer->url();
1017 setCaption(url.fileName());
1018 if (m_viewTab.count() > 1 && m_viewTab[m_tabIndex].secondaryView != 0) {
1019 m_tabBar->setTabText(m_tabIndex, tabName(url));
1020 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1021 }
1022
1023 emit urlChanged(url);
1024 }
1025
1026 void DolphinMainWindow::setupActions()
1027 {
1028 // setup 'File' menu
1029 m_newMenu = new DolphinNewMenu(this, this);
1030 KMenu* menu = m_newMenu->menu();
1031 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1032 menu->setIcon(KIcon("document-new"));
1033 connect(menu, SIGNAL(aboutToShow()),
1034 this, SLOT(updateNewMenu()));
1035
1036 KAction* newWindow = actionCollection()->addAction("new_window");
1037 newWindow->setIcon(KIcon("window-new"));
1038 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1039 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1040 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1041
1042 KAction* newTab = actionCollection()->addAction("new_tab");
1043 newTab->setIcon(KIcon("tab-new"));
1044 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1045 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1046 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1047
1048 KAction* closeTab = actionCollection()->addAction("close_tab");
1049 closeTab->setIcon(KIcon("tab-close"));
1050 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1051 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1052 closeTab->setEnabled(false);
1053 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1054
1055 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1056
1057 // setup 'Edit' menu
1058 KStandardAction::undo(this,
1059 SLOT(undo()),
1060 actionCollection());
1061
1062 // need to remove shift+del from cut action, else the shortcut for deletejob
1063 // doesn't work
1064 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1065 KShortcut cutShortcut = cut->shortcut();
1066 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
1067 cut->setShortcut(cutShortcut);
1068 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1069 KStandardAction::paste(this, SLOT(paste()), actionCollection());
1070
1071 KAction* selectAll = actionCollection()->addAction("select_all");
1072 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1073 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1074 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1075
1076 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1077 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1078 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1079 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1080
1081 // setup 'View' menu
1082 // (note that most of it is set up in DolphinViewActionHandler)
1083
1084 KAction* split = actionCollection()->addAction("split_view");
1085 split->setShortcut(Qt::Key_F3);
1086 updateSplitAction();
1087 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1088
1089 KAction* reload = actionCollection()->addAction("reload");
1090 reload->setText(i18nc("@action:inmenu View", "Reload"));
1091 reload->setShortcut(Qt::Key_F5);
1092 reload->setIcon(KIcon("view-refresh"));
1093 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1094
1095 KAction* stop = actionCollection()->addAction("stop");
1096 stop->setText(i18nc("@action:inmenu View", "Stop"));
1097 stop->setIcon(KIcon("process-stop"));
1098 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1099
1100 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1101 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1102 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1103 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1104
1105 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1106 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1107 replaceLocation->setShortcut(Qt::Key_F6);
1108 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1109
1110 // setup 'Go' menu
1111 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1112 KShortcut backShortcut = backAction->shortcut();
1113 backShortcut.setAlternate(Qt::Key_Backspace);
1114 backAction->setShortcut(backShortcut);
1115
1116 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1117 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1118 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1119 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1120 this, SLOT(restoreClosedTab(QAction *)));
1121
1122 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1123 action->setIcon(KIcon("edit-clear-list"));
1124 action->setData(QVariant::fromValue(true));
1125 m_recentTabsMenu->addAction(action);
1126 m_recentTabsMenu->addSeparator();
1127 m_recentTabsMenu->setEnabled(false);
1128
1129 KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1130 KStandardAction::up(this, SLOT(goUp()), actionCollection());
1131 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1132
1133 // setup 'Tools' menu
1134 KToggleAction* showSearchBar = actionCollection()->add<KToggleAction>("show_search_bar");
1135 showSearchBar->setText(i18nc("@action:inmenu Tools", "Show Search Bar"));
1136 showSearchBar->setShortcut(Qt::CTRL | Qt::Key_S);
1137 connect(showSearchBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1138
1139 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1140 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1141 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1142 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1143
1144 KAction* compareFiles = actionCollection()->addAction("compare_files");
1145 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1146 compareFiles->setIcon(KIcon("kompare"));
1147 compareFiles->setEnabled(false);
1148 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1149
1150 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1151 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1152 openTerminal->setIcon(KIcon("terminal"));
1153 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1154 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1155
1156 // setup 'Settings' menu
1157 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1158 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1159
1160 // not in menu actions
1161 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1162 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1163 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1164 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() :
1165 KStandardShortcut::tabNext());
1166
1167 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1168 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1169 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1170 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() :
1171 KStandardShortcut::tabPrev());
1172
1173 // for context menu
1174 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1175 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1176 openInNewTab->setIcon(KIcon("tab-new"));
1177 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1178
1179 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1180 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1181 openInNewWindow->setIcon(KIcon("window-new"));
1182 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1183
1184 // 'Search' toolbar
1185 m_searchBox = new DolphinSearchBox(this);
1186 connect(m_searchBox, SIGNAL(search(KUrl)), this, SLOT(searchItems(KUrl)));
1187
1188 KAction* search = new KAction(this);
1189 actionCollection()->addAction("search_bar", search);
1190 search->setText(i18nc("@action:inmenu", "Search Bar"));
1191 search->setDefaultWidget(m_searchBox);
1192 search->setShortcutConfigurable(false);
1193 }
1194
1195 void DolphinMainWindow::setupDockWidgets()
1196 {
1197 // setup "Information"
1198 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
1199 infoDock->setObjectName("infoDock");
1200 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1201 Panel* infoPanel = new InformationPanel(infoDock);
1202 infoDock->setWidget(infoPanel);
1203
1204 QAction* infoAction = infoDock->toggleViewAction();
1205 infoAction->setText(i18nc("@title:window", "Information"));
1206 infoAction->setShortcut(Qt::Key_F11);
1207 infoAction->setIcon(KIcon("dialog-information"));
1208 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1209
1210 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1211 connect(this, SIGNAL(urlChanged(KUrl)),
1212 infoPanel, SLOT(setUrl(KUrl)));
1213 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1214 infoPanel, SLOT(setSelection(KFileItemList)));
1215 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1216 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1217
1218 // setup "Folders"
1219 QDockWidget* foldersDock = new QDockWidget(i18nc("@title:window", "Folders"));
1220 foldersDock->setObjectName("foldersDock");
1221 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1222 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1223 foldersDock->setWidget(foldersPanel);
1224
1225 QAction* foldersAction = foldersDock->toggleViewAction();
1226 foldersAction->setText(i18nc("@title:window", "Folders"));
1227 foldersAction->setShortcut(Qt::Key_F7);
1228 foldersAction->setIcon(KIcon("folder"));
1229 actionCollection()->addAction("show_folders_panel", foldersDock->toggleViewAction());
1230
1231 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1232 connect(this, SIGNAL(urlChanged(KUrl)),
1233 foldersPanel, SLOT(setUrl(KUrl)));
1234 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1235 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1236 connect(foldersPanel, SIGNAL(changeSelection(KFileItemList)),
1237 this, SLOT(changeSelection(KFileItemList)));
1238
1239 // setup "Terminal"
1240 #ifndef Q_OS_WIN
1241 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1242 terminalDock->setObjectName("terminalDock");
1243 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1244 Panel* terminalPanel = new TerminalPanel(terminalDock);
1245 terminalDock->setWidget(terminalPanel);
1246
1247 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1248
1249 QAction* terminalAction = terminalDock->toggleViewAction();
1250 terminalAction->setText(i18nc("@title:window Shell terminal", "Terminal"));
1251 terminalAction->setShortcut(Qt::Key_F4);
1252 terminalAction->setIcon(KIcon("terminal"));
1253 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
1254
1255 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1256 connect(this, SIGNAL(urlChanged(KUrl)),
1257 terminalPanel, SLOT(setUrl(KUrl)));
1258 #endif
1259
1260 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1261 if (firstRun) {
1262 foldersDock->hide();
1263 #ifndef Q_OS_WIN
1264 terminalDock->hide();
1265 #endif
1266 }
1267
1268 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
1269 placesDock->setObjectName("placesDock");
1270 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1271
1272 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1273 placesDock->setWidget(placesPanel);
1274 placesPanel->setModel(DolphinSettings::instance().placesModel());
1275 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1276
1277 QAction* placesAction = placesDock->toggleViewAction();
1278 placesAction->setText(i18nc("@title:window", "Places"));
1279 placesAction->setShortcut(Qt::Key_F9);
1280 placesAction->setIcon(KIcon("bookmarks"));
1281 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
1282
1283 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1284 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1285 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1286 connect(this, SIGNAL(urlChanged(KUrl)),
1287 placesPanel, SLOT(setUrl(KUrl)));
1288 }
1289
1290 void DolphinMainWindow::updateEditActions()
1291 {
1292 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1293 if (list.isEmpty()) {
1294 stateChanged("has_no_selection");
1295 } else {
1296 stateChanged("has_selection");
1297
1298 KActionCollection* col = actionCollection();
1299 QAction* renameAction = col->action("rename");
1300 QAction* moveToTrashAction = col->action("move_to_trash");
1301 QAction* deleteAction = col->action("delete");
1302 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1303 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1304
1305 KonqFileItemCapabilities capabilities(list);
1306 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1307
1308 renameAction->setEnabled(capabilities.supportsMoving());
1309 moveToTrashAction->setEnabled(enableMoveToTrash);
1310 deleteAction->setEnabled(capabilities.supportsDeleting());
1311 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1312 cutAction->setEnabled(capabilities.supportsMoving());
1313 }
1314 updatePasteAction();
1315 }
1316
1317 void DolphinMainWindow::updateViewActions()
1318 {
1319 m_actionHandler->updateViewActions();
1320
1321 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1322 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1323
1324 updateSplitAction();
1325
1326 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1327 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1328 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1329 }
1330
1331 void DolphinMainWindow::updateGoActions()
1332 {
1333 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1334 const KUrl& currentUrl = m_activeViewContainer->url();
1335 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1336 }
1337
1338 void DolphinMainWindow::rememberClosedTab(int index)
1339 {
1340 KMenu* tabsMenu = m_recentTabsMenu->menu();
1341
1342 const QString primaryPath = m_viewTab[index].primaryView->url().path();
1343 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
1344
1345 const QFontMetrics fm = fontMetrics();
1346 const QString actionText = fm.elidedText(primaryPath, Qt::ElideMiddle, fm.maxWidth() * 20);
1347
1348 QAction* action = new QAction(actionText, tabsMenu);
1349
1350 ClosedTab closedTab;
1351 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
1352
1353 if (m_viewTab[index].secondaryView != 0) {
1354 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
1355 closedTab.isSplit = true;
1356 } else {
1357 closedTab.isSplit = false;
1358 }
1359
1360 action->setData(QVariant::fromValue(closedTab));
1361 action->setIcon(KIcon(iconName));
1362
1363 // add the closed tab menu entry after the separator and
1364 // "Empty Recently Closed Tabs" entry
1365 if (tabsMenu->actions().size() == 2) {
1366 tabsMenu->addAction(action);
1367 } else {
1368 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
1369 }
1370
1371 // assure that only up to 8 closed tabs are shown in the menu
1372 if (tabsMenu->actions().size() > 8) {
1373 tabsMenu->removeAction(tabsMenu->actions().last());
1374 }
1375 actionCollection()->action("closed_tabs")->setEnabled(true);
1376 KAcceleratorManager::manage(tabsMenu);
1377 }
1378
1379 void DolphinMainWindow::clearStatusBar()
1380 {
1381 m_activeViewContainer->statusBar()->clear();
1382 }
1383
1384 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1385 {
1386 connect(container, SIGNAL(showFilterBarChanged(bool)),
1387 this, SLOT(updateFilterBarAction(bool)));
1388
1389 DolphinView* view = container->view();
1390 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1391 this, SLOT(slotSelectionChanged(KFileItemList)));
1392 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1393 this, SLOT(slotRequestItemInfo(KFileItem)));
1394 connect(view, SIGNAL(activated()),
1395 this, SLOT(toggleActiveView()));
1396 connect(view, SIGNAL(tabRequested(const KUrl&)),
1397 this, SLOT(openNewTab(const KUrl&)));
1398
1399 const KUrlNavigator* navigator = container->urlNavigator();
1400 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1401 this, SLOT(changeUrl(const KUrl&)));
1402 connect(navigator, SIGNAL(historyChanged()),
1403 this, SLOT(updateHistory()));
1404 connect(navigator, SIGNAL(editableStateChanged(bool)),
1405 this, SLOT(slotEditableStateChanged(bool)));
1406 }
1407
1408 void DolphinMainWindow::updateSplitAction()
1409 {
1410 QAction* splitAction = actionCollection()->action("split_view");
1411 if (m_viewTab[m_tabIndex].secondaryView != 0) {
1412 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
1413 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1414 splitAction->setIcon(KIcon("view-right-close"));
1415 } else {
1416 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1417 splitAction->setIcon(KIcon("view-left-close"));
1418 }
1419 } else {
1420 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1421 splitAction->setIcon(KIcon("view-right-new"));
1422 }
1423 }
1424
1425 QString DolphinMainWindow::tabName(const KUrl& url) const
1426 {
1427 QString name;
1428 if (url.equals(KUrl("file:///"))) {
1429 name = "/";
1430 } else {
1431 name = url.fileName();
1432 if (name.isEmpty()) {
1433 name = url.protocol();
1434 } else {
1435 // Make sure that a '&' inside the directory name is displayed correctly
1436 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
1437 name.replace('&', "&&");
1438 }
1439 }
1440 return name;
1441 }
1442
1443 bool DolphinMainWindow::isKompareInstalled() const
1444 {
1445 static bool initialized = false;
1446 static bool installed = false;
1447 if (!initialized) {
1448 // TODO: maybe replace this approach later by using a menu
1449 // plugin like kdiff3plugin.cpp
1450 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
1451 initialized = true;
1452 }
1453 return installed;
1454 }
1455
1456 void DolphinMainWindow::createSecondaryView(int tabIndex)
1457 {
1458 QSplitter* splitter = m_viewTab[tabIndex].splitter;
1459 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
1460
1461 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
1462 m_viewTab[tabIndex].secondaryView = new DolphinViewContainer(this, 0, view->rootUrl());
1463 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
1464 splitter->setSizes(QList<int>() << newWidth << newWidth);
1465 connectViewSignals(m_viewTab[tabIndex].secondaryView);
1466 m_viewTab[tabIndex].secondaryView->view()->reload();
1467 m_viewTab[tabIndex].secondaryView->setActive(false);
1468 m_viewTab[tabIndex].secondaryView->show();
1469 }
1470
1471 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1472 KIO::FileUndoManager::UiInterface()
1473 {
1474 }
1475
1476 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1477 {
1478 }
1479
1480 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1481 {
1482 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1483 if (mainWin) {
1484 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
1485 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1486 } else {
1487 KIO::FileUndoManager::UiInterface::jobError(job);
1488 }
1489 }
1490
1491 #include "dolphinmainwindow.moc"