1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinview.h"
25 #include <QApplication>
27 #include <QItemSelectionModel>
28 #include <QMouseEvent>
29 #include <QVBoxLayout>
31 #include <kdirmodel.h>
32 #include <kfileitemdelegate.h>
34 #include <kio/netaccess.h>
35 #include <kio/renamedialog.h>
36 #include <kmimetyperesolver.h>
37 #include <konq_operations.h>
40 #include "dolphinstatusbar.h"
41 #include "dolphinmainwindow.h"
42 #include "dolphindirlister.h"
43 #include "dolphinsortfilterproxymodel.h"
44 #include "dolphindetailsview.h"
45 #include "dolphiniconsview.h"
46 #include "dolphincontextmenu.h"
47 #include "filterbar.h"
48 #include "renamedialog.h"
49 #include "urlnavigator.h"
50 #include "viewproperties.h"
52 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
56 bool showHiddenFiles
) :
58 m_showProgress(false),
63 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 UrlNavigator(url
, this);
81 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
82 this, SLOT(loadDirectory(const KUrl
&)));
84 m_statusBar
= new DolphinStatusBar(this);
86 m_dirLister
= new DolphinDirLister();
87 m_dirLister
->setAutoUpdate(true);
88 m_dirLister
->setMainWindow(this);
89 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
90 m_dirLister
->setDelayedMimeTypes(true);
92 connect(m_dirLister
, SIGNAL(clear()),
93 this, SLOT(updateStatusBar()));
94 connect(m_dirLister
, SIGNAL(percent(int)),
95 this, SLOT(updateProgress(int)));
96 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
97 this, SLOT(updateStatusBar()));
98 connect(m_dirLister
, SIGNAL(completed()),
99 this, SLOT(updateItemCount()));
100 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
101 this, SLOT(showInfoMessage(const QString
&)));
102 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
103 this, SLOT(showErrorMessage(const QString
&)));
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
);
114 m_iconSize
= K3Icon::SizeMedium
;
116 m_filterBar
= new FilterBar(this);
118 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
119 this, SLOT(changeNameFilter(const QString
&)));
120 connect(m_filterBar
, SIGNAL(closeRequest()),
121 this, SLOT(closeFilterBar()));
123 m_topLayout
->addWidget(m_urlNavigator
);
124 m_topLayout
->addWidget(itemView());
125 m_topLayout
->addWidget(m_filterBar
);
126 m_topLayout
->addWidget(m_statusBar
);
128 loadDirectory(m_urlNavigator
->url());
131 DolphinView::~DolphinView()
137 void DolphinView::setUrl(const KUrl
& url
)
139 m_urlNavigator
->setUrl(url
);
142 const KUrl
& DolphinView::url() const
144 return m_urlNavigator
->url();
147 void DolphinView::requestActivation()
149 mainWindow()->setActiveView(this);
152 bool DolphinView::isActive() const
154 return (mainWindow()->activeView() == this);
157 void DolphinView::setMode(Mode mode
)
159 if (mode
== m_mode
) {
160 return; // the wished mode is already set
165 ViewProperties
props(m_urlNavigator
->url());
166 props
.setViewMode(m_mode
);
169 startDirLister(m_urlNavigator
->url());
174 DolphinView::Mode
DolphinView::mode() const
179 void DolphinView::setShowPreview(bool show
)
181 ViewProperties
props(m_urlNavigator
->url());
182 props
.setShowPreview(show
);
184 // TODO: wait until previews are possible with KFileItemDelegate
186 emit
showPreviewChanged();
189 bool DolphinView::showPreview() const
191 // TODO: wait until previews are possible with KFileItemDelegate
195 void DolphinView::setShowHiddenFiles(bool show
)
197 if (m_dirLister
->showingDotFiles() == show
) {
201 ViewProperties
props(m_urlNavigator
->url());
202 props
.setShowHiddenFiles(show
);
205 m_dirLister
->setShowingDotFiles(show
);
207 emit
showHiddenFilesChanged();
212 bool DolphinView::showHiddenFiles() const
214 return m_dirLister
->showingDotFiles();
217 void DolphinView::renameSelectedItems()
219 const KUrl::List urls
= selectedUrls();
220 if (urls
.count() > 1) {
221 // More than one item has been selected for renaming. Open
222 // a rename dialog and rename all items afterwards.
223 RenameDialog
dialog(urls
);
224 if (dialog
.exec() == QDialog::Rejected
) {
228 DolphinView
* view
= mainWindow()->activeView();
229 const QString
& newName
= dialog
.newName();
230 if (newName
.isEmpty()) {
231 view
->statusBar()->setMessage(i18n("The new item name is invalid."),
232 DolphinStatusBar::Error
);
235 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
237 //UndoManager& undoMan = UndoManager::instance();
238 //undoMan.beginMacro();
240 assert(newName
.contains('#'));
242 const int urlsCount
= urls
.count();
244 // iterate through all selected items and rename them...
245 const int replaceIndex
= newName
.indexOf('#');
246 assert(replaceIndex
>= 0);
247 for (int i
= 0; i
< urlsCount
; ++i
) {
248 const KUrl
& source
= urls
[i
];
250 number
.setNum(i
+ 1);
252 QString
name(newName
);
253 name
.replace(replaceIndex
, 1, number
);
255 if (source
.fileName() != name
) {
256 KUrl
dest(source
.upUrl());
259 const bool destExists
= KIO::NetAccess::exists(dest
, false, view
);
261 view
->statusBar()->setMessage(i18n("Renaming failed (item '%1' already exists).",name
),
262 DolphinStatusBar::Error
);
265 else if (KIO::NetAccess::file_move(source
, dest
)) {
266 // TODO: From the users point of view he executed one 'rename n files' operation,
267 // but internally we store it as n 'rename 1 file' operations for the undo mechanism.
268 //DolphinCommand command(DolphinCommand::Rename, source, dest);
269 //undoMan.addCommand(command);
274 //undoMan.endMacro();
278 // Only one item has been selected for renaming. Use the custom
279 // renaming mechanism from the views.
280 assert(urls
.count() == 1);
282 /*if (m_mode == DetailsView) {
283 Q3ListViewItem* item = m_iconsView->firstChild();
285 if (item->isSelected()) {
286 m_iconsView->rename(item, DolphinDetailsView::NameColumn);
289 item = item->nextSibling();
293 KFileIconViewItem* item = static_cast<KFileIconViewItem*>(m_iconsView->firstItem());
295 if (item->isSelected()) {
299 item = static_cast<KFileIconViewItem*>(item->nextItem());
305 void DolphinView::selectAll()
307 selectAll(QItemSelectionModel::Select
);
310 void DolphinView::invertSelection()
312 selectAll(QItemSelectionModel::Toggle
);
315 DolphinStatusBar
* DolphinView::statusBar() const
320 int DolphinView::contentsX() const
323 return itemView()->horizontalScrollBar()->value();
326 int DolphinView::contentsY() const
328 return itemView()->verticalScrollBar()->value();
331 void DolphinView::refreshSettings()
333 startDirLister(m_urlNavigator
->url());
336 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
338 emit
requestItemInfo(url
);
341 bool DolphinView::isFilterBarVisible() const
343 return m_filterBar
->isVisible();
346 bool DolphinView::isUrlEditable() const
348 return m_urlNavigator
->isUrlEditable();
351 void DolphinView::zoomIn()
353 //itemEffectsManager()->zoomIn();
356 void DolphinView::zoomOut()
358 //itemEffectsManager()->zoomOut();
361 bool DolphinView::isZoomInPossible() const
363 return false; //itemEffectsManager()->isZoomInPossible();
366 bool DolphinView::isZoomOutPossible() const
368 return false; //itemEffectsManager()->isZoomOutPossible();
371 void DolphinView::setSorting(Sorting sorting
)
373 if (sorting
!= this->sorting()) {
374 ViewProperties
props(url());
375 props
.setSorting(sorting
);
377 m_proxyModel
->setSorting(sorting
);
379 emit
sortingChanged(sorting
);
383 DolphinView::Sorting
DolphinView::sorting() const
385 return m_proxyModel
->sorting();
388 void DolphinView::setSortOrder(Qt::SortOrder order
)
390 if (sortOrder() != order
) {
391 ViewProperties
props(url());
392 props
.setSortOrder(order
);
394 m_proxyModel
->setSortOrder(order
);
396 emit
sortOrderChanged(order
);
400 Qt::SortOrder
DolphinView::sortOrder() const
402 return m_proxyModel
->sortOrder();
405 void DolphinView::goBack()
407 m_urlNavigator
->goBack();
410 void DolphinView::goForward()
412 m_urlNavigator
->goForward();
415 void DolphinView::goUp()
417 m_urlNavigator
->goUp();
420 void DolphinView::goHome()
422 m_urlNavigator
->goHome();
425 void DolphinView::setUrlEditable(bool editable
)
427 m_urlNavigator
->editUrl(editable
);
430 const QLinkedList
<UrlNavigator::HistoryElem
> DolphinView::urlHistory(int& index
) const
432 return m_urlNavigator
->history(index
);
435 bool DolphinView::hasSelection() const
437 return itemView()->selectionModel()->hasSelection();
440 KFileItemList
DolphinView::selectedItems() const
442 const QAbstractItemView
* view
= itemView();
444 // Our view has a selection, we will map them back to the DirModel
445 // and then fill the KFileItemList.
446 assert((view
!= 0) && (view
->selectionModel() != 0));
448 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
449 KFileItemList itemList
;
451 const QModelIndexList indexList
= selection
.indexes();
452 QModelIndexList::const_iterator end
= indexList
.end();
453 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
454 assert((*it
).isValid());
456 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
458 itemList
.append(item
);
465 KUrl::List
DolphinView::selectedUrls() const
469 const KFileItemList list
= selectedItems();
470 KFileItemList::const_iterator it
= list
.begin();
471 const KFileItemList::const_iterator end
= list
.end();
473 KFileItem
* item
= *it
;
474 urls
.append(item
->url());
481 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
483 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
484 return m_dirModel
->itemForIndex(dirModelIndex
);
487 void DolphinView::openContextMenu(KFileItem
* fileInfo
, const QPoint
& pos
)
489 DolphinContextMenu
contextMenu(this, fileInfo
, pos
);
493 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
497 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
501 KUrl
dest(source
.upUrl());
502 dest
.addPath(newName
);
504 const bool destExists
= KIO::NetAccess::exists(dest
,
506 mainWindow()->activeView());
508 // the destination already exists, hence ask the user
510 KIO::RenameDialog
renameDialog(this,
511 i18n("File Already Exists"),
515 switch (renameDialog
.exec()) {
516 case KIO::R_OVERWRITE
:
517 // the destination should be overwritten
518 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
521 case KIO::R_RENAME
: {
522 // a new name for the destination has been used
523 KUrl
newDest(renameDialog
.newDestUrl());
524 ok
= KIO::NetAccess::file_move(source
, newDest
);
529 // the renaming operation has been canceled
535 // no destination exists, hence just move the file to
537 ok
= KIO::NetAccess::file_move(source
, dest
);
540 const QString destFileName
= dest
.fileName();
542 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.",source
.fileName(), destFileName
),
543 DolphinStatusBar::OperationCompleted
);
545 KonqOperations::rename(this, source
, destFileName
);
548 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source
.fileName(), destFileName
),
549 DolphinStatusBar::Error
);
554 void DolphinView::reload()
556 startDirLister(m_urlNavigator
->url(), true);
559 void DolphinView::declareViewActive()
561 mainWindow()->setActiveView( this );
564 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
566 QWidget::mouseReleaseEvent(event
);
567 mainWindow()->setActiveView(this);
570 DolphinMainWindow
* DolphinView::mainWindow() const
575 void DolphinView::loadDirectory(const KUrl
& url
)
577 const ViewProperties
props(url
);
579 const Mode mode
= props
.viewMode();
580 if (m_mode
!= mode
) {
586 const bool showHiddenFiles
= props
.showHiddenFiles();
587 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
588 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
589 emit
showHiddenFilesChanged();
592 const DolphinView::Sorting sorting
= props
.sorting();
593 if (sorting
!= m_proxyModel
->sorting()) {
594 m_proxyModel
->setSorting(sorting
);
595 emit
sortingChanged(sorting
);
598 const Qt::SortOrder sortOrder
= props
.sortOrder();
599 if (sortOrder
!= m_proxyModel
->sortOrder()) {
600 m_proxyModel
->setSortOrder(sortOrder
);
601 emit
sortOrderChanged(sortOrder
);
604 // TODO: handle previews (props.showPreview())
607 emit
urlChanged(url
);
609 m_statusBar
->clear();
612 void DolphinView::triggerItem(const QModelIndex
& index
)
614 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
615 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
616 // items are selected by the user, hence don't trigger the
617 // item specified by 'index'
621 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
627 // Prefer the local path over the URL. This assures that the
628 // volume space information is correct. Assuming that the URL is media:/sda1,
629 // and the local path is /windows/C: For the URL the space info is related
630 // to the root partition (and hence wrong) and for the local path the space
631 // info is related to the windows partition (-> correct).
632 const QString
localPath(item
->localPath());
633 if (localPath
.isEmpty()) {
637 setUrl(KUrl(localPath
));
645 void DolphinView::updateProgress(int percent
)
647 if (m_showProgress
) {
648 m_statusBar
->setProgress(percent
);
652 void DolphinView::updateItemCount()
654 if (m_showProgress
) {
655 m_statusBar
->setProgressText(QString::null
);
656 m_statusBar
->setProgress(100);
657 m_showProgress
= false;
660 KFileItemList
items(m_dirLister
->items());
661 KFileItemList::const_iterator it
= items
.begin();
662 const KFileItemList::const_iterator end
= items
.end();
668 KFileItem
* item
= *it
;
680 QTimer::singleShot(0, this, SLOT(restoreContentsPos()));
683 void DolphinView::restoreContentsPos()
686 const QLinkedList
<UrlNavigator::HistoryElem
> history
= urlHistory(index
);
687 if (!history
.isEmpty()) {
688 QAbstractItemView
* view
= itemView();
689 // TODO: view->setCurrentItem(history[index].currentFileName());
691 QLinkedList
<UrlNavigator::HistoryElem
>::const_iterator it
= history
.begin();
693 view
->horizontalScrollBar()->setValue((*it
).contentsX());
694 view
->verticalScrollBar()->setValue((*it
).contentsY());
698 void DolphinView::showInfoMessage(const QString
& msg
)
700 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
703 void DolphinView::showErrorMessage(const QString
& msg
)
705 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
708 void DolphinView::emitSelectionChangedSignal()
710 emit
selectionChanged();
713 void DolphinView::closeFilterBar()
716 emit
showFilterBarChanged(false);
719 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
721 if (!url
.isValid()) {
722 const QString
location(url
.pathOrUrl());
723 if (location
.isEmpty()) {
724 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
727 m_statusBar
->setMessage(i18n("The location '%1' is invalid.",location
),
728 DolphinStatusBar::Error
);
733 // Only show the directory loading progress if the status bar does
734 // not contain another progress information. This means that
735 // the directory loading progress information has the lowest priority.
736 const QString
progressText(m_statusBar
->progressText());
737 m_showProgress
= progressText
.isEmpty() ||
738 (progressText
== i18n("Loading directory..."));
739 if (m_showProgress
) {
740 m_statusBar
->setProgressText(i18n("Loading directory..."));
741 m_statusBar
->setProgress(0);
745 m_dirLister
->openUrl(url
, false, reload
);
748 QString
DolphinView::defaultStatusBarText() const
750 return KIO::itemsSummaryString(m_fileCount
+ m_folderCount
,
756 QString
DolphinView::selectionStatusBarText() const
759 const KFileItemList list
= selectedItems();
760 if (list
.isEmpty()) {
761 // when an item is triggered, it is temporary selected but selectedItems()
762 // will return an empty list
768 KIO::filesize_t byteSize
= 0;
769 KFileItemList::const_iterator it
= list
.begin();
770 const KFileItemList::const_iterator end
= list
.end();
772 KFileItem
* item
= *it
;
778 byteSize
+= item
->size();
783 if (folderCount
> 0) {
784 text
= i18np("1 Folder selected", "%1 Folders selected", folderCount
);
791 const QString
sizeText(KIO::convertSize(byteSize
));
792 text
+= i18np("1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
);
798 void DolphinView::showFilterBar(bool show
)
800 assert(m_filterBar
!= 0);
809 void DolphinView::updateStatusBar()
811 // As the item count information is less important
812 // in comparison with other messages, it should only
814 // - the status bar is empty or
815 // - shows already the item count information or
816 // - shows only a not very important information
817 // - if any progress is given don't show the item count info at all
818 const QString
msg(m_statusBar
->message());
819 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
820 (msg
== m_statusBar
->defaultText()) ||
821 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
822 (m_statusBar
->progress() == 100);
824 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
825 m_statusBar
->setDefaultText(text
);
827 if (updateStatusBarMsg
) {
828 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
832 void DolphinView::changeNameFilter(const QString
& nameFilter
)
834 // The name filter of KDirLister does a 'hard' filtering, which
835 // means that only the items are shown where the names match
836 // exactly the filter. This is non-transparent for the user, which
837 // just wants to have a 'soft' filtering: does the name contain
838 // the filter string?
839 QString
adjustedFilter(nameFilter
);
840 adjustedFilter
.insert(0, '*');
841 adjustedFilter
.append('*');
843 // Use the ProxyModel to filter:
844 // This code is #ifdefed as setNameFilter behaves
845 // slightly different than the QSortFilterProxyModel
846 // as it will not remove directories. I will ask
847 // our beloved usability experts for input
850 m_dirLister
->setNameFilter(adjustedFilter
);
851 m_dirLister
->emitChanges();
853 m_proxyModel
->setFilterRegExp( nameFilter
);
857 void DolphinView::createView()
859 // delete current view
860 QAbstractItemView
* view
= itemView();
862 m_topLayout
->removeWidget(view
);
869 assert(m_iconsView
== 0);
870 assert(m_detailsView
== 0);
872 // ... and recreate it representing the current mode
875 m_iconsView
= new DolphinIconsView(this);
876 m_iconsView
->setViewMode(QListView::IconMode
);
877 m_iconsView
->setSpacing(32);
879 // TODO: read out view settings
883 m_detailsView
= new DolphinDetailsView(this);
884 view
= m_detailsView
;
885 // TODO: read out view settings
889 view
->setModel(m_proxyModel
);
891 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
893 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
894 delegate
->setAdditionalInformation(KFileItemDelegate::FriendlyMimeType
);
895 view
->setItemDelegate(delegate
);
897 new KMimeTypeResolver(view
, m_dirModel
);
899 connect(view
, SIGNAL(clicked(const QModelIndex
&)),
900 this, SLOT(triggerItem(const QModelIndex
&)));
901 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
902 this, SLOT(emitSelectionChangedSignal()));
904 m_topLayout
->insertWidget(1, view
);
907 int DolphinView::columnIndex(Sorting sorting
) const
911 case SortByName
: index
= KDirModel::Name
; break;
912 case SortBySize
: index
= KDirModel::Size
; break;
913 case SortByDate
: index
= KDirModel::ModifiedTime
; break;
914 default: assert(false);
919 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
921 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
922 const QAbstractItemModel
* itemModel
= selectionModel
->model();
924 const QModelIndex topLeft
= itemModel
->index(0, 0);
925 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
926 itemModel
->columnCount() - 1);
928 QItemSelection
selection(topLeft
, bottomRight
);
929 selectionModel
->select(selection
, flags
);
932 QAbstractItemView
* DolphinView::itemView() const
934 assert((m_iconsView
== 0) || (m_detailsView
== 0));
935 if (m_detailsView
!= 0) {
936 return m_detailsView
;
941 #include "dolphinview.moc"