]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphintabwidget.cpp
Fix detach tab not working when path contains spaces.
[dolphin.git] / src / dolphintabwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphintabwidget.h"
21
22 #include "dolphintabbar.h"
23 #include "dolphintabpage.h"
24 #include "dolphinviewcontainer.h"
25
26 #include <QApplication>
27 #include <KConfigGroup>
28 #include <KShell>
29 #include <kio/global.h>
30 #include <KRun>
31
32 DolphinTabWidget::DolphinTabWidget(QWidget* parent) :
33 QTabWidget(parent),
34 m_placesSelectorVisible(true)
35 {
36 connect(this, SIGNAL(tabCloseRequested(int)),
37 this, SLOT(closeTab(int)));
38 connect(this, SIGNAL(currentChanged(int)),
39 this, SLOT(currentTabChanged(int)));
40
41 DolphinTabBar* tabBar = new DolphinTabBar(this);
42 connect(tabBar, SIGNAL(openNewActivatedTab(int)),
43 this, SLOT(openNewActivatedTab(int)));
44 connect(tabBar, SIGNAL(tabDropEvent(int,QDropEvent*)),
45 this, SLOT(tabDropEvent(int,QDropEvent*)));
46 connect(tabBar, SIGNAL(tabDetachRequested(int)),
47 this, SLOT(detachTab(int)));
48 tabBar->hide();
49
50 setTabBar(tabBar);
51 setDocumentMode(true);
52 setElideMode(Qt::ElideRight);
53 setUsesScrollButtons(true);
54 }
55
56 DolphinTabPage* DolphinTabWidget::currentTabPage() const
57 {
58 return tabPageAt(currentIndex());
59 }
60
61 DolphinTabPage* DolphinTabWidget::tabPageAt(const int index) const
62 {
63 return static_cast<DolphinTabPage*>(widget(index));
64 }
65
66 void DolphinTabWidget::saveProperties(KConfigGroup& group) const
67 {
68 const int tabCount = count();
69 group.writeEntry("Tab Count", tabCount);
70 group.writeEntry("Active Tab Index", currentIndex());
71
72 for (int i = 0; i < tabCount; ++i) {
73 const DolphinTabPage* tabPage = tabPageAt(i);
74 group.writeEntry("Tab Data " % QString::number(i), tabPage->saveState());
75 }
76 }
77
78 void DolphinTabWidget::readProperties(const KConfigGroup& group)
79 {
80 const int tabCount = group.readEntry("Tab Count", 0);
81 for (int i = 0; i < tabCount; ++i) {
82 if (i >= count()) {
83 openNewActivatedTab();
84 }
85 if (group.hasKey("Tab Data " % QString::number(i))) {
86 // Tab state created with Dolphin > 4.14.x
87 const QByteArray state = group.readEntry("Tab Data " % QString::number(i), QByteArray());
88 tabPageAt(i)->restoreState(state);
89 } else {
90 // Tab state created with Dolphin <= 4.14.x
91 const QByteArray state = group.readEntry("Tab " % QString::number(i), QByteArray());
92 tabPageAt(i)->restoreStateV1(state);
93 }
94 }
95
96 const int index = group.readEntry("Active Tab Index", 0);
97 setCurrentIndex(index);
98 }
99
100 void DolphinTabWidget::refreshViews()
101 {
102 const int tabCount = count();
103 for (int i = 0; i < tabCount; ++i) {
104 tabPageAt(i)->refreshViews();
105 }
106 }
107
108 void DolphinTabWidget::openNewActivatedTab()
109 {
110 const DolphinViewContainer* oldActiveViewContainer = currentTabPage()->activeViewContainer();
111 Q_ASSERT(oldActiveViewContainer);
112
113 const bool isUrlEditable = oldActiveViewContainer->urlNavigator()->isUrlEditable();
114
115 openNewActivatedTab(oldActiveViewContainer->url());
116
117 DolphinViewContainer* newActiveViewContainer = currentTabPage()->activeViewContainer();
118 Q_ASSERT(newActiveViewContainer);
119
120 // The URL navigator of the new tab should have the same editable state
121 // as the current tab
122 KUrlNavigator* navigator = newActiveViewContainer->urlNavigator();
123 navigator->setUrlEditable(isUrlEditable);
124
125 if (isUrlEditable) {
126 // If a new tab is opened and the URL is editable, assure that
127 // the user can edit the URL without manually setting the focus
128 navigator->setFocus();
129 }
130 }
131
132 void DolphinTabWidget::openNewActivatedTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
133 {
134 openNewTab(primaryUrl, secondaryUrl);
135 setCurrentIndex(count() - 1);
136 }
137
138 void DolphinTabWidget::openNewTab(const QUrl& primaryUrl, const QUrl& secondaryUrl)
139 {
140 QWidget* focusWidget = QApplication::focusWidget();
141
142 DolphinTabPage* tabPage = new DolphinTabPage(primaryUrl, secondaryUrl, this);
143 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
144 connect(tabPage, SIGNAL(activeViewChanged(DolphinViewContainer*)),
145 this, SIGNAL(activeViewChanged(DolphinViewContainer*)));
146 connect(tabPage, SIGNAL(activeViewUrlChanged(QUrl)),
147 this, SLOT(tabUrlChanged(QUrl)));
148 addTab(tabPage, QIcon::fromTheme(KIO::iconNameForUrl(primaryUrl)), tabName(primaryUrl));
149
150 if (focusWidget) {
151 // The DolphinViewContainer grabbed the keyboard focus. As the tab is opened
152 // in background, assure that the previous focused widget gets the focus back.
153 focusWidget->setFocus();
154 }
155 }
156
157 void DolphinTabWidget::openDirectories(const QList<QUrl>& dirs, bool splitView)
158 {
159 Q_ASSERT(dirs.size() > 0);
160
161 QList<QUrl>::const_iterator it = dirs.constBegin();
162 while (it != dirs.constEnd()) {
163 const QUrl& primaryUrl = *(it++);
164 if (splitView && (it != dirs.constEnd())) {
165 const QUrl& secondaryUrl = *(it++);
166 openNewTab(primaryUrl, secondaryUrl);
167 } else {
168 openNewTab(primaryUrl);
169 }
170 }
171 }
172
173 void DolphinTabWidget::openFiles(const QList<QUrl>& files, bool splitView)
174 {
175 Q_ASSERT(files.size() > 0);
176
177 // Get all distinct directories from 'files' and open a tab
178 // for each directory. If the "split view" option is enabled, two
179 // directories are shown inside one tab (see openDirectories()).
180 QList<QUrl> dirs;
181 foreach (const QUrl& url, files) {
182 const QUrl dir(url.adjusted(QUrl::RemoveFilename));
183 if (!dirs.contains(dir)) {
184 dirs.append(dir);
185 }
186 }
187
188 const int oldTabCount = count();
189 openDirectories(dirs, splitView);
190 const int tabCount = count();
191
192 // Select the files. Although the files can be split between several
193 // tabs, there is no need to split 'files' accordingly, as
194 // the DolphinView will just ignore invalid selections.
195 for (int i = oldTabCount; i < tabCount; ++i) {
196 DolphinTabPage* tabPage = tabPageAt(i);
197 tabPage->markUrlsAsSelected(files);
198 tabPage->markUrlAsCurrent(files.first());
199 }
200 }
201
202 void DolphinTabWidget::closeTab()
203 {
204 closeTab(currentIndex());
205 }
206
207 void DolphinTabWidget::closeTab(const int index)
208 {
209 Q_ASSERT(index >= 0);
210 Q_ASSERT(index < count());
211
212 if (count() < 2) {
213 // Never close the last tab.
214 return;
215 }
216
217 DolphinTabPage* tabPage = tabPageAt(index);
218 emit rememberClosedTab(tabPage->activeViewContainer()->url(), tabPage->saveState());
219
220 removeTab(index);
221 tabPage->deleteLater();
222 }
223
224 void DolphinTabWidget::activateNextTab()
225 {
226 const int index = currentIndex() + 1;
227 setCurrentIndex(index < count() ? index : 0);
228 }
229
230 void DolphinTabWidget::activatePrevTab()
231 {
232 const int index = currentIndex() - 1;
233 setCurrentIndex(index >= 0 ? index : (count() - 1));
234 }
235
236 void DolphinTabWidget::slotPlacesPanelVisibilityChanged(bool visible)
237 {
238 // The places-selector from the URL navigator should only be shown
239 // if the places dock is invisible
240 m_placesSelectorVisible = !visible;
241
242 const int tabCount = count();
243 for (int i = 0; i < tabCount; ++i) {
244 DolphinTabPage* tabPage = tabPageAt(i);
245 tabPage->setPlacesSelectorVisible(m_placesSelectorVisible);
246 }
247 }
248
249 void DolphinTabWidget::restoreClosedTab(const QByteArray& state)
250 {
251 openNewActivatedTab();
252 currentTabPage()->restoreState(state);
253 }
254
255 void DolphinTabWidget::detachTab(int index)
256 {
257 Q_ASSERT(index >= 0);
258
259 QStringList args;
260
261 const DolphinTabPage* tabPage = tabPageAt(index);
262 args << tabPage->primaryViewContainer()->url().url();
263 if (tabPage->splitViewEnabled()) {
264 args << tabPage->secondaryViewContainer()->url().url();
265 args << QStringLiteral("--split");
266 }
267
268 const QString command = QStringLiteral("dolphin %1").arg(KShell::joinArgs(args));
269 KRun::runCommand(command, this);
270
271 closeTab(index);
272 }
273
274 void DolphinTabWidget::openNewActivatedTab(int index)
275 {
276 Q_ASSERT(index >= 0);
277 const DolphinTabPage* tabPage = tabPageAt(index);
278 openNewActivatedTab(tabPage->activeViewContainer()->url());
279 }
280
281 void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event)
282 {
283 if (index >= 0) {
284 DolphinView* view = tabPageAt(index)->activeViewContainer()->view();
285 view->dropUrls(view->url(), event);
286 }
287 }
288
289 void DolphinTabWidget::tabUrlChanged(const QUrl& url)
290 {
291 const int index = indexOf(qobject_cast<QWidget*>(sender()));
292 if (index >= 0) {
293 tabBar()->setTabText(index, tabName(url));
294 tabBar()->setTabIcon(index, QIcon::fromTheme(KIO::iconNameForUrl(url)));
295
296 // Emit the currentUrlChanged signal if the url of the current tab has been changed.
297 if (index == currentIndex()) {
298 emit currentUrlChanged(url);
299 }
300 }
301 }
302
303 void DolphinTabWidget::currentTabChanged(int index)
304 {
305 DolphinViewContainer* viewContainer = tabPageAt(index)->activeViewContainer();
306 emit activeViewChanged(viewContainer);
307 emit currentUrlChanged(viewContainer->url());
308 viewContainer->view()->setFocus();
309 }
310
311 void DolphinTabWidget::tabInserted(int index)
312 {
313 QTabWidget::tabInserted(index);
314
315 if (count() > 1) {
316 tabBar()->show();
317 }
318
319 emit tabCountChanged(count());
320 }
321
322 void DolphinTabWidget::tabRemoved(int index)
323 {
324 QTabWidget::tabRemoved(index);
325
326 // If only one tab is left, then remove the tab entry so that
327 // closing the last tab is not possible.
328 if (count() < 2) {
329 tabBar()->hide();
330 }
331
332 emit tabCountChanged(count());
333 }
334
335 QString DolphinTabWidget::tabName(const QUrl& url) const
336 {
337 QString name;
338 if (url == QUrl("file:///")) {
339 name = '/';
340 } else {
341 name = url.adjusted(QUrl::StripTrailingSlash).fileName();
342 if (name.isEmpty()) {
343 name = url.scheme();
344 } else {
345 // Make sure that a '&' inside the directory name is displayed correctly
346 // and not misinterpreted as a keyboard shortcut in QTabBar::setTabText()
347 name.replace('&', "&&");
348 }
349 }
350 return name;
351 }