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