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/previewjob.h>
37 #include <kmimetyperesolver.h>
38 #include <konqmimedata.h>
39 #include <konq_operations.h>
42 #include "dolphinmodel.h"
43 #include "dolphincolumnview.h"
44 #include "dolphincontroller.h"
45 #include "dolphinstatusbar.h"
46 #include "dolphinmainwindow.h"
47 #include "dolphindirlister.h"
48 #include "dolphinsortfilterproxymodel.h"
49 #include "dolphindetailsview.h"
50 #include "dolphiniconsview.h"
51 #include "dolphincontextmenu.h"
52 #include "filterbar.h"
53 #include "kurlnavigator.h"
54 #include "viewproperties.h"
55 #include "dolphinsettings.h"
56 #include "dolphin_generalsettings.h"
58 DolphinViewContainer::DolphinViewContainer(DolphinMainWindow
* mainWindow
,
62 m_showProgress(false),
63 m_mainWindow(mainWindow
),
73 setFocusPolicy(Qt::StrongFocus
);
74 m_topLayout
= new QVBoxLayout(this);
75 m_topLayout
->setSpacing(0);
76 m_topLayout
->setMargin(0);
78 m_urlNavigator
= new KUrlNavigator(DolphinSettings::instance().placesModel(), url
, this);
79 connect(m_urlNavigator
, SIGNAL(urlsDropped(const KUrl::List
&, const KUrl
&)),
80 m_mainWindow
, SLOT(dropUrls(const KUrl::List
&, const KUrl
&)));
81 connect(m_urlNavigator
, SIGNAL(activated()),
82 this, SLOT(activate()));
84 const GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
85 m_urlNavigator
->setUrlEditable(settings
->editableUrl());
86 m_urlNavigator
->setHomeUrl(settings
->homeUrl());
88 m_dirLister
= new DolphinDirLister();
89 m_dirLister
->setAutoUpdate(true);
90 m_dirLister
->setMainWindow(this);
91 m_dirLister
->setDelayedMimeTypes(true);
93 m_dolphinModel
= new DolphinModel(this);
94 m_dolphinModel
->setDirLister(m_dirLister
);
95 m_dolphinModel
->setDropsAllowed(DolphinModel::DropOnDirectory
);
97 m_proxyModel
= new DolphinSortFilterProxyModel(this);
98 m_proxyModel
->setSourceModel(m_dolphinModel
);
99 m_proxyModel
->setFilterCaseSensitivity(Qt::CaseInsensitive
);
101 connect(m_dirLister
, SIGNAL(clear()),
102 this, SLOT(updateStatusBar()));
103 connect(m_dirLister
, SIGNAL(percent(int)),
104 this, SLOT(updateProgress(int)));
105 connect(m_dirLister
, SIGNAL(deleteItem(const KFileItem
&)),
106 this, SLOT(updateStatusBar()));
107 connect(m_dirLister
, SIGNAL(completed()),
108 this, SLOT(slotDirListerCompleted()));
109 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
110 this, SLOT(showInfoMessage(const QString
&)));
111 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
112 this, SLOT(showErrorMessage(const QString
&)));
114 m_view
= new DolphinView(this,
119 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
120 m_urlNavigator
, SLOT(setUrl(const KUrl
&)));
121 connect(m_view
, SIGNAL(requestContextMenu(KFileItem
, const KUrl
&)),
122 this, SLOT(openContextMenu(KFileItem
, const KUrl
&)));
123 connect(m_view
, SIGNAL(contentsMoved(int, int)),
124 this, SLOT(saveContentsPos(int, int)));
125 connect(m_view
, SIGNAL(requestItemInfo(KFileItem
)),
126 this, SLOT(showItemInfo(KFileItem
)));
127 connect(m_view
, SIGNAL(errorMessage(const QString
&)),
128 this, SLOT(showErrorMessage(const QString
&)));
129 connect(m_view
, SIGNAL(infoMessage(const QString
&)),
130 this, SLOT(showInfoMessage(const QString
&)));
131 connect(m_view
, SIGNAL(operationCompletedMessage(const QString
&)),
132 this, SLOT(showOperationCompletedMessage(const QString
&)));
133 connect(m_view
, SIGNAL(itemTriggered(KFileItem
)),
134 this, SLOT(slotItemTriggered(KFileItem
)));
135 connect(m_view
, SIGNAL(startedPathLoading(const KUrl
&)),
136 this, SLOT(saveRootUrl(const KUrl
&)));
138 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
139 this, SLOT(restoreView(const KUrl
&)));
141 m_statusBar
= new DolphinStatusBar(this, url
);
142 connect(m_view
, SIGNAL(urlChanged(const KUrl
&)),
143 m_statusBar
, SLOT(updateSpaceInfoContent(const KUrl
&)));
145 m_filterBar
= new FilterBar(this);
146 m_filterBar
->setVisible(settings
->filterBar());
147 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
148 this, SLOT(setNameFilter(const QString
&)));
149 connect(m_filterBar
, SIGNAL(closeRequest()),
150 this, SLOT(closeFilterBar()));
152 m_topLayout
->addWidget(m_urlNavigator
);
153 m_topLayout
->addWidget(m_view
);
154 m_topLayout
->addWidget(m_filterBar
);
155 m_topLayout
->addWidget(m_statusBar
);
158 DolphinViewContainer::~DolphinViewContainer()
160 m_dirLister
->disconnect();
164 delete m_dolphinModel
;
166 m_dirLister
= 0; // deleted by m_dolphinModel
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 bool DolphinViewContainer::isFilterBarVisible() const
193 return m_filterBar
->isVisible();
196 bool DolphinViewContainer::isUrlEditable() const
198 return m_urlNavigator
->isUrlEditable();
201 KFileItem
DolphinViewContainer::fileItem(const QModelIndex
& index
) const
203 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
204 return m_dolphinModel
->itemForIndex(dolphinModelIndex
);
207 void DolphinViewContainer::updateProgress(int percent
)
209 if (!m_showProgress
) {
210 // Only show the directory loading progress if the status bar does
211 // not contain another progress information. This means that
212 // the directory loading progress information has the lowest priority.
213 const QString
progressText(m_statusBar
->progressText());
214 const QString
loadingText(i18nc("@info:progress", "Loading folder..."));
215 m_showProgress
= progressText
.isEmpty() || (progressText
== loadingText
);
216 if (m_showProgress
) {
217 m_statusBar
->setProgressText(loadingText
);
218 m_statusBar
->setProgress(0);
222 if (m_showProgress
) {
223 m_statusBar
->setProgress(percent
);
227 void DolphinViewContainer::slotDirListerCompleted()
229 if (m_showProgress
) {
230 m_statusBar
->setProgressText(QString());
231 m_statusBar
->setProgress(100);
232 m_showProgress
= false;
237 QTimer::singleShot(100, this, SLOT(restoreContentsPos()));
240 void DolphinViewContainer::showItemInfo(const KFileItem
& item
)
243 m_statusBar
->clear();
245 m_statusBar
->setMessage(item
.getStatusBarInfo(), DolphinStatusBar::Default
);
249 void DolphinViewContainer::showInfoMessage(const QString
& msg
)
251 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
254 void DolphinViewContainer::showErrorMessage(const QString
& msg
)
256 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
259 void DolphinViewContainer::showOperationCompletedMessage(const QString
& msg
)
261 m_statusBar
->setMessage(msg
, DolphinStatusBar::OperationCompleted
);
264 void DolphinViewContainer::closeFilterBar()
267 emit
showFilterBarChanged(false);
270 QString
DolphinViewContainer::defaultStatusBarText() const
274 m_view
->calculateItemCount(fileCount
, folderCount
);
275 return KIO::itemsSummaryString(fileCount
+ folderCount
,
281 QString
DolphinViewContainer::selectionStatusBarText() const
284 const KFileItemList list
= m_view
->selectedItems();
285 if (list
.isEmpty()) {
286 // when an item is triggered, it is temporary selected but selectedItems()
287 // will return an empty list
293 KIO::filesize_t byteSize
= 0;
294 KFileItemList::const_iterator it
= list
.begin();
295 const KFileItemList::const_iterator end
= list
.end();
297 const KFileItem
& item
= *it
;
302 byteSize
+= item
.size();
307 if (folderCount
> 0) {
308 text
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
315 const QString
sizeText(KIO::convertSize(byteSize
));
316 text
+= i18ncp("@info:status", "1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
322 void DolphinViewContainer::showFilterBar(bool show
)
324 Q_ASSERT(m_filterBar
!= 0);
325 m_filterBar
->setVisible(show
);
328 void DolphinViewContainer::updateStatusBar()
330 // As the item count information is less important
331 // in comparison with other messages, it should only
333 // - the status bar is empty or
334 // - shows already the item count information or
335 // - shows only a not very important information
336 // - if any progress is given don't show the item count info at all
337 const QString
msg(m_statusBar
->message());
338 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
339 (msg
== m_statusBar
->defaultText()) ||
340 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
341 (m_statusBar
->progress() == 100);
343 const QString
text(m_view
->hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
344 m_statusBar
->setDefaultText(text
);
346 if (updateStatusBarMsg
) {
347 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
351 void DolphinViewContainer::setNameFilter(const QString
& nameFilter
)
353 m_view
->setNameFilter(nameFilter
);
357 void DolphinViewContainer::openContextMenu(const KFileItem
& item
,
360 DolphinContextMenu
contextMenu(m_mainWindow
, item
, url
);
364 void DolphinViewContainer::saveContentsPos(int x
, int y
)
366 m_urlNavigator
->savePosition(x
, y
);
369 void DolphinViewContainer::restoreContentsPos()
371 if (!url().isEmpty()) {
372 const QPoint pos
= m_urlNavigator
->savedPosition();
373 m_view
->setContentsPosition(pos
.x(), pos
.y());
377 void DolphinViewContainer::activate()
382 void DolphinViewContainer::restoreView(const KUrl
& url
)
384 m_view
->updateView(url
, m_urlNavigator
->savedRootUrl());
387 void DolphinViewContainer::saveRootUrl(const KUrl
& url
)
390 m_urlNavigator
->saveRootUrl(m_view
->rootUrl());
393 void DolphinViewContainer::slotItemTriggered(const KFileItem
& item
)
395 // Prefer the local path over the URL.
397 KUrl url
= item
.mostLocalUrl(isLocal
);
401 } else if (item
.isFile() && url
.isLocalFile()) {
402 // allow to browse through ZIP and tar files
403 // TODO: make this configurable for Dolphin in KDE 4.1
405 KMimeType::Ptr mime
= item
.mimeTypePtr();
407 // Don't use mime->is("application/zip"), as this would
408 // also browse through Open Office files:
409 if (mime
->name() == "application/zip") {
410 url
.setProtocol("zip");
412 } else if (mime
->is("application/x-tar") ||
413 mime
->is("application/x-tarz") ||
414 mime
->is("application/x-bzip-compressed-tar") ||
415 mime
->is("application/x-compressed-tar") ||
416 mime
->is("application/x-tzo")) {
417 url
.setProtocol("tar");
427 #include "dolphinviewcontainer.moc"