]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Fixed issue the the current view does not get the focus after opening Dolphin. Althou...
[dolphin.git] / src / dolphinviewcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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 "dolphinviewcontainer.h"
21 #include <kprotocolmanager.h>
22
23 #include <QtGui/QApplication>
24 #include <QtGui/QClipboard>
25 #include <QtGui/QKeyEvent>
26 #include <QtGui/QItemSelection>
27 #include <QtGui/QBoxLayout>
28 #include <QtCore/QTimer>
29 #include <QtGui/QScrollBar>
30
31 #include <kfileitemdelegate.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobalsettings.h>
34 #include <klocale.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/previewjob.h>
38 #include <kmimetyperesolver.h>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
41 #include <kurl.h>
42
43 #include "dolphinmodel.h"
44 #include "dolphincolumnview.h"
45 #include "dolphincontroller.h"
46 #include "dolphinstatusbar.h"
47 #include "dolphinmainwindow.h"
48 #include "dolphindirlister.h"
49 #include "dolphinsortfilterproxymodel.h"
50 #include "dolphindetailsview.h"
51 #include "dolphiniconsview.h"
52 #include "dolphincontextmenu.h"
53 #include "filterbar.h"
54 #include "kurlnavigator.h"
55 #include "viewproperties.h"
56 #include "dolphinsettings.h"
57 #include "dolphin_generalsettings.h"
58
59 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow,
60 QWidget* parent,
61 const KUrl& url) :
62 QWidget(parent),
63 m_showProgress(false),
64 m_mainWindow(mainWindow),
65 m_topLayout(0),
66 m_urlNavigator(0),
67 m_view(0),
68 m_filterBar(0),
69 m_statusBar(0),
70 m_dirLister(0),
71 m_proxyModel(0)
72 {
73 hide();
74
75 m_topLayout = new QVBoxLayout(this);
76 m_topLayout->setSpacing(0);
77 m_topLayout->setMargin(0);
78
79 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
80 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
81 m_mainWindow, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
82 connect(m_urlNavigator, SIGNAL(activated()),
83 this, SLOT(activate()));
84
85 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
86 m_urlNavigator->setUrlEditable(settings->editableUrl());
87 m_urlNavigator->setHomeUrl(settings->homeUrl());
88
89 m_dirLister = new DolphinDirLister();
90 m_dirLister->setAutoUpdate(true);
91 m_dirLister->setMainWindow(window());
92 m_dirLister->setDelayedMimeTypes(true);
93
94 m_dolphinModel = new DolphinModel(this);
95 m_dolphinModel->setDirLister(m_dirLister);
96 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
97
98 m_proxyModel = new DolphinSortFilterProxyModel(this);
99 m_proxyModel->setSourceModel(m_dolphinModel);
100 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
101
102 connect(m_dirLister, SIGNAL(clear()),
103 this, SLOT(updateStatusBar()));
104 connect(m_dirLister, SIGNAL(percent(int)),
105 this, SLOT(updateProgress(int)));
106 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
107 this, SLOT(updateStatusBar()));
108 connect(m_dirLister, SIGNAL(completed()),
109 this, SLOT(slotDirListerCompleted()));
110 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
111 this, SLOT(showInfoMessage(const QString&)));
112 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
113 this, SLOT(showErrorMessage(const QString&)));
114
115 m_view = new DolphinView(this,
116 url,
117 m_dirLister,
118 m_dolphinModel,
119 m_proxyModel);
120 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
121 m_urlNavigator, SLOT(setUrl(const KUrl&)));
122 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&)),
123 this, SLOT(openContextMenu(KFileItem, const KUrl&)));
124 connect(m_view, SIGNAL(contentsMoved(int, int)),
125 this, SLOT(saveContentsPos(int, int)));
126 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
127 this, SLOT(showItemInfo(KFileItem)));
128 connect(m_view, SIGNAL(errorMessage(const QString&)),
129 this, SLOT(showErrorMessage(const QString&)));
130 connect(m_view, SIGNAL(infoMessage(const QString&)),
131 this, SLOT(showInfoMessage(const QString&)));
132 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
133 this, SLOT(showOperationCompletedMessage(const QString&)));
134 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
135 this, SLOT(slotItemTriggered(KFileItem)));
136 connect(m_view, SIGNAL(startedPathLoading(const KUrl&)),
137 this, SLOT(saveRootUrl(const KUrl&)));
138
139 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
140 this, SLOT(restoreView(const KUrl&)));
141
142 m_statusBar = new DolphinStatusBar(this, url);
143 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
144 m_statusBar, SLOT(updateSpaceInfoContent(const KUrl&)));
145
146 m_filterBar = new FilterBar(this);
147 m_filterBar->setVisible(settings->filterBar());
148 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
149 this, SLOT(setNameFilter(const QString&)));
150 connect(m_filterBar, SIGNAL(closeRequest()),
151 this, SLOT(closeFilterBar()));
152
153 m_topLayout->addWidget(m_urlNavigator);
154 m_topLayout->addWidget(m_view);
155 m_topLayout->addWidget(m_filterBar);
156 m_topLayout->addWidget(m_statusBar);
157 }
158
159 DolphinViewContainer::~DolphinViewContainer()
160 {
161 m_dirLister->disconnect();
162
163 delete m_proxyModel;
164 m_proxyModel = 0;
165 delete m_dolphinModel;
166 m_dolphinModel = 0;
167 m_dirLister = 0; // deleted by m_dolphinModel
168 }
169
170 void DolphinViewContainer::setUrl(const KUrl& url)
171 {
172 m_urlNavigator->setUrl(url);
173 }
174
175 const KUrl& DolphinViewContainer::url() const
176 {
177 return m_urlNavigator->url();
178 }
179
180 void DolphinViewContainer::setActive(bool active)
181 {
182 m_urlNavigator->setActive(active);
183 m_view->setActive(active);
184 }
185
186 bool DolphinViewContainer::isActive() const
187 {
188 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
189 return m_view->isActive();
190 }
191
192 bool DolphinViewContainer::isFilterBarVisible() const
193 {
194 return m_filterBar->isVisible();
195 }
196
197 bool DolphinViewContainer::isUrlEditable() const
198 {
199 return m_urlNavigator->isUrlEditable();
200 }
201
202 KFileItem DolphinViewContainer::fileItem(const QModelIndex& index) const
203 {
204 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
205 return m_dolphinModel->itemForIndex(dolphinModelIndex);
206 }
207
208 void DolphinViewContainer::updateProgress(int percent)
209 {
210 if (!m_showProgress) {
211 // Only show the directory loading progress if the status bar does
212 // not contain another progress information. This means that
213 // the directory loading progress information has the lowest priority.
214 const QString progressText(m_statusBar->progressText());
215 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
216 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
217 if (m_showProgress) {
218 m_statusBar->setProgressText(loadingText);
219 m_statusBar->setProgress(0);
220 }
221 }
222
223 if (m_showProgress) {
224 m_statusBar->setProgress(percent);
225 }
226 }
227
228 void DolphinViewContainer::slotDirListerCompleted()
229 {
230 if (m_showProgress) {
231 m_statusBar->setProgressText(QString());
232 m_statusBar->setProgress(100);
233 m_showProgress = false;
234 }
235
236 updateStatusBar();
237
238 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
239 }
240
241 void DolphinViewContainer::showItemInfo(const KFileItem& item)
242 {
243 if (item.isNull()) {
244 m_statusBar->clear();
245 } else {
246 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
247 }
248 }
249
250 void DolphinViewContainer::showInfoMessage(const QString& msg)
251 {
252 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
253 }
254
255 void DolphinViewContainer::showErrorMessage(const QString& msg)
256 {
257 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
258 }
259
260 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
261 {
262 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
263 }
264
265 void DolphinViewContainer::closeFilterBar()
266 {
267 m_filterBar->hide();
268 emit showFilterBarChanged(false);
269 }
270
271 QString DolphinViewContainer::defaultStatusBarText() const
272 {
273 int folderCount = 0;
274 int fileCount = 0;
275 m_view->calculateItemCount(fileCount, folderCount);
276 return KIO::itemsSummaryString(fileCount + folderCount,
277 fileCount,
278 folderCount,
279 0, false);
280 }
281
282 QString DolphinViewContainer::selectionStatusBarText() const
283 {
284 QString text;
285 const KFileItemList list = m_view->selectedItems();
286 if (list.isEmpty()) {
287 // when an item is triggered, it is temporary selected but selectedItems()
288 // will return an empty list
289 return QString();
290 }
291
292 int fileCount = 0;
293 int folderCount = 0;
294 KIO::filesize_t byteSize = 0;
295 KFileItemList::const_iterator it = list.begin();
296 const KFileItemList::const_iterator end = list.end();
297 while (it != end) {
298 const KFileItem& item = *it;
299 if (item.isDir()) {
300 ++folderCount;
301 } else {
302 ++fileCount;
303 byteSize += item.size();
304 }
305 ++it;
306 }
307
308 if (folderCount > 0) {
309 text = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
310 if (fileCount > 0) {
311 text += ", ";
312 }
313 }
314
315 if (fileCount > 0) {
316 const QString sizeText(KIO::convertSize(byteSize));
317 text += i18ncp("@info:status", "1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
318 }
319
320 return text;
321 }
322
323 void DolphinViewContainer::showFilterBar(bool show)
324 {
325 Q_ASSERT(m_filterBar != 0);
326 m_filterBar->setVisible(show);
327 }
328
329 void DolphinViewContainer::updateStatusBar()
330 {
331 // As the item count information is less important
332 // in comparison with other messages, it should only
333 // be shown if:
334 // - the status bar is empty or
335 // - shows already the item count information or
336 // - shows only a not very important information
337 // - if any progress is given don't show the item count info at all
338 const QString msg(m_statusBar->message());
339 const bool updateStatusBarMsg = (msg.isEmpty() ||
340 (msg == m_statusBar->defaultText()) ||
341 (m_statusBar->type() == DolphinStatusBar::Information)) &&
342 (m_statusBar->progress() == 100);
343
344 const QString text(m_view->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
345 m_statusBar->setDefaultText(text);
346
347 if (updateStatusBarMsg) {
348 m_statusBar->setMessage(text, DolphinStatusBar::Default);
349 }
350 }
351
352 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
353 {
354 m_view->setNameFilter(nameFilter);
355 updateStatusBar();
356 }
357
358 void DolphinViewContainer::openContextMenu(const KFileItem& item,
359 const KUrl& url)
360 {
361 DolphinContextMenu contextMenu(m_mainWindow, item, url);
362 contextMenu.open();
363 }
364
365 void DolphinViewContainer::saveContentsPos(int x, int y)
366 {
367 m_urlNavigator->savePosition(x, y);
368 }
369
370 void DolphinViewContainer::restoreContentsPos()
371 {
372 if (!url().isEmpty()) {
373 const QPoint pos = m_urlNavigator->savedPosition();
374 m_view->setContentsPosition(pos.x(), pos.y());
375 }
376 }
377
378 void DolphinViewContainer::activate()
379 {
380 setActive(true);
381 }
382
383 void DolphinViewContainer::restoreView(const KUrl& url)
384 {
385 m_view->updateView(url, m_urlNavigator->savedRootUrl());
386 }
387
388 void DolphinViewContainer::saveRootUrl(const KUrl& url)
389 {
390 Q_UNUSED(url);
391 m_urlNavigator->saveRootUrl(m_view->rootUrl());
392 }
393
394 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
395 {
396 KUrl url = item.targetUrl();
397
398 if (item.isDir()) {
399 m_view->setUrl(url);
400 return;
401 }
402
403 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
404 const bool browseThroughArchives = settings->browseThroughArchives();
405 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
406 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
407 // zip:/<path>/ when clicking on a zip file, etc.
408 // The .protocol file specifies the mimetype that the kioslave handles.
409 // Note that we don't use mimetype inheritance since we don't want to
410 // open OpenDocument files as zip folders...
411 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
412 if (!protocol.isEmpty()) {
413 url.setProtocol(protocol);
414 m_view->setUrl(url);
415 return;
416 }
417 }
418
419 item.run();
420 }
421
422 #include "dolphinviewcontainer.moc"