]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
allow to configure whether the zoom slider and/or the space information should be...
[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 <kmenu.h>
39 #include <kmimetyperesolver.h>
40 #include <knewmenu.h>
41 #include <konqmimedata.h>
42 #include <konq_fileitemcapabilities.h>
43 #include <konq_operations.h>
44 #include <kurl.h>
45
46 #include "dolphinmodel.h"
47 #include "dolphincolumnview.h"
48 #include "dolphincontroller.h"
49 #include "dolphindropcontroller.h"
50 #include "dolphinstatusbar.h"
51 #include "dolphinmainwindow.h"
52 #include "dolphindirlister.h"
53 #include "dolphinsortfilterproxymodel.h"
54 #include "dolphindetailsview.h"
55 #include "dolphiniconsview.h"
56 #include "dolphincontextmenu.h"
57 #include "filterbar.h"
58 #include "kurlnavigator.h"
59 #include "viewproperties.h"
60 #include "dolphinsettings.h"
61 #include "dolphin_generalsettings.h"
62
63 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow,
64 QWidget* parent,
65 const KUrl& url) :
66 QWidget(parent),
67 m_showProgress(false),
68 m_mainWindow(mainWindow),
69 m_topLayout(0),
70 m_urlNavigator(0),
71 m_view(0),
72 m_filterBar(0),
73 m_statusBar(0),
74 m_dirLister(0),
75 m_proxyModel(0)
76 {
77 hide();
78
79 m_topLayout = new QVBoxLayout(this);
80 m_topLayout->setSpacing(0);
81 m_topLayout->setMargin(0);
82
83 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
84 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl&, QDropEvent*)),
85 this, SLOT(dropUrls(const KUrl&, QDropEvent*)));
86 connect(m_urlNavigator, SIGNAL(activated()),
87 this, SLOT(activate()));
88
89 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
90 m_urlNavigator->setUrlEditable(settings->editableUrl());
91 m_urlNavigator->setShowFullPath(settings->showFullPath());
92 m_urlNavigator->setHomeUrl(settings->homeUrl());
93
94 m_dirLister = new DolphinDirLister();
95 m_dirLister->setAutoUpdate(true);
96 m_dirLister->setMainWindow(window());
97 m_dirLister->setDelayedMimeTypes(true);
98
99 m_dolphinModel = new DolphinModel(this);
100 m_dolphinModel->setDirLister(m_dirLister);
101 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
102
103 m_proxyModel = new DolphinSortFilterProxyModel(this);
104 m_proxyModel->setSourceModel(m_dolphinModel);
105 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
106
107 connect(m_dirLister, SIGNAL(clear()),
108 this, SLOT(updateStatusBar()));
109 connect(m_dirLister, SIGNAL(percent(int)),
110 this, SLOT(updateProgress(int)));
111 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
112 this, SLOT(updateStatusBar()));
113 connect(m_dirLister, SIGNAL(completed()),
114 this, SLOT(slotDirListerCompleted()));
115 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
116 this, SLOT(showInfoMessage(const QString&)));
117 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
118 this, SLOT(showErrorMessage(const QString&)));
119
120 m_view = new DolphinView(this,
121 url,
122 m_dirLister,
123 m_dolphinModel,
124 m_proxyModel);
125 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
126 m_urlNavigator, SLOT(setUrl(const KUrl&)));
127 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&)),
128 this, SLOT(openContextMenu(KFileItem, const KUrl&)));
129 connect(m_view, SIGNAL(contentsMoved(int, int)),
130 this, SLOT(saveContentsPos(int, int)));
131 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
132 this, SLOT(showItemInfo(KFileItem)));
133 connect(m_view, SIGNAL(errorMessage(const QString&)),
134 this, SLOT(showErrorMessage(const QString&)));
135 connect(m_view, SIGNAL(infoMessage(const QString&)),
136 this, SLOT(showInfoMessage(const QString&)));
137 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
138 this, SLOT(showOperationCompletedMessage(const QString&)));
139 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
140 this, SLOT(slotItemTriggered(KFileItem)));
141 connect(m_view, SIGNAL(startedPathLoading(const KUrl&)),
142 this, SLOT(saveRootUrl(const KUrl&)));
143
144 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
145 this, SLOT(restoreView(const KUrl&)));
146
147 m_statusBar = new DolphinStatusBar(this, m_view);
148
149 m_filterBar = new FilterBar(this);
150 m_filterBar->setVisible(settings->filterBar());
151 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
152 this, SLOT(setNameFilter(const QString&)));
153 connect(m_filterBar, SIGNAL(closeRequest()),
154 this, SLOT(closeFilterBar()));
155
156 m_topLayout->addWidget(m_urlNavigator);
157 m_topLayout->addWidget(m_view);
158 m_topLayout->addWidget(m_filterBar);
159 m_topLayout->addWidget(m_statusBar);
160 }
161
162 DolphinViewContainer::~DolphinViewContainer()
163 {
164 m_dirLister->disconnect();
165
166 delete m_proxyModel;
167 m_proxyModel = 0;
168 delete m_dolphinModel;
169 m_dolphinModel = 0;
170 m_dirLister = 0; // deleted by m_dolphinModel
171 }
172
173 void DolphinViewContainer::setUrl(const KUrl& newUrl)
174 {
175 m_urlNavigator->setUrl(newUrl);
176
177 // Temporary disable the 'File'->'Create New...' menu until
178 // the write permissions can be checked in a fast way at
179 // DolphinViewContainer::slotDirListerCompleted().
180 m_mainWindow->newMenu()->menu()->setEnabled(false);
181 }
182
183 const KUrl& DolphinViewContainer::url() const
184 {
185 return m_urlNavigator->url();
186 }
187
188 void DolphinViewContainer::setActive(bool active)
189 {
190 m_urlNavigator->setActive(active);
191 m_view->setActive(active);
192 }
193
194 bool DolphinViewContainer::isActive() const
195 {
196 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
197 return m_view->isActive();
198 }
199
200 void DolphinViewContainer::refresh()
201 {
202 m_view->refresh();
203 m_statusBar->refresh();
204 }
205
206 bool DolphinViewContainer::isFilterBarVisible() const
207 {
208 return m_filterBar->isVisible();
209 }
210
211 bool DolphinViewContainer::isUrlEditable() const
212 {
213 return m_urlNavigator->isUrlEditable();
214 }
215
216 void DolphinViewContainer::updateProgress(int percent)
217 {
218 if (!m_showProgress) {
219 // Only show the directory loading progress if the status bar does
220 // not contain another progress information. This means that
221 // the directory loading progress information has the lowest priority.
222 const QString progressText(m_statusBar->progressText());
223 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
224 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
225 if (m_showProgress) {
226 m_statusBar->setProgressText(loadingText);
227 m_statusBar->setProgress(0);
228 }
229 }
230
231 if (m_showProgress) {
232 m_statusBar->setProgress(percent);
233 }
234 }
235
236 void DolphinViewContainer::slotDirListerCompleted()
237 {
238 if (m_showProgress) {
239 m_statusBar->setProgressText(QString());
240 m_statusBar->setProgress(100);
241 m_showProgress = false;
242 }
243
244 updateStatusBar();
245 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
246
247 // Enable the 'File'->'Create New...' menu only if the directory
248 // supports writing.
249 KMenu* createNew = m_mainWindow->newMenu()->menu();
250 KFileItem item = m_dirLister->rootItem();
251 if (item.isNull()) {
252 // it is unclear whether writing is supported
253 createNew->setEnabled(true);
254 } else {
255 KonqFileItemCapabilities capabilities(KFileItemList() << item);
256 createNew->setEnabled(capabilities.supportsWriting());
257 }
258 }
259
260 void DolphinViewContainer::showItemInfo(const KFileItem& item)
261 {
262 if (item.isNull()) {
263 m_statusBar->clear();
264 } else {
265 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
266 }
267 }
268
269 void DolphinViewContainer::showInfoMessage(const QString& msg)
270 {
271 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
272 }
273
274 void DolphinViewContainer::showErrorMessage(const QString& msg)
275 {
276 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
277 }
278
279 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
280 {
281 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
282 }
283
284 void DolphinViewContainer::closeFilterBar()
285 {
286 m_filterBar->hide();
287 m_filterBar->clear();
288 emit showFilterBarChanged(false);
289 }
290
291 void DolphinViewContainer::showFilterBar(bool show)
292 {
293 Q_ASSERT(m_filterBar != 0);
294 m_filterBar->setVisible(show);
295 }
296
297 void DolphinViewContainer::updateStatusBar()
298 {
299 // As the item count information is less important
300 // in comparison with other messages, it should only
301 // be shown if:
302 // - the status bar is empty or
303 // - shows already the item count information or
304 // - shows only a not very important information
305 // - if any progress is given don't show the item count info at all
306 const QString msg(m_statusBar->message());
307 const bool updateStatusBarMsg = (msg.isEmpty() ||
308 (msg == m_statusBar->defaultText()) ||
309 (m_statusBar->type() == DolphinStatusBar::Information)) &&
310 (m_statusBar->progress() == 100);
311
312 const QString text(m_view->statusBarText());
313 m_statusBar->setDefaultText(text);
314
315 if (updateStatusBarMsg) {
316 m_statusBar->setMessage(text, DolphinStatusBar::Default);
317 }
318 }
319
320 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
321 {
322 m_view->setNameFilter(nameFilter);
323 updateStatusBar();
324 }
325
326 void DolphinViewContainer::openContextMenu(const KFileItem& item,
327 const KUrl& url)
328 {
329 DolphinContextMenu contextMenu(m_mainWindow, item, url);
330 contextMenu.open();
331 }
332
333 void DolphinViewContainer::saveContentsPos(int x, int y)
334 {
335 m_urlNavigator->savePosition(x, y);
336 }
337
338 void DolphinViewContainer::restoreContentsPos()
339 {
340 if (!url().isEmpty()) {
341 const QPoint pos = m_urlNavigator->savedPosition();
342 m_view->setContentsPosition(pos.x(), pos.y());
343 }
344 }
345
346 void DolphinViewContainer::activate()
347 {
348 setActive(true);
349 }
350
351 void DolphinViewContainer::restoreView(const KUrl& url)
352 {
353 m_view->updateView(url, m_urlNavigator->savedRootUrl());
354 }
355
356 void DolphinViewContainer::saveRootUrl(const KUrl& url)
357 {
358 Q_UNUSED(url);
359 m_urlNavigator->saveRootUrl(m_view->rootUrl());
360 }
361
362 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
363 {
364 DolphinDropController::dropUrls(KFileItem(), destination, event, this);
365 }
366
367 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
368 {
369 KUrl url = item.targetUrl();
370
371 if (item.isDir()) {
372 m_view->setUrl(url);
373 return;
374 }
375
376 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
377 const bool browseThroughArchives = settings->browseThroughArchives();
378 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
379 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
380 // zip:/<path>/ when clicking on a zip file, etc.
381 // The .protocol file specifies the mimetype that the kioslave handles.
382 // Note that we don't use mimetype inheritance since we don't want to
383 // open OpenDocument files as zip folders...
384 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
385 if (!protocol.isEmpty()) {
386 url.setProtocol(protocol);
387 m_view->setUrl(url);
388 return;
389 }
390 }
391
392 item.run();
393 }
394
395 #include "dolphinviewcontainer.moc"