1 /***************************************************************************
2 * Copyright (C) 2007 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "dolphinviewcontainer.h"
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>
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
32 #include <kfileplacesmodel.h>
33 #include <kglobalsettings.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>
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"
61 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
64 DolphinView::Mode mode
,
65 bool showHiddenFiles
) :
67 m_showProgress(false),
70 m_mainWindow(mainWindow
),
81 setFocusPolicy(Qt::StrongFocus
);
82 m_topLayout
= new QVBoxLayout(this);
83 m_topLayout
->setSpacing(0);
84 m_topLayout
->setMargin(0);
86 connect(m_mainWindow
, SIGNAL(activeViewChanged()),
87 this, SLOT(updateActivationState()));
89 QClipboard
* clipboard
= QApplication::clipboard();
90 connect(clipboard
, SIGNAL(dataChanged()),
91 this, SLOT(updateCutItems()));
93 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
95 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
96 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
97 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
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);
105 m_dirModel
= new KDirModel();
106 m_dirModel
->setDirLister(m_dirLister
);
107 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
109 m_proxyModel
= new DolphinSortFilterProxyModel(this);
110 m_proxyModel
->setSourceModel(m_dirModel
);
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
&)));
129 m_view
= new DolphinView(this,
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 connect(m_view
, SIGNAL(requestItemInfo(const KUrl
&)),
143 this, SLOT(showItemInfo(const KUrl
&)));
145 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
146 m_view
, SLOT(setUrl(const KUrl
&)));
148 m_statusBar
= new DolphinStatusBar(this, url
);
150 m_filterBar
= new FilterBar(this);
151 m_filterBar
->setVisible(settings
->filterBar());
152 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
153 this, SLOT(changeNameFilter(const QString
&)));
154 connect(m_filterBar
, SIGNAL(closeRequest()),
155 this, SLOT(closeFilterBar()));
157 m_topLayout
->addWidget(m_urlNavigator
);
158 m_topLayout
->addWidget(m_view
);
159 m_topLayout
->addWidget(m_filterBar
);
160 m_topLayout
->addWidget(m_statusBar
);
163 DolphinViewContainer::~DolphinViewContainer()
169 void DolphinViewContainer::setUrl(const KUrl
& url
)
171 m_urlNavigator
->setUrl(url
);
174 const KUrl
& DolphinViewContainer::url() const
176 return m_urlNavigator
->url();
179 void DolphinViewContainer::setActive(bool active
)
181 m_urlNavigator
->setActive(active
);
182 m_view
->setActive(active
);
185 bool DolphinViewContainer::isActive() const
187 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
188 return m_view
->isActive();
191 void DolphinViewContainer::renameSelectedItems()
193 DolphinViewContainer
* view
= m_mainWindow
->activeViewContainer();
194 const KUrl::List urls
= m_view
->selectedUrls();
195 if (urls
.count() > 1) {
196 // More than one item has been selected for renaming. Open
197 // a rename dialog and rename all items afterwards.
198 RenameDialog
dialog(urls
);
199 if (dialog
.exec() == QDialog::Rejected
) {
203 const QString
& newName
= dialog
.newName();
204 if (newName
.isEmpty()) {
205 view
->statusBar()->setMessage(dialog
.errorString(),
206 DolphinStatusBar::Error
);
208 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
209 // as one operation instead of n rename operations like it is done now...
210 Q_ASSERT(newName
.contains('#'));
212 // iterate through all selected items and rename them...
213 const int replaceIndex
= newName
.indexOf('#');
214 Q_ASSERT(replaceIndex
>= 0);
217 KUrl::List::const_iterator it
= urls
.begin();
218 KUrl::List::const_iterator end
= urls
.end();
220 const KUrl
& oldUrl
= *it
;
222 number
.setNum(index
++);
224 QString
name(newName
);
225 name
.replace(replaceIndex
, 1, number
);
227 if (oldUrl
.fileName() != name
) {
228 KUrl newUrl
= oldUrl
;
229 newUrl
.setFileName(name
);
230 m_mainWindow
->rename(oldUrl
, newUrl
);
236 // Only one item has been selected for renaming. Use the custom
237 // renaming mechanism from the views.
238 Q_ASSERT(urls
.count() == 1);
240 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
241 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
242 // is a benefit for the user at all -> let's wait for some input first...
243 RenameDialog
dialog(urls
);
244 if (dialog
.exec() == QDialog::Rejected
) {
248 const QString
& newName
= dialog
.newName();
249 if (newName
.isEmpty()) {
250 view
->statusBar()->setMessage(dialog
.errorString(),
251 DolphinStatusBar::Error
);
253 const KUrl
& oldUrl
= urls
.first();
254 KUrl newUrl
= oldUrl
;
255 newUrl
.setFileName(newName
);
256 m_mainWindow
->rename(oldUrl
, newUrl
);
261 DolphinStatusBar
* DolphinViewContainer::statusBar() const
266 bool DolphinViewContainer::isFilterBarVisible() const
268 return m_filterBar
->isVisible();
271 bool DolphinViewContainer::isUrlEditable() const
273 return m_urlNavigator
->isUrlEditable();
276 KFileItem
* DolphinViewContainer::fileItem(const QModelIndex index
) const
278 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
279 return m_dirModel
->itemForIndex(dirModelIndex
);
282 DolphinMainWindow
* DolphinViewContainer::mainWindow() const
287 void DolphinViewContainer::updateProgress(int percent
)
289 if (!m_showProgress
) {
290 // Only show the directory loading progress if the status bar does
291 // not contain another progress information. This means that
292 // the directory loading progress information has the lowest priority.
293 const QString
progressText(m_statusBar
->progressText());
294 m_showProgress
= progressText
.isEmpty() ||
295 (progressText
== i18n("Loading folder..."));
296 if (m_showProgress
) {
297 m_statusBar
->setProgressText(i18n("Loading folder..."));
298 m_statusBar
->setProgress(0);
302 if (m_showProgress
) {
303 m_statusBar
->setProgress(percent
);
307 void DolphinViewContainer::updateItemCount()
309 if (m_showProgress
) {
310 m_statusBar
->setProgressText(QString());
311 m_statusBar
->setProgress(100);
312 m_showProgress
= false;
315 KFileItemList
items(m_dirLister
->items());
316 KFileItemList::const_iterator it
= items
.begin();
317 const KFileItemList::const_iterator end
= items
.end();
323 KFileItem
* item
= *it
;
334 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
337 void DolphinViewContainer::showItemInfo(const KUrl
& url
)
340 m_statusBar
->clear();
344 const QModelIndex index
= m_dirModel
->indexForUrl(url
);
345 const KFileItem
* item
= m_dirModel
->itemForIndex(index
);
347 m_statusBar
->setMessage(item
->getStatusBarInfo(), DolphinStatusBar::Default
);
351 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
353 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
356 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
358 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
361 void DolphinViewContainer::closeFilterBar()
364 emit
showFilterBarChanged(false);
367 QString
DolphinViewContainer::defaultStatusBarText() const
369 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
375 QString
DolphinViewContainer::selectionStatusBarText() const
378 const KFileItemList list
= m_view
->selectedItems();
379 if (list
.isEmpty()) {
380 // when an item is triggered, it is temporary selected but selectedItems()
381 // will return an empty list
387 KIO::filesize_t byteSize
= 0;
388 KFileItemList::const_iterator it
= list
.begin();
389 const KFileItemList::const_iterator end
= list
.end();
391 KFileItem
* item
= *it
;
396 byteSize
+= item
->size();
401 if (folderCount
> 0) {
402 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
409 const QString
sizeText(KIO::convertSize(byteSize
));
410 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
416 void DolphinViewContainer::showFilterBar(bool show
)
418 Q_ASSERT(m_filterBar
!= 0);
419 m_filterBar
->setVisible(show
);
422 void DolphinViewContainer::updateStatusBar()
424 // As the item count information is less important
425 // in comparison with other messages, it should only
427 // - the status bar is empty or
428 // - shows already the item count information or
429 // - shows only a not very important information
430 // - if any progress is given don't show the item count info at all
431 const QString
msg(m_statusBar
->message());
432 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
433 (msg
== m_statusBar
->defaultText()) ||
434 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
435 (m_statusBar
->progress() == 100);
437 const QString
text(m_view
->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
438 m_statusBar
->setDefaultText(text
);
440 if (updateStatusBarMsg
) {
441 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
445 void DolphinViewContainer::changeNameFilter(const QString
& nameFilter
)
447 // The name filter of KDirLister does a 'hard' filtering, which
448 // means that only the items are shown where the names match
449 // exactly the filter. This is non-transparent for the user, which
450 // just wants to have a 'soft' filtering: does the name contain
451 // the filter string?
452 QString
adjustedFilter(nameFilter
);
453 adjustedFilter
.insert(0, '*');
454 adjustedFilter
.append('*');
456 // Use the ProxyModel to filter:
457 // This code is #ifdefed as setNameFilter behaves
458 // slightly different than the QSortFilterProxyModel
459 // as it will not remove directories. I will ask
460 // our beloved usability experts for input
463 m_dirLister
->setNameFilter(adjustedFilter
);
464 m_dirLister
->emitChanges();
466 m_proxyModel
->setFilterRegExp(nameFilter
);
470 void DolphinViewContainer::openContextMenu(KFileItem
* item
,
473 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
477 #include "dolphinviewcontainer.moc"