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