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