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