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