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