]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
better fix so that it links, too...
[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->slotCheckUpToDate();
556 m_newMenu->setPopupFiles(activeViewContainer()->url());
557 }
558
559 void DolphinMainWindow::createDirectory()
560 {
561 m_newMenu->setPopupFiles(activeViewContainer()->url());
562 m_newMenu->createDirectory();
563 }
564
565 void DolphinMainWindow::quit()
566 {
567 close();
568 }
569
570 void DolphinMainWindow::showErrorMessage(const QString& message)
571 {
572 if (!message.isEmpty()) {
573 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
574 statusBar->setMessage(message, DolphinStatusBar::Error);
575 }
576 }
577
578 void DolphinMainWindow::slotUndoAvailable(bool available)
579 {
580 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
581 if (undoAction != 0) {
582 undoAction->setEnabled(available);
583 }
584 }
585
586 void DolphinMainWindow::restoreClosedTab(QAction* action)
587 {
588 if (action->data().toBool()) {
589 // clear all actions except the "Empty Recently Closed Tabs"
590 // action and the separator
591 QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
592 const int count = actions.size();
593 for (int i = 2; i < count; ++i) {
594 m_recentTabsMenu->menu()->removeAction(actions.at(i));
595 }
596 } else {
597 const ClosedTab closedTab = action->data().value<ClosedTab>();
598 openNewTab(closedTab.primaryUrl);
599 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
600
601 if (closedTab.isSplit) {
602 // create secondary view
603 toggleSplitView();
604 m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
605 }
606
607 m_recentTabsMenu->removeAction(action);
608 }
609
610 if (m_recentTabsMenu->menu()->actions().count() == 2) {
611 m_recentTabsMenu->setEnabled(false);
612 }
613 }
614
615 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
616 {
617 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
618 if (undoAction != 0) {
619 undoAction->setText(text);
620 }
621 }
622
623 void DolphinMainWindow::undo()
624 {
625 clearStatusBar();
626 KIO::FileUndoManager::self()->uiInterface()->setParentWidget(this);
627 KIO::FileUndoManager::self()->undo();
628 }
629
630 void DolphinMainWindow::cut()
631 {
632 m_activeViewContainer->view()->cutSelectedItems();
633 }
634
635 void DolphinMainWindow::copy()
636 {
637 m_activeViewContainer->view()->copySelectedItems();
638 }
639
640 void DolphinMainWindow::paste()
641 {
642 m_activeViewContainer->view()->paste();
643 }
644
645 void DolphinMainWindow::updatePasteAction()
646 {
647 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
648 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
649 pasteAction->setEnabled(pasteInfo.first);
650 pasteAction->setText(pasteInfo.second);
651 }
652
653 void DolphinMainWindow::selectAll()
654 {
655 clearStatusBar();
656
657 // if the URL navigator is editable and focused, select the whole
658 // URL instead of all items of the view
659
660 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
661 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit(); // krazy:exclude=qclasses
662 const bool selectUrl = urlNavigator->isUrlEditable() &&
663 lineEdit->hasFocus();
664 if (selectUrl) {
665 lineEdit->selectAll();
666 } else {
667 m_activeViewContainer->view()->selectAll();
668 }
669 }
670
671 void DolphinMainWindow::invertSelection()
672 {
673 clearStatusBar();
674 m_activeViewContainer->view()->invertSelection();
675 }
676
677 void DolphinMainWindow::toggleSplitView()
678 {
679 if (m_viewTab[m_tabIndex].secondaryView == 0) {
680 createSecondaryView(m_tabIndex);
681 setActiveViewContainer(m_viewTab[m_tabIndex].secondaryView);
682 } else if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
683 // remove secondary view
684 m_viewTab[m_tabIndex].secondaryView->close();
685 m_viewTab[m_tabIndex].secondaryView->deleteLater();
686 m_viewTab[m_tabIndex].secondaryView = 0;
687
688 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
689 } else {
690 // The primary view is active and should be closed. Hence from a users point of view
691 // the content of the secondary view should be moved to the primary view.
692 // From an implementation point of view it is more efficient to close
693 // the primary view and exchange the internal pointers afterwards.
694
695 m_viewTab[m_tabIndex].primaryView->close();
696 m_viewTab[m_tabIndex].primaryView->deleteLater();
697 m_viewTab[m_tabIndex].primaryView = m_viewTab[m_tabIndex].secondaryView;
698 m_viewTab[m_tabIndex].secondaryView = 0;
699
700 setActiveViewContainer(m_viewTab[m_tabIndex].primaryView);
701 }
702
703 updateViewActions();
704 }
705
706 void DolphinMainWindow::reloadView()
707 {
708 clearStatusBar();
709 m_activeViewContainer->view()->reload();
710 }
711
712 void DolphinMainWindow::stopLoading()
713 {
714 }
715
716 void DolphinMainWindow::toggleFilterBarVisibility(bool show)
717 {
718 m_activeViewContainer->showFilterBar(show);
719 }
720
721 void DolphinMainWindow::toggleEditLocation()
722 {
723 clearStatusBar();
724
725 QAction* action = actionCollection()->action("editable_location");
726 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
727 urlNavigator->setUrlEditable(action->isChecked());
728 }
729
730 void DolphinMainWindow::replaceLocation()
731 {
732 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
733 navigator->setUrlEditable(true);
734 navigator->setFocus();
735
736 // select the whole text of the combo box editor
737 QLineEdit* lineEdit = navigator->editor()->lineEdit(); // krazy:exclude=qclasses
738 const QString text = lineEdit->text();
739 lineEdit->setSelection(0, text.length());
740 }
741
742 void DolphinMainWindow::goBack()
743 {
744 clearStatusBar();
745 m_activeViewContainer->urlNavigator()->goBack();
746 }
747
748 void DolphinMainWindow::goForward()
749 {
750 clearStatusBar();
751 m_activeViewContainer->urlNavigator()->goForward();
752 }
753
754 void DolphinMainWindow::goUp()
755 {
756 clearStatusBar();
757 m_activeViewContainer->urlNavigator()->goUp();
758 }
759
760 void DolphinMainWindow::goBack(Qt::MouseButtons buttons)
761 {
762 // The default case (left button pressed) is handled in goBack().
763 if (buttons == Qt::MidButton) {
764 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
765 openNewTab(urlNavigator->historyUrl(urlNavigator->historyIndex() + 1));
766 }
767 }
768
769 void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
770 {
771 // The default case (left button pressed) is handled in goForward().
772 if (buttons == Qt::MidButton) {
773 KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
774 openNewTab(urlNavigator->historyUrl(urlNavigator->historyIndex() - 1));
775 }
776 }
777
778 void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
779 {
780 // The default case (left button pressed) is handled in goUp().
781 if (buttons == Qt::MidButton) {
782 openNewTab(activeViewContainer()->url().upUrl());
783 }
784 }
785
786 void DolphinMainWindow::goHome()
787 {
788 clearStatusBar();
789 m_activeViewContainer->urlNavigator()->goHome();
790 }
791
792 void DolphinMainWindow::compareFiles()
793 {
794 // The method is only invoked if exactly 2 files have
795 // been selected. The selected files may be:
796 // - both in the primary view
797 // - both in the secondary view
798 // - one in the primary view and the other in the secondary
799 // view
800 Q_ASSERT(m_viewTab[m_tabIndex].primaryView != 0);
801
802 KUrl urlA;
803 KUrl urlB;
804 KUrl::List urls = m_viewTab[m_tabIndex].primaryView->view()->selectedUrls();
805
806 switch (urls.count()) {
807 case 0: {
808 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView != 0);
809 urls = m_viewTab[m_tabIndex].secondaryView->view()->selectedUrls();
810 Q_ASSERT(urls.count() == 2);
811 urlA = urls[0];
812 urlB = urls[1];
813 break;
814 }
815
816 case 1: {
817 urlA = urls[0];
818 Q_ASSERT(m_viewTab[m_tabIndex].secondaryView != 0);
819 urls = m_viewTab[m_tabIndex].secondaryView->view()->selectedUrls();
820 Q_ASSERT(urls.count() == 1);
821 urlB = urls[0];
822 break;
823 }
824
825 case 2: {
826 urlA = urls[0];
827 urlB = urls[1];
828 break;
829 }
830
831 default: {
832 // may not happen: compareFiles may only get invoked if 2
833 // files are selected
834 Q_ASSERT(false);
835 }
836 }
837
838 QString command("kompare -c \"");
839 command.append(urlA.pathOrUrl());
840 command.append("\" \"");
841 command.append(urlB.pathOrUrl());
842 command.append('\"');
843 KRun::runCommand(command, "Kompare", "kompare", this);
844 }
845
846 void DolphinMainWindow::toggleShowMenuBar()
847 {
848 const bool visible = menuBar()->isVisible();
849 menuBar()->setVisible(!visible);
850 }
851
852 void DolphinMainWindow::openTerminal()
853 {
854 QString dir(QDir::homePath());
855
856 // If the given directory is not local, it can still be the URL of an
857 // ioslave using UDS_LOCAL_PATH which to be converted first.
858 KUrl url = KIO::NetAccess::mostLocalUrl(m_activeViewContainer->url(), this);
859
860 //If the URL is local after the above conversion, set the directory.
861 if (url.isLocalFile()) {
862 dir = url.toLocalFile();
863 }
864
865 KToolInvocation::invokeTerminal(QString(), dir);
866 }
867
868 void DolphinMainWindow::editSettings()
869 {
870 if (m_settingsDialog == 0) {
871 const KUrl& url = activeViewContainer()->url();
872 m_settingsDialog = new DolphinSettingsDialog(url, this);
873 m_settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
874 m_settingsDialog->show();
875 } else {
876 m_settingsDialog->raise();
877 }
878 }
879
880 void DolphinMainWindow::setActiveTab(int index)
881 {
882 Q_ASSERT(index >= 0);
883 Q_ASSERT(index < m_viewTab.count());
884 if (index == m_tabIndex) {
885 return;
886 }
887
888 // hide current tab content
889 ViewTab& hiddenTab = m_viewTab[m_tabIndex];
890 hiddenTab.isPrimaryViewActive = hiddenTab.primaryView->isActive();
891 hiddenTab.primaryView->setActive(false);
892 if (hiddenTab.secondaryView != 0) {
893 hiddenTab.secondaryView->setActive(false);
894 }
895 QSplitter* splitter = m_viewTab[m_tabIndex].splitter;
896 splitter->hide();
897 m_centralWidgetLayout->removeWidget(splitter);
898
899 // show active tab content
900 m_tabIndex = index;
901
902 ViewTab& viewTab = m_viewTab[index];
903 m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
904 viewTab.primaryView->show();
905 if (viewTab.secondaryView != 0) {
906 viewTab.secondaryView->show();
907 }
908 viewTab.splitter->show();
909
910 setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
911 viewTab.secondaryView);
912 }
913
914 void DolphinMainWindow::closeTab()
915 {
916 closeTab(m_tabBar->currentIndex());
917 }
918
919 void DolphinMainWindow::closeTab(int index)
920 {
921 Q_ASSERT(index >= 0);
922 Q_ASSERT(index < m_viewTab.count());
923 if (m_viewTab.count() == 1) {
924 // the last tab may never get closed
925 return;
926 }
927
928 if (index == m_tabIndex) {
929 // The tab that should be closed is the active tab. Activate the
930 // previous tab before closing the tab.
931 m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
932 }
933 rememberClosedTab(index);
934
935 // delete tab
936 m_viewTab[index].primaryView->deleteLater();
937 if (m_viewTab[index].secondaryView != 0) {
938 m_viewTab[index].secondaryView->deleteLater();
939 }
940 m_viewTab[index].splitter->deleteLater();
941 m_viewTab.erase(m_viewTab.begin() + index);
942
943 m_tabBar->blockSignals(true);
944 m_tabBar->removeTab(index);
945
946 if (m_tabIndex > index) {
947 m_tabIndex--;
948 Q_ASSERT(m_tabIndex >= 0);
949 }
950
951 // if only one tab is left, also remove the tab entry so that
952 // closing the last tab is not possible
953 if (m_viewTab.count() == 1) {
954 m_tabBar->removeTab(0);
955 actionCollection()->action("close_tab")->setEnabled(false);
956 } else {
957 m_tabBar->blockSignals(false);
958 }
959 }
960
961 void DolphinMainWindow::openTabContextMenu(int index, const QPoint& pos)
962 {
963 KMenu menu(this);
964
965 QAction* newTabAction = menu.addAction(KIcon("tab-new"), i18nc("@action:inmenu", "New Tab"));
966 newTabAction->setShortcut(actionCollection()->action("new_tab")->shortcut());
967
968 QAction* closeOtherTabsAction = menu.addAction(KIcon("tab-close-other"), i18nc("@action:inmenu", "Close Other Tabs"));
969
970 QAction* closeTabAction = menu.addAction(KIcon("tab-close"), i18nc("@action:inmenu", "Close Tab"));
971 closeTabAction->setShortcut(actionCollection()->action("close_tab")->shortcut());
972 QAction* selectedAction = menu.exec(pos);
973 if (selectedAction == newTabAction) {
974 const ViewTab& tab = m_viewTab[index];
975 Q_ASSERT(tab.primaryView != 0);
976 const KUrl url = (tab.secondaryView != 0) && tab.secondaryView->isActive() ?
977 tab.secondaryView->url() : tab.primaryView->url();
978 openNewTab(url);
979 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
980 } else if (selectedAction == closeOtherTabsAction) {
981 const int count = m_tabBar->count();
982 for (int i = 0; i < index; ++i) {
983 closeTab(0);
984 }
985 for (int i = index + 1; i < count; ++i) {
986 closeTab(1);
987 }
988 } else if (selectedAction == closeTabAction) {
989 closeTab(index);
990 }
991 }
992
993 void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
994 {
995 if (buttons & Qt::MidButton) {
996 openNewTab(url);
997 m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
998 } else {
999 changeUrl(url);
1000 }
1001 }
1002
1003 void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
1004 {
1005 canDecode = KUrl::List::canDecode(event->mimeData());
1006 }
1007
1008 void DolphinMainWindow::searchItems()
1009 {
1010 #ifdef HAVE_NEPOMUK
1011 const QString searchOptions = m_searchOptionsConfigurator->options();
1012
1013 QString searchString = m_searchBox->text();
1014 if (!searchString.isEmpty() && !searchOptions.isEmpty()) {
1015 searchString += ' ' + searchOptions;
1016 } else if (!searchOptions.isEmpty()) {
1017 searchString += searchOptions;
1018 }
1019
1020 if (!searchString.isEmpty()) {
1021 m_activeViewContainer->setUrl(KUrl("nepomuksearch:/" + searchString));
1022 }
1023 #endif
1024 }
1025
1026 void DolphinMainWindow::slotTabMoved(int from, int to)
1027 {
1028 m_viewTab.move(from, to);
1029 m_tabIndex = m_tabBar->currentIndex();
1030 }
1031
1032 void DolphinMainWindow::showSearchOptions()
1033 {
1034 #ifdef HAVE_NEPOMUK
1035 m_searchOptionsConfigurator->show();
1036 #endif
1037 }
1038
1039 void DolphinMainWindow::init()
1040 {
1041 DolphinSettings& settings = DolphinSettings::instance();
1042
1043 // Check whether Dolphin runs the first time. If yes then
1044 // a proper default window size is given at the end of DolphinMainWindow::init().
1045 GeneralSettings* generalSettings = settings.generalSettings();
1046 const bool firstRun = generalSettings->firstRun();
1047 if (firstRun) {
1048 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
1049 }
1050
1051 setAcceptDrops(true);
1052
1053 m_viewTab[m_tabIndex].splitter = new QSplitter(this);
1054 m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
1055
1056 setupActions();
1057
1058 const KUrl& homeUrl = generalSettings->homeUrl();
1059 setUrlAsCaption(homeUrl);
1060 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
1061 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
1062 connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
1063 ViewProperties props(homeUrl);
1064 m_viewTab[m_tabIndex].primaryView = new DolphinViewContainer(this,
1065 m_viewTab[m_tabIndex].splitter,
1066 homeUrl);
1067
1068 m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
1069 connectViewSignals(m_activeViewContainer);
1070 DolphinView* view = m_activeViewContainer->view();
1071 view->reload();
1072 m_activeViewContainer->show();
1073 m_actionHandler->setCurrentView(view);
1074
1075 m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler);
1076 connect(this, SIGNAL(urlChanged(const KUrl&)),
1077 m_remoteEncoding, SLOT(slotAboutToOpenUrl()));
1078
1079 #ifdef HAVE_NEPOMUK
1080 m_searchOptionsConfigurator = new DolphinSearchOptionsConfigurator(this);
1081 m_searchOptionsConfigurator->hide();
1082 connect(m_searchOptionsConfigurator, SIGNAL(searchOptionsChanged(QString)),
1083 this, SLOT(searchItems()));
1084 #endif
1085
1086 m_tabBar = new KTabBar(this);
1087 m_tabBar->setMovable(true);
1088 m_tabBar->setTabsClosable(true);
1089 connect(m_tabBar, SIGNAL(currentChanged(int)),
1090 this, SLOT(setActiveTab(int)));
1091 connect(m_tabBar, SIGNAL(tabCloseRequested(int)),
1092 this, SLOT(closeTab(int)));
1093 connect(m_tabBar, SIGNAL(contextMenu(int, const QPoint&)),
1094 this, SLOT(openTabContextMenu(int, const QPoint&)));
1095 connect(m_tabBar, SIGNAL(newTabRequest()),
1096 this, SLOT(openNewTab()));
1097 connect(m_tabBar, SIGNAL(testCanDecode(const QDragMoveEvent*, bool&)),
1098 this, SLOT(slotTestCanDecode(const QDragMoveEvent*, bool&)));
1099 connect(m_tabBar, SIGNAL(wheelDelta(int)),
1100 this, SLOT(slotWheelMoved(int)));
1101 connect(m_tabBar, SIGNAL(mouseMiddleClick(int)),
1102 this, SLOT(closeTab(int)));
1103 connect(m_tabBar, SIGNAL(tabMoved(int, int)),
1104 this, SLOT(slotTabMoved(int, int)));
1105
1106 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
1107
1108 QWidget* centralWidget = new QWidget(this);
1109 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
1110 m_centralWidgetLayout->setSpacing(0);
1111 m_centralWidgetLayout->setMargin(0);
1112 #ifdef HAVE_NEPOMUK
1113 m_centralWidgetLayout->addWidget(m_searchOptionsConfigurator);
1114 #endif
1115 m_centralWidgetLayout->addWidget(m_tabBar);
1116 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
1117
1118 setCentralWidget(centralWidget);
1119 setupDockWidgets();
1120 emit urlChanged(homeUrl);
1121
1122 setupGUI(Keys | Save | Create | ToolBar);
1123
1124 m_searchBox->setParent(toolBar("searchToolBar"));
1125 m_searchBox->show();
1126 connect(m_searchBox, SIGNAL(requestSearchOptions()),
1127 this, SLOT(showSearchOptions()));
1128
1129 stateChanged("new_file");
1130
1131 QClipboard* clipboard = QApplication::clipboard();
1132 connect(clipboard, SIGNAL(dataChanged()),
1133 this, SLOT(updatePasteAction()));
1134 updatePasteAction();
1135 updateGoActions();
1136
1137 if (generalSettings->splitView()) {
1138 toggleSplitView();
1139 }
1140 updateViewActions();
1141
1142 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1143 showFilterBarAction->setChecked(generalSettings->filterBar());
1144
1145 if (firstRun) {
1146 // assure a proper default size if Dolphin runs the first time
1147 resize(750, 500);
1148 }
1149
1150 m_showMenuBar->setChecked(!menuBar()->isHidden()); // workaround for bug #171080
1151 }
1152
1153 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
1154 {
1155 Q_ASSERT(viewContainer != 0);
1156 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
1157 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
1158 if (m_activeViewContainer == viewContainer) {
1159 return;
1160 }
1161
1162 m_activeViewContainer->setActive(false);
1163 m_activeViewContainer = viewContainer;
1164
1165 // Activating the view container might trigger a recursive setActiveViewContainer() call
1166 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
1167 // disconnect the activated() signal in this case:
1168 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1169 m_activeViewContainer->setActive(true);
1170 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
1171
1172 m_actionHandler->setCurrentView(viewContainer->view());
1173
1174 updateHistory();
1175 updateEditActions();
1176 updateViewActions();
1177 updateGoActions();
1178
1179 const KUrl& url = m_activeViewContainer->url();
1180 setUrlAsCaption(url);
1181 if (m_viewTab.count() > 1 && m_viewTab[m_tabIndex].secondaryView != 0) {
1182 m_tabBar->setTabText(m_tabIndex, tabName(url));
1183 m_tabBar->setTabIcon(m_tabIndex, KIcon(KMimeType::iconNameForUrl(url)));
1184 }
1185
1186 emit urlChanged(url);
1187 }
1188
1189 void DolphinMainWindow::setupActions()
1190 {
1191 // setup 'File' menu
1192 m_newMenu = new DolphinNewMenu(this, this);
1193 KMenu* menu = m_newMenu->menu();
1194 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
1195 menu->setIcon(KIcon("document-new"));
1196 connect(menu, SIGNAL(aboutToShow()),
1197 this, SLOT(updateNewMenu()));
1198
1199 KAction* newWindow = actionCollection()->addAction("new_window");
1200 newWindow->setIcon(KIcon("window-new"));
1201 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
1202 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
1203 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
1204
1205 KAction* newTab = actionCollection()->addAction("new_tab");
1206 newTab->setIcon(KIcon("tab-new"));
1207 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
1208 newTab->setShortcut(KShortcut(Qt::CTRL | Qt::Key_T, Qt::CTRL | Qt::SHIFT | Qt::Key_N));
1209 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
1210
1211 KAction* closeTab = actionCollection()->addAction("close_tab");
1212 closeTab->setIcon(KIcon("tab-close"));
1213 closeTab->setText(i18nc("@action:inmenu File", "Close Tab"));
1214 closeTab->setShortcut(Qt::CTRL | Qt::Key_W);
1215 closeTab->setEnabled(false);
1216 connect(closeTab, SIGNAL(triggered()), this, SLOT(closeTab()));
1217
1218 KStandardAction::quit(this, SLOT(quit()), actionCollection());
1219
1220 // setup 'Edit' menu
1221 KStandardAction::undo(this,
1222 SLOT(undo()),
1223 actionCollection());
1224
1225 // need to remove shift+del from cut action, else the shortcut for deletejob
1226 // doesn't work
1227 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
1228 KShortcut cutShortcut = cut->shortcut();
1229 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
1230 cut->setShortcut(cutShortcut);
1231 KStandardAction::copy(this, SLOT(copy()), actionCollection());
1232 KAction* paste = KStandardAction::paste(this, SLOT(paste()), actionCollection());
1233 // The text of the paste-action is modified dynamically by Dolphin
1234 // (e. g. to "Paste One Folder"). To prevent that the size of the toolbar changes
1235 // due to the long text, the text "Paste" is used:
1236 paste->setIconText(i18nc("@action:inmenu Edit", "Paste"));
1237
1238 KAction* selectAll = actionCollection()->addAction("select_all");
1239 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
1240 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
1241 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
1242
1243 KAction* invertSelection = actionCollection()->addAction("invert_selection");
1244 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
1245 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
1246 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
1247
1248 // setup 'View' menu
1249 // (note that most of it is set up in DolphinViewActionHandler)
1250
1251 KAction* split = actionCollection()->addAction("split_view");
1252 split->setShortcut(Qt::Key_F3);
1253 updateSplitAction();
1254 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
1255
1256 KAction* reload = actionCollection()->addAction("reload");
1257 reload->setText(i18nc("@action:inmenu View", "Reload"));
1258 reload->setShortcut(Qt::Key_F5);
1259 reload->setIcon(KIcon("view-refresh"));
1260 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
1261
1262 KAction* stop = actionCollection()->addAction("stop");
1263 stop->setText(i18nc("@action:inmenu View", "Stop"));
1264 stop->setToolTip(i18nc("@info", "Stop loading"));
1265 stop->setIcon(KIcon("process-stop"));
1266 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
1267
1268 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
1269 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Editable Location"));
1270 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
1271 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
1272
1273 KAction* replaceLocation = actionCollection()->addAction("replace_location");
1274 replaceLocation->setText(i18nc("@action:inmenu Navigation Bar", "Replace Location"));
1275 replaceLocation->setShortcut(Qt::Key_F6);
1276 connect(replaceLocation, SIGNAL(triggered()), this, SLOT(replaceLocation()));
1277
1278 // setup 'Go' menu
1279 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
1280 connect(backAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goBack(Qt::MouseButtons)));
1281 KShortcut backShortcut = backAction->shortcut();
1282 backShortcut.setAlternate(Qt::Key_Backspace);
1283 backAction->setShortcut(backShortcut);
1284
1285 m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
1286 m_recentTabsMenu->setIcon(KIcon("edit-undo"));
1287 actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
1288 connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction *)),
1289 this, SLOT(restoreClosedTab(QAction *)));
1290
1291 QAction* action = new QAction("Empty Recently Closed Tabs", m_recentTabsMenu);
1292 action->setIcon(KIcon("edit-clear-list"));
1293 action->setData(QVariant::fromValue(true));
1294 m_recentTabsMenu->addAction(action);
1295 m_recentTabsMenu->addSeparator();
1296 m_recentTabsMenu->setEnabled(false);
1297
1298 KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
1299 connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
1300
1301 KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
1302 connect(upAction, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
1303
1304 KStandardAction::home(this, SLOT(goHome()), actionCollection());
1305
1306 // setup 'Tools' menu
1307 KToggleAction* showSearchBar = actionCollection()->add<KToggleAction>("show_search_bar");
1308 showSearchBar->setText(i18nc("@action:inmenu Tools", "Show Search Bar"));
1309 showSearchBar->setShortcut(Qt::CTRL | Qt::Key_S);
1310 connect(showSearchBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1311
1312 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
1313 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
1314 showFilterBar->setIcon(KIcon("view-filter"));
1315 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
1316 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
1317
1318 KAction* compareFiles = actionCollection()->addAction("compare_files");
1319 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
1320 compareFiles->setIcon(KIcon("kompare"));
1321 compareFiles->setEnabled(false);
1322 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
1323
1324 KAction* openTerminal = actionCollection()->addAction("open_terminal");
1325 openTerminal->setText(i18nc("@action:inmenu Tools", "Open Terminal"));
1326 openTerminal->setIcon(KIcon("utilities-terminal"));
1327 openTerminal->setShortcut(Qt::SHIFT | Qt::Key_F4);
1328 connect(openTerminal, SIGNAL(triggered()), this, SLOT(openTerminal()));
1329
1330 // setup 'Settings' menu
1331 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
1332 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
1333
1334 // not in menu actions
1335 KAction* activateNextTab = actionCollection()->addAction("activate_next_tab");
1336 activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab"));
1337 connect(activateNextTab, SIGNAL(triggered()), SLOT(activateNextTab()));
1338 activateNextTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabPrev() :
1339 KStandardShortcut::tabNext());
1340
1341 KAction* activatePrevTab = actionCollection()->addAction("activate_prev_tab");
1342 activatePrevTab->setText(i18nc("@action:inmenu", "Activate Previous Tab"));
1343 connect(activatePrevTab, SIGNAL(triggered()), SLOT(activatePrevTab()));
1344 activatePrevTab->setShortcuts(QApplication::isRightToLeft() ? KStandardShortcut::tabNext() :
1345 KStandardShortcut::tabPrev());
1346
1347 // for context menu
1348 KAction* openInNewTab = actionCollection()->addAction("open_in_new_tab");
1349 openInNewTab->setText(i18nc("@action:inmenu", "Open in New Tab"));
1350 openInNewTab->setIcon(KIcon("tab-new"));
1351 connect(openInNewTab, SIGNAL(triggered()), this, SLOT(openInNewTab()));
1352
1353 KAction* openInNewWindow = actionCollection()->addAction("open_in_new_window");
1354 openInNewWindow->setText(i18nc("@action:inmenu", "Open in New Window"));
1355 openInNewWindow->setIcon(KIcon("window-new"));
1356 connect(openInNewWindow, SIGNAL(triggered()), this, SLOT(openInNewWindow()));
1357
1358 // 'Search' toolbar
1359 m_searchBox = new DolphinSearchBox(this);
1360 connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(searchItems()));
1361
1362 KAction* search = new KAction(this);
1363 actionCollection()->addAction("search_bar", search);
1364 search->setText(i18nc("@action:inmenu", "Search Bar"));
1365 search->setDefaultWidget(m_searchBox);
1366 search->setShortcutConfigurable(false);
1367 }
1368
1369 void DolphinMainWindow::setupDockWidgets()
1370 {
1371 // setup "Information"
1372 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
1373 infoDock->setObjectName("infoDock");
1374 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1375 Panel* infoPanel = new InformationPanel(infoDock);
1376 infoDock->setWidget(infoPanel);
1377
1378 QAction* infoAction = infoDock->toggleViewAction();
1379 infoAction->setText(i18nc("@title:window", "Information"));
1380 infoAction->setShortcut(Qt::Key_F11);
1381 infoAction->setIcon(KIcon("dialog-information"));
1382 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
1383
1384 addDockWidget(Qt::RightDockWidgetArea, infoDock);
1385 connect(this, SIGNAL(urlChanged(KUrl)),
1386 infoPanel, SLOT(setUrl(KUrl)));
1387 connect(this, SIGNAL(selectionChanged(KFileItemList)),
1388 infoPanel, SLOT(setSelection(KFileItemList)));
1389 connect(this, SIGNAL(requestItemInfo(KFileItem)),
1390 infoPanel, SLOT(requestDelayedItemInfo(KFileItem)));
1391
1392 // setup "Folders"
1393 QDockWidget* foldersDock = new QDockWidget(i18nc("@title:window", "Folders"));
1394 foldersDock->setObjectName("foldersDock");
1395 foldersDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1396 FoldersPanel* foldersPanel = new FoldersPanel(foldersDock);
1397 foldersDock->setWidget(foldersPanel);
1398
1399 QAction* foldersAction = foldersDock->toggleViewAction();
1400 foldersAction->setText(i18nc("@title:window", "Folders"));
1401 foldersAction->setShortcut(Qt::Key_F7);
1402 foldersAction->setIcon(KIcon("folder"));
1403 actionCollection()->addAction("show_folders_panel", foldersDock->toggleViewAction());
1404
1405 addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
1406 connect(this, SIGNAL(urlChanged(KUrl)),
1407 foldersPanel, SLOT(setUrl(KUrl)));
1408 connect(foldersPanel, SIGNAL(changeUrl(KUrl, Qt::MouseButtons)),
1409 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1410 connect(foldersPanel, SIGNAL(changeSelection(KFileItemList)),
1411 this, SLOT(changeSelection(KFileItemList)));
1412
1413 // setup "Terminal"
1414 #ifndef Q_OS_WIN
1415 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1416 terminalDock->setObjectName("terminalDock");
1417 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1418 Panel* terminalPanel = new TerminalPanel(terminalDock);
1419 terminalDock->setWidget(terminalPanel);
1420
1421 connect(terminalPanel, SIGNAL(hideTerminalPanel()), terminalDock, SLOT(hide()));
1422
1423 QAction* terminalAction = terminalDock->toggleViewAction();
1424 terminalAction->setText(i18nc("@title:window Shell terminal", "Terminal"));
1425 terminalAction->setShortcut(Qt::Key_F4);
1426 terminalAction->setIcon(KIcon("utilities-terminal"));
1427 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
1428
1429 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1430 connect(this, SIGNAL(urlChanged(KUrl)),
1431 terminalPanel, SLOT(setUrl(KUrl)));
1432 #endif
1433
1434 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1435 if (firstRun) {
1436 foldersDock->hide();
1437 #ifndef Q_OS_WIN
1438 terminalDock->hide();
1439 #endif
1440 }
1441
1442 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
1443 placesDock->setObjectName("placesDock");
1444 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1445
1446 PlacesPanel* placesPanel = new PlacesPanel(placesDock);
1447 placesDock->setWidget(placesPanel);
1448 placesPanel->setModel(DolphinSettings::instance().placesModel());
1449 placesPanel->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1450
1451 QAction* placesAction = placesDock->toggleViewAction();
1452 placesAction->setText(i18nc("@title:window", "Places"));
1453 placesAction->setShortcut(Qt::Key_F9);
1454 placesAction->setIcon(KIcon("bookmarks"));
1455 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
1456
1457 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1458 connect(placesPanel, SIGNAL(urlChanged(KUrl, Qt::MouseButtons)),
1459 this, SLOT(handlePlacesClick(KUrl, Qt::MouseButtons)));
1460 connect(this, SIGNAL(urlChanged(KUrl)),
1461 placesPanel, SLOT(setUrl(KUrl)));
1462 }
1463
1464 void DolphinMainWindow::updateEditActions()
1465 {
1466 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1467 if (list.isEmpty()) {
1468 stateChanged("has_no_selection");
1469 } else {
1470 stateChanged("has_selection");
1471
1472 KActionCollection* col = actionCollection();
1473 QAction* renameAction = col->action("rename");
1474 QAction* moveToTrashAction = col->action("move_to_trash");
1475 QAction* deleteAction = col->action("delete");
1476 QAction* cutAction = col->action(KStandardAction::name(KStandardAction::Cut));
1477 QAction* deleteWithTrashShortcut = col->action("delete_shortcut"); // see DolphinViewActionHandler
1478
1479 KFileItemListProperties capabilities(list);
1480 const bool enableMoveToTrash = capabilities.isLocal() && capabilities.supportsMoving();
1481
1482 renameAction->setEnabled(capabilities.supportsMoving());
1483 moveToTrashAction->setEnabled(enableMoveToTrash);
1484 deleteAction->setEnabled(capabilities.supportsDeleting());
1485 deleteWithTrashShortcut->setEnabled(capabilities.supportsDeleting() && !enableMoveToTrash);
1486 cutAction->setEnabled(capabilities.supportsMoving());
1487 }
1488 updatePasteAction();
1489 }
1490
1491 void DolphinMainWindow::updateViewActions()
1492 {
1493 m_actionHandler->updateViewActions();
1494
1495 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1496 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1497
1498 updateSplitAction();
1499
1500 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1501 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1502 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1503 }
1504
1505 void DolphinMainWindow::updateGoActions()
1506 {
1507 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1508 const KUrl& currentUrl = m_activeViewContainer->url();
1509 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1510 }
1511
1512 void DolphinMainWindow::rememberClosedTab(int index)
1513 {
1514 KMenu* tabsMenu = m_recentTabsMenu->menu();
1515
1516 const QString primaryPath = m_viewTab[index].primaryView->url().path();
1517 const QString iconName = KMimeType::iconNameForUrl(primaryPath);
1518
1519 QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
1520
1521 ClosedTab closedTab;
1522 closedTab.primaryUrl = m_viewTab[index].primaryView->url();
1523
1524 if (m_viewTab[index].secondaryView != 0) {
1525 closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
1526 closedTab.isSplit = true;
1527 } else {
1528 closedTab.isSplit = false;
1529 }
1530
1531 action->setData(QVariant::fromValue(closedTab));
1532 action->setIcon(KIcon(iconName));
1533
1534 // add the closed tab menu entry after the separator and
1535 // "Empty Recently Closed Tabs" entry
1536 if (tabsMenu->actions().size() == 2) {
1537 tabsMenu->addAction(action);
1538 } else {
1539 tabsMenu->insertAction(tabsMenu->actions().at(2), action);
1540 }
1541
1542 // assure that only up to 8 closed tabs are shown in the menu
1543 if (tabsMenu->actions().size() > 8) {
1544 tabsMenu->removeAction(tabsMenu->actions().last());
1545 }
1546 actionCollection()->action("closed_tabs")->setEnabled(true);
1547 KAcceleratorManager::manage(tabsMenu);
1548 }
1549
1550 void DolphinMainWindow::clearStatusBar()
1551 {
1552 m_activeViewContainer->statusBar()->clear();
1553 }
1554
1555 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1556 {
1557 connect(container, SIGNAL(showFilterBarChanged(bool)),
1558 this, SLOT(updateFilterBarAction(bool)));
1559
1560 DolphinView* view = container->view();
1561 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1562 this, SLOT(slotSelectionChanged(KFileItemList)));
1563 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1564 this, SLOT(slotRequestItemInfo(KFileItem)));
1565 connect(view, SIGNAL(activated()),
1566 this, SLOT(toggleActiveView()));
1567 connect(view, SIGNAL(tabRequested(const KUrl&)),
1568 this, SLOT(openNewTab(const KUrl&)));
1569
1570 const KUrlNavigator* navigator = container->urlNavigator();
1571 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1572 this, SLOT(changeUrl(const KUrl&)));
1573 connect(navigator, SIGNAL(historyChanged()),
1574 this, SLOT(updateHistory()));
1575 connect(navigator, SIGNAL(editableStateChanged(bool)),
1576 this, SLOT(slotEditableStateChanged(bool)));
1577 }
1578
1579 void DolphinMainWindow::updateSplitAction()
1580 {
1581 QAction* splitAction = actionCollection()->action("split_view");
1582 if (m_viewTab[m_tabIndex].secondaryView != 0) {
1583 if (m_activeViewContainer == m_viewTab[m_tabIndex].secondaryView) {
1584 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1585 splitAction->setToolTip(i18nc("@info", "Close right view"));
1586 splitAction->setIcon(KIcon("view-right-close"));
1587 } else {
1588 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1589 splitAction->setToolTip(i18nc("@info", "Close left view"));
1590 splitAction->setIcon(KIcon("view-left-close"));
1591 }
1592 } else {
1593 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1594 splitAction->setToolTip(i18nc("@info", "Split view"));
1595 splitAction->setIcon(KIcon("view-right-new"));
1596 }
1597 }
1598
1599 QString DolphinMainWindow::tabName(const KUrl& url) const
1600 {
1601 QString name;
1602 if (url.equals(KUrl("file:///"))) {
1603 name = '/';
1604 } else {
1605 name = url.fileName();
1606 if (name.isEmpty()) {
1607 name = url.protocol();
1608 } else {
1609 // Make sure that a '&' inside the directory name is displayed correctly
1610 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
1611 name.replace('&', "&&");
1612 }
1613 }
1614 return name;
1615 }
1616
1617 bool DolphinMainWindow::isKompareInstalled() const
1618 {
1619 static bool initialized = false;
1620 static bool installed = false;
1621 if (!initialized) {
1622 // TODO: maybe replace this approach later by using a menu
1623 // plugin like kdiff3plugin.cpp
1624 installed = !KGlobal::dirs()->findExe("kompare").isEmpty();
1625 initialized = true;
1626 }
1627 return installed;
1628 }
1629
1630 void DolphinMainWindow::createSecondaryView(int tabIndex)
1631 {
1632 QSplitter* splitter = m_viewTab[tabIndex].splitter;
1633 const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
1634
1635 const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
1636 m_viewTab[tabIndex].secondaryView = new DolphinViewContainer(this, 0, view->rootUrl());
1637 splitter->addWidget(m_viewTab[tabIndex].secondaryView);
1638 splitter->setSizes(QList<int>() << newWidth << newWidth);
1639 connectViewSignals(m_viewTab[tabIndex].secondaryView);
1640 m_viewTab[tabIndex].secondaryView->view()->reload();
1641 m_viewTab[tabIndex].secondaryView->setActive(false);
1642 m_viewTab[tabIndex].secondaryView->show();
1643 }
1644
1645 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
1646 {
1647 return "Tab " + QString::number(tabIndex) + ' ' + property;
1648 }
1649
1650 void DolphinMainWindow::setUrlAsCaption(const KUrl& url)
1651 {
1652 QString caption;
1653 if (url.equals(KUrl("file:///"))) {
1654 caption = '/';
1655 } else {
1656 caption = url.fileName();
1657 if (caption.isEmpty()) {
1658 caption = url.protocol();
1659 }
1660 }
1661
1662 setCaption(caption);
1663 }
1664
1665 QString DolphinMainWindow::squeezedText(const QString& text) const
1666 {
1667 const QFontMetrics fm = fontMetrics();
1668 return fm.elidedText(text, Qt::ElideMiddle, fm.maxWidth() * 10);
1669 }
1670
1671 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
1672 KIO::FileUndoManager::UiInterface()
1673 {
1674 }
1675
1676 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1677 {
1678 }
1679
1680 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1681 {
1682 DolphinMainWindow* mainWin= qobject_cast<DolphinMainWindow *>(parentWidget());
1683 if (mainWin) {
1684 DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar();
1685 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1686 } else {
1687 KIO::FileUndoManager::UiInterface::jobError(job);
1688 }
1689 }
1690
1691 #include "dolphinmainwindow.moc"