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 <kfileitemdelegate.h>
31 #include <kfileplacesmodel.h>
32 #include <kglobalsettings.h>
34 #include <kiconeffect.h>
35 #include <kio/netaccess.h>
36 #include <kio/renamedialog.h>
37 #include <kio/previewjob.h>
38 #include <kmimetyperesolver.h>
39 #include <konqmimedata.h>
40 #include <konq_operations.h>
43 #include "dolphinmodel.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 "filterbar.h"
54 #include "renamedialog.h"
55 #include "kurlnavigator.h"
56 #include "viewproperties.h"
57 #include "dolphinsettings.h"
58 #include "dolphin_generalsettings.h"
60 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
64 m_showProgress(false),
65 m_mainWindow(mainWindow
),
75 setFocusPolicy(Qt::StrongFocus
);
76 m_topLayout
= new QVBoxLayout(this);
77 m_topLayout
->setSpacing(0);
78 m_topLayout
->setMargin(0);
80 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
81 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
82 m_mainWindow
, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
83 connect(m_urlNavigator
, SIGNAL(activated()),
84 this, SLOT(activate()));
86 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
87 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
88 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
90 m_dirLister
= new DolphinDirLister();
91 m_dirLister
->setAutoUpdate(true);
92 m_dirLister
->setMainWindow(this);
93 m_dirLister
->setDelayedMimeTypes(true);
95 m_dolphinModel
= new DolphinModel(this);
96 m_dolphinModel
->setDirLister(m_dirLister
);
97 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
99 m_proxyModel
= new DolphinSortFilterProxyModel(this);
100 m_proxyModel
->setSourceModel(m_dolphinModel
);
102 connect(m_dirLister
, SIGNAL(clear()),
103 this, SLOT(updateStatusBar()));
104 connect(m_dirLister
, SIGNAL(percent(int)),
105 this, SLOT(updateProgress(int)));
106 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
107 this, SLOT(updateStatusBar()));
108 connect(m_dirLister
, SIGNAL(completed()),
109 this, SLOT(slotDirListerCompleted()));
110 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
111 this, SLOT(showInfoMessage(const QString
&)));
112 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
113 this, SLOT(showErrorMessage(const QString
&)));
115 m_view
= new DolphinView(this,
120 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
121 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
122 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&)),
123 this, SLOT(openContextMenu(KFileItem
, const KUrl
&)));
124 connect(m_view
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
125 m_mainWindow
, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
126 connect(m_view
, SIGNAL(contentsMoved(int, int)),
127 this, SLOT(saveContentsPos(int, int)));
128 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
129 this, SLOT(showItemInfo(KFileItem
)));
130 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
131 this, SLOT(showErrorMessage(const QString
&)));
132 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
133 this, SLOT(showInfoMessage(const QString
&)));
134 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
135 this, SLOT(slotItemTriggered(KFileItem
)));
136 connect(m_view
, SIGNAL(startedPathLoading(const KUrl
&)),
137 this, SLOT(saveRootUrl(const KUrl
&)));
139 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
140 this, SLOT(restoreView(const KUrl
&)));
142 m_statusBar
= new DolphinStatusBar(this, url
);
143 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
144 m_statusBar
, SLOT(updateSpaceInfoContent(const KUrl
&)));
146 m_filterBar
= new FilterBar(this);
147 m_filterBar
->setVisible(settings
->filterBar());
148 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
149 this, SLOT(setNameFilter(const QString
&)));
150 connect(m_filterBar
, SIGNAL(closeRequest()),
151 this, SLOT(closeFilterBar()));
153 m_topLayout
->addWidget(m_urlNavigator
);
154 m_topLayout
->addWidget(m_view
);
155 m_topLayout
->addWidget(m_filterBar
);
156 m_topLayout
->addWidget(m_statusBar
);
159 DolphinViewContainer::~DolphinViewContainer()
165 void DolphinViewContainer::setUrl(const KUrl
& url
)
167 m_urlNavigator
->setUrl(url
);
170 const KUrl
& DolphinViewContainer::url() const
172 return m_urlNavigator
->url();
175 void DolphinViewContainer::setActive(bool active
)
177 m_urlNavigator
->setActive(active
);
178 m_view
->setActive(active
);
181 bool DolphinViewContainer::isActive() const
183 Q_ASSERT(m_view
->isActive() == m_urlNavigator
->isActive());
184 return m_view
->isActive();
187 void DolphinViewContainer::renameSelectedItems()
189 DolphinViewContainer
* view
= m_mainWindow
->activeViewContainer();
190 const KFileItemList items
= m_view
->selectedItems();
191 if (items
.count() > 1) {
192 // More than one item has been selected for renaming. Open
193 // a rename dialog and rename all items afterwards.
194 RenameDialog
dialog(items
);
195 if (dialog
.exec() == QDialog::Rejected
) {
199 const QString
& newName
= dialog
.newName();
200 if (newName
.isEmpty()) {
201 view
->statusBar()->setMessage(dialog
.errorString(),
202 DolphinStatusBar::Error
);
204 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
205 // as one operation instead of n rename operations like it is done now...
206 Q_ASSERT(newName
.contains('#'));
208 // iterate through all selected items and rename them...
209 const int replaceIndex
= newName
.indexOf('#');
210 Q_ASSERT(replaceIndex
>= 0);
213 KFileItemList::const_iterator it
= items
.begin();
214 KFileItemList::const_iterator end
= items
.end();
216 const KUrl
& oldUrl
= (*it
).url();
218 number
.setNum(index
++);
220 QString
name(newName
);
221 name
.replace(replaceIndex
, 1, number
);
223 if (oldUrl
.fileName() != name
) {
224 KUrl newUrl
= oldUrl
;
225 newUrl
.setFileName(name
);
226 m_mainWindow
->rename(oldUrl
, newUrl
);
232 // Only one item has been selected for renaming. Use the custom
233 // renaming mechanism from the views.
234 Q_ASSERT(items
.count() == 1);
236 // TODO: Think about using KFileItemDelegate as soon as it supports editing.
237 // Currently the RenameDialog is used, but I'm not sure whether inline renaming
238 // is a benefit for the user at all -> let's wait for some input first...
239 RenameDialog
dialog(items
);
240 if (dialog
.exec() == QDialog::Rejected
) {
244 const QString
& newName
= dialog
.newName();
245 if (newName
.isEmpty()) {
246 view
->statusBar()->setMessage(dialog
.errorString(),
247 DolphinStatusBar::Error
);
249 const KUrl
& oldUrl
= items
.first().url();
250 KUrl newUrl
= oldUrl
;
251 newUrl
.setFileName(newName
);
252 m_mainWindow
->rename(oldUrl
, newUrl
);
257 bool DolphinViewContainer::isFilterBarVisible() const
259 return m_filterBar
->isVisible();
262 bool DolphinViewContainer::isUrlEditable() const
264 return m_urlNavigator
->isUrlEditable();
267 KFileItem
DolphinViewContainer::fileItem(const QModelIndex
& index
) const
269 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
270 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
273 void DolphinViewContainer::updateProgress(int percent
)
275 if (!m_showProgress
) {
276 // Only show the directory loading progress if the status bar does
277 // not contain another progress information. This means that
278 // the directory loading progress information has the lowest priority.
279 const QString
progressText(m_statusBar
->progressText());
280 const QString
loadingText(i18nc("@info:progress", "Loading folder..."));
281 m_showProgress
= progressText
.isEmpty() || (progressText
== loadingText
);
282 if (m_showProgress
) {
283 m_statusBar
->setProgressText(loadingText
);
284 m_statusBar
->setProgress(0);
288 if (m_showProgress
) {
289 m_statusBar
->setProgress(percent
);
293 void DolphinViewContainer::slotDirListerCompleted()
295 if (m_showProgress
) {
296 m_statusBar
->setProgressText(QString());
297 m_statusBar
->setProgress(100);
298 m_showProgress
= false;
303 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
306 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
309 m_statusBar
->clear();
311 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
315 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
317 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
320 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
322 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
325 void DolphinViewContainer::closeFilterBar()
328 emit
showFilterBarChanged(false);
331 QString
DolphinViewContainer::defaultStatusBarText() const
335 m_view
->calculateItemCount(fileCount
, folderCount
);
336 return KIO::itemsSummaryString(fileCount
+ folderCount
,
342 QString
DolphinViewContainer::selectionStatusBarText() const
345 const KFileItemList list
= m_view
->selectedItems();
346 if (list
.isEmpty()) {
347 // when an item is triggered, it is temporary selected but selectedItems()
348 // will return an empty list
354 KIO::filesize_t byteSize
= 0;
355 KFileItemList::const_iterator it
= list
.begin();
356 const KFileItemList::const_iterator end
= list
.end();
358 const KFileItem
& item
= *it
;
363 byteSize
+= item
.size();
368 if (folderCount
> 0) {
369 text
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
376 const QString
sizeText(KIO::convertSize(byteSize
));
377 text
+= i18ncp("@info:status", "1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
383 void DolphinViewContainer::showFilterBar(bool show
)
385 Q_ASSERT(m_filterBar
!= 0);
386 m_filterBar
->setVisible(show
);
389 void DolphinViewContainer::updateStatusBar()
391 // As the item count information is less important
392 // in comparison with other messages, it should only
394 // - the status bar is empty or
395 // - shows already the item count information or
396 // - shows only a not very important information
397 // - if any progress is given don't show the item count info at all
398 const QString
msg(m_statusBar
->message());
399 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
400 (msg
== m_statusBar
->defaultText()) ||
401 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
402 (m_statusBar
->progress() == 100);
404 const QString
text(m_view
->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
405 m_statusBar
->setDefaultText(text
);
407 if (updateStatusBarMsg
) {
408 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
412 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
414 m_view
->setNameFilter(nameFilter
);
418 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
421 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
425 void DolphinViewContainer::saveContentsPos(int x
, int y
)
427 m_urlNavigator
->savePosition(x
, y
);
430 void DolphinViewContainer::restoreContentsPos()
432 if (!url().isEmpty()) {
433 const QPoint pos
= m_urlNavigator
->savedPosition();
434 m_view
->setContentsPosition(pos
.x(), pos
.y());
438 void DolphinViewContainer::activate()
443 void DolphinViewContainer::restoreView(const KUrl
& url
)
445 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
448 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
451 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
454 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
456 // Prefer the local path over the URL.
458 KUrl url
= item
.mostLocalUrl(isLocal
);
462 } else if (item
.isFile()) {
463 // allow to browse through ZIP and tar files
464 KMimeType::Ptr mime
= item
.mimeTypePtr();
465 if (mime
->is("application/zip")) {
466 url
.setProtocol("zip");
468 } else if (mime
->is("application/x-tar") ||
469 mime
->is("application/x-tarz") ||
470 mime
->is("application/x-bzip-compressed-tar") ||
471 mime
->is("application/x-compressed-tar") ||
472 mime
->is("application/x-tzo")) {
473 url
.setProtocol("tar");
483 #include "dolphinviewcontainer.moc"