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