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