]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Don't re-open already-open URLs when using session-restore feature
[dolphin.git] / src / dolphintabwidget.cpp
1 /*
2 * SPDX-FileCopyrightText: 2014 Emmanuel Pescosta <emmanuelpescosta099@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "dolphintabwidget.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "dolphintabbar.h"
11 #include "dolphinviewcontainer.h"
12
13 #include <KConfigGroup>
14 #include <KShell>
15 #include <kio/global.h>
16 #include <KIO/CommandLauncherJob>
17 #include <KAcceleratorManager>
18
19 #include <QApplication>
20 #include <QDropEvent>
21
22 DolphinTabWidget::DolphinTabWidget(DolphinNavigatorsWidgetAction *navigatorsWidget, QWidget* parent) :
23 QTabWidget(parent),
24 m_lastViewedTab(nullptr),
25 m_navigatorsWidget{navigatorsWidget}
26 {
27 KAcceleratorManager::setNoAccel(this);
28
29 connect(this, &DolphinTabWidget::tabCloseRequested,
30 this, QOverload<int>::of(&DolphinTabWidget::closeTab));
31 connect(this, &DolphinTabWidget::currentChanged,
32 this, &DolphinTabWidget::currentTabChanged);
33
34 DolphinTabBar* tabBar = new DolphinTabBar(this);
35 connect(tabBar, &DolphinTabBar::openNewActivatedTab,
36 this, QOverload<int>::of(&DolphinTabWidget::openNewActivatedTab));
37 connect(tabBar, &DolphinTabBar::tabDropEvent,
38 this, &DolphinTabWidget::tabDropEvent);
39 connect(tabBar, &DolphinTabBar::tabDetachRequested,
40 this, &DolphinTabWidget::detachTab);
41 tabBar->hide();
42
43 setTabBar(tabBar);
44 setDocumentMode(true);
45 setElideMode(Qt::ElideRight);
46 setUsesScrollButtons(true);
47 }
48
49 DolphinTabPage* DolphinTabWidget::currentTabPage() const
50 {
51 return tabPageAt(currentIndex());
52 }
53
54 DolphinTabPage* DolphinTabWidget::nextTabPage() const
55 {
56 const int index = currentIndex() + 1;
57 return tabPageAt(index < count() ? index : 0);
58 }
59
60 DolphinTabPage* DolphinTabWidget::prevTabPage() const
61 {
62 const int index = currentIndex() - 1;
63 return tabPageAt(index >= 0 ? index : (count() - 1));
64 }
65
66 DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
67 {
68 return static_cast<DolphinTabPage*>(widget(index));
69 }
70
71 void DolphinTabWidget::saveProperties(KConfigGroup& group) const
72 {
73 const int tabCount = count();
74 group.writeEntry("Tab Count", tabCount);
75 group.writeEntry("Active Tab Index", currentIndex());
76
77 for (int i = 0; i < tabCount; ++i) {
78 const DolphinTabPage* tabPage = tabPageAt(i);
79 group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState());
80 }
81 }
82
83 void DolphinTabWidget::readProperties(const KConfigGroup& group)
84 {
85 const int tabCount = group.readEntry("Tab Count", 0);
86 for (int i = 0; i < tabCount; ++i) {
87 if (i >= count()) {
88 openNewActivatedTab();
89 }
90 if (group.hasKey("Tab Data " % QString::number(i))) {
91 // Tab state created with Dolphin > 4.14.x
92 const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray());
93 tabPageAt(i)->restoreState(state);
94 } else {
95 // Tab state created with Dolphin <= 4.14.x
96 const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray());
97 tabPageAt(i)->restoreStateV1(state);
98 }
99 }
100
101 const int index = group.readEntry("Active Tab Index", 0);
102 setCurrentIndex(index);
103 }
104
105 void DolphinTabWidget::refreshViews()
106 {
107 // Left-elision is better when showing full paths, since you care most
108 // about the current directory which is on the right
109 if (GeneralSettings::showFullPathInTitlebar()) {
110 setElideMode(Qt::ElideLeft);
111 } else {
112 setElideMode(Qt::ElideRight);
113 }
114
115 const int tabCount = count();
116 for (int i = 0; i < tabCount; ++i) {
117 tabBar()->setTabText(i, tabName(tabPageAt(i)));
118 tabPageAt(i)->refreshViews();
119 }
120 }
121
122 bool DolphinTabWidget::isUrlOpen(const QUrl &url) const
123 {
124 return indexByUrl(url).first >= 0;
125 }
126
127 void DolphinTabWidget::openNewActivatedTab()
128 {
129 std::unique_ptr<DolphinUrlNavigator::VisualState> oldNavigatorState;
130 if (currentTabPage()->primaryViewActive() || !m_navigatorsWidget->secondaryUrlNavigator()) {
131 oldNavigatorState = m_navigatorsWidget->primaryUrlNavigator()->visualState();
132 } else {
133 oldNavigatorState = m_navigatorsWidget->secondaryUrlNavigator()->visualState();
134 }
135
136 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
137 Q_ASSERT(oldActiveViewContainer);
138
139 openNewActivatedTab(oldActiveViewContainer->url());
140
141 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
142 Q_ASSERT(newActiveViewContainer);
143
144 // The URL navigator of the new tab should have the same editable state
145 // as the current tab
146 newActiveViewContainer->urlNavigator()->setVisualState(*oldNavigatorState.get());
147
148 // Always focus the new tab's view
149 newActiveViewContainer->view()->setFocus();
150 }
151
152 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
153 {
154 openNewTab(primaryUrl, secondaryUrl);
155 if (GeneralSettings::openNewTabAfterLastTab()) {
156 setCurrentIndex(count() - 1);
157 } else {
158 setCurrentIndex(currentIndex() + 1);
159 }
160 }
161
162 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
163 {
164 QWidget* focusWidget = QApplication::focusWidget();
165
166 DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
167 tabPage->setActive(false);
168 connect(tabPage, &DolphinTabPage::activeViewChanged,
169 this, &DolphinTabWidget::activeViewChanged);
170 connect(tabPage, &DolphinTabPage::activeViewUrlChanged,
171 this, &DolphinTabWidget::tabUrlChanged);
172 int newTabIndex = -1;
173 if (!GeneralSettings::openNewTabAfterLastTab()) {
174 newTabIndex = currentIndex() + 1;
175 }
176 insertTab(newTabIndex, tabPage, QIcon() /* loaded in tabInserted */, tabName(tabPage));
177
178 if (focusWidget) {
179 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
180 // in background, assure that the previous focused widget gets the focus back.
181 focusWidget->setFocus();
182 }
183 }
184
185 void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
186 {
187 Q_ASSERT(dirs.size() > 0);
188
189 bool somethingWasAlreadyOpen = false;
190
191 QList<QUrl>::const_iterator it = dirs.constBegin();
192 while (it != dirs.constEnd()) {
193 const QUrl& primaryUrl = *(it++);
194 const QPair<int, bool> indexInfo = indexByUrl(primaryUrl);
195 const int index = indexInfo.first;
196 const bool isInPrimaryView = indexInfo.second;
197
198 // When the user asks for a URL that's already open, activate it instead
199 // of opening a second copy
200 if (index >= 0) {
201 somethingWasAlreadyOpen = true;
202 activateTab(index);
203 const auto tabPage = tabPageAt(index);
204 if (isInPrimaryView) {
205 tabPage->primaryViewContainer()->setActive(true);
206 } else {
207 tabPage->secondaryViewContainer()->setActive(true);
208 }
209 // BUG: 417230
210 // Required for updateViewState() call in openFiles() to work as expected
211 // If there is a selection, updateViewState() calls are effectively a no-op
212 tabPage->activeViewContainer()->view()->clearSelection();
213 } else if (splitView) {
214 const QUrl& secondaryUrl = *(it++);
215 if (somethingWasAlreadyOpen) {
216 openNewTab(primaryUrl, secondaryUrl);
217 } else {
218 openNewActivatedTab(primaryUrl, secondaryUrl);
219 }
220 } else {
221 if (somethingWasAlreadyOpen) {
222 openNewTab(primaryUrl);
223 } else {
224 openNewActivatedTab(primaryUrl);
225 }
226 }
227 }
228 }
229
230 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
231 {
232 Q_ASSERT(files.size() > 0);
233
234 // Get all distinct directories from 'files' and open a tab
235 // for each directory. If the "split view" option is enabled, two
236 // directories are shown inside one tab (see openDirectories()).
237 QList<QUrl> dirs;
238 for (const QUrl& url : files) {
239 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
240 if (!dirs.contains(dir)) {
241 dirs.append(dir);
242 }
243 }
244
245 const int oldTabCount = count();
246 openDirectories(dirs, splitView);
247 const int tabCount = count();
248
249 // Select the files. Although the files can be split between several
250 // tabs, there is no need to split 'files' accordingly, as
251 // the DolphinView will just ignore invalid selections.
252 for (int i = 0; i < tabCount; ++i) {
253 DolphinTabPage* tabPage = tabPageAt(i);
254 tabPage->markUrlsAsSelected(files);
255 tabPage->markUrlAsCurrent(files.first());
256 if (i < oldTabCount) {
257 // Force selection of file if directory was already open, BUG: 417230
258 tabPage->activeViewContainer()->view()->updateViewState();
259 }
260 }
261 }
262
263 void DolphinTabWidget::closeTab()
264 {
265 closeTab(currentIndex());
266 }
267
268 void DolphinTabWidget::closeTab(const int index)
269 {
270 Q_ASSERT(index >= 0);
271 Q_ASSERT(index < count());
272
273 if (count() < 2) {
274 // Close Dolphin when closing the last tab.
275 parentWidget()->close();
276 return;
277 }
278
279 DolphinTabPage* tabPage = tabPageAt(index);
280 Q_EMIT rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
281
282 removeTab(index);
283 tabPage->deleteLater();
284 }
285
286 void DolphinTabWidget::activateTab(const int index)
287 {
288 if (index < count()) {
289 setCurrentIndex(index);
290 }
291 }
292
293 void DolphinTabWidget::activateLastTab()
294 {
295 setCurrentIndex(count() - 1);
296 }
297
298 void DolphinTabWidget::activateNextTab()
299 {
300 const int index = currentIndex() + 1;
301 setCurrentIndex(index < count() ? index : 0);
302 }
303
304 void DolphinTabWidget::activatePrevTab()
305 {
306 const int index = currentIndex() - 1;
307 setCurrentIndex(index >= 0 ? index : (count() - 1));
308 }
309
310 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
311 {
312 openNewActivatedTab();
313 currentTabPage()->restoreState(state);
314 }
315
316 void DolphinTabWidget::copyToInactiveSplitView()
317 {
318 const DolphinTabPage* tabPage = tabPageAt(currentIndex());
319 DolphinViewContainer* activeViewContainer = currentTabPage()->activeViewContainer();
320 if (!tabPage->splitViewEnabled() || activeViewContainer->view()->selectedItems().isEmpty()) {
321 return;
322 }
323
324 if (tabPage->primaryViewActive()) {
325 // copy from left panel to right
326 activeViewContainer->view()->copySelectedItems(activeViewContainer->view()->selectedItems(), tabPage->secondaryViewContainer()->url());
327 } else {
328 // copy from right panel to left
329 activeViewContainer->view()->copySelectedItems(activeViewContainer->view()->selectedItems(), tabPage->primaryViewContainer()->url());
330 }
331 }
332
333 void DolphinTabWidget::moveToInactiveSplitView()
334 {
335 const DolphinTabPage* tabPage = tabPageAt(currentIndex());
336 DolphinViewContainer* activeViewContainer = currentTabPage()->activeViewContainer();
337 if (!tabPage->splitViewEnabled() || activeViewContainer->view()->selectedItems().isEmpty()) {
338 return;
339 }
340
341 if (tabPage->primaryViewActive()) {
342 // move from left panel to right
343 activeViewContainer->view()->moveSelectedItems(activeViewContainer->view()->selectedItems(), tabPage->secondaryViewContainer()->url());
344 } else {
345 // move from right panel to left
346 activeViewContainer->view()->moveSelectedItems(activeViewContainer->view()->selectedItems(), tabPage->primaryViewContainer()->url());
347 }
348 }
349
350 void DolphinTabWidget::detachTab(int index)
351 {
352 Q_ASSERT(index >= 0);
353
354 QStringList args;
355
356 const DolphinTabPage* tabPage = tabPageAt(index);
357 args << tabPage->primaryViewContainer()->url().url();
358 if (tabPage->splitViewEnabled()) {
359 args << tabPage->secondaryViewContainer()->url().url();
360 args << QStringLiteral("--split");
361 }
362 args << QStringLiteral("--new-window");
363
364 KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob("dolphin", args, this);
365 job->setDesktopName(QStringLiteral("org.kde.dolphin"));
366 job->start();
367
368 closeTab(index);
369 }
370
371 void DolphinTabWidget::openNewActivatedTab(int index)
372 {
373 Q_ASSERT(index >= 0);
374 const DolphinTabPage* tabPage = tabPageAt(index);
375 openNewActivatedTab(tabPage->activeViewContainer()->url());
376 }
377
378 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
379 {
380 if (index >= 0) {
381 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
382 view->dropUrls(view->url(), event, view);
383 }
384 }
385
386 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
387 {
388 const int index = indexOf(qobject_cast<QWidget*>(sender()));
389 if (index >= 0) {
390 tabBar()->setTabText(index, tabName(tabPageAt(index)));
391 tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile));
392 if (tabBar()->isVisible()) {
393 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
394 } else {
395 // Mark as dirty, actually load once the tab bar actually gets shown
396 tabBar()->setTabIcon(index, QIcon());
397 }
398
399 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
400 if (index == currentIndex()) {
401 Q_EMIT currentUrlChanged(url);
402 }
403 }
404 }
405
406 void DolphinTabWidget::currentTabChanged(int index)
407 {
408 DolphinTabPage *tabPage = tabPageAt(index);
409 if (tabPage == m_lastViewedTab) {
410 return;
411 }
412 if (m_lastViewedTab) {
413 m_lastViewedTab->disconnectNavigators();
414 m_lastViewedTab->setActive(false);
415 }
416 if (tabPage->splitViewEnabled() && !m_navigatorsWidget->secondaryUrlNavigator()) {
417 m_navigatorsWidget->createSecondaryUrlNavigator();
418 }
419 DolphinViewContainer* viewContainer = tabPage->activeViewContainer();
420 Q_EMIT activeViewChanged(viewContainer);
421 Q_EMIT currentUrlChanged(viewContainer->url());
422 tabPage->setActive(true);
423 tabPage->connectNavigators(m_navigatorsWidget);
424 m_navigatorsWidget->setSecondaryNavigatorVisible(tabPage->splitViewEnabled());
425 m_lastViewedTab = tabPage;
426 }
427
428 void DolphinTabWidget::tabInserted(int index)
429 {
430 QTabWidget::tabInserted(index);
431
432 if (count() > 1) {
433 // Resolve all pending tab icons
434 for (int i = 0; i < count(); ++i) {
435 const QUrl url = tabPageAt(i)->activeViewContainer()->url();
436 if (tabBar()->tabIcon(i).isNull()) {
437 tabBar()->setTabIcon(i, QIcon::fromTheme(KIO::iconNameForUrl(url)));
438 }
439 if (tabBar()->tabToolTip(i).isEmpty()) {
440 tabBar()->setTabToolTip(index, url.toDisplayString(QUrl::PreferLocalFile));
441 }
442 }
443
444 tabBar()->show();
445 }
446
447 Q_EMIT tabCountChanged(count());
448 }
449
450 void DolphinTabWidget::tabRemoved(int index)
451 {
452 QTabWidget::tabRemoved(index);
453
454 // If only one tab is left, then remove the tab entry so that
455 // closing the last tab is not possible.
456 if (count() < 2) {
457 tabBar()->hide();
458 }
459
460 Q_EMIT tabCountChanged(count());
461 }
462
463 QString DolphinTabWidget::tabName(DolphinTabPage* tabPage) const
464 {
465 if (!tabPage) {
466 return QString();
467 }
468 QString name = tabPage->activeViewContainer()->caption();
469 // Make sure that a '&' inside the directory name is displayed correctly
470 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
471 return name.replace('&', QLatin1String("&&"));
472 }
473
474 QPair<int, bool> DolphinTabWidget::indexByUrl(const QUrl& url) const
475 {
476 for (int i = 0; i < count(); i++) {
477 const auto tabPage = tabPageAt(i);
478 if (url == tabPage->primaryViewContainer()->url()) {
479 return qMakePair(i, true);
480 }
481
482 if (tabPage->splitViewEnabled() && url == tabPage->secondaryViewContainer()->url()) {
483 return qMakePair(i, false);
484 }
485 }
486 return qMakePair(-1, false);
487 }