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