]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
Add unit test for #149736 (an old bug where a javascript handler closes the window...
[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 "dolphindropcontroller.h"
25
26 #include <config-nepomuk.h>
27
28 #include "dolphinapplication.h"
29 #include "dolphinfileplacesview.h"
30 #include "dolphinnewmenu.h"
31 #include "dolphinsettings.h"
32 #include "dolphinsettingsdialog.h"
33 #include "dolphinstatusbar.h"
34 #include "dolphinviewcontainer.h"
35 #include "infosidebarpage.h"
36 #include "metadatawidget.h"
37 #include "mainwindowadaptor.h"
38 #include "terminalsidebarpage.h"
39 #include "treeviewsidebarpage.h"
40 #include "viewpropertiesdialog.h"
41 #include "viewproperties.h"
42
43 #include "dolphin_generalsettings.h"
44 #include "dolphin_iconsmodesettings.h"
45
46 #include <kaction.h>
47 #include <kactioncollection.h>
48 #include <kconfig.h>
49 #include <kdesktopfile.h>
50 #include <kdeversion.h>
51 #include <kfiledialog.h>
52 #include <kfileplacesmodel.h>
53 #include <kglobal.h>
54 #include <kicon.h>
55 #include <kiconloader.h>
56 #include <kio/netaccess.h>
57 #include <kinputdialog.h>
58 #include <klocale.h>
59 #include <kmenu.h>
60 #include <kmenubar.h>
61 #include <kmessagebox.h>
62 #include <kurlnavigator.h>
63 #include <konqmimedata.h>
64 #include <kpropertiesdialog.h>
65 #include <kprotocolinfo.h>
66 #include <ktoggleaction.h>
67 #include <krun.h>
68 #include <kshell.h>
69 #include <kstandarddirs.h>
70 #include <kstatusbar.h>
71 #include <kstandardaction.h>
72 #include <kurl.h>
73 #include <kurlcombobox.h>
74
75 #include <QKeyEvent>
76 #include <QClipboard>
77 #include <QLineEdit>
78 #include <QSplitter>
79 #include <QDockWidget>
80
81 DolphinMainWindow::DolphinMainWindow(int id) :
82 KXmlGuiWindow(0),
83 m_newMenu(0),
84 m_showMenuBar(0),
85 m_splitter(0),
86 m_activeViewContainer(0),
87 m_id(id)
88 {
89 setObjectName("Dolphin");
90 m_viewContainer[PrimaryView] = 0;
91 m_viewContainer[SecondaryView] = 0;
92
93 new MainWindowAdaptor(this);
94 QDBusConnection::sessionBus().registerObject(QString("/dolphin/MainWindow%1").arg(m_id), this);
95
96 KonqFileUndoManager::incRef();
97
98 KonqFileUndoManager* undoManager = KonqFileUndoManager::self();
99 undoManager->setUiInterface(new UndoUiInterface(this));
100
101 connect(undoManager, SIGNAL(undoAvailable(bool)),
102 this, SLOT(slotUndoAvailable(bool)));
103 connect(undoManager, SIGNAL(undoTextChanged(const QString&)),
104 this, SLOT(slotUndoTextChanged(const QString&)));
105 connect(DolphinSettings::instance().placesModel(), SIGNAL(errorMessage(const QString&)),
106 this, SLOT(slotHandlePlacesError(const QString&)));
107 }
108
109 DolphinMainWindow::~DolphinMainWindow()
110 {
111 KonqFileUndoManager::decRef();
112 DolphinApplication::app()->removeMainWindow(this);
113 }
114
115 void DolphinMainWindow::toggleViews()
116 {
117 if (m_viewContainer[SecondaryView] == 0) {
118 return;
119 }
120
121 // move secondary view from the last position of the splitter
122 // to the first position
123 m_splitter->insertWidget(0, m_viewContainer[SecondaryView]);
124
125 DolphinViewContainer* container = m_viewContainer[PrimaryView];
126 m_viewContainer[PrimaryView] = m_viewContainer[SecondaryView];
127 m_viewContainer[SecondaryView] = container;
128 }
129
130 void DolphinMainWindow::slotDoingOperation(KonqFileUndoManager::CommandType commandType)
131 {
132 clearStatusBar();
133 m_undoCommandTypes.append(commandType);
134 }
135
136 void DolphinMainWindow::refreshViews()
137 {
138 Q_ASSERT(m_viewContainer[PrimaryView] != 0);
139
140 // remember the current active view, as because of
141 // the refreshing the active view might change to
142 // the secondary view
143 DolphinViewContainer* activeViewContainer = m_activeViewContainer;
144
145 m_viewContainer[PrimaryView]->view()->refresh();
146 if (m_viewContainer[SecondaryView] != 0) {
147 m_viewContainer[SecondaryView]->view()->refresh();
148 }
149
150 setActiveViewContainer(activeViewContainer);
151 }
152
153 void DolphinMainWindow::dropUrls(const KUrl::List& urls,
154 const KUrl& destination)
155 {
156 DolphinDropController dropController(this);
157 connect(&dropController, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
158 this, SLOT(slotDoingOperation(KonqFileUndoManager::CommandType)));
159 dropController.dropUrls(urls, destination);
160 }
161
162 void DolphinMainWindow::changeUrl(const KUrl& url)
163 {
164 DolphinViewContainer* view = activeViewContainer();
165 if (view != 0) {
166 view->setUrl(url);
167 updateEditActions();
168 updateViewActions();
169 updateGoActions();
170 setCaption(url.fileName());
171 emit urlChanged(url);
172 }
173 }
174
175 void DolphinMainWindow::changeSelection(const KFileItemList& selection)
176 {
177 activeViewContainer()->view()->changeSelection(selection);
178 }
179
180 void DolphinMainWindow::slotEditableStateChanged(bool editable)
181 {
182 KToggleAction* editableLocationAction =
183 static_cast<KToggleAction*>(actionCollection()->action("editable_location"));
184 editableLocationAction->setChecked(editable);
185 }
186
187 void DolphinMainWindow::slotSelectionChanged(const KFileItemList& selection)
188 {
189 updateEditActions();
190
191 Q_ASSERT(m_viewContainer[PrimaryView] != 0);
192 int selectedUrlsCount = m_viewContainer[PrimaryView]->view()->selectedUrls().count();
193 if (m_viewContainer[SecondaryView] != 0) {
194 selectedUrlsCount += m_viewContainer[SecondaryView]->view()->selectedUrls().count();
195 }
196
197 QAction* compareFilesAction = actionCollection()->action("compare_files");
198 if (selectedUrlsCount == 2) {
199 const bool kompareInstalled = !KGlobal::dirs()->findExe("kompare").isEmpty();
200 compareFilesAction->setEnabled(selectedUrlsCount == 2 && kompareInstalled);
201 } else {
202 compareFilesAction->setEnabled(false);
203 }
204
205 m_activeViewContainer->updateStatusBar();
206
207 emit selectionChanged(selection);
208 }
209
210 void DolphinMainWindow::slotRequestItemInfo(const KFileItem& item)
211 {
212 emit requestItemInfo(item);
213 }
214
215 void DolphinMainWindow::slotHistoryChanged()
216 {
217 updateHistory();
218 }
219
220 void DolphinMainWindow::updateFilterBarAction(bool show)
221 {
222 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
223 showFilterBarAction->setChecked(show);
224 }
225
226 void DolphinMainWindow::openNewMainWindow()
227 {
228 DolphinApplication::app()->createMainWindow()->show();
229 }
230
231 void DolphinMainWindow::toggleActiveView()
232 {
233 if (m_viewContainer[SecondaryView] == 0) {
234 // only one view is available
235 return;
236 }
237
238 Q_ASSERT(m_activeViewContainer != 0);
239 Q_ASSERT(m_viewContainer[PrimaryView] != 0);
240
241 DolphinViewContainer* left = m_viewContainer[PrimaryView];
242 DolphinViewContainer* right = m_viewContainer[SecondaryView];
243 setActiveViewContainer(m_activeViewContainer == right ? left : right);
244 }
245
246 void DolphinMainWindow::closeEvent(QCloseEvent* event)
247 {
248 DolphinSettings& settings = DolphinSettings::instance();
249 GeneralSettings* generalSettings = settings.generalSettings();
250 generalSettings->setFirstRun(false);
251
252 settings.save();
253
254 KXmlGuiWindow::closeEvent(event);
255 }
256
257 void DolphinMainWindow::saveProperties(KConfigGroup& group)
258 {
259 DolphinViewContainer* cont = m_viewContainer[PrimaryView];
260 group.writeEntry("Primary Url", cont->url().url());
261 group.writeEntry("Primary Editable Url", cont->isUrlEditable());
262
263 cont = m_viewContainer[SecondaryView];
264 if (cont != 0) {
265 group.writeEntry("Secondary Url", cont->url().url());
266 group.writeEntry("Secondary Editable Url", cont->isUrlEditable());
267 }
268 }
269
270 void DolphinMainWindow::readProperties(const KConfigGroup& group)
271 {
272 DolphinViewContainer* cont = m_viewContainer[PrimaryView];
273
274 cont->setUrl(group.readEntry("Primary Url"));
275 bool editable = group.readEntry("Primary Editable Url", false);
276 cont->urlNavigator()->setUrlEditable(editable);
277
278 cont = m_viewContainer[SecondaryView];
279 const QString secondaryUrl = group.readEntry("Secondary Url");
280 if (!secondaryUrl.isEmpty()) {
281 if (cont == 0) {
282 // a secondary view should be shown, but no one is available
283 // currently -> create a new view
284 toggleSplitView();
285 cont = m_viewContainer[SecondaryView];
286 Q_ASSERT(cont != 0);
287 }
288
289 cont->setUrl(secondaryUrl);
290 bool editable = group.readEntry("Secondary Editable Url", false);
291 cont->urlNavigator()->setUrlEditable(editable);
292 } else if (cont != 0) {
293 // no secondary view should be shown, but the default setting shows
294 // one already -> close the view
295 toggleSplitView();
296 }
297 }
298
299 void DolphinMainWindow::updateNewMenu()
300 {
301 m_newMenu->slotCheckUpToDate();
302 m_newMenu->setPopupFiles(activeViewContainer()->url());
303 }
304
305 void DolphinMainWindow::properties()
306 {
307 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
308
309 KPropertiesDialog *dialog = new KPropertiesDialog(list, this);
310 dialog->setAttribute(Qt::WA_DeleteOnClose);
311 dialog->show();
312 dialog->raise();
313 dialog->activateWindow();
314 }
315
316 void DolphinMainWindow::quit()
317 {
318 close();
319 }
320
321 void DolphinMainWindow::slotHandlePlacesError(const QString &message)
322 {
323 if (!message.isEmpty()) {
324 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
325 statusBar->setMessage(message, DolphinStatusBar::Error);
326 }
327 }
328
329 void DolphinMainWindow::slotUndoAvailable(bool available)
330 {
331 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
332 if (undoAction != 0) {
333 undoAction->setEnabled(available);
334 }
335
336 if (available && (m_undoCommandTypes.count() > 0)) {
337 const KonqFileUndoManager::CommandType command = m_undoCommandTypes.takeFirst();
338 DolphinStatusBar* statusBar = m_activeViewContainer->statusBar();
339 switch (command) {
340 case KonqFileUndoManager::COPY:
341 statusBar->setMessage(i18nc("@info:status", "Copy operation completed."),
342 DolphinStatusBar::OperationCompleted);
343 break;
344 case KonqFileUndoManager::MOVE:
345 statusBar->setMessage(i18nc("@info:status", "Move operation completed."),
346 DolphinStatusBar::OperationCompleted);
347 break;
348 case KonqFileUndoManager::LINK:
349 statusBar->setMessage(i18nc("@info:status", "Link operation completed."),
350 DolphinStatusBar::OperationCompleted);
351 break;
352 case KonqFileUndoManager::TRASH:
353 statusBar->setMessage(i18nc("@info:status", "Move to trash operation completed."),
354 DolphinStatusBar::OperationCompleted);
355 break;
356 case KonqFileUndoManager::RENAME:
357 statusBar->setMessage(i18nc("@info:status", "Renaming operation completed."),
358 DolphinStatusBar::OperationCompleted);
359 break;
360
361 case KonqFileUndoManager::MKDIR:
362 statusBar->setMessage(i18nc("@info:status", "Created folder."),
363 DolphinStatusBar::OperationCompleted);
364 break;
365
366 default:
367 break;
368 }
369
370 }
371 }
372
373 void DolphinMainWindow::slotUndoTextChanged(const QString& text)
374 {
375 QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
376 if (undoAction != 0) {
377 undoAction->setText(text);
378 }
379 }
380
381 void DolphinMainWindow::undo()
382 {
383 clearStatusBar();
384 KonqFileUndoManager::self()->undo();
385 }
386
387 void DolphinMainWindow::cut()
388 {
389 m_activeViewContainer->view()->cutSelectedItems();
390 }
391
392 void DolphinMainWindow::copy()
393 {
394 m_activeViewContainer->view()->copySelectedItems();
395 }
396
397 void DolphinMainWindow::paste()
398 {
399 m_activeViewContainer->view()->paste();
400 }
401
402 void DolphinMainWindow::updatePasteAction()
403 {
404 QAction* pasteAction = actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
405 if (pasteAction == 0) {
406 return;
407 }
408
409 QPair<bool, QString> pasteInfo = m_activeViewContainer->view()->pasteInfo();
410 pasteAction->setEnabled(pasteInfo.first);
411 pasteAction->setText(pasteInfo.second);
412 }
413
414 void DolphinMainWindow::selectAll()
415 {
416 clearStatusBar();
417
418 // if the URL navigator is editable and focused, select the whole
419 // URL instead of all items of the view
420
421 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
422 QLineEdit* lineEdit = urlNavigator->editor()->lineEdit();
423 const bool selectUrl = urlNavigator->isUrlEditable() &&
424 lineEdit->hasFocus();
425 if (selectUrl) {
426 lineEdit->selectAll();
427 } else {
428 m_activeViewContainer->view()->selectAll();
429 }
430 }
431
432 void DolphinMainWindow::invertSelection()
433 {
434 clearStatusBar();
435 m_activeViewContainer->view()->invertSelection();
436 }
437
438 void DolphinMainWindow::toggleSplitView()
439 {
440 if (m_viewContainer[SecondaryView] == 0) {
441 // create a secondary view
442 const int newWidth = (m_viewContainer[PrimaryView]->width() - m_splitter->handleWidth()) / 2;
443
444 const DolphinView* view = m_viewContainer[PrimaryView]->view();
445 m_viewContainer[SecondaryView] = new DolphinViewContainer(this, 0, view->rootUrl());
446 connectViewSignals(SecondaryView);
447 m_splitter->addWidget(m_viewContainer[SecondaryView]);
448 m_splitter->setSizes(QList<int>() << newWidth << newWidth);
449 m_viewContainer[SecondaryView]->view()->reload();
450 m_viewContainer[SecondaryView]->setActive(false);
451 m_viewContainer[SecondaryView]->show();
452 } else if (m_activeViewContainer == m_viewContainer[PrimaryView]) {
453 // remove secondary view
454 m_viewContainer[SecondaryView]->close();
455 m_viewContainer[SecondaryView]->deleteLater();
456 m_viewContainer[SecondaryView] = 0;
457 } else {
458 // The secondary view is active, hence from a users point of view
459 // the content of the secondary view should be moved to the primary view.
460 // From an implementation point of view it is more efficient to close
461 // the primary view and exchange the internal pointers afterwards.
462 m_viewContainer[PrimaryView]->close();
463 m_viewContainer[PrimaryView]->deleteLater();
464 m_viewContainer[PrimaryView] = m_viewContainer[SecondaryView];
465 m_viewContainer[SecondaryView] = 0;
466 }
467
468 setActiveViewContainer(m_viewContainer[PrimaryView]);
469 updateViewActions();
470 emit activeViewChanged(); // TODO unused; remove?
471 }
472
473 void DolphinMainWindow::reloadView()
474 {
475 clearStatusBar();
476 m_activeViewContainer->view()->reload();
477 }
478
479 void DolphinMainWindow::stopLoading()
480 {
481 }
482
483 void DolphinMainWindow::toggleFilterBarVisibility(bool show)
484 {
485 m_activeViewContainer->showFilterBar(show);
486 }
487
488 void DolphinMainWindow::toggleEditLocation()
489 {
490 clearStatusBar();
491
492 QAction* action = actionCollection()->action("editable_location");
493 KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
494 urlNavigator->setUrlEditable(action->isChecked());
495 }
496
497 void DolphinMainWindow::editLocation()
498 {
499 KUrlNavigator* navigator = m_activeViewContainer->urlNavigator();
500 navigator->setUrlEditable(true);
501 navigator->setFocus();
502 }
503
504 void DolphinMainWindow::adjustViewProperties()
505 {
506 clearStatusBar();
507 ViewPropertiesDialog dlg(m_activeViewContainer->view());
508 dlg.exec();
509 }
510
511 void DolphinMainWindow::goBack()
512 {
513 clearStatusBar();
514 m_activeViewContainer->urlNavigator()->goBack();
515 }
516
517 void DolphinMainWindow::goForward()
518 {
519 clearStatusBar();
520 m_activeViewContainer->urlNavigator()->goForward();
521 }
522
523 void DolphinMainWindow::goUp()
524 {
525 clearStatusBar();
526 m_activeViewContainer->urlNavigator()->goUp();
527 }
528
529 void DolphinMainWindow::goHome()
530 {
531 clearStatusBar();
532 m_activeViewContainer->urlNavigator()->goHome();
533 }
534
535 void DolphinMainWindow::findFile()
536 {
537 KRun::run("kfind", m_activeViewContainer->url(), this);
538 }
539
540 void DolphinMainWindow::compareFiles()
541 {
542 // The method is only invoked if exactly 2 files have
543 // been selected. The selected files may be:
544 // - both in the primary view
545 // - both in the secondary view
546 // - one in the primary view and the other in the secondary
547 // view
548 Q_ASSERT(m_viewContainer[PrimaryView] != 0);
549
550 KUrl urlA;
551 KUrl urlB;
552 KUrl::List urls = m_viewContainer[PrimaryView]->view()->selectedUrls();
553
554 switch (urls.count()) {
555 case 0: {
556 Q_ASSERT(m_viewContainer[SecondaryView] != 0);
557 urls = m_viewContainer[SecondaryView]->view()->selectedUrls();
558 Q_ASSERT(urls.count() == 2);
559 urlA = urls[0];
560 urlB = urls[1];
561 break;
562 }
563
564 case 1: {
565 urlA = urls[0];
566 Q_ASSERT(m_viewContainer[SecondaryView] != 0);
567 urls = m_viewContainer[SecondaryView]->view()->selectedUrls();
568 Q_ASSERT(urls.count() == 1);
569 urlB = urls[0];
570 break;
571 }
572
573 case 2: {
574 urlA = urls[0];
575 urlB = urls[1];
576 break;
577 }
578
579 default: {
580 // may not happen: compareFiles may only get invoked if 2
581 // files are selected
582 Q_ASSERT(false);
583 }
584 }
585
586 QString command("kompare -c \"");
587 command.append(urlA.pathOrUrl());
588 command.append("\" \"");
589 command.append(urlB.pathOrUrl());
590 command.append('\"');
591 KRun::runCommand(command, "Kompare", "kompare", this);
592
593 }
594
595 void DolphinMainWindow::toggleShowMenuBar()
596 {
597 const bool visible = menuBar()->isVisible();
598 menuBar()->setVisible(!visible);
599 }
600
601 void DolphinMainWindow::editSettings()
602 {
603 DolphinSettingsDialog dialog(this);
604 dialog.exec();
605 }
606
607 void DolphinMainWindow::init()
608 {
609 DolphinSettings& settings = DolphinSettings::instance();
610
611 // Check whether Dolphin runs the first time. If yes then
612 // a proper default window size is given at the end of DolphinMainWindow::init().
613 GeneralSettings* generalSettings = settings.generalSettings();
614 const bool firstRun = generalSettings->firstRun();
615 if (firstRun) {
616 generalSettings->setViewPropsTimestamp(QDateTime::currentDateTime());
617 Q_ASSERT(generalSettings->homeUrl().isEmpty());
618 const KUrl homeUrl(QDir::homePath());
619 generalSettings->setHomeUrl(homeUrl.prettyUrl());
620 }
621
622 setAcceptDrops(true);
623
624 m_splitter = new QSplitter(this);
625
626 setupActions();
627
628 const KUrl& homeUrl = generalSettings->homeUrl();
629 setCaption(homeUrl.fileName());
630 m_actionHandler = new DolphinViewActionHandler(actionCollection(), this);
631 connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
632 ViewProperties props(homeUrl);
633 m_viewContainer[PrimaryView] = new DolphinViewContainer(this,
634 m_splitter,
635 homeUrl);
636
637 m_activeViewContainer = m_viewContainer[PrimaryView];
638 connectViewSignals(PrimaryView);
639 DolphinView* view = m_viewContainer[PrimaryView]->view();
640 view->reload();
641 m_viewContainer[PrimaryView]->show();
642 m_actionHandler->setCurrentView(view);
643
644 setCentralWidget(m_splitter);
645 setupDockWidgets();
646
647 setupGUI(Keys | Save | Create | ToolBar);
648 createGUI();
649
650 stateChanged("new_file");
651 setAutoSaveSettings();
652
653 QClipboard* clipboard = QApplication::clipboard();
654 connect(clipboard, SIGNAL(dataChanged()),
655 this, SLOT(updatePasteAction()));
656 updatePasteAction();
657 updateGoActions();
658
659 if (generalSettings->splitView()) {
660 toggleSplitView();
661 }
662 updateViewActions();
663
664 if (firstRun) {
665 // assure a proper default size if Dolphin runs the first time
666 resize(700, 500);
667 }
668
669 emit urlChanged(homeUrl);
670 }
671
672 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
673 {
674 Q_ASSERT(viewContainer != 0);
675 Q_ASSERT((viewContainer == m_viewContainer[PrimaryView]) || (viewContainer == m_viewContainer[SecondaryView]));
676 if (m_activeViewContainer == viewContainer) {
677 return;
678 }
679
680 m_activeViewContainer->setActive(false);
681 m_activeViewContainer = viewContainer;
682 m_activeViewContainer->setActive(true);
683
684 updateHistory();
685 updateEditActions();
686 updateViewActions();
687 updateGoActions();
688
689 const KUrl& url = m_activeViewContainer->url();
690 setCaption(url.fileName());
691
692 m_actionHandler->setCurrentView(viewContainer->view());
693
694 emit activeViewChanged(); // TODO unused; remove?
695 emit urlChanged(url);
696 }
697
698 void DolphinMainWindow::setupActions()
699 {
700 // setup 'File' menu
701 m_newMenu = new DolphinNewMenu(this);
702 KMenu* menu = m_newMenu->menu();
703 menu->setTitle(i18nc("@title:menu", "Create New"));
704 menu->setIcon(KIcon("document-new"));
705 connect(menu, SIGNAL(aboutToShow()),
706 this, SLOT(updateNewMenu()));
707
708 KAction* newWindow = actionCollection()->addAction("new_window");
709 newWindow->setIcon(KIcon("window-new"));
710 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
711 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
712 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
713
714 KAction* properties = actionCollection()->addAction("properties");
715 properties->setText(i18nc("@action:inmenu File", "Properties"));
716 properties->setShortcut(Qt::ALT | Qt::Key_Return);
717 connect(properties, SIGNAL(triggered()), this, SLOT(properties()));
718
719 KStandardAction::quit(this, SLOT(quit()), actionCollection());
720
721 // setup 'Edit' menu
722 KStandardAction::undo(this,
723 SLOT(undo()),
724 actionCollection());
725
726 // need to remove shift+del from cut action, else the shortcut for deletejob
727 // doesn't work
728 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
729 KShortcut cutShortcut = cut->shortcut();
730 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
731 cut->setShortcut(cutShortcut);
732 KStandardAction::copy(this, SLOT(copy()), actionCollection());
733 KStandardAction::paste(this, SLOT(paste()), actionCollection());
734
735 KAction* selectAll = actionCollection()->addAction("select_all");
736 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
737 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
738 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
739
740 KAction* invertSelection = actionCollection()->addAction("invert_selection");
741 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
742 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
743 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
744
745 // setup 'View' menu
746 // (note that most of it is set up in DolphinViewActionHandler)
747
748 KAction* split = actionCollection()->addAction("split_view");
749 split->setShortcut(Qt::Key_F3);
750 updateSplitAction();
751 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
752
753 KAction* reload = actionCollection()->addAction("reload");
754 reload->setText(i18nc("@action:inmenu View", "Reload"));
755 reload->setShortcut(Qt::Key_F5);
756 reload->setIcon(KIcon("view-refresh"));
757 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
758
759 KAction* stop = actionCollection()->addAction("stop");
760 stop->setText(i18nc("@action:inmenu View", "Stop"));
761 stop->setIcon(KIcon("process-stop"));
762 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
763
764 // TODO: the naming "Show full Location" is currently confusing...
765 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
766 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Show Full Location"));
767 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
768 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
769
770 KAction* editLocation = actionCollection()->addAction("edit_location");
771 editLocation->setText(i18nc("@action:inmenu Navigation Bar", "Edit Location"));
772 editLocation->setShortcut(Qt::Key_F6);
773 connect(editLocation, SIGNAL(triggered()), this, SLOT(editLocation()));
774
775 KAction* adjustViewProps = actionCollection()->addAction("view_properties");
776 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
777 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(adjustViewProperties()));
778
779 // setup 'Go' menu
780 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
781 KShortcut backShortcut = backAction->shortcut();
782 backShortcut.setAlternate(Qt::Key_Backspace);
783 backAction->setShortcut(backShortcut);
784
785 KStandardAction::forward(this, SLOT(goForward()), actionCollection());
786 KStandardAction::up(this, SLOT(goUp()), actionCollection());
787 KStandardAction::home(this, SLOT(goHome()), actionCollection());
788
789 // setup 'Tools' menu
790 QAction* findFile = actionCollection()->addAction("find_file");
791 findFile->setText(i18nc("@action:inmenu Tools", "Find File..."));
792 findFile->setShortcut(Qt::CTRL | Qt::Key_F);
793 findFile->setIcon(KIcon("edit-find"));
794 connect(findFile, SIGNAL(triggered()), this, SLOT(findFile()));
795
796 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
797 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
798 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
799 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
800
801 KAction* compareFiles = actionCollection()->addAction("compare_files");
802 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
803 compareFiles->setIcon(KIcon("kompare"));
804 compareFiles->setEnabled(false);
805 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
806
807 // setup 'Settings' menu
808 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
809 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
810 }
811
812 void DolphinMainWindow::setupDockWidgets()
813 {
814 // setup "Information"
815 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
816 infoDock->setObjectName("infoDock");
817 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
818 SidebarPage* infoWidget = new InfoSidebarPage(infoDock);
819 infoDock->setWidget(infoWidget);
820
821 infoDock->toggleViewAction()->setText(i18nc("@title:window", "Information"));
822 infoDock->toggleViewAction()->setShortcut(Qt::Key_F11);
823 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
824
825 addDockWidget(Qt::RightDockWidgetArea, infoDock);
826 connect(this, SIGNAL(urlChanged(KUrl)),
827 infoWidget, SLOT(setUrl(KUrl)));
828 connect(this, SIGNAL(selectionChanged(KFileItemList)),
829 infoWidget, SLOT(setSelection(KFileItemList)));
830 connect(this, SIGNAL(requestItemInfo(KFileItem)),
831 infoWidget, SLOT(requestDelayedItemInfo(KFileItem)));
832
833 // setup "Tree View"
834 QDockWidget* treeViewDock = new QDockWidget(i18nc("@title:window", "Folders"));
835 treeViewDock->setObjectName("treeViewDock");
836 treeViewDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
837 TreeViewSidebarPage* treeWidget = new TreeViewSidebarPage(treeViewDock);
838 treeViewDock->setWidget(treeWidget);
839
840 treeViewDock->toggleViewAction()->setText(i18nc("@title:window", "Folders"));
841 treeViewDock->toggleViewAction()->setShortcut(Qt::Key_F7);
842 actionCollection()->addAction("show_folders_panel", treeViewDock->toggleViewAction());
843
844 addDockWidget(Qt::LeftDockWidgetArea, treeViewDock);
845 connect(this, SIGNAL(urlChanged(KUrl)),
846 treeWidget, SLOT(setUrl(KUrl)));
847 connect(treeWidget, SIGNAL(changeUrl(KUrl)),
848 this, SLOT(changeUrl(KUrl)));
849 connect(treeWidget, SIGNAL(changeSelection(KFileItemList)),
850 this, SLOT(changeSelection(KFileItemList)));
851 connect(treeWidget, SIGNAL(urlsDropped(KUrl::List, KUrl)),
852 this, SLOT(dropUrls(KUrl::List, KUrl)));
853
854 // setup "Terminal"
855 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window", "Terminal"));
856 terminalDock->setObjectName("terminalDock");
857 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
858 SidebarPage* terminalWidget = new TerminalSidebarPage(terminalDock);
859 terminalDock->setWidget(terminalWidget);
860
861 connect(terminalWidget, SIGNAL(hideTerminalSidebarPage()), terminalDock, SLOT(hide()));
862
863 terminalDock->toggleViewAction()->setText(i18nc("@title:window", "Terminal"));
864 terminalDock->toggleViewAction()->setShortcut(Qt::Key_F4);
865 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
866
867 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
868 connect(this, SIGNAL(urlChanged(KUrl)),
869 terminalWidget, SLOT(setUrl(KUrl)));
870
871 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
872 if (firstRun) {
873 infoDock->hide();
874 treeViewDock->hide();
875 terminalDock->hide();
876 }
877
878 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
879 placesDock->setObjectName("placesDock");
880 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
881
882 DolphinFilePlacesView* placesView = new DolphinFilePlacesView(placesDock);
883 placesDock->setWidget(placesView);
884 placesView->setModel(DolphinSettings::instance().placesModel());
885 placesView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
886
887 placesDock->toggleViewAction()->setText(i18nc("@title:window", "Places"));
888 placesDock->toggleViewAction()->setShortcut(Qt::Key_F9);
889 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
890
891 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
892 connect(placesView, SIGNAL(urlChanged(KUrl)),
893 this, SLOT(changeUrl(KUrl)));
894 connect(this, SIGNAL(urlChanged(KUrl)),
895 placesView, SLOT(setUrl(KUrl)));
896 }
897
898 void DolphinMainWindow::updateHistory()
899 {
900 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
901 const int index = urlNavigator->historyIndex();
902
903 QAction* backAction = actionCollection()->action("go_back");
904 if (backAction != 0) {
905 backAction->setEnabled(index < urlNavigator->historySize() - 1);
906 }
907
908 QAction* forwardAction = actionCollection()->action("go_forward");
909 if (forwardAction != 0) {
910 forwardAction->setEnabled(index > 0);
911 }
912 }
913
914 void DolphinMainWindow::updateEditActions()
915 {
916 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
917 if (list.isEmpty()) {
918 stateChanged("has_no_selection");
919 } else {
920 stateChanged("has_selection");
921
922 QAction* renameAction = actionCollection()->action("rename");
923 if (renameAction != 0) {
924 renameAction->setEnabled(true);
925 }
926
927 bool enableMoveToTrash = true;
928
929 KFileItemList::const_iterator it = list.begin();
930 const KFileItemList::const_iterator end = list.end();
931 while (it != end) {
932 const KUrl& url = (*it).url();
933 // only enable the 'Move to Trash' action for local files
934 if (!url.isLocalFile()) {
935 enableMoveToTrash = false;
936 }
937 ++it;
938 }
939
940 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
941 moveToTrashAction->setEnabled(enableMoveToTrash);
942 }
943 updatePasteAction();
944 }
945
946 void DolphinMainWindow::updateViewActions()
947 {
948 m_actionHandler->updateViewActions();
949
950 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
951 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
952
953 updateSplitAction();
954
955 QAction* editableLocactionAction = actionCollection()->action("editable_location");
956 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
957 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
958 }
959
960 void DolphinMainWindow::updateGoActions()
961 {
962 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
963 const KUrl& currentUrl = m_activeViewContainer->url();
964 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
965 }
966
967 void DolphinMainWindow::clearStatusBar()
968 {
969 m_activeViewContainer->statusBar()->clear();
970 }
971
972 void DolphinMainWindow::connectViewSignals(int viewIndex)
973 {
974 DolphinViewContainer* container = m_viewContainer[viewIndex];
975 connect(container, SIGNAL(showFilterBarChanged(bool)),
976 this, SLOT(updateFilterBarAction(bool)));
977
978 DolphinView* view = container->view();
979 connect(view, SIGNAL(selectionChanged(KFileItemList)),
980 this, SLOT(slotSelectionChanged(KFileItemList)));
981 connect(view, SIGNAL(requestItemInfo(KFileItem)),
982 this, SLOT(slotRequestItemInfo(KFileItem)));
983 connect(view, SIGNAL(activated()),
984 this, SLOT(toggleActiveView()));
985 connect(view, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
986 this, SLOT(slotDoingOperation(KonqFileUndoManager::CommandType)));
987
988 const KUrlNavigator* navigator = container->urlNavigator();
989 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
990 this, SLOT(changeUrl(const KUrl&)));
991 connect(navigator, SIGNAL(historyChanged()),
992 this, SLOT(slotHistoryChanged()));
993 connect(navigator, SIGNAL(editableStateChanged(bool)),
994 this, SLOT(slotEditableStateChanged(bool)));
995 }
996
997 void DolphinMainWindow::updateSplitAction()
998 {
999 QAction* splitAction = actionCollection()->action("split_view");
1000 if (m_viewContainer[SecondaryView] != 0) {
1001 if (m_activeViewContainer == m_viewContainer[PrimaryView]) {
1002 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1003 splitAction->setIcon(KIcon("view-right-close"));
1004 } else {
1005 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1006 splitAction->setIcon(KIcon("view-left-close"));
1007 }
1008 } else {
1009 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1010 splitAction->setIcon(KIcon("view-right-new"));
1011 }
1012 }
1013
1014 DolphinMainWindow::UndoUiInterface::UndoUiInterface(DolphinMainWindow* mainWin) :
1015 KonqFileUndoManager::UiInterface(mainWin),
1016 m_mainWin(mainWin)
1017 {
1018 Q_ASSERT(m_mainWin != 0);
1019 }
1020
1021 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1022 {
1023 }
1024
1025 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1026 {
1027 DolphinStatusBar* statusBar = m_mainWin->activeViewContainer()->statusBar();
1028 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1029 }
1030
1031 #include "dolphinmainwindow.moc"