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