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