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