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