]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
* Adjust code to use the improved KUrlNavigator API.
[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 <kfileitemlistproperties.h>
43 #include <konq_operations.h>
44 #include <kshell.h>
45 #include <kurl.h>
46 #include <kurlcombobox.h>
47 #include <kurlnavigator.h>
48 #include <krun.h>
49
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.h"
53 #include "dolphinmainwindow.h"
54 #include "dolphindirlister.h"
55 #include "dolphinsortfilterproxymodel.h"
56 #include "dolphindetailsview.h"
57 #include "dolphiniconsview.h"
58 #include "draganddrophelper.h"
59 #include "filterbar.h"
60 #include "statusbar/dolphinstatusbar.h"
61 #include "viewproperties.h"
62 #include "settings/dolphinsettings.h"
63 #include "dolphin_generalsettings.h"
64
65 DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) :
66 QWidget(parent),
67 m_isFolderWritable(false),
68 m_topLayout(0),
69 m_urlNavigator(0),
70 m_view(0),
71 m_filterBar(0),
72 m_statusBar(0),
73 m_statusBarTimer(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 //connect(m_urlNavigator, SIGNAL(tabRequested(const KUrl&)),
89 // this,
90 connect(m_urlNavigator->editor(), SIGNAL(completionModeChanged(KGlobalSettings::Completion)),
91 this, SLOT(saveUrlCompletionMode(KGlobalSettings::Completion)));
92
93 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
94 m_urlNavigator->setUrlEditable(settings->editableUrl());
95 m_urlNavigator->setShowFullPath(settings->showFullPath());
96 m_urlNavigator->setHomeUrl(KUrl(settings->homeUrl()));
97 KUrlComboBox* editor = m_urlNavigator->editor();
98 editor->setCompletionMode(KGlobalSettings::Completion(settings->urlCompletionMode()));
99
100 m_dirLister = new DolphinDirLister();
101 m_dirLister->setAutoUpdate(true);
102 m_dirLister->setMainWindow(window());
103 m_dirLister->setDelayedMimeTypes(true);
104
105 m_dolphinModel = new DolphinModel(this);
106 m_dolphinModel->setDirLister(m_dirLister);
107 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
108
109 m_proxyModel = new DolphinSortFilterProxyModel(this);
110 m_proxyModel->setSourceModel(m_dolphinModel);
111 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
112
113 connect(m_dirLister, SIGNAL(started(KUrl)),
114 this, SLOT(initializeProgress()));
115 connect(m_dirLister, SIGNAL(clear()),
116 this, SLOT(delayedStatusBarUpdate()));
117 connect(m_dirLister, SIGNAL(percent(int)),
118 this, SLOT(updateProgress(int)));
119 connect(m_dirLister, SIGNAL(itemsDeleted(const KFileItemList&)),
120 this, SLOT(delayedStatusBarUpdate()));
121 connect(m_dirLister, SIGNAL(completed()),
122 this, SLOT(slotDirListerCompleted()));
123 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
124 this, SLOT(showInfoMessage(const QString&)));
125 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
126 this, SLOT(showErrorMessage(const QString&)));
127 connect(m_dirLister, SIGNAL(urlIsFileError(const KUrl&)),
128 this, SLOT(openFile(const KUrl&)));
129
130 m_view = new DolphinView(this, url, m_proxyModel);
131 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
132 m_urlNavigator, SLOT(setUrl(const KUrl&)));
133 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
134 this, SLOT(showItemInfo(KFileItem)));
135 connect(m_view, SIGNAL(errorMessage(const QString&)),
136 this, SLOT(showErrorMessage(const QString&)));
137 connect(m_view, SIGNAL(infoMessage(const QString&)),
138 this, SLOT(showInfoMessage(const QString&)));
139 connect(m_view, SIGNAL(operationCompletedMessage(const QString&)),
140 this, SLOT(showOperationCompletedMessage(const QString&)));
141 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
142 this, SLOT(slotItemTriggered(KFileItem)));
143 connect(m_view, SIGNAL(redirection(KUrl, KUrl)),
144 this, SLOT(redirect(KUrl, KUrl)));
145 connect(m_view, SIGNAL(selectionChanged(const KFileItemList&)),
146 this, SLOT(delayedStatusBarUpdate()));
147
148 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
149 this, SLOT(slotUrlNavigatorLocationChanged(const KUrl&)));
150 connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(const KUrl&)),
151 this, SLOT(saveViewState()));
152 connect(m_urlNavigator, SIGNAL(historyChanged()),
153 this, SLOT(slotHistoryChanged()));
154
155 // initialize status bar
156 m_statusBar = new DolphinStatusBar(this, m_view);
157 m_statusBarTimer = new QTimer(this);
158 m_statusBarTimer->setSingleShot(true);
159 m_statusBarTimer->setInterval(300);
160 connect(m_statusBarTimer, SIGNAL(timeout()),
161 this, SLOT(updateStatusBar()));
162
163 KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
164 connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)),
165 this, SLOT(delayedStatusBarUpdate()));
166
167 // initialize filter bar
168 m_filterBar = new FilterBar(this);
169 m_filterBar->setVisible(settings->filterBar());
170 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
171 this, SLOT(setNameFilter(const QString&)));
172 connect(m_filterBar, SIGNAL(closeRequest()),
173 this, SLOT(closeFilterBar()));
174 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
175 m_filterBar, SLOT(clear()));
176
177 m_topLayout->addWidget(m_urlNavigator);
178 m_topLayout->addWidget(m_view);
179 m_topLayout->addWidget(m_filterBar);
180 m_topLayout->addWidget(m_statusBar);
181 }
182
183 DolphinViewContainer::~DolphinViewContainer()
184 {
185 m_dirLister->disconnect();
186
187 delete m_proxyModel;
188 m_proxyModel = 0;
189 delete m_dolphinModel;
190 m_dolphinModel = 0;
191 m_dirLister = 0; // deleted by m_dolphinModel
192 }
193
194 KUrl DolphinViewContainer::url() const
195 {
196 return m_urlNavigator->locationUrl();
197 }
198
199 void DolphinViewContainer::setActive(bool active)
200 {
201 m_urlNavigator->setActive(active);
202 m_view->setActive(active);
203 if (active) {
204 emit writeStateChanged(m_isFolderWritable);
205 }
206 }
207
208 bool DolphinViewContainer::isActive() const
209 {
210 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
211 return m_view->isActive();
212 }
213
214 void DolphinViewContainer::refresh()
215 {
216 m_view->refresh();
217 m_statusBar->refresh();
218 }
219
220 bool DolphinViewContainer::isFilterBarVisible() const
221 {
222 return m_filterBar->isVisible();
223 }
224
225 void DolphinViewContainer::setUrl(const KUrl& newUrl)
226 {
227 if (newUrl != m_urlNavigator->locationUrl()) {
228 m_urlNavigator->setLocationUrl(newUrl);
229 // Temporary disable the 'File'->'Create New...' menu until
230 // the write permissions can be checked in a fast way at
231 // DolphinViewContainer::slotDirListerCompleted().
232 m_isFolderWritable = false;
233 if (isActive()) {
234 emit writeStateChanged(false);
235 }
236 }
237 }
238
239 void DolphinViewContainer::showFilterBar(bool show)
240 {
241 Q_ASSERT(m_filterBar != 0);
242 if (show) {
243 m_filterBar->show();
244 } else {
245 closeFilterBar();
246 }
247 }
248
249 void DolphinViewContainer::delayedStatusBarUpdate()
250 {
251 // Invoke updateStatusBar() with a small delay. This assures that
252 // when a lot of delayedStatusBarUpdates() are done in a short time,
253 // no bottleneck is given.
254 m_statusBarTimer->start();
255 }
256
257 void DolphinViewContainer::updateStatusBar()
258 {
259 // As the item count information is less important
260 // in comparison with other messages, it should only
261 // be shown if:
262 // - the status bar is empty or
263 // - shows already the item count information or
264 // - shows only a not very important information
265 // - if any progress is given don't show the item count info at all
266 const QString msg = m_statusBar->message();
267 const bool updateStatusBarMsg = (msg.isEmpty()
268 || (msg == m_statusBar->defaultText())
269 || (m_statusBar->type() == DolphinStatusBar::Information))
270 && (m_statusBar->progress() == 100);
271
272 const QString text = m_view->statusBarText();
273 m_statusBar->setDefaultText(text);
274
275 if (updateStatusBarMsg) {
276 m_statusBar->setMessage(text, DolphinStatusBar::Default);
277 }
278 }
279
280 void DolphinViewContainer::initializeProgress()
281 {
282 if (url().protocol() == "nepomuksearch") {
283 // The Nepomuk IO-slave does not provide any progress information. Give
284 // an immediate hint to the user that a searching is done:
285 m_statusBar->setProgressText(i18nc("@info", "Searching..."));
286 m_statusBar->setProgress(-1);
287 }
288 }
289
290 void DolphinViewContainer::updateProgress(int percent)
291 {
292 if (m_statusBar->progressText().isEmpty()) {
293 m_statusBar->setProgressText(i18nc("@info:progress", "Loading folder..."));
294 }
295 m_statusBar->setProgress(percent);
296 }
297
298 void DolphinViewContainer::slotDirListerCompleted()
299 {
300 if (!m_statusBar->progressText().isEmpty()) {
301 m_statusBar->setProgressText(QString());
302 m_statusBar->setProgress(100);
303 }
304
305 if ((url().protocol() == "nepomuksearch") && (m_dirLister->items().count() == 0)) {
306 // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead
307 // of showing the default status bar information ("0 items") a more helpful information is given:
308 m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information);
309 } else {
310 updateStatusBar();
311 }
312 QMetaObject::invokeMethod(this, "restoreViewState", Qt::QueuedConnection);
313
314 // Enable the 'File'->'Create New...' menu only if the directory
315 // supports writing.
316 KFileItem item = m_dirLister->rootItem();
317 if (item.isNull()) {
318 // it is unclear whether writing is supported
319 m_isFolderWritable = true;
320 } else {
321 KFileItemListProperties capabilities(KFileItemList() << item);
322 m_isFolderWritable = capabilities.supportsWriting();
323 }
324
325 if (isActive()) {
326 emit writeStateChanged(m_isFolderWritable);
327 }
328 }
329
330 void DolphinViewContainer::showItemInfo(const KFileItem& item)
331 {
332 if (item.isNull()) {
333 // Only clear the status bar if unimportant messages are shown.
334 // This prevents that information- or error-messages get hidden
335 // by moving the mouse above the viewport or when closing the
336 // context menu.
337 if (m_statusBar->type() == DolphinStatusBar::Default) {
338 m_statusBar->clear();
339 }
340 } else {
341 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
342 }
343 }
344
345 void DolphinViewContainer::showInfoMessage(const QString& msg)
346 {
347 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
348 }
349
350 void DolphinViewContainer::showErrorMessage(const QString& msg)
351 {
352 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
353 }
354
355 void DolphinViewContainer::showOperationCompletedMessage(const QString& msg)
356 {
357 m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted);
358 }
359
360 void DolphinViewContainer::closeFilterBar()
361 {
362 m_filterBar->hide();
363 m_filterBar->clear();
364 m_view->setFocus();
365 emit showFilterBarChanged(false);
366 }
367
368 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
369 {
370 m_view->setNameFilter(nameFilter);
371 delayedStatusBarUpdate();
372 }
373
374 void DolphinViewContainer::restoreViewState()
375 {
376 QByteArray locationState = m_urlNavigator->locationState();
377 QDataStream stream(&locationState, QIODevice::ReadOnly);
378 m_view->restoreState(stream);
379 }
380
381 void DolphinViewContainer::activate()
382 {
383 setActive(true);
384 }
385
386 void DolphinViewContainer::saveViewState()
387 {
388 QByteArray locationState;
389 QDataStream stream(&locationState, QIODevice::WriteOnly);
390 m_view->saveState(stream);
391 m_urlNavigator->saveLocationState(locationState);
392 }
393
394 void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url)
395 {
396 if (KProtocolManager::supportsListing(url)) {
397 m_view->setUrl(url);
398 if (isActive()) {
399 // When an URL has been entered, the view should get the focus.
400 // The focus must be requested asynchronously, as changing the URL might create
401 // a new view widget. Using QTimer::singleShow() works reliable, however
402 // QMetaObject::invokeMethod() with a queued connection does not work, which might
403 // indicate that we should pass a hint to DolphinView::updateView()
404 // regarding the focus instead. To test: Enter an URL and press CTRL+Enter.
405 // Expected result: The view should get the focus.
406 QTimer::singleShot(0, this, SLOT(requestFocus()));
407 }
408 } else if (KProtocolManager::isSourceProtocol(url)) {
409 QString app = "konqueror";
410 if (url.protocol().startsWith(QLatin1String("http"))) {
411 showErrorMessage(i18nc("@info:status",
412 "Dolphin does not support web pages, the web browser has been launched"));
413 const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General");
414 const QString browser = config.readEntry("BrowserApplication");
415 if (!browser.isEmpty()) {
416 app = browser;
417 if (app.startsWith('!')) {
418 // a literal command has been configured, remove the '!' prefix
419 app = app.mid(1);
420 }
421 }
422 } else {
423 showErrorMessage(i18nc("@info:status",
424 "Protocol not supported by Dolphin, Konqueror has been launched"));
425 }
426
427 const QString secureUrl = KShell::quoteArg(url.pathOrUrl());
428 const QString command = app + ' ' + secureUrl;
429 KRun::runCommand(command, app, app, this);
430 } else {
431 showErrorMessage(i18nc("@info:status", "Invalid protocol"));
432 }
433 }
434
435 void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event)
436 {
437 DragAndDropHelper::instance().dropUrls(KFileItem(), destination, event, this);
438 }
439
440 void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl)
441 {
442 Q_UNUSED(oldUrl);
443 const bool block = m_urlNavigator->signalsBlocked();
444 m_urlNavigator->blockSignals(true);
445 m_urlNavigator->setLocationUrl(newUrl);
446 m_urlNavigator->blockSignals(block);
447 }
448
449 void DolphinViewContainer::requestFocus()
450 {
451 m_view->setFocus();
452 }
453
454 void DolphinViewContainer::saveUrlCompletionMode(KGlobalSettings::Completion completion)
455 {
456 DolphinSettings& settings = DolphinSettings::instance();
457 settings.generalSettings()->setUrlCompletionMode(completion);
458 settings.save();
459 }
460
461 void DolphinViewContainer::slotHistoryChanged()
462 {
463 const int index = m_urlNavigator->historyIndex();
464 if (index > 0) {
465 // The "Go Forward" action is enabled. Try to mark
466 // the previous directory as active item:
467 const KUrl url = m_urlNavigator->locationUrl(index - 1);
468 m_view->activateItem(url);
469 }
470 }
471
472 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
473 {
474 KUrl url = item.targetUrl();
475
476 if (item.isDir()) {
477 m_view->setUrl(url);
478 return;
479 }
480
481 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
482 const bool browseThroughArchives = settings->browseThroughArchives();
483 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
484 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
485 // zip:/<path>/ when clicking on a zip file, etc.
486 // The .protocol file specifies the mimetype that the kioslave handles.
487 // Note that we don't use mimetype inheritance since we don't want to
488 // open OpenDocument files as zip folders...
489 const QString protocol = KProtocolManager::protocolForArchiveMimetype(item.mimetype());
490 if (!protocol.isEmpty()) {
491 url.setProtocol(protocol);
492 m_view->setUrl(url);
493 return;
494 }
495 }
496
497 item.run();
498 }
499
500 void DolphinViewContainer::openFile(const KUrl& url)
501 {
502 // Using m_dolphinModel for getting the file item instance is not possible
503 // here: openFile() is triggered by an error of the directory lister
504 // job, so the file item must be received "manually".
505 const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
506 slotItemTriggered(item);
507 }
508
509 #include "dolphinviewcontainer.moc"