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