]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Don't show the search options, if no Nepomuk is available. Hopefully Nepomuk will...
[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(const KUrl& url)
1007 {
1008 m_activeViewContainer->setUrl(url);
1009 }
1010
1011 void DolphinMainWindow::slotTabMoved(int from, int to)
1012 {
1013 m_viewTab.move(from, to);
1014 m_tabIndex = m_tabBar->currentIndex();
1015 }
1016
1017 void DolphinMainWindow::showSearchOptions()
1018 {
1019 #ifdef HAVE_NEPOMUK
1020 m_searchOptionsConfigurator->show();
1021 #endif
1022 }
1023
1024 void DolphinMainWindow::init()
1025 {
1026 DolphinSettings& settings = DolphinSettings::instance();
1027
1028 // Check whether Dolphin runs the first time. If yes then
1029 // a proper default window size is given at the end of DolphinMainWindow::init().
1030 GeneralSettings* generalSettings = settings.generalSettings();
1031 const bool firstRun = generalSettings->firstRun();
1032 if (firstRun) {
1033 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1034 }
1035
1036 setAcceptDrops(true);
1037
1038 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1039 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1040
1041 setupActions();
1042
1043 const KUrl& homeUrl = generalSettings->homeUrl();
1044 setUrlAsCaption(homeUrl);
1045 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1046 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1047 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1048 ViewProperties props(homeUrl);
1049 m_viewTab[m_tabIndex].primaryView = new DolphinViewContainer(this,
1050 m_viewTab[m_tabIndex].splitter,
1051 homeUrl);
1052
1053 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1054 connectViewSignals(m_activeViewContainer);
1055 DolphinView* view = m_activeViewContainer->view();
1056 view->reload();
1057 m_activeViewContainer->show();
1058 m_actionHandler->setCurrentView(view);
1059
1060 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1061 connect(this, SIGNAL(urlChanged(const KUrl&)),
1062 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1063
1064 #ifdef HAVE_NEPOMUK
1065 m_searchOptionsConfigurator = new DolphinSearchOptionsConfigurator(this);
1066 m_searchOptionsConfigurator->hide();
1067 #endif
1068
1069 m_tabBar = new KTabBar(this);
1070 m_tabBar->setMovable(true);
1071 m_tabBar->setTabsClosable(true);
1072 connect(m_tabBar, SIGNAL(currentChanged(int)),
1073 this, SLOT(setActiveTab(int)));
1074 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1075 this, SLOT(closeTab(int)));
1076 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1077 this, SLOT(openTabContextMenu(int, const QPoint&)));
1078 connect(m_tabBar, SIGNAL(newTabRequest()),
1079 this, SLOT(openNewTab()));
1080 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1081 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1082 connect(m_tabBar, SIGNAL(wheelDelta(int)),
1083 this, SLOT(slotWheelMoved(int)));
1084 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1085 this, SLOT(closeTab(int)));
1086 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1087 this, SLOT(slotTabMoved(int, int)));
1088
1089 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1090
1091 QWidget* centralWidget = new QWidget(this);
1092 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1093 m_centralWidgetLayout->setSpacing(0);
1094 m_centralWidgetLayout->setMargin(0);
1095 #ifdef HAVE_NEPOMUK
1096 m_centralWidgetLayout->addWidget(m_searchOptionsConfigurator);
1097 #endif
1098 m_centralWidgetLayout->addWidget(m_tabBar);
1099 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1100
1101 setCentralWidget(centralWidget);
1102 setupDockWidgets();
1103 emit urlChanged(homeUrl);
1104
1105 setupGUI(Keys | Save | Create | ToolBar);
1106
1107 m_searchBox->setParent(toolBar("searchToolBar"));
1108 m_searchBox->show();
1109 connect(m_searchBox, SIGNAL(requestSearchOptions()),
1110 this, SLOT(showSearchOptions()));
1111
1112 stateChanged("new_file");
1113
1114 QClipboard* clipboard = QApplication::clipboard();
1115 connect(clipboard, SIGNAL(dataChanged()),
1116 this, SLOT(updatePasteAction()));
1117 updatePasteAction();
1118 updateGoActions();
1119
1120 if (generalSettings->splitView()) {
1121 toggleSplitView();
1122 }
1123 updateViewActions();
1124
1125 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1126 showFilterBarAction->setChecked(generalSettings->filterBar());
1127
1128 if (firstRun) {
1129 // assure a proper default size if Dolphin runs the first time
1130 resize(750, 500);
1131 }
1132
1133 m_showMenuBar->setChecked(!menuBar()->isHidden()); // workaround for bug #171080
1134 }
1135
1136 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1137 {
1138 Q_ASSERT(viewContainer != 0);
1139 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1140 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1141 if (m_activeViewContainer == viewContainer) {
1142 return;
1143 }
1144
1145 m_activeViewContainer->setActive(false);
1146 m_activeViewContainer = viewContainer;
1147
1148 // Activating the view container might trigger a recursive setActiveViewContainer() call
1149 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1150 // disconnect the activated() signal in this case:
1151 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1152 m_activeViewContainer->setActive(true);
1153 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1154
1155 m_actionHandler->setCurrentView(viewContainer->view());
1156
1157 updateHistory();
1158 updateEditActions();
1159 updateViewActions();
1160 updateGoActions();
1161
1162 const KUrl& url = m_activeViewContainer->url();
1163 setUrlAsCaption(url);
1164 if (m_viewTab.count() > 1 && m_viewTab[m_tabIndex].secondaryView != 0) {
1165 m_tabBar->setTabText(m_tabIndex, tabName(url));
1166 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1167 }
1168
1169 emit urlChanged(url);
1170 }
1171
1172 void DolphinMainWindow::setupActions()
1173 {
1174 // setup 'File' menu
1175 m_newMenu = new DolphinNewMenu(this, this);
1176 KMenu* menu = m_newMenu->menu();
1177 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1178 menu->setIcon(KIcon("document-new"));
1179 connect(menu, SIGNAL(aboutToShow()),
1180 this, SLOT(updateNewMenu()));
1181
1182 KAction* newWindow = actionCollection()->addAction("new_window");
1183 newWindow->setIcon(KIcon("window-new"));
1184 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1185 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1186 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1187
1188 KAction* newTab = actionCollection()->addAction("new_tab");
1189 newTab->setIcon(KIcon("tab-new"));
1190 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1191 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1192 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1193
1194 KAction* closeTab = actionCollection()->addAction("close_tab");
1195 closeTab->setIcon(KIcon("tab-close"));
1196 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1197 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1198 closeTab->setEnabled(false);
1199 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1200
1201 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1202
1203 // setup 'Edit' menu
1204 KStandardAction::undo(this,
1205 SLOT(undo()),
1206 actionCollection());
1207
1208 // need to remove shift+del from cut action, else the shortcut for deletejob
1209 // doesn't work
1210 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1211 KShortcut cutShortcut = cut->shortcut();
1212 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
1213 cut->setShortcut(cutShortcut);
1214 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1215 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1216 // The text of the paste-action is modified dynamically by Dolphin
1217 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1218 // due to the long text, the text "Paste" is used:
1219 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1220
1221 KAction* selectAll = actionCollection()->addAction("select_all");
1222 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1223 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1224 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1225
1226 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1227 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1228 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1229 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1230
1231 // setup 'View' menu
1232 // (note that most of it is set up in DolphinViewActionHandler)
1233
1234 KAction* split = actionCollection()->addAction("split_view");
1235 split->setShortcut(Qt::Key_F3);
1236 updateSplitAction();
1237 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1238
1239 KAction* reload = actionCollection()->addAction("reload");
1240 reload->setText(i18nc("@action:inmenu View", "Reload"));
1241 reload->setShortcut(Qt::Key_F5);
1242 reload->setIcon(KIcon("view-refresh"));
1243 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1244
1245 KAction* stop = actionCollection()->addAction("stop");
1246 stop->setText(i18nc("@action:inmenu View", "Stop"));
1247 stop->setToolTip(i18nc("@info", "Stop loading"));
1248 stop->setIcon(KIcon("process-stop"));
1249 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1250
1251 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1252 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1253 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1254 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1255
1256 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1257 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1258 replaceLocation->setShortcut(Qt::Key_F6);
1259 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1260
1261 // setup 'Go' menu
1262 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1263 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1264 KShortcut backShortcut = backAction->shortcut();
1265 backShortcut.setAlternate(Qt::Key_Backspace);
1266 backAction->setShortcut(backShortcut);
1267
1268 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1269 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1270 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1271 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1272 this, SLOT(restoreClosedTab(QAction *)));
1273
1274 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1275 action->setIcon(KIcon("edit-clear-list"));
1276 action->setData(QVariant::fromValue(true));
1277 m_recentTabsMenu->addAction(action);
1278 m_recentTabsMenu->addSeparator();
1279 m_recentTabsMenu->setEnabled(false);
1280
1281 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1282 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1283
1284 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1285 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1286
1287 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1288
1289 // setup 'Tools' menu
1290 KToggleAction* showSearchBar = actionCollection()->add<KToggleAction>("show_search_bar");
1291 showSearchBar->setText(i18nc("@action:inmenu Tools", "Show Search Bar"));
1292 showSearchBar->setShortcut(Qt::CTRL | Qt::Key_S);
1293 connect(showSearchBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1294
1295 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1296 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1297 showFilterBar->setIcon(KIcon("view-filter"));
1298 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1299 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1300
1301 KAction* compareFiles = actionCollection()->addAction("compare_files");
1302 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1303 compareFiles->setIcon(KIcon("kompare"));
1304 compareFiles->setEnabled(false);
1305 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1306
1307 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1308 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1309 openTerminal->setIcon(KIcon("utilities-terminal"));
1310 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1311 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1312
1313 // setup 'Settings' menu
1314 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1315 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1316
1317 // not in menu actions
1318 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1319 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1320 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1321 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() :
1322 KStandardShortcut::tabNext());
1323
1324 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1325 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1326 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1327 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() :
1328 KStandardShortcut::tabPrev());
1329
1330 // for context menu
1331 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1332 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1333 openInNewTab->setIcon(KIcon("tab-new"));
1334 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1335
1336 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1337 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1338 openInNewWindow->setIcon(KIcon("window-new"));
1339 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1340
1341 // 'Search' toolbar
1342 m_searchBox = new DolphinSearchBox(this);
1343 connect(m_searchBox, SIGNAL(search(KUrl)), this, SLOT(searchItems(KUrl)));
1344
1345 KAction* search = new KAction(this);
1346 actionCollection()->addAction("search_bar", search);
1347 search->setText(i18nc("@action:inmenu", "Search Bar"));
1348 search->setDefaultWidget(m_searchBox);
1349 search->setShortcutConfigurable(false);
1350 }
1351
1352 void DolphinMainWindow::setupDockWidgets()
1353 {
1354 // setup "Information"
1355 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
1356 infoDock->setObjectName("infoDock");
1357 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1358 Panel* infoPanel = new InformationPanel(infoDock);
1359 infoDock->setWidget(infoPanel);
1360
1361 QAction* infoAction = infoDock->toggleViewAction();
1362 infoAction->setText(i18nc("@title:window", "Information"));
1363 infoAction->setShortcut(Qt::Key_F11);
1364 infoAction->setIcon(KIcon("dialog-information"));
1365 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1366
1367 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1368 connect(this, SIGNAL(urlChanged(KUrl)),
1369 infoPanel, SLOT(setUrl(KUrl)));
1370 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1371 infoPanel, SLOT(setSelection(KFileItemList)));
1372 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1373 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1374
1375 // setup "Folders"
1376 QDockWidget* foldersDock = new QDockWidget(i18nc("@title:window", "Folders"));
1377 foldersDock->setObjectName("foldersDock");
1378 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1379 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1380 foldersDock->setWidget(foldersPanel);
1381
1382 QAction* foldersAction = foldersDock->toggleViewAction();
1383 foldersAction->setText(i18nc("@title:window", "Folders"));
1384 foldersAction->setShortcut(Qt::Key_F7);
1385 foldersAction->setIcon(KIcon("folder"));
1386 actionCollection()->addAction("show_folders_panel", foldersDock->toggleViewAction());
1387
1388 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1389 connect(this, SIGNAL(urlChanged(KUrl)),
1390 foldersPanel, SLOT(setUrl(KUrl)));
1391 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1392 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1393 connect(foldersPanel, SIGNAL(changeSelection(KFileItemList)),
1394 this, SLOT(changeSelection(KFileItemList)));
1395
1396 // setup "Terminal"
1397 #ifndef Q_OS_WIN
1398 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1399 terminalDock->setObjectName("terminalDock");
1400 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1401 Panel* terminalPanel = new TerminalPanel(terminalDock);
1402 terminalDock->setWidget(terminalPanel);
1403
1404 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1405
1406 QAction* terminalAction = terminalDock->toggleViewAction();
1407 terminalAction->setText(i18nc("@title:window Shell terminal", "Terminal"));
1408 terminalAction->setShortcut(Qt::Key_F4);
1409 terminalAction->setIcon(KIcon("utilities-terminal"));
1410 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
1411
1412 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1413 connect(this, SIGNAL(urlChanged(KUrl)),
1414 terminalPanel, SLOT(setUrl(KUrl)));
1415 #endif
1416
1417 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1418 if (firstRun) {
1419 foldersDock->hide();
1420 #ifndef Q_OS_WIN
1421 terminalDock->hide();
1422 #endif
1423 }
1424
1425 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
1426 placesDock->setObjectName("placesDock");
1427 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1428
1429 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1430 placesDock->setWidget(placesPanel);
1431 placesPanel->setModel(DolphinSettings::instance().placesModel());
1432 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1433
1434 QAction* placesAction = placesDock->toggleViewAction();
1435 placesAction->setText(i18nc("@title:window", "Places"));
1436 placesAction->setShortcut(Qt::Key_F9);
1437 placesAction->setIcon(KIcon("bookmarks"));
1438 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
1439
1440 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1441 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1442 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1443 connect(this, SIGNAL(urlChanged(KUrl)),
1444 placesPanel, SLOT(setUrl(KUrl)));
1445 }
1446
1447 void DolphinMainWindow::updateEditActions()
1448 {
1449 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1450 if (list.isEmpty()) {
1451 stateChanged("has_no_selection");
1452 } else {
1453 stateChanged("has_selection");
1454
1455 KActionCollection* col = actionCollection();
1456 QAction* renameAction = col->action("rename");
1457 QAction* moveToTrashAction = col->action("move_to_trash");
1458 QAction* deleteAction = col->action("delete");
1459 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1460 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1461
1462 KFileItemListProperties capabilities(list);
1463 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1464
1465 renameAction->setEnabled(capabilities.supportsMoving());
1466 moveToTrashAction->setEnabled(enableMoveToTrash);
1467 deleteAction->setEnabled(capabilities.supportsDeleting());
1468 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1469 cutAction->setEnabled(capabilities.supportsMoving());
1470 }
1471 updatePasteAction();
1472 }
1473
1474 void DolphinMainWindow::updateViewActions()
1475 {
1476 m_actionHandler->updateViewActions();
1477
1478 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1479 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1480
1481 updateSplitAction();
1482
1483 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1484 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1485 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1486 }
1487
1488 void DolphinMainWindow::updateGoActions()
1489 {
1490 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1491 const KUrl& currentUrl = m_activeViewContainer->url();
1492 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1493 }
1494
1495 void DolphinMainWindow::rememberClosedTab(int index)
1496 {
1497 KMenu* tabsMenu = m_recentTabsMenu->menu();
1498
1499 const QString primaryPath = m_viewTab[index].primaryView->url().path();
1500 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
1501
1502 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
1503
1504 ClosedTab closedTab;
1505 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
1506
1507 if (m_viewTab[index].secondaryView != 0) {
1508 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
1509 closedTab.isSplit = true;
1510 } else {
1511 closedTab.isSplit = false;
1512 }
1513
1514 action->setData(QVariant::fromValue(closedTab));
1515 action->setIcon(KIcon(iconName));
1516
1517 // add the closed tab menu entry after the separator and
1518 // "Empty Recently Closed Tabs" entry
1519 if (tabsMenu->actions().size() == 2) {
1520 tabsMenu->addAction(action);
1521 } else {
1522 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
1523 }
1524
1525 // assure that only up to 8 closed tabs are shown in the menu
1526 if (tabsMenu->actions().size() > 8) {
1527 tabsMenu->removeAction(tabsMenu->actions().last());
1528 }
1529 actionCollection()->action("closed_tabs")->setEnabled(true);
1530 KAcceleratorManager::manage(tabsMenu);
1531 }
1532
1533 void DolphinMainWindow::clearStatusBar()
1534 {
1535 m_activeViewContainer->statusBar()->clear();
1536 }
1537
1538 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1539 {
1540 connect(container, SIGNAL(showFilterBarChanged(bool)),
1541 this, SLOT(updateFilterBarAction(bool)));
1542
1543 DolphinView* view = container->view();
1544 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1545 this, SLOT(slotSelectionChanged(KFileItemList)));
1546 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1547 this, SLOT(slotRequestItemInfo(KFileItem)));
1548 connect(view, SIGNAL(activated()),
1549 this, SLOT(toggleActiveView()));
1550 connect(view, SIGNAL(tabRequested(const KUrl&)),
1551 this, SLOT(openNewTab(const KUrl&)));
1552
1553 const KUrlNavigator* navigator = container->urlNavigator();
1554 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1555 this, SLOT(changeUrl(const KUrl&)));
1556 connect(navigator, SIGNAL(historyChanged()),
1557 this, SLOT(updateHistory()));
1558 connect(navigator, SIGNAL(editableStateChanged(bool)),
1559 this, SLOT(slotEditableStateChanged(bool)));
1560 }
1561
1562 void DolphinMainWindow::updateSplitAction()
1563 {
1564 QAction* splitAction = actionCollection()->action("split_view");
1565 if (m_viewTab[m_tabIndex].secondaryView != 0) {
1566 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
1567 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1568 splitAction->setToolTip(i18nc("@info", "Close right view"));
1569 splitAction->setIcon(KIcon("view-right-close"));
1570 } else {
1571 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1572 splitAction->setToolTip(i18nc("@info", "Close left view"));
1573 splitAction->setIcon(KIcon("view-left-close"));
1574 }
1575 } else {
1576 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1577 splitAction->setToolTip(i18nc("@info", "Split view"));
1578 splitAction->setIcon(KIcon("view-right-new"));
1579 }
1580 }
1581
1582 QString DolphinMainWindow::tabName(const KUrl& url) const
1583 {
1584 QString name;
1585 if (url.equals(KUrl("file:///"))) {
1586 name = '/';
1587 } else {
1588 name = url.fileName();
1589 if (name.isEmpty()) {
1590 name = url.protocol();
1591 } else {
1592 // Make sure that a '&' inside the directory name is displayed correctly
1593 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
1594 name.replace('&', "&&");
1595 }
1596 }
1597 return name;
1598 }
1599
1600 bool DolphinMainWindow::isKompareInstalled() const
1601 {
1602 static bool initialized = false;
1603 static bool installed = false;
1604 if (!initialized) {
1605 // TODO: maybe replace this approach later by using a menu
1606 // plugin like kdiff3plugin.cpp
1607 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
1608 initialized = true;
1609 }
1610 return installed;
1611 }
1612
1613 void DolphinMainWindow::createSecondaryView(int tabIndex)
1614 {
1615 QSplitter* splitter = m_viewTab[tabIndex].splitter;
1616 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
1617
1618 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
1619 m_viewTab[tabIndex].secondaryView = new DolphinViewContainer(this, 0, view->rootUrl());
1620 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
1621 splitter->setSizes(QList<int>() << newWidth << newWidth);
1622 connectViewSignals(m_viewTab[tabIndex].secondaryView);
1623 m_viewTab[tabIndex].secondaryView->view()->reload();
1624 m_viewTab[tabIndex].secondaryView->setActive(false);
1625 m_viewTab[tabIndex].secondaryView->show();
1626 }
1627
1628 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
1629 {
1630 return "Tab " + QString::number(tabIndex) + ' ' + property;
1631 }
1632
1633 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
1634 {
1635 QString caption;
1636 if (url.equals(KUrl("file:///"))) {
1637 caption = '/';
1638 } else {
1639 caption = url.fileName();
1640 if (caption.isEmpty()) {
1641 caption = url.protocol();
1642 }
1643 }
1644
1645 setCaption(caption);
1646 }
1647
1648 QString DolphinMainWindow::squeezedText(const QString& text) const
1649 {
1650 const QFontMetrics fm = fontMetrics();
1651 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
1652 }
1653
1654 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1655 KIO::FileUndoManager::UiInterface()
1656 {
1657 }
1658
1659 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1660 {
1661 }
1662
1663 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1664 {
1665 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1666 if (mainWin) {
1667 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
1668 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1669 } else {
1670 KIO::FileUndoManager::UiInterface::jobError(job);
1671 }
1672 }
1673
1674 #include "dolphinmainwindow.moc"