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