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