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