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