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