]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Provide a zoom slider in the status bar. It is configurable whether the zoom slider...
[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 bool DolphinViewContainer::isFilterBarVisible() const
201 {
202 return m_filterBar->isVisible();
203 }
204
205 bool DolphinViewContainer::isUrlEditable() const
206 {
207 return m_urlNavigator->isUrlEditable();
208 }
209
210 void DolphinViewContainer::updateProgress(int percent)
211 {
212 if (!m_showProgress) {
213 // Only show the directory loading progress if the status bar does
214 // not contain another progress information. This means that
215 // the directory loading progress information has the lowest priority.
216 const QString progressText(m_statusBar->progressText());
217 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
218 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
219 if (m_showProgress) {
220 m_statusBar->setProgressText(loadingText);
221 m_statusBar->setProgress(0);
222 }
223 }
224
225 if (m_showProgress) {
226 m_statusBar->setProgress(percent);
227 }
228 }
229
230 void DolphinViewContainer::slotDirListerCompleted()
231 {
232 if (m_showProgress) {
233 m_statusBar->setProgressText(QString());
234 m_statusBar->setProgress(100);
235 m_showProgress = false;
236 }
237
238 updateStatusBar();
239 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
240
241 // Enable the 'File'->'Create New...' menu only if the directory
242 // supports writing.
243 KMenu* createNew = m_mainWindow->newMenu()->menu();
244 KFileItem item = m_dirLister->rootItem();
245 if (item.isNull()) {
246 // it is unclear whether writing is supported
247 createNew->setEnabled(true);
248 } else {
249 KonqFileItemCapabilities capabilities(KFileItemList() << item);
250 createNew->setEnabled(capabilities.supportsWriting());
251 }
252 }
253
254 void DolphinViewContainer::showItemInfo(const KFileItem& item)
255 {
256 if (item.isNull()) {
257 m_statusBar->clear();
258 } else {
259 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
260 }
261 }
262
263 void DolphinViewContainer::showInfoMessage(const QString& msg)
264 {
265 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
266 }
267
268 void DolphinViewContainer::showErrorMessage(const QString& msg)
269 {
270 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
271 }
272
273 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
274 {
275 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
276 }
277
278 void DolphinViewContainer::closeFilterBar()
279 {
280 m_filterBar->hide();
281 m_filterBar->clear();
282 emit showFilterBarChanged(false);
283 }
284
285 void DolphinViewContainer::showFilterBar(bool show)
286 {
287 Q_ASSERT(m_filterBar != 0);
288 m_filterBar->setVisible(show);
289 }
290
291 void DolphinViewContainer::updateStatusBar()
292 {
293 // As the item count information is less important
294 // in comparison with other messages, it should only
295 // be shown if:
296 // - the status bar is empty or
297 // - shows already the item count information or
298 // - shows only a not very important information
299 // - if any progress is given don't show the item count info at all
300 const QString msg(m_statusBar->message());
301 const bool updateStatusBarMsg = (msg.isEmpty() ||
302 (msg == m_statusBar->defaultText()) ||
303 (m_statusBar->type() == DolphinStatusBar::Information)) &&
304 (m_statusBar->progress() == 100);
305
306 const QString text(m_view->statusBarText());
307 m_statusBar->setDefaultText(text);
308
309 if (updateStatusBarMsg) {
310 m_statusBar->setMessage(text, DolphinStatusBar::Default);
311 }
312 }
313
314 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
315 {
316 m_view->setNameFilter(nameFilter);
317 updateStatusBar();
318 }
319
320 void DolphinViewContainer::openContextMenu(const KFileItem& item,
321 const KUrl& url)
322 {
323 DolphinContextMenu contextMenu(m_mainWindow, item, url);
324 contextMenu.open();
325 }
326
327 void DolphinViewContainer::saveContentsPos(int x, int y)
328 {
329 m_urlNavigator->savePosition(x, y);
330 }
331
332 void DolphinViewContainer::restoreContentsPos()
333 {
334 if (!url().isEmpty()) {
335 const QPoint pos = m_urlNavigator->savedPosition();
336 m_view->setContentsPosition(pos.x(), pos.y());
337 }
338 }
339
340 void DolphinViewContainer::activate()
341 {
342 setActive(true);
343 }
344
345 void DolphinViewContainer::restoreView(const KUrl& url)
346 {
347 m_view->updateView(url, m_urlNavigator->savedRootUrl());
348 }
349
350 void DolphinViewContainer::saveRootUrl(const KUrl& url)
351 {
352 Q_UNUSED(url);
353 m_urlNavigator->saveRootUrl(m_view->rootUrl());
354 }
355
356 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
357 {
358 DolphinDropController::dropUrls(KFileItem(), destination, event, this);
359 }
360
361 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
362 {
363 KUrl url = item.targetUrl();
364
365 if (item.isDir()) {
366 m_view->setUrl(url);
367 return;
368 }
369
370 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
371 const bool browseThroughArchives = settings->browseThroughArchives();
372 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
373 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
374 // zip:/<path>/ when clicking on a zip file, etc.
375 // The .protocol file specifies the mimetype that the kioslave handles.
376 // Note that we don't use mimetype inheritance since we don't want to
377 // open OpenDocument files as zip folders...
378 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
379 if (!protocol.isEmpty()) {
380 url.setProtocol(protocol);
381 m_view->setUrl(url);
382 return;
383 }
384 }
385
386 item.run();
387 }
388
389 #include "dolphinviewcontainer.moc"