]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinviewcontainer.cpp
Simplify DolphinController: don't remember the show-preview state in the controller...
[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
22 #include <QtGui/QApplication>
23 #include <QtGui/QClipboard>
24 #include <QtGui/QKeyEvent>
25 #include <QtGui/QItemSelection>
26 #include <QtGui/QBoxLayout>
27 #include <QtCore/QTimer>
28 #include <QtGui/QScrollBar>
29
30 #include <kfileitemdelegate.h>
31 #include <kfileplacesmodel.h>
32 #include <kglobalsettings.h>
33 #include <klocale.h>
34 #include <kiconeffect.h>
35 #include <kio/netaccess.h>
36 #include <kio/renamedialog.h>
37 #include <kio/previewjob.h>
38 #include <kmimetyperesolver.h>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
41 #include <kurl.h>
42
43 #include "dolphinmodel.h"
44 #include "dolphincolumnview.h"
45 #include "dolphincontroller.h"
46 #include "dolphinstatusbar.h"
47 #include "dolphinmainwindow.h"
48 #include "dolphindirlister.h"
49 #include "dolphinsortfilterproxymodel.h"
50 #include "dolphindetailsview.h"
51 #include "dolphiniconsview.h"
52 #include "dolphincontextmenu.h"
53 #include "filterbar.h"
54 #include "renamedialog.h"
55 #include "kurlnavigator.h"
56 #include "viewproperties.h"
57 #include "dolphinsettings.h"
58 #include "dolphin_generalsettings.h"
59
60 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow* mainWindow,
61 QWidget* parent,
62 const KUrl& url) :
63 QWidget(parent),
64 m_showProgress(false),
65 m_mainWindow(mainWindow),
66 m_topLayout(0),
67 m_urlNavigator(0),
68 m_view(0),
69 m_filterBar(0),
70 m_statusBar(0),
71 m_dirLister(0),
72 m_proxyModel(0)
73 {
74 hide();
75 setFocusPolicy(Qt::StrongFocus);
76 m_topLayout = new QVBoxLayout(this);
77 m_topLayout->setSpacing(0);
78 m_topLayout->setMargin(0);
79
80 m_urlNavigator = new KUrlNavigator(DolphinSettings::instance().placesModel(), url, this);
81 connect(m_urlNavigator, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
82 m_mainWindow, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
83 connect(m_urlNavigator, SIGNAL(activated()),
84 this, SLOT(activate()));
85
86 const GeneralSettings* settings = DolphinSettings::instance().generalSettings();
87 m_urlNavigator->setUrlEditable(settings->editableUrl());
88 m_urlNavigator->setHomeUrl(settings->homeUrl());
89
90 m_dirLister = new DolphinDirLister();
91 m_dirLister->setAutoUpdate(true);
92 m_dirLister->setMainWindow(this);
93 m_dirLister->setDelayedMimeTypes(true);
94
95 m_dolphinModel = new DolphinModel(this);
96 m_dolphinModel->setDirLister(m_dirLister);
97 m_dolphinModel->setDropsAllowed(DolphinModel::DropOnDirectory);
98
99 m_proxyModel = new DolphinSortFilterProxyModel(this);
100 m_proxyModel->setSourceModel(m_dolphinModel);
101
102 connect(m_dirLister, SIGNAL(clear()),
103 this, SLOT(updateStatusBar()));
104 connect(m_dirLister, SIGNAL(percent(int)),
105 this, SLOT(updateProgress(int)));
106 connect(m_dirLister, SIGNAL(deleteItem(const KFileItem&)),
107 this, SLOT(updateStatusBar()));
108 connect(m_dirLister, SIGNAL(completed()),
109 this, SLOT(slotDirListerCompleted()));
110 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
111 this, SLOT(showInfoMessage(const QString&)));
112 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
113 this, SLOT(showErrorMessage(const QString&)));
114
115 m_view = new DolphinView(this,
116 url,
117 m_dirLister,
118 m_dolphinModel,
119 m_proxyModel);
120 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
121 m_urlNavigator, SLOT(setUrl(const KUrl&)));
122 connect(m_view, SIGNAL(requestContextMenu(KFileItem, const KUrl&)),
123 this, SLOT(openContextMenu(KFileItem, const KUrl&)));
124 connect(m_view, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&)),
125 m_mainWindow, SLOT(dropUrls(const KUrl::List&, const KUrl&)));
126 connect(m_view, SIGNAL(contentsMoved(int, int)),
127 this, SLOT(saveContentsPos(int, int)));
128 connect(m_view, SIGNAL(requestItemInfo(KFileItem)),
129 this, SLOT(showItemInfo(KFileItem)));
130 connect(m_view, SIGNAL(errorMessage(const QString&)),
131 this, SLOT(showErrorMessage(const QString&)));
132 connect(m_view, SIGNAL(infoMessage(const QString&)),
133 this, SLOT(showInfoMessage(const QString&)));
134 connect(m_view, SIGNAL(itemTriggered(KFileItem)),
135 this, SLOT(slotItemTriggered(KFileItem)));
136 connect(m_view, SIGNAL(startedPathLoading(const KUrl&)),
137 this, SLOT(saveRootUrl(const KUrl&)));
138
139 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
140 this, SLOT(restoreView(const KUrl&)));
141
142 m_statusBar = new DolphinStatusBar(this, url);
143 connect(m_view, SIGNAL(urlChanged(const KUrl&)),
144 m_statusBar, SLOT(updateSpaceInfoContent(const KUrl&)));
145
146 m_filterBar = new FilterBar(this);
147 m_filterBar->setVisible(settings->filterBar());
148 connect(m_filterBar, SIGNAL(filterChanged(const QString&)),
149 this, SLOT(setNameFilter(const QString&)));
150 connect(m_filterBar, SIGNAL(closeRequest()),
151 this, SLOT(closeFilterBar()));
152
153 m_topLayout->addWidget(m_urlNavigator);
154 m_topLayout->addWidget(m_view);
155 m_topLayout->addWidget(m_filterBar);
156 m_topLayout->addWidget(m_statusBar);
157 }
158
159 DolphinViewContainer::~DolphinViewContainer()
160 {
161 delete m_dirLister;
162 m_dirLister = 0;
163 }
164
165 void DolphinViewContainer::setUrl(const KUrl& url)
166 {
167 m_urlNavigator->setUrl(url);
168 }
169
170 const KUrl& DolphinViewContainer::url() const
171 {
172 return m_urlNavigator->url();
173 }
174
175 void DolphinViewContainer::setActive(bool active)
176 {
177 m_urlNavigator->setActive(active);
178 m_view->setActive(active);
179 }
180
181 bool DolphinViewContainer::isActive() const
182 {
183 Q_ASSERT(m_view->isActive() == m_urlNavigator->isActive());
184 return m_view->isActive();
185 }
186
187 void DolphinViewContainer::renameSelectedItems()
188 {
189 DolphinViewContainer* view = m_mainWindow->activeViewContainer();
190 const KFileItemList items = m_view->selectedItems();
191 if (items.count() > 1) {
192 // More than one item has been selected for renaming. Open
193 // a rename dialog and rename all items afterwards.
194 RenameDialog dialog(items);
195 if (dialog.exec() == QDialog::Rejected) {
196 return;
197 }
198
199 const QString& newName = dialog.newName();
200 if (newName.isEmpty()) {
201 view->statusBar()->setMessage(dialog.errorString(),
202 DolphinStatusBar::Error);
203 } else {
204 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
205 // as one operation instead of n rename operations like it is done now...
206 Q_ASSERT(newName.contains('#'));
207
208 // iterate through all selected items and rename them...
209 const int replaceIndex = newName.indexOf('#');
210 Q_ASSERT(replaceIndex >= 0);
211 int index = 1;
212
213 KFileItemList::const_iterator it = items.begin();
214 KFileItemList::const_iterator end = items.end();
215 while (it != end) {
216 const KUrl& oldUrl = (*it).url();
217 QString number;
218 number.setNum(index++);
219
220 QString name(newName);
221 name.replace(replaceIndex, 1, number);
222
223 if (oldUrl.fileName() != name) {
224 KUrl newUrl = oldUrl;
225 newUrl.setFileName(name);
226 m_mainWindow->rename(oldUrl, newUrl);
227 }
228 ++it;
229 }
230 }
231 } else {
232 // Only one item has been selected for renaming. Use the custom
233 // renaming mechanism from the views.
234 Q_ASSERT(items.count() == 1);
235
236 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
237 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
238 // is a benefit for the user at all -> let's wait for some input first...
239 RenameDialog dialog(items);
240 if (dialog.exec() == QDialog::Rejected) {
241 return;
242 }
243
244 const QString& newName = dialog.newName();
245 if (newName.isEmpty()) {
246 view->statusBar()->setMessage(dialog.errorString(),
247 DolphinStatusBar::Error);
248 } else {
249 const KUrl& oldUrl = items.first().url();
250 KUrl newUrl = oldUrl;
251 newUrl.setFileName(newName);
252 m_mainWindow->rename(oldUrl, newUrl);
253 }
254 }
255 }
256
257 bool DolphinViewContainer::isFilterBarVisible() const
258 {
259 return m_filterBar->isVisible();
260 }
261
262 bool DolphinViewContainer::isUrlEditable() const
263 {
264 return m_urlNavigator->isUrlEditable();
265 }
266
267 KFileItem DolphinViewContainer::fileItem(const QModelIndex& index) const
268 {
269 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
270 return m_dolphinModel->itemForIndex(dolphinModelIndex);
271 }
272
273 void DolphinViewContainer::updateProgress(int percent)
274 {
275 if (!m_showProgress) {
276 // Only show the directory loading progress if the status bar does
277 // not contain another progress information. This means that
278 // the directory loading progress information has the lowest priority.
279 const QString progressText(m_statusBar->progressText());
280 const QString loadingText(i18nc("@info:progress", "Loading folder..."));
281 m_showProgress = progressText.isEmpty() || (progressText == loadingText);
282 if (m_showProgress) {
283 m_statusBar->setProgressText(loadingText);
284 m_statusBar->setProgress(0);
285 }
286 }
287
288 if (m_showProgress) {
289 m_statusBar->setProgress(percent);
290 }
291 }
292
293 void DolphinViewContainer::slotDirListerCompleted()
294 {
295 if (m_showProgress) {
296 m_statusBar->setProgressText(QString());
297 m_statusBar->setProgress(100);
298 m_showProgress = false;
299 }
300
301 updateStatusBar();
302
303 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
304 }
305
306 void DolphinViewContainer::showItemInfo(const KFileItem& item)
307 {
308 if (item.isNull()) {
309 m_statusBar->clear();
310 } else {
311 m_statusBar->setMessage(item.getStatusBarInfo(), DolphinStatusBar::Default);
312 }
313 }
314
315 void DolphinViewContainer::showInfoMessage(const QString& msg)
316 {
317 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
318 }
319
320 void DolphinViewContainer::showErrorMessage(const QString& msg)
321 {
322 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
323 }
324
325 void DolphinViewContainer::closeFilterBar()
326 {
327 m_filterBar->hide();
328 emit showFilterBarChanged(false);
329 }
330
331 QString DolphinViewContainer::defaultStatusBarText() const
332 {
333 int folderCount = 0;
334 int fileCount = 0;
335 m_view->calculateItemCount(fileCount, folderCount);
336 return KIO::itemsSummaryString(fileCount + folderCount,
337 fileCount,
338 folderCount,
339 0, false);
340 }
341
342 QString DolphinViewContainer::selectionStatusBarText() const
343 {
344 QString text;
345 const KFileItemList list = m_view->selectedItems();
346 if (list.isEmpty()) {
347 // when an item is triggered, it is temporary selected but selectedItems()
348 // will return an empty list
349 return QString();
350 }
351
352 int fileCount = 0;
353 int folderCount = 0;
354 KIO::filesize_t byteSize = 0;
355 KFileItemList::const_iterator it = list.begin();
356 const KFileItemList::const_iterator end = list.end();
357 while (it != end) {
358 const KFileItem& item = *it;
359 if (item.isDir()) {
360 ++folderCount;
361 } else {
362 ++fileCount;
363 byteSize += item.size();
364 }
365 ++it;
366 }
367
368 if (folderCount > 0) {
369 text = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
370 if (fileCount > 0) {
371 text += ", ";
372 }
373 }
374
375 if (fileCount > 0) {
376 const QString sizeText(KIO::convertSize(byteSize));
377 text += i18ncp("@info:status", "1 File selected (%2)", "%1 Files selected (%2)", fileCount, sizeText);
378 }
379
380 return text;
381 }
382
383 void DolphinViewContainer::showFilterBar(bool show)
384 {
385 Q_ASSERT(m_filterBar != 0);
386 m_filterBar->setVisible(show);
387 }
388
389 void DolphinViewContainer::updateStatusBar()
390 {
391 // As the item count information is less important
392 // in comparison with other messages, it should only
393 // be shown if:
394 // - the status bar is empty or
395 // - shows already the item count information or
396 // - shows only a not very important information
397 // - if any progress is given don't show the item count info at all
398 const QString msg(m_statusBar->message());
399 const bool updateStatusBarMsg = (msg.isEmpty() ||
400 (msg == m_statusBar->defaultText()) ||
401 (m_statusBar->type() == DolphinStatusBar::Information)) &&
402 (m_statusBar->progress() == 100);
403
404 const QString text(m_view->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
405 m_statusBar->setDefaultText(text);
406
407 if (updateStatusBarMsg) {
408 m_statusBar->setMessage(text, DolphinStatusBar::Default);
409 }
410 }
411
412 void DolphinViewContainer::setNameFilter(const QString& nameFilter)
413 {
414 m_view->setNameFilter(nameFilter);
415 updateStatusBar();
416 }
417
418 void DolphinViewContainer::openContextMenu(const KFileItem& item,
419 const KUrl& url)
420 {
421 DolphinContextMenu contextMenu(m_mainWindow, item, url);
422 contextMenu.open();
423 }
424
425 void DolphinViewContainer::saveContentsPos(int x, int y)
426 {
427 m_urlNavigator->savePosition(x, y);
428 }
429
430 void DolphinViewContainer::restoreContentsPos()
431 {
432 if (!url().isEmpty()) {
433 const QPoint pos = m_urlNavigator->savedPosition();
434 m_view->setContentsPosition(pos.x(), pos.y());
435 }
436 }
437
438 void DolphinViewContainer::activate()
439 {
440 setActive(true);
441 }
442
443 void DolphinViewContainer::restoreView(const KUrl& url)
444 {
445 m_view->updateView(url, m_urlNavigator->savedRootUrl());
446 }
447
448 void DolphinViewContainer::saveRootUrl(const KUrl& url)
449 {
450 Q_UNUSED(url);
451 m_urlNavigator->saveRootUrl(m_view->rootUrl());
452 }
453
454 void DolphinViewContainer::slotItemTriggered(const KFileItem& item)
455 {
456 // Prefer the local path over the URL.
457 bool isLocal;
458 KUrl url = item.mostLocalUrl(isLocal);
459
460 if (item.isDir()) {
461 m_view->setUrl(url);
462 } else if (item.isFile()) {
463 // allow to browse through ZIP and tar files
464 KMimeType::Ptr mime = item.mimeTypePtr();
465 if (mime->is("application/zip")) {
466 url.setProtocol("zip");
467 m_view->setUrl(url);
468 } else if (mime->is("application/x-tar") ||
469 mime->is("application/x-tarz") ||
470 mime->is("application/x-bzip-compressed-tar") ||
471 mime->is("application/x-compressed-tar") ||
472 mime->is("application/x-tzo")) {
473 url.setProtocol("tar");
474 m_view->setUrl(url);
475 } else {
476 item.run();
477 }
478 } else {
479 item.run();
480 }
481 }
482
483 #include "dolphinviewcontainer.moc"