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