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