]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
* Show the search options as soon as the search bar gains focus.
[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::goBack(Qt::MouseButtons buttons)
757 {
758 // The default case (left button pressed) is handled in goBack().
759 if (buttons == Qt::MidButton) {
760 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
761 openNewTab(urlNavigator->historyUrl(urlNavigator->historyIndex() + 1));
762 }
763 }
764
765 void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
766 {
767 // The default case (left button pressed) is handled in goForward().
768 if (buttons == Qt::MidButton) {
769 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
770 openNewTab(urlNavigator->historyUrl(urlNavigator->historyIndex() - 1));
771 }
772 }
773
774 void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
775 {
776 // The default case (left button pressed) is handled in goUp().
777 if (buttons == Qt::MidButton) {
778 openNewTab(activeViewContainer()->url().upUrl());
779 }
780 }
781
782 void DolphinMainWindow::goHome()
783 {
784 clearStatusBar();
785 m_activeViewContainer->urlNavigator()->goHome();
786 }
787
788 void DolphinMainWindow::compareFiles()
789 {
790 // The method is only invoked if exactly 2 files have
791 // been selected. The selected files may be:
792 // - both in the primary view
793 // - both in the secondary view
794 // - one in the primary view and the other in the secondary
795 // view
796 Q_ASSERT(m_viewTab[m_tabIndex].primaryView != 0);
797
798 KUrl urlA;
799 KUrl urlB;
800 KUrl::List urls = m_viewTab[m_tabIndex].primaryView->view()->selectedUrls();
801
802 switch (urls.count()) {
803 case 0: {
804 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView != 0);
805 urls = m_viewTab[m_tabIndex].secondaryView->view()->selectedUrls();
806 Q_ASSERT(urls.count() == 2);
807 urlA = urls[0];
808 urlB = urls[1];
809 break;
810 }
811
812 case 1: {
813 urlA = urls[0];
814 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView != 0);
815 urls = m_viewTab[m_tabIndex].secondaryView->view()->selectedUrls();
816 Q_ASSERT(urls.count() == 1);
817 urlB = urls[0];
818 break;
819 }
820
821 case 2: {
822 urlA = urls[0];
823 urlB = urls[1];
824 break;
825 }
826
827 default: {
828 // may not happen: compareFiles may only get invoked if 2
829 // files are selected
830 Q_ASSERT(false);
831 }
832 }
833
834 QString command("kompare -c \"");
835 command.append(urlA.pathOrUrl());
836 command.append("\" \"");
837 command.append(urlB.pathOrUrl());
838 command.append('\"');
839 KRun::runCommand(command, "Kompare", "kompare", this);
840 }
841
842 void DolphinMainWindow::toggleShowMenuBar()
843 {
844 const bool visible = menuBar()->isVisible();
845 menuBar()->setVisible(!visible);
846 }
847
848 void DolphinMainWindow::openTerminal()
849 {
850 QString dir(QDir::homePath());
851
852 // If the given directory is not local, it can still be the URL of an
853 // ioslave using UDS_LOCAL_PATH which to be converted first.
854 KUrl url = KIO::NetAccess::mostLocalUrl(m_activeViewContainer->url(), this);
855
856 //If the URL is local after the above conversion, set the directory.
857 if (url.isLocalFile()) {
858 dir = url.toLocalFile();
859 }
860
861 KToolInvocation::invokeTerminal(QString(), dir);
862 }
863
864 void DolphinMainWindow::editSettings()
865 {
866 if (m_settingsDialog == 0) {
867 const KUrl& url = activeViewContainer()->url();
868 m_settingsDialog = new DolphinSettingsDialog(url, this);
869 m_settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
870 m_settingsDialog->show();
871 } else {
872 m_settingsDialog->raise();
873 }
874 }
875
876 void DolphinMainWindow::setActiveTab(int index)
877 {
878 Q_ASSERT(index >= 0);
879 Q_ASSERT(index < m_viewTab.count());
880 if (index == m_tabIndex) {
881 return;
882 }
883
884 // hide current tab content
885 ViewTab& hiddenTab = m_viewTab[m_tabIndex];
886 hiddenTab.isPrimaryViewActive = hiddenTab.primaryView->isActive();
887 hiddenTab.primaryView->setActive(false);
888 if (hiddenTab.secondaryView != 0) {
889 hiddenTab.secondaryView->setActive(false);
890 }
891 QSplitter* splitter = m_viewTab[m_tabIndex].splitter;
892 splitter->hide();
893 m_centralWidgetLayout->removeWidget(splitter);
894
895 // show active tab content
896 m_tabIndex = index;
897
898 ViewTab& viewTab = m_viewTab[index];
899 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
900 viewTab.primaryView->show();
901 if (viewTab.secondaryView != 0) {
902 viewTab.secondaryView->show();
903 }
904 viewTab.splitter->show();
905
906 setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
907 viewTab.secondaryView);
908 }
909
910 void DolphinMainWindow::closeTab()
911 {
912 closeTab(m_tabBar->currentIndex());
913 }
914
915 void DolphinMainWindow::closeTab(int index)
916 {
917 Q_ASSERT(index >= 0);
918 Q_ASSERT(index < m_viewTab.count());
919 if (m_viewTab.count() == 1) {
920 // the last tab may never get closed
921 return;
922 }
923
924 if (index == m_tabIndex) {
925 // The tab that should be closed is the active tab. Activate the
926 // previous tab before closing the tab.
927 m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
928 }
929 rememberClosedTab(index);
930
931 // delete tab
932 m_viewTab[index].primaryView->deleteLater();
933 if (m_viewTab[index].secondaryView != 0) {
934 m_viewTab[index].secondaryView->deleteLater();
935 }
936 m_viewTab[index].splitter->deleteLater();
937 m_viewTab.erase(m_viewTab.begin() + index);
938
939 m_tabBar->blockSignals(true);
940 m_tabBar->removeTab(index);
941
942 if (m_tabIndex > index) {
943 m_tabIndex--;
944 Q_ASSERT(m_tabIndex >= 0);
945 }
946
947 // if only one tab is left, also remove the tab entry so that
948 // closing the last tab is not possible
949 if (m_viewTab.count() == 1) {
950 m_tabBar->removeTab(0);
951 actionCollection()->action("close_tab")->setEnabled(false);
952 } else {
953 m_tabBar->blockSignals(false);
954 }
955 }
956
957 void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
958 {
959 KMenu menu(this);
960
961 QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
962 newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
963
964 QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
965
966 QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
967 closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
968 QAction* selectedAction = menu.exec(pos);
969 if (selectedAction == newTabAction) {
970 const ViewTab& tab = m_viewTab[index];
971 Q_ASSERT(tab.primaryView != 0);
972 const KUrl url = (tab.secondaryView != 0) && tab.secondaryView->isActive() ?
973 tab.secondaryView->url() : tab.primaryView->url();
974 openNewTab(url);
975 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
976 } else if (selectedAction == closeOtherTabsAction) {
977 const int count = m_tabBar->count();
978 for (int i = 0; i < index; ++i) {
979 closeTab(0);
980 }
981 for (int i = index + 1; i < count; ++i) {
982 closeTab(1);
983 }
984 } else if (selectedAction == closeTabAction) {
985 closeTab(index);
986 }
987 }
988
989 void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
990 {
991 if (buttons & Qt::MidButton) {
992 openNewTab(url);
993 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
994 } else {
995 changeUrl(url);
996 }
997 }
998
999 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
1000 {
1001 canDecode = KUrl::List::canDecode(event->mimeData());
1002 }
1003
1004 void DolphinMainWindow::searchItems(const KUrl& url)
1005 {
1006 m_activeViewContainer->setUrl(url);
1007 }
1008
1009 void DolphinMainWindow::slotTabMoved(int from, int to)
1010 {
1011 m_viewTab.move(from, to);
1012 m_tabIndex = m_tabBar->currentIndex();
1013 }
1014
1015 void DolphinMainWindow::showSearchOptions()
1016 {
1017 m_searchOptionsConfigurator->show();
1018 }
1019
1020 void DolphinMainWindow::init()
1021 {
1022 DolphinSettings& settings = DolphinSettings::instance();
1023
1024 // Check whether Dolphin runs the first time. If yes then
1025 // a proper default window size is given at the end of DolphinMainWindow::init().
1026 GeneralSettings* generalSettings = settings.generalSettings();
1027 const bool firstRun = generalSettings->firstRun();
1028 if (firstRun) {
1029 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1030 }
1031
1032 setAcceptDrops(true);
1033
1034 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1035 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1036
1037 setupActions();
1038
1039 const KUrl& homeUrl = generalSettings->homeUrl();
1040 setUrlAsCaption(homeUrl);
1041 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1042 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1043 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1044 ViewProperties props(homeUrl);
1045 m_viewTab[m_tabIndex].primaryView = new DolphinViewContainer(this,
1046 m_viewTab[m_tabIndex].splitter,
1047 homeUrl);
1048
1049 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1050 connectViewSignals(m_activeViewContainer);
1051 DolphinView* view = m_activeViewContainer->view();
1052 view->reload();
1053 m_activeViewContainer->show();
1054 m_actionHandler->setCurrentView(view);
1055
1056 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1057 connect(this, SIGNAL(urlChanged(const KUrl&)),
1058 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1059
1060 m_searchOptionsConfigurator = new DolphinSearchOptionsConfigurator(this);
1061 m_searchOptionsConfigurator->hide();
1062
1063 m_tabBar = new KTabBar(this);
1064 m_tabBar->setMovable(true);
1065 m_tabBar->setTabsClosable(true);
1066 connect(m_tabBar, SIGNAL(currentChanged(int)),
1067 this, SLOT(setActiveTab(int)));
1068 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1069 this, SLOT(closeTab(int)));
1070 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1071 this, SLOT(openTabContextMenu(int, const QPoint&)));
1072 connect(m_tabBar, SIGNAL(newTabRequest()),
1073 this, SLOT(openNewTab()));
1074 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1075 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1076 connect(m_tabBar, SIGNAL(wheelDelta(int)),
1077 this, SLOT(slotWheelMoved(int)));
1078 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1079 this, SLOT(closeTab(int)));
1080 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1081 this, SLOT(slotTabMoved(int, int)));
1082
1083 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1084
1085 QWidget* centralWidget = new QWidget(this);
1086 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1087 m_centralWidgetLayout->setSpacing(0);
1088 m_centralWidgetLayout->setMargin(0);
1089 m_centralWidgetLayout->addWidget(m_searchOptionsConfigurator);
1090 m_centralWidgetLayout->addWidget(m_tabBar);
1091 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1092
1093 setCentralWidget(centralWidget);
1094 setupDockWidgets();
1095 emit urlChanged(homeUrl);
1096
1097 setupGUI(Keys | Save | Create | ToolBar);
1098
1099 m_searchBox->setParent(toolBar("searchToolBar"));
1100 m_searchBox->show();
1101 connect(m_searchBox, SIGNAL(requestSearchOptions()),
1102 this, SLOT(showSearchOptions()));
1103
1104 stateChanged("new_file");
1105
1106 QClipboard* clipboard = QApplication::clipboard();
1107 connect(clipboard, SIGNAL(dataChanged()),
1108 this, SLOT(updatePasteAction()));
1109 updatePasteAction();
1110 updateGoActions();
1111
1112 if (generalSettings->splitView()) {
1113 toggleSplitView();
1114 }
1115 updateViewActions();
1116
1117 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1118 showFilterBarAction->setChecked(generalSettings->filterBar());
1119
1120 if (firstRun) {
1121 // assure a proper default size if Dolphin runs the first time
1122 resize(750, 500);
1123 }
1124
1125 m_showMenuBar->setChecked(!menuBar()->isHidden()); // workaround for bug #171080
1126 }
1127
1128 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1129 {
1130 Q_ASSERT(viewContainer != 0);
1131 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1132 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1133 if (m_activeViewContainer == viewContainer) {
1134 return;
1135 }
1136
1137 m_activeViewContainer->setActive(false);
1138 m_activeViewContainer = viewContainer;
1139
1140 // Activating the view container might trigger a recursive setActiveViewContainer() call
1141 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1142 // disconnect the activated() signal in this case:
1143 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1144 m_activeViewContainer->setActive(true);
1145 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1146
1147 m_actionHandler->setCurrentView(viewContainer->view());
1148
1149 updateHistory();
1150 updateEditActions();
1151 updateViewActions();
1152 updateGoActions();
1153
1154 const KUrl& url = m_activeViewContainer->url();
1155 setUrlAsCaption(url);
1156 if (m_viewTab.count() > 1 && m_viewTab[m_tabIndex].secondaryView != 0) {
1157 m_tabBar->setTabText(m_tabIndex, tabName(url));
1158 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1159 }
1160
1161 emit urlChanged(url);
1162 }
1163
1164 void DolphinMainWindow::setupActions()
1165 {
1166 // setup 'File' menu
1167 m_newMenu = new DolphinNewMenu(this, this);
1168 KMenu* menu = m_newMenu->menu();
1169 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1170 menu->setIcon(KIcon("document-new"));
1171 connect(menu, SIGNAL(aboutToShow()),
1172 this, SLOT(updateNewMenu()));
1173
1174 KAction* newWindow = actionCollection()->addAction("new_window");
1175 newWindow->setIcon(KIcon("window-new"));
1176 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1177 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1178 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1179
1180 KAction* newTab = actionCollection()->addAction("new_tab");
1181 newTab->setIcon(KIcon("tab-new"));
1182 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1183 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1184 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1185
1186 KAction* closeTab = actionCollection()->addAction("close_tab");
1187 closeTab->setIcon(KIcon("tab-close"));
1188 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1189 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1190 closeTab->setEnabled(false);
1191 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1192
1193 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1194
1195 // setup 'Edit' menu
1196 KStandardAction::undo(this,
1197 SLOT(undo()),
1198 actionCollection());
1199
1200 // need to remove shift+del from cut action, else the shortcut for deletejob
1201 // doesn't work
1202 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1203 KShortcut cutShortcut = cut->shortcut();
1204 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
1205 cut->setShortcut(cutShortcut);
1206 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1207 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1208 // The text of the paste-action is modified dynamically by Dolphin
1209 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1210 // due to the long text, the text "Paste" is used:
1211 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1212
1213 KAction* selectAll = actionCollection()->addAction("select_all");
1214 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1215 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1216 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1217
1218 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1219 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1220 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1221 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1222
1223 // setup 'View' menu
1224 // (note that most of it is set up in DolphinViewActionHandler)
1225
1226 KAction* split = actionCollection()->addAction("split_view");
1227 split->setShortcut(Qt::Key_F3);
1228 updateSplitAction();
1229 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1230
1231 KAction* reload = actionCollection()->addAction("reload");
1232 reload->setText(i18nc("@action:inmenu View", "Reload"));
1233 reload->setShortcut(Qt::Key_F5);
1234 reload->setIcon(KIcon("view-refresh"));
1235 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1236
1237 KAction* stop = actionCollection()->addAction("stop");
1238 stop->setText(i18nc("@action:inmenu View", "Stop"));
1239 stop->setToolTip(i18nc("@info", "Stop loading"));
1240 stop->setIcon(KIcon("process-stop"));
1241 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1242
1243 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1244 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1245 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1246 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1247
1248 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1249 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1250 replaceLocation->setShortcut(Qt::Key_F6);
1251 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1252
1253 // setup 'Go' menu
1254 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1255 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1256 KShortcut backShortcut = backAction->shortcut();
1257 backShortcut.setAlternate(Qt::Key_Backspace);
1258 backAction->setShortcut(backShortcut);
1259
1260 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1261 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1262 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1263 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1264 this, SLOT(restoreClosedTab(QAction *)));
1265
1266 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1267 action->setIcon(KIcon("edit-clear-list"));
1268 action->setData(QVariant::fromValue(true));
1269 m_recentTabsMenu->addAction(action);
1270 m_recentTabsMenu->addSeparator();
1271 m_recentTabsMenu->setEnabled(false);
1272
1273 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1274 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1275
1276 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1277 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1278
1279 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1280
1281 // setup 'Tools' menu
1282 KToggleAction* showSearchBar = actionCollection()->add<KToggleAction>("show_search_bar");
1283 showSearchBar->setText(i18nc("@action:inmenu Tools", "Show Search Bar"));
1284 showSearchBar->setShortcut(Qt::CTRL | Qt::Key_S);
1285 connect(showSearchBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1286
1287 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1288 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1289 showFilterBar->setIcon(KIcon("view-filter"));
1290 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1291 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1292
1293 KAction* compareFiles = actionCollection()->addAction("compare_files");
1294 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1295 compareFiles->setIcon(KIcon("kompare"));
1296 compareFiles->setEnabled(false);
1297 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1298
1299 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1300 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1301 openTerminal->setIcon(KIcon("utilities-terminal"));
1302 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1303 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1304
1305 // setup 'Settings' menu
1306 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1307 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1308
1309 // not in menu actions
1310 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1311 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1312 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1313 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() :
1314 KStandardShortcut::tabNext());
1315
1316 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1317 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1318 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1319 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() :
1320 KStandardShortcut::tabPrev());
1321
1322 // for context menu
1323 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1324 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1325 openInNewTab->setIcon(KIcon("tab-new"));
1326 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1327
1328 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1329 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1330 openInNewWindow->setIcon(KIcon("window-new"));
1331 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1332
1333 // 'Search' toolbar
1334 m_searchBox = new DolphinSearchBox(this);
1335 connect(m_searchBox, SIGNAL(search(KUrl)), this, SLOT(searchItems(KUrl)));
1336
1337 KAction* search = new KAction(this);
1338 actionCollection()->addAction("search_bar", search);
1339 search->setText(i18nc("@action:inmenu", "Search Bar"));
1340 search->setDefaultWidget(m_searchBox);
1341 search->setShortcutConfigurable(false);
1342 }
1343
1344 void DolphinMainWindow::setupDockWidgets()
1345 {
1346 // setup "Information"
1347 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
1348 infoDock->setObjectName("infoDock");
1349 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1350 Panel* infoPanel = new InformationPanel(infoDock);
1351 infoDock->setWidget(infoPanel);
1352
1353 QAction* infoAction = infoDock->toggleViewAction();
1354 infoAction->setText(i18nc("@title:window", "Information"));
1355 infoAction->setShortcut(Qt::Key_F11);
1356 infoAction->setIcon(KIcon("dialog-information"));
1357 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1358
1359 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1360 connect(this, SIGNAL(urlChanged(KUrl)),
1361 infoPanel, SLOT(setUrl(KUrl)));
1362 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1363 infoPanel, SLOT(setSelection(KFileItemList)));
1364 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1365 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1366
1367 // setup "Folders"
1368 QDockWidget* foldersDock = new QDockWidget(i18nc("@title:window", "Folders"));
1369 foldersDock->setObjectName("foldersDock");
1370 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1371 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1372 foldersDock->setWidget(foldersPanel);
1373
1374 QAction* foldersAction = foldersDock->toggleViewAction();
1375 foldersAction->setText(i18nc("@title:window", "Folders"));
1376 foldersAction->setShortcut(Qt::Key_F7);
1377 foldersAction->setIcon(KIcon("folder"));
1378 actionCollection()->addAction("show_folders_panel", foldersDock->toggleViewAction());
1379
1380 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1381 connect(this, SIGNAL(urlChanged(KUrl)),
1382 foldersPanel, SLOT(setUrl(KUrl)));
1383 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1384 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1385 connect(foldersPanel, SIGNAL(changeSelection(KFileItemList)),
1386 this, SLOT(changeSelection(KFileItemList)));
1387
1388 // setup "Terminal"
1389 #ifndef Q_OS_WIN
1390 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1391 terminalDock->setObjectName("terminalDock");
1392 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1393 Panel* terminalPanel = new TerminalPanel(terminalDock);
1394 terminalDock->setWidget(terminalPanel);
1395
1396 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1397
1398 QAction* terminalAction = terminalDock->toggleViewAction();
1399 terminalAction->setText(i18nc("@title:window Shell terminal", "Terminal"));
1400 terminalAction->setShortcut(Qt::Key_F4);
1401 terminalAction->setIcon(KIcon("utilities-terminal"));
1402 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
1403
1404 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1405 connect(this, SIGNAL(urlChanged(KUrl)),
1406 terminalPanel, SLOT(setUrl(KUrl)));
1407 #endif
1408
1409 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1410 if (firstRun) {
1411 foldersDock->hide();
1412 #ifndef Q_OS_WIN
1413 terminalDock->hide();
1414 #endif
1415 }
1416
1417 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
1418 placesDock->setObjectName("placesDock");
1419 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1420
1421 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1422 placesDock->setWidget(placesPanel);
1423 placesPanel->setModel(DolphinSettings::instance().placesModel());
1424 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1425
1426 QAction* placesAction = placesDock->toggleViewAction();
1427 placesAction->setText(i18nc("@title:window", "Places"));
1428 placesAction->setShortcut(Qt::Key_F9);
1429 placesAction->setIcon(KIcon("bookmarks"));
1430 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
1431
1432 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1433 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1434 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1435 connect(this, SIGNAL(urlChanged(KUrl)),
1436 placesPanel, SLOT(setUrl(KUrl)));
1437 }
1438
1439 void DolphinMainWindow::updateEditActions()
1440 {
1441 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1442 if (list.isEmpty()) {
1443 stateChanged("has_no_selection");
1444 } else {
1445 stateChanged("has_selection");
1446
1447 KActionCollection* col = actionCollection();
1448 QAction* renameAction = col->action("rename");
1449 QAction* moveToTrashAction = col->action("move_to_trash");
1450 QAction* deleteAction = col->action("delete");
1451 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1452 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1453
1454 KFileItemListProperties capabilities(list);
1455 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1456
1457 renameAction->setEnabled(capabilities.supportsMoving());
1458 moveToTrashAction->setEnabled(enableMoveToTrash);
1459 deleteAction->setEnabled(capabilities.supportsDeleting());
1460 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1461 cutAction->setEnabled(capabilities.supportsMoving());
1462 }
1463 updatePasteAction();
1464 }
1465
1466 void DolphinMainWindow::updateViewActions()
1467 {
1468 m_actionHandler->updateViewActions();
1469
1470 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1471 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1472
1473 updateSplitAction();
1474
1475 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1476 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1477 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1478 }
1479
1480 void DolphinMainWindow::updateGoActions()
1481 {
1482 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1483 const KUrl& currentUrl = m_activeViewContainer->url();
1484 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1485 }
1486
1487 void DolphinMainWindow::rememberClosedTab(int index)
1488 {
1489 KMenu* tabsMenu = m_recentTabsMenu->menu();
1490
1491 const QString primaryPath = m_viewTab[index].primaryView->url().path();
1492 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
1493
1494 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
1495
1496 ClosedTab closedTab;
1497 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
1498
1499 if (m_viewTab[index].secondaryView != 0) {
1500 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
1501 closedTab.isSplit = true;
1502 } else {
1503 closedTab.isSplit = false;
1504 }
1505
1506 action->setData(QVariant::fromValue(closedTab));
1507 action->setIcon(KIcon(iconName));
1508
1509 // add the closed tab menu entry after the separator and
1510 // "Empty Recently Closed Tabs" entry
1511 if (tabsMenu->actions().size() == 2) {
1512 tabsMenu->addAction(action);
1513 } else {
1514 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
1515 }
1516
1517 // assure that only up to 8 closed tabs are shown in the menu
1518 if (tabsMenu->actions().size() > 8) {
1519 tabsMenu->removeAction(tabsMenu->actions().last());
1520 }
1521 actionCollection()->action("closed_tabs")->setEnabled(true);
1522 KAcceleratorManager::manage(tabsMenu);
1523 }
1524
1525 void DolphinMainWindow::clearStatusBar()
1526 {
1527 m_activeViewContainer->statusBar()->clear();
1528 }
1529
1530 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1531 {
1532 connect(container, SIGNAL(showFilterBarChanged(bool)),
1533 this, SLOT(updateFilterBarAction(bool)));
1534
1535 DolphinView* view = container->view();
1536 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1537 this, SLOT(slotSelectionChanged(KFileItemList)));
1538 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1539 this, SLOT(slotRequestItemInfo(KFileItem)));
1540 connect(view, SIGNAL(activated()),
1541 this, SLOT(toggleActiveView()));
1542 connect(view, SIGNAL(tabRequested(const KUrl&)),
1543 this, SLOT(openNewTab(const KUrl&)));
1544
1545 const KUrlNavigator* navigator = container->urlNavigator();
1546 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1547 this, SLOT(changeUrl(const KUrl&)));
1548 connect(navigator, SIGNAL(historyChanged()),
1549 this, SLOT(updateHistory()));
1550 connect(navigator, SIGNAL(editableStateChanged(bool)),
1551 this, SLOT(slotEditableStateChanged(bool)));
1552 }
1553
1554 void DolphinMainWindow::updateSplitAction()
1555 {
1556 QAction* splitAction = actionCollection()->action("split_view");
1557 if (m_viewTab[m_tabIndex].secondaryView != 0) {
1558 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
1559 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1560 splitAction->setToolTip(i18nc("@info", "Close right view"));
1561 splitAction->setIcon(KIcon("view-right-close"));
1562 } else {
1563 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1564 splitAction->setToolTip(i18nc("@info", "Close left view"));
1565 splitAction->setIcon(KIcon("view-left-close"));
1566 }
1567 } else {
1568 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1569 splitAction->setToolTip(i18nc("@info", "Split view"));
1570 splitAction->setIcon(KIcon("view-right-new"));
1571 }
1572 }
1573
1574 QString DolphinMainWindow::tabName(const KUrl& url) const
1575 {
1576 QString name;
1577 if (url.equals(KUrl("file:///"))) {
1578 name = '/';
1579 } else {
1580 name = url.fileName();
1581 if (name.isEmpty()) {
1582 name = url.protocol();
1583 } else {
1584 // Make sure that a '&' inside the directory name is displayed correctly
1585 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
1586 name.replace('&', "&&");
1587 }
1588 }
1589 return name;
1590 }
1591
1592 bool DolphinMainWindow::isKompareInstalled() const
1593 {
1594 static bool initialized = false;
1595 static bool installed = false;
1596 if (!initialized) {
1597 // TODO: maybe replace this approach later by using a menu
1598 // plugin like kdiff3plugin.cpp
1599 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
1600 initialized = true;
1601 }
1602 return installed;
1603 }
1604
1605 void DolphinMainWindow::createSecondaryView(int tabIndex)
1606 {
1607 QSplitter* splitter = m_viewTab[tabIndex].splitter;
1608 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
1609
1610 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
1611 m_viewTab[tabIndex].secondaryView = new DolphinViewContainer(this, 0, view->rootUrl());
1612 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
1613 splitter->setSizes(QList<int>() << newWidth << newWidth);
1614 connectViewSignals(m_viewTab[tabIndex].secondaryView);
1615 m_viewTab[tabIndex].secondaryView->view()->reload();
1616 m_viewTab[tabIndex].secondaryView->setActive(false);
1617 m_viewTab[tabIndex].secondaryView->show();
1618 }
1619
1620 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
1621 {
1622 return "Tab " + QString::number(tabIndex) + ' ' + property;
1623 }
1624
1625 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
1626 {
1627 QString caption;
1628 if (url.equals(KUrl("file:///"))) {
1629 caption = '/';
1630 } else {
1631 caption = url.fileName();
1632 if (caption.isEmpty()) {
1633 caption = url.protocol();
1634 }
1635 }
1636
1637 setCaption(caption);
1638 }
1639
1640 QString DolphinMainWindow::squeezedText(const QString& text) const
1641 {
1642 const QFontMetrics fm = fontMetrics();
1643 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
1644 }
1645
1646 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1647 KIO::FileUndoManager::UiInterface()
1648 {
1649 }
1650
1651 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1652 {
1653 }
1654
1655 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1656 {
1657 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1658 if (mainWin) {
1659 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
1660 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1661 } else {
1662 KIO::FileUndoManager::UiInterface::jobError(job);
1663 }
1664 }
1665
1666 #include "dolphinmainwindow.moc"