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