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