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