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
,
65 m_showProgress(false),
68 m_mainWindow(mainWindow
),
79 setFocusPolicy(Qt::StrongFocus
);
80 m_topLayout
= new QVBoxLayout(this);
81 m_topLayout
->setSpacing(0);
82 m_topLayout
->setMargin(0);
84 QClipboard
* clipboard
= QApplication::clipboard();
85 connect(clipboard
, SIGNAL(dataChanged()),
86 this, SLOT(updateCutItems()));
88 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
89 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
90 m_mainWindow
, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
91 connect(m_urlNavigator
, SIGNAL(activated()),
92 this, SLOT(activate()));
94 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
95 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
96 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
98 m_dirLister
= new DolphinDirLister();
99 m_dirLister
->setAutoUpdate(true);
100 m_dirLister
->setMainWindow(this);
101 m_dirLister
->setDelayedMimeTypes(true);
103 m_dirModel
= new KDirModel();
104 m_dirModel
->setDirLister(m_dirLister
);
105 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
107 m_proxyModel
= new DolphinSortFilterProxyModel(this);
108 m_proxyModel
->setSourceModel(m_dirModel
);
110 connect(m_dirLister
, SIGNAL(clear()),
111 this, SLOT(updateStatusBar()));
112 connect(m_dirLister
, SIGNAL(percent(int)),
113 this, SLOT(updateProgress(int)));
114 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
115 this, SLOT(updateStatusBar()));
116 connect(m_dirLister
, SIGNAL(completed()),
117 this, SLOT(updateItemCount()));
118 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
119 this, SLOT(showInfoMessage(const QString
&)));
120 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
121 this, SLOT(showErrorMessage(const QString
&)));
123 m_view
= new DolphinView(this,
128 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
129 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
130 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&)),
131 this, SLOT(openContextMenu(KFileItem
, const KUrl
&)));
132 connect(m_view
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
133 m_mainWindow
, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
134 connect(m_view
, SIGNAL(contentsMoved(int, int)),
135 this, SLOT(saveContentsPos(int, int)));
136 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
137 this, SLOT(showItemInfo(KFileItem
)));
138 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
139 this, SLOT(showErrorMessage(const QString
&)));
140 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
141 this, SLOT(showInfoMessage(const QString
&)));
143 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
144 m_view
, SLOT(setUrl(const KUrl
&)));
146 m_statusBar
= new DolphinStatusBar(this, url
);
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()));
155 m_topLayout
->addWidget(m_urlNavigator
);
156 m_topLayout
->addWidget(m_view
);
157 m_topLayout
->addWidget(m_filterBar
);
158 m_topLayout
->addWidget(m_statusBar
);
161 DolphinViewContainer::~DolphinViewContainer()
167 void DolphinViewContainer::setUrl(const KUrl
& url
)
169 m_urlNavigator
->setUrl(url
);
172 const KUrl
& DolphinViewContainer::url() const
174 return m_urlNavigator
->url();
177 void DolphinViewContainer::setActive(bool active
)
179 m_urlNavigator
->setActive(active
);
180 m_view
->setActive(active
);
183 bool DolphinViewContainer::isActive() const
185 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
186 return m_view
->isActive();
189 void DolphinViewContainer::renameSelectedItems()
191 DolphinViewContainer
* view
= m_mainWindow
->activeViewContainer();
192 const KUrl::List urls
= m_view
->selectedUrls();
193 const QList
<KFileItem
> items
= m_view
->selectedItems();
194 if (urls
.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(urls
, items
);
198 if (dialog
.exec() == QDialog::Rejected
) {
202 const QString
& newName
= dialog
.newName();
203 if (newName
.isEmpty()) {
204 view
->statusBar()->setMessage(dialog
.errorString(),
205 DolphinStatusBar::Error
);
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('#'));
211 // iterate through all selected items and rename them...
212 const int replaceIndex
= newName
.indexOf('#');
213 Q_ASSERT(replaceIndex
>= 0);
216 KUrl::List::const_iterator it
= urls
.begin();
217 KUrl::List::const_iterator end
= urls
.end();
219 const KUrl
& oldUrl
= *it
;
221 number
.setNum(index
++);
223 QString
name(newName
);
224 name
.replace(replaceIndex
, 1, number
);
226 if (oldUrl
.fileName() != name
) {
227 KUrl newUrl
= oldUrl
;
228 newUrl
.setFileName(name
);
229 m_mainWindow
->rename(oldUrl
, newUrl
);
235 // Only one item has been selected for renaming. Use the custom
236 // renaming mechanism from the views.
237 Q_ASSERT(urls
.count() == 1);
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(urls
, items
);
243 if (dialog
.exec() == QDialog::Rejected
) {
247 const QString
& newName
= dialog
.newName();
248 if (newName
.isEmpty()) {
249 view
->statusBar()->setMessage(dialog
.errorString(),
250 DolphinStatusBar::Error
);
252 const KUrl
& oldUrl
= urls
.first();
253 KUrl newUrl
= oldUrl
;
254 newUrl
.setFileName(newName
);
255 m_mainWindow
->rename(oldUrl
, newUrl
);
260 bool DolphinViewContainer::isFilterBarVisible() const
262 return m_filterBar
->isVisible();
265 bool DolphinViewContainer::isUrlEditable() const
267 return m_urlNavigator
->isUrlEditable();
270 KFileItem
DolphinViewContainer::fileItem(const QModelIndex
& index
) const
272 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
273 return m_dirModel
->itemForIndex(dirModelIndex
);
276 void DolphinViewContainer::updateProgress(int percent
)
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);
291 if (m_showProgress
) {
292 m_statusBar
->setProgress(percent
);
296 void DolphinViewContainer::updateItemCount()
298 if (m_showProgress
) {
299 m_statusBar
->setProgressText(QString());
300 m_statusBar
->setProgress(100);
301 m_showProgress
= false;
304 KFileItemList
items(m_dirLister
->items());
305 KFileItemList::const_iterator it
= items
.begin();
306 const KFileItemList::const_iterator end
= items
.end();
312 KFileItem
* item
= *it
;
323 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
326 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
329 m_statusBar
->clear();
331 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
335 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
337 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
340 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
342 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
345 void DolphinViewContainer::closeFilterBar()
348 emit
showFilterBarChanged(false);
351 QString
DolphinViewContainer::defaultStatusBarText() const
353 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
359 QString
DolphinViewContainer::selectionStatusBarText() const
362 const QList
<KFileItem
> list
= m_view
->selectedItems();
363 if (list
.isEmpty()) {
364 // when an item is triggered, it is temporary selected but selectedItems()
365 // will return an empty list
371 KIO::filesize_t byteSize
= 0;
372 QList
<KFileItem
>::const_iterator it
= list
.begin();
373 const QList
<KFileItem
>::const_iterator end
= list
.end();
375 const KFileItem
& item
= *it
;
380 byteSize
+= item
.size();
385 if (folderCount
> 0) {
386 text
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
393 const QString
sizeText(KIO::convertSize(byteSize
));
394 text
+= i18ncp("@info:status", "1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
400 void DolphinViewContainer::showFilterBar(bool show
)
402 Q_ASSERT(m_filterBar
!= 0);
403 m_filterBar
->setVisible(show
);
406 void DolphinViewContainer::updateStatusBar()
408 // As the item count information is less important
409 // in comparison with other messages, it should only
411 // - the status bar is empty or
412 // - shows already the item count information or
413 // - shows only a not very important information
414 // - if any progress is given don't show the item count info at all
415 const QString
msg(m_statusBar
->message());
416 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
417 (msg
== m_statusBar
->defaultText()) ||
418 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
419 (m_statusBar
->progress() == 100);
421 const QString
text(m_view
->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
422 m_statusBar
->setDefaultText(text
);
424 if (updateStatusBarMsg
) {
425 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
429 void DolphinViewContainer::changeNameFilter(const QString
& nameFilter
)
431 // The name filter of KDirLister does a 'hard' filtering, which
432 // means that only the items are shown where the names match
433 // exactly the filter. This is non-transparent for the user, which
434 // just wants to have a 'soft' filtering: does the name contain
435 // the filter string?
436 QString
adjustedFilter(nameFilter
);
437 adjustedFilter
.insert(0, '*');
438 adjustedFilter
.append('*');
440 // Use the ProxyModel to filter:
441 // This code is #ifdefed as setNameFilter behaves
442 // slightly different than the QSortFilterProxyModel
443 // as it will not remove directories. I will ask
444 // our beloved usability experts for input
447 m_dirLister
->setNameFilter(adjustedFilter
);
448 m_dirLister
->emitChanges();
450 m_proxyModel
->setFilterRegExp(nameFilter
);
454 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
457 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
461 void DolphinViewContainer::saveContentsPos(int x
, int y
)
463 m_urlNavigator
->savePosition(x
, y
);
466 void DolphinViewContainer::restoreContentsPos()
468 if (!url().isEmpty()) {
469 const QPoint pos
= m_urlNavigator
->savedPosition();
470 m_view
->setContentsPosition(pos
.x(), pos
.y());
474 void DolphinViewContainer::activate()
479 #include "dolphinviewcontainer.moc"