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