]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinmainwindow.cpp
let Dolphin use the new tab-close-buttons
[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->setCloseButtonEnabled(true);
773 connect(m_tabBar, SIGNAL(currentChanged(int)),
774 this, SLOT(setActiveTab(int)));
775 connect(m_tabBar, SIGNAL(closeRequest(int)),
776 this, SLOT(closeTab(int)));
777 m_tabBar->blockSignals(true); // signals get unblocked after at least 2 tabs are open
778
779 QWidget* centralWidget = new QWidget(this);
780 m_centralWidgetLayout = new QVBoxLayout(centralWidget);
781 m_centralWidgetLayout->setSpacing(0);
782 m_centralWidgetLayout->setMargin(0);
783 m_centralWidgetLayout->addWidget(m_tabBar);
784 m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter);
785
786
787 setCentralWidget(centralWidget);
788 setupDockWidgets();
789
790 setupGUI(Keys | Save | Create | ToolBar);
791 createGUI();
792
793 stateChanged("new_file");
794 setAutoSaveSettings();
795
796 QClipboard* clipboard = QApplication::clipboard();
797 connect(clipboard, SIGNAL(dataChanged()),
798 this, SLOT(updatePasteAction()));
799 updatePasteAction();
800 updateGoActions();
801
802 if (generalSettings->splitView()) {
803 toggleSplitView();
804 }
805 updateViewActions();
806
807 if (firstRun) {
808 // assure a proper default size if Dolphin runs the first time
809 resize(750, 500);
810 }
811
812 emit urlChanged(homeUrl);
813 }
814
815 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
816 {
817 Q_ASSERT(viewContainer != 0);
818 Q_ASSERT((viewContainer == m_viewTab[m_tabIndex].primaryView) ||
819 (viewContainer == m_viewTab[m_tabIndex].secondaryView));
820 if (m_activeViewContainer == viewContainer) {
821 return;
822 }
823
824 m_activeViewContainer->setActive(false);
825 m_activeViewContainer = viewContainer;
826
827 // Activating the view container might trigger a recursive setActiveViewContainer() call
828 // inside DolphinMainWindow::toggleActiveView() when having a split view. Temporary
829 // disconnect the activated() signal in this case:
830 disconnect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
831 m_activeViewContainer->setActive(true);
832 connect(m_activeViewContainer->view(), SIGNAL(activated()), this, SLOT(toggleActiveView()));
833
834 m_actionHandler->setCurrentView(viewContainer->view());
835
836 updateHistory();
837 updateEditActions();
838 updateViewActions();
839 updateGoActions();
840
841 const KUrl& url = m_activeViewContainer->url();
842 const QString caption = url.fileName();
843 setCaption(caption);
844 if (m_viewTab.count() > 1) {
845 m_tabBar->setTabText(m_tabIndex, caption);
846 }
847
848 emit urlChanged(url);
849 }
850
851 void DolphinMainWindow::setupActions()
852 {
853 // setup 'File' menu
854 m_newMenu = new DolphinNewMenu(this);
855 KMenu* menu = m_newMenu->menu();
856 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
857 menu->setIcon(KIcon("document-new"));
858 connect(menu, SIGNAL(aboutToShow()),
859 this, SLOT(updateNewMenu()));
860
861 KAction* newWindow = actionCollection()->addAction("new_window");
862 newWindow->setIcon(KIcon("window-new"));
863 newWindow->setText(i18nc("@action:inmenu File", "New &Window"));
864 newWindow->setShortcut(Qt::CTRL | Qt::Key_N);
865 connect(newWindow, SIGNAL(triggered()), this, SLOT(openNewMainWindow()));
866
867 KAction* newTab = actionCollection()->addAction("new_tab");
868 newTab->setIcon(KIcon("tab-new"));
869 newTab->setText(i18nc("@action:inmenu File", "New Tab"));
870 newTab->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_N);
871 connect(newTab, SIGNAL(triggered()), this, SLOT(openNewTab()));
872
873 KAction* properties = actionCollection()->addAction("properties");
874 properties->setText(i18nc("@action:inmenu File", "Properties"));
875 properties->setShortcut(Qt::ALT | Qt::Key_Return);
876 connect(properties, SIGNAL(triggered()), this, SLOT(properties()));
877
878 KStandardAction::quit(this, SLOT(quit()), actionCollection());
879
880 // setup 'Edit' menu
881 KStandardAction::undo(this,
882 SLOT(undo()),
883 actionCollection());
884
885 // need to remove shift+del from cut action, else the shortcut for deletejob
886 // doesn't work
887 KAction* cut = KStandardAction::cut(this, SLOT(cut()), actionCollection());
888 KShortcut cutShortcut = cut->shortcut();
889 cutShortcut.remove(Qt::SHIFT + Qt::Key_Delete, KShortcut::KeepEmpty);
890 cut->setShortcut(cutShortcut);
891 KStandardAction::copy(this, SLOT(copy()), actionCollection());
892 KStandardAction::paste(this, SLOT(paste()), actionCollection());
893
894 KAction* selectAll = actionCollection()->addAction("select_all");
895 selectAll->setText(i18nc("@action:inmenu Edit", "Select All"));
896 selectAll->setShortcut(Qt::CTRL + Qt::Key_A);
897 connect(selectAll, SIGNAL(triggered()), this, SLOT(selectAll()));
898
899 KAction* invertSelection = actionCollection()->addAction("invert_selection");
900 invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection"));
901 invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A);
902 connect(invertSelection, SIGNAL(triggered()), this, SLOT(invertSelection()));
903
904 // setup 'View' menu
905 // (note that most of it is set up in DolphinViewActionHandler)
906
907 KAction* split = actionCollection()->addAction("split_view");
908 split->setShortcut(Qt::Key_F3);
909 updateSplitAction();
910 connect(split, SIGNAL(triggered()), this, SLOT(toggleSplitView()));
911
912 KAction* reload = actionCollection()->addAction("reload");
913 reload->setText(i18nc("@action:inmenu View", "Reload"));
914 reload->setShortcut(Qt::Key_F5);
915 reload->setIcon(KIcon("view-refresh"));
916 connect(reload, SIGNAL(triggered()), this, SLOT(reloadView()));
917
918 KAction* stop = actionCollection()->addAction("stop");
919 stop->setText(i18nc("@action:inmenu View", "Stop"));
920 stop->setIcon(KIcon("process-stop"));
921 connect(stop, SIGNAL(triggered()), this, SLOT(stopLoading()));
922
923 // TODO: the naming "Show full Location" is currently confusing...
924 KToggleAction* showFullLocation = actionCollection()->add<KToggleAction>("editable_location");
925 showFullLocation->setText(i18nc("@action:inmenu Navigation Bar", "Show Full Location"));
926 showFullLocation->setShortcut(Qt::CTRL | Qt::Key_L);
927 connect(showFullLocation, SIGNAL(triggered()), this, SLOT(toggleEditLocation()));
928
929 KAction* editLocation = actionCollection()->addAction("edit_location");
930 editLocation->setText(i18nc("@action:inmenu Navigation Bar", "Edit Location"));
931 editLocation->setShortcut(Qt::Key_F6);
932 connect(editLocation, SIGNAL(triggered()), this, SLOT(editLocation()));
933
934 KAction* adjustViewProps = actionCollection()->addAction("view_properties");
935 adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Properties..."));
936 connect(adjustViewProps, SIGNAL(triggered()), this, SLOT(adjustViewProperties()));
937
938 // setup 'Go' menu
939 KAction* backAction = KStandardAction::back(this, SLOT(goBack()), actionCollection());
940 KShortcut backShortcut = backAction->shortcut();
941 backShortcut.setAlternate(Qt::Key_Backspace);
942 backAction->setShortcut(backShortcut);
943
944 KStandardAction::forward(this, SLOT(goForward()), actionCollection());
945 KStandardAction::up(this, SLOT(goUp()), actionCollection());
946 KStandardAction::home(this, SLOT(goHome()), actionCollection());
947
948 // setup 'Tools' menu
949 QAction* findFile = actionCollection()->addAction("find_file");
950 findFile->setText(i18nc("@action:inmenu Tools", "Find File..."));
951 findFile->setShortcut(Qt::CTRL | Qt::Key_F);
952 findFile->setIcon(KIcon("edit-find"));
953 connect(findFile, SIGNAL(triggered()), this, SLOT(findFile()));
954
955 KToggleAction* showFilterBar = actionCollection()->add<KToggleAction>("show_filter_bar");
956 showFilterBar->setText(i18nc("@action:inmenu Tools", "Show Filter Bar"));
957 showFilterBar->setShortcut(Qt::CTRL | Qt::Key_I);
958 connect(showFilterBar, SIGNAL(triggered(bool)), this, SLOT(toggleFilterBarVisibility(bool)));
959
960 KAction* compareFiles = actionCollection()->addAction("compare_files");
961 compareFiles->setText(i18nc("@action:inmenu Tools", "Compare Files"));
962 compareFiles->setIcon(KIcon("kompare"));
963 compareFiles->setEnabled(false);
964 connect(compareFiles, SIGNAL(triggered()), this, SLOT(compareFiles()));
965
966 // setup 'Settings' menu
967 m_showMenuBar = KStandardAction::showMenubar(this, SLOT(toggleShowMenuBar()), actionCollection());
968 KStandardAction::preferences(this, SLOT(editSettings()), actionCollection());
969 }
970
971 void DolphinMainWindow::setupDockWidgets()
972 {
973 // setup "Information"
974 QDockWidget* infoDock = new QDockWidget(i18nc("@title:window", "Information"));
975 infoDock->setObjectName("infoDock");
976 infoDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
977 SidebarPage* infoWidget = new InfoSidebarPage(infoDock);
978 infoDock->setWidget(infoWidget);
979
980 infoDock->toggleViewAction()->setText(i18nc("@title:window", "Information"));
981 infoDock->toggleViewAction()->setShortcut(Qt::Key_F11);
982 actionCollection()->addAction("show_info_panel", infoDock->toggleViewAction());
983
984 addDockWidget(Qt::RightDockWidgetArea, infoDock);
985 connect(this, SIGNAL(urlChanged(KUrl)),
986 infoWidget, SLOT(setUrl(KUrl)));
987 connect(this, SIGNAL(selectionChanged(KFileItemList)),
988 infoWidget, SLOT(setSelection(KFileItemList)));
989 connect(this, SIGNAL(requestItemInfo(KFileItem)),
990 infoWidget, SLOT(requestDelayedItemInfo(KFileItem)));
991
992 // setup "Tree View"
993 QDockWidget* treeViewDock = new QDockWidget(i18nc("@title:window", "Folders"));
994 treeViewDock->setObjectName("treeViewDock");
995 treeViewDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
996 TreeViewSidebarPage* treeWidget = new TreeViewSidebarPage(treeViewDock);
997 treeViewDock->setWidget(treeWidget);
998
999 treeViewDock->toggleViewAction()->setText(i18nc("@title:window", "Folders"));
1000 treeViewDock->toggleViewAction()->setShortcut(Qt::Key_F7);
1001 actionCollection()->addAction("show_folders_panel", treeViewDock->toggleViewAction());
1002
1003 addDockWidget(Qt::LeftDockWidgetArea, treeViewDock);
1004 connect(this, SIGNAL(urlChanged(KUrl)),
1005 treeWidget, SLOT(setUrl(KUrl)));
1006 connect(treeWidget, SIGNAL(changeUrl(KUrl)),
1007 this, SLOT(changeUrl(KUrl)));
1008 connect(treeWidget, SIGNAL(changeSelection(KFileItemList)),
1009 this, SLOT(changeSelection(KFileItemList)));
1010 connect(treeWidget, SIGNAL(urlsDropped(KUrl::List, KUrl)),
1011 this, SLOT(dropUrls(KUrl::List, KUrl)));
1012
1013 // setup "Terminal"
1014 #ifndef Q_OS_WIN
1015 QDockWidget* terminalDock = new QDockWidget(i18nc("@title:window Shell terminal", "Terminal"));
1016 terminalDock->setObjectName("terminalDock");
1017 terminalDock->setAllowedAreas(Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
1018 SidebarPage* terminalWidget = new TerminalSidebarPage(terminalDock);
1019 terminalDock->setWidget(terminalWidget);
1020
1021 connect(terminalWidget, SIGNAL(hideTerminalSidebarPage()), terminalDock, SLOT(hide()));
1022
1023 terminalDock->toggleViewAction()->setText(i18nc("@title:window Shell terminal", "Terminal"));
1024 terminalDock->toggleViewAction()->setShortcut(Qt::Key_F4);
1025 actionCollection()->addAction("show_terminal_panel", terminalDock->toggleViewAction());
1026
1027 addDockWidget(Qt::BottomDockWidgetArea, terminalDock);
1028 connect(this, SIGNAL(urlChanged(KUrl)),
1029 terminalWidget, SLOT(setUrl(KUrl)));
1030 #endif
1031
1032 const bool firstRun = DolphinSettings::instance().generalSettings()->firstRun();
1033 if (firstRun) {
1034 treeViewDock->hide();
1035 #ifndef Q_OS_WIN
1036 terminalDock->hide();
1037 #endif
1038 }
1039
1040 QDockWidget* placesDock = new QDockWidget(i18nc("@title:window", "Places"));
1041 placesDock->setObjectName("placesDock");
1042 placesDock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
1043
1044 DolphinFilePlacesView* placesView = new DolphinFilePlacesView(placesDock);
1045 placesDock->setWidget(placesView);
1046 placesView->setModel(DolphinSettings::instance().placesModel());
1047 placesView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
1048
1049 placesDock->toggleViewAction()->setText(i18nc("@title:window", "Places"));
1050 placesDock->toggleViewAction()->setShortcut(Qt::Key_F9);
1051 actionCollection()->addAction("show_places_panel", placesDock->toggleViewAction());
1052
1053 addDockWidget(Qt::LeftDockWidgetArea, placesDock);
1054 connect(placesView, SIGNAL(urlChanged(KUrl)),
1055 this, SLOT(changeUrl(KUrl)));
1056 connect(this, SIGNAL(urlChanged(KUrl)),
1057 placesView, SLOT(setUrl(KUrl)));
1058 }
1059
1060 void DolphinMainWindow::updateEditActions()
1061 {
1062 const KFileItemList list = m_activeViewContainer->view()->selectedItems();
1063 if (list.isEmpty()) {
1064 stateChanged("has_no_selection");
1065 } else {
1066 stateChanged("has_selection");
1067
1068 QAction* renameAction = actionCollection()->action("rename");
1069 if (renameAction != 0) {
1070 renameAction->setEnabled(true);
1071 }
1072
1073 bool enableMoveToTrash = true;
1074
1075 KFileItemList::const_iterator it = list.begin();
1076 const KFileItemList::const_iterator end = list.end();
1077 while (it != end) {
1078 const KUrl& url = (*it).url();
1079 // only enable the 'Move to Trash' action for local files
1080 if (!url.isLocalFile()) {
1081 enableMoveToTrash = false;
1082 }
1083 ++it;
1084 }
1085
1086 QAction* moveToTrashAction = actionCollection()->action("move_to_trash");
1087 moveToTrashAction->setEnabled(enableMoveToTrash);
1088 }
1089 updatePasteAction();
1090 }
1091
1092 void DolphinMainWindow::updateViewActions()
1093 {
1094 m_actionHandler->updateViewActions();
1095
1096 QAction* showFilterBarAction = actionCollection()->action("show_filter_bar");
1097 showFilterBarAction->setChecked(m_activeViewContainer->isFilterBarVisible());
1098
1099 updateSplitAction();
1100
1101 QAction* editableLocactionAction = actionCollection()->action("editable_location");
1102 const KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator();
1103 editableLocactionAction->setChecked(urlNavigator->isUrlEditable());
1104 }
1105
1106 void DolphinMainWindow::updateGoActions()
1107 {
1108 QAction* goUpAction = actionCollection()->action(KStandardAction::name(KStandardAction::Up));
1109 const KUrl& currentUrl = m_activeViewContainer->url();
1110 goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
1111 }
1112
1113 void DolphinMainWindow::clearStatusBar()
1114 {
1115 m_activeViewContainer->statusBar()->clear();
1116 }
1117
1118 void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
1119 {
1120 connect(container, SIGNAL(showFilterBarChanged(bool)),
1121 this, SLOT(updateFilterBarAction(bool)));
1122
1123 DolphinView* view = container->view();
1124 connect(view, SIGNAL(selectionChanged(KFileItemList)),
1125 this, SLOT(slotSelectionChanged(KFileItemList)));
1126 connect(view, SIGNAL(requestItemInfo(KFileItem)),
1127 this, SLOT(slotRequestItemInfo(KFileItem)));
1128 connect(view, SIGNAL(activated()),
1129 this, SLOT(toggleActiveView()));
1130 connect(view, SIGNAL(doingOperation(KonqFileUndoManager::CommandType)),
1131 this, SLOT(slotDoingOperation(KonqFileUndoManager::CommandType)));
1132 connect(view, SIGNAL(tabRequested(const KUrl&)),
1133 this, SLOT(openNewTab(const KUrl&)));
1134
1135 const KUrlNavigator* navigator = container->urlNavigator();
1136 connect(navigator, SIGNAL(urlChanged(const KUrl&)),
1137 this, SLOT(changeUrl(const KUrl&)));
1138 connect(navigator, SIGNAL(historyChanged()),
1139 this, SLOT(updateHistory()));
1140 connect(navigator, SIGNAL(editableStateChanged(bool)),
1141 this, SLOT(slotEditableStateChanged(bool)));
1142 }
1143
1144 void DolphinMainWindow::updateSplitAction()
1145 {
1146 QAction* splitAction = actionCollection()->action("split_view");
1147 if (m_viewTab[m_tabIndex].secondaryView != 0) {
1148 if (m_activeViewContainer == m_viewTab[m_tabIndex].primaryView) {
1149 splitAction->setText(i18nc("@action:intoolbar Close right view", "Close"));
1150 splitAction->setIcon(KIcon("view-right-close"));
1151 } else {
1152 splitAction->setText(i18nc("@action:intoolbar Close left view", "Close"));
1153 splitAction->setIcon(KIcon("view-left-close"));
1154 }
1155 } else {
1156 splitAction->setText(i18nc("@action:intoolbar Split view", "Split"));
1157 splitAction->setIcon(KIcon("view-right-new"));
1158 }
1159 }
1160
1161 DolphinMainWindow::UndoUiInterface::UndoUiInterface(DolphinMainWindow* mainWin) :
1162 KonqFileUndoManager::UiInterface(mainWin),
1163 m_mainWin(mainWin)
1164 {
1165 Q_ASSERT(m_mainWin != 0);
1166 }
1167
1168 DolphinMainWindow::UndoUiInterface::~UndoUiInterface()
1169 {
1170 }
1171
1172 void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
1173 {
1174 DolphinStatusBar* statusBar = m_mainWin->activeViewContainer()->statusBar();
1175 statusBar->setMessage(job->errorString(), DolphinStatusBar::Error);
1176 }
1177
1178 #include "dolphinmainwindow.moc"