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"
26 #include <QItemSelectionModel>
27 #include <QMouseEvent>
28 #include <QVBoxLayout>
30 #include <kdirmodel.h>
31 #include <kfileitemdelegate.h>
33 #include <kio/netaccess.h>
34 #include <kio/renamedialog.h>
35 #include <kmimetyperesolver.h>
36 #include <konq_operations.h>
39 #include "dolphinstatusbar.h"
40 #include "dolphinmainwindow.h"
41 #include "dolphindirlister.h"
42 #include "dolphinsortfilterproxymodel.h"
43 #include "dolphindetailsview.h"
44 #include "dolphiniconsview.h"
45 #include "dolphincontextmenu.h"
46 #include "filterbar.h"
47 #include "renamedialog.h"
48 #include "urlnavigator.h"
49 #include "viewproperties.h"
51 DolphinView::DolphinView(DolphinMainWindow
* mainWindow
,
55 bool showHiddenFiles
) :
57 m_showProgress(false),
62 m_mainWindow(mainWindow
),
74 setFocusPolicy(Qt::StrongFocus
);
75 m_topLayout
= new QVBoxLayout(this);
76 m_topLayout
->setSpacing(0);
77 m_topLayout
->setMargin(0);
79 m_urlNavigator
= new UrlNavigator(url
, this);
80 connect(m_urlNavigator
, SIGNAL(urlChanged(const KUrl
&)),
81 this, SLOT(loadDirectory(const KUrl
&)));
83 m_statusBar
= new DolphinStatusBar(this);
85 m_dirLister
= new DolphinDirLister();
86 m_dirLister
->setAutoUpdate(true);
87 m_dirLister
->setMainWindow(this);
88 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
89 m_dirLister
->setDelayedMimeTypes(true);
91 connect(m_dirLister
, SIGNAL(clear()),
92 this, SLOT(updateStatusBar()));
93 connect(m_dirLister
, SIGNAL(percent(int)),
94 this, SLOT(updateProgress(int)));
95 connect(m_dirLister
, SIGNAL(deleteItem(KFileItem
*)),
96 this, SLOT(updateStatusBar()));
97 connect(m_dirLister
, SIGNAL(completed()),
98 this, SLOT(updateItemCount()));
99 connect(m_dirLister
, SIGNAL(infoMessage(const QString
&)),
100 this, SLOT(showInfoMessage(const QString
&)));
101 connect(m_dirLister
, SIGNAL(errorMessage(const QString
&)),
102 this, SLOT(showErrorMessage(const QString
&)));
104 m_dirModel
= new KDirModel();
105 m_dirModel
->setDirLister(m_dirLister
);
106 m_dirModel
->setDropsAllowed(KDirModel::DropOnDirectory
);
108 m_proxyModel
= new DolphinSortFilterProxyModel(this);
109 m_proxyModel
->setSourceModel(m_dirModel
);
113 m_iconSize
= K3Icon::SizeMedium
;
115 m_filterBar
= new FilterBar(this);
117 connect(m_filterBar
, SIGNAL(filterChanged(const QString
&)),
118 this, SLOT(changeNameFilter(const QString
&)));
119 connect(m_filterBar
, SIGNAL(closed()),
120 this, SLOT(closeFilterBar()));
122 m_topLayout
->addWidget(m_urlNavigator
);
123 m_topLayout
->addWidget(itemView());
124 m_topLayout
->addWidget(m_filterBar
);
125 m_topLayout
->addWidget(m_statusBar
);
127 loadDirectory(m_urlNavigator
->url());
130 DolphinView::~DolphinView()
136 void DolphinView::setUrl(const KUrl
& url
)
138 m_urlNavigator
->setUrl(url
);
141 const KUrl
& DolphinView::url() const
143 return m_urlNavigator
->url();
146 void DolphinView::requestActivation()
148 mainWindow()->setActiveView(this);
151 bool DolphinView::isActive() const
153 return (mainWindow()->activeView() == this);
156 void DolphinView::setMode(Mode mode
)
158 if (mode
== m_mode
) {
159 return; // the wished mode is already set
164 ViewProperties
props(m_urlNavigator
->url());
165 props
.setViewMode(m_mode
);
168 startDirLister(m_urlNavigator
->url());
173 DolphinView::Mode
DolphinView::mode() const
178 void DolphinView::setShowPreview(bool show
)
180 ViewProperties
props(m_urlNavigator
->url());
181 props
.setShowPreview(show
);
183 // TODO: wait until previews are possible with KFileItemDelegate
185 emit
showPreviewChanged();
188 bool DolphinView::showPreview() const
190 // TODO: wait until previews are possible with KFileItemDelegate
194 void DolphinView::setShowHiddenFiles(bool show
)
196 if (m_dirLister
->showingDotFiles() == show
) {
200 ViewProperties
props(m_urlNavigator
->url());
201 props
.setShowHiddenFiles(show
);
204 m_dirLister
->setShowingDotFiles(show
);
206 emit
showHiddenFilesChanged();
211 bool DolphinView::showHiddenFiles() const
213 return m_dirLister
->showingDotFiles();
216 void DolphinView::renameSelectedItems()
218 const KUrl::List urls
= selectedUrls();
219 if (urls
.count() > 1) {
220 // More than one item has been selected for renaming. Open
221 // a rename dialog and rename all items afterwards.
222 RenameDialog
dialog(urls
);
223 if (dialog
.exec() == QDialog::Rejected
) {
227 DolphinView
* view
= mainWindow()->activeView();
228 const QString
& newName
= dialog
.newName();
229 if (newName
.isEmpty()) {
230 view
->statusBar()->setMessage(i18n("The new item name is invalid."),
231 DolphinStatusBar::Error
);
234 // TODO: check how this can be integrated into KonqUndoManager/KonqOperations
236 //UndoManager& undoMan = UndoManager::instance();
237 //undoMan.beginMacro();
239 assert(newName
.contains('#'));
241 const int urlsCount
= urls
.count();
243 // iterate through all selected items and rename them...
244 const int replaceIndex
= newName
.indexOf('#');
245 assert(replaceIndex
>= 0);
246 for (int i
= 0; i
< urlsCount
; ++i
) {
247 const KUrl
& source
= urls
[i
];
249 number
.setNum(i
+ 1);
251 QString
name(newName
);
252 name
.replace(replaceIndex
, 1, number
);
254 if (source
.fileName() != name
) {
255 KUrl
dest(source
.upUrl());
258 const bool destExists
= KIO::NetAccess::exists(dest
, false, view
);
260 view
->statusBar()->setMessage(i18n("Renaming failed (item '%1' already exists).",name
),
261 DolphinStatusBar::Error
);
264 else if (KIO::NetAccess::file_move(source
, dest
)) {
265 // TODO: From the users point of view he executed one 'rename n files' operation,
266 // but internally we store it as n 'rename 1 file' operations for the undo mechanism.
267 //DolphinCommand command(DolphinCommand::Rename, source, dest);
268 //undoMan.addCommand(command);
273 //undoMan.endMacro();
277 // Only one item has been selected for renaming. Use the custom
278 // renaming mechanism from the views.
279 assert(urls
.count() == 1);
281 /*if (m_mode == DetailsView) {
282 Q3ListViewItem* item = m_iconsView->firstChild();
284 if (item->isSelected()) {
285 m_iconsView->rename(item, DolphinDetailsView::NameColumn);
288 item = item->nextSibling();
292 KFileIconViewItem* item = static_cast<KFileIconViewItem*>(m_iconsView->firstItem());
294 if (item->isSelected()) {
298 item = static_cast<KFileIconViewItem*>(item->nextItem());
304 void DolphinView::selectAll()
306 selectAll(QItemSelectionModel::Select
);
309 void DolphinView::invertSelection()
311 selectAll(QItemSelectionModel::Toggle
);
314 DolphinStatusBar
* DolphinView::statusBar() const
319 int DolphinView::contentsX() const
322 return 0; //scrollView()->contentsX();
325 int DolphinView::contentsY() const
327 return 0; //scrollView()->contentsY();
330 void DolphinView::refreshSettings()
332 startDirLister(m_urlNavigator
->url());
335 void DolphinView::emitRequestItemInfo(const KUrl
& url
)
337 emit
requestItemInfo(url
);
340 bool DolphinView::isFilterBarVisible() const
342 return m_filterBar
->isVisible();
345 bool DolphinView::isUrlEditable() const
347 return m_urlNavigator
->isUrlEditable();
350 void DolphinView::zoomIn()
352 //itemEffectsManager()->zoomIn();
355 void DolphinView::zoomOut()
357 //itemEffectsManager()->zoomOut();
360 bool DolphinView::isZoomInPossible() const
362 return false; //itemEffectsManager()->isZoomInPossible();
365 bool DolphinView::isZoomOutPossible() const
367 return false; //itemEffectsManager()->isZoomOutPossible();
370 void DolphinView::setSorting(Sorting sorting
)
372 if (sorting
!= this->sorting()) {
373 ViewProperties
props(url());
374 props
.setSorting(sorting
);
376 m_proxyModel
->setSorting(sorting
);
378 emit
sortingChanged(sorting
);
382 DolphinView::Sorting
DolphinView::sorting() const
384 return m_proxyModel
->sorting();
387 void DolphinView::setSortOrder(Qt::SortOrder order
)
389 if (sortOrder() != order
) {
390 ViewProperties
props(url());
391 props
.setSortOrder(order
);
393 m_proxyModel
->setSortOrder(order
);
395 emit
sortOrderChanged(order
);
399 Qt::SortOrder
DolphinView::sortOrder() const
401 return m_proxyModel
->sortOrder();
404 void DolphinView::goBack()
406 m_urlNavigator
->goBack();
409 void DolphinView::goForward()
411 m_urlNavigator
->goForward();
414 void DolphinView::goUp()
416 m_urlNavigator
->goUp();
419 void DolphinView::goHome()
421 m_urlNavigator
->goHome();
424 void DolphinView::setUrlEditable(bool editable
)
426 m_urlNavigator
->editUrl(editable
);
429 const QLinkedList
<UrlNavigator::HistoryElem
> DolphinView::urlHistory(int& index
) const
431 return m_urlNavigator
->history(index
);
434 bool DolphinView::hasSelection() const
436 return itemView()->selectionModel()->hasSelection();
439 KFileItemList
DolphinView::selectedItems() const
441 const QAbstractItemView
* view
= itemView();
443 // Our view has a selection, we will map them back to the DirModel
444 // and then fill the KFileItemList.
445 assert((view
!= 0) && (view
->selectionModel() != 0));
447 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
448 KFileItemList itemList
;
450 const QModelIndexList indexList
= selection
.indexes();
451 QModelIndexList::const_iterator end
= indexList
.end();
452 for (QModelIndexList::const_iterator it
= indexList
.begin(); it
!= end
; ++it
) {
453 assert((*it
).isValid());
455 KFileItem
* item
= m_dirModel
->itemForIndex(*it
);
457 itemList
.append(item
);
464 KUrl::List
DolphinView::selectedUrls() const
468 const KFileItemList list
= selectedItems();
469 KFileItemList::const_iterator it
= list
.begin();
470 const KFileItemList::const_iterator end
= list
.end();
472 KFileItem
* item
= *it
;
473 urls
.append(item
->url());
480 KFileItem
* DolphinView::fileItem(const QModelIndex index
) const
482 const QModelIndex dirModelIndex
= m_proxyModel
->mapToSource(index
);
483 return m_dirModel
->itemForIndex(dirModelIndex
);
486 void DolphinView::openContextMenu(KFileItem
* fileInfo
, const QPoint
& pos
)
488 DolphinContextMenu
contextMenu(this, fileInfo
, pos
);
492 void DolphinView::rename(const KUrl
& source
, const QString
& newName
)
496 if (newName
.isEmpty() || (source
.fileName() == newName
)) {
500 KUrl
dest(source
.upUrl());
501 dest
.addPath(newName
);
503 const bool destExists
= KIO::NetAccess::exists(dest
,
505 mainWindow()->activeView());
507 // the destination already exists, hence ask the user
509 KIO::RenameDialog
renameDialog(this,
510 i18n("File Already Exists"),
514 switch (renameDialog
.exec()) {
515 case KIO::R_OVERWRITE
:
516 // the destination should be overwritten
517 ok
= KIO::NetAccess::file_move(source
, dest
, -1, true);
520 case KIO::R_RENAME
: {
521 // a new name for the destination has been used
522 KUrl
newDest(renameDialog
.newDestUrl());
523 ok
= KIO::NetAccess::file_move(source
, newDest
);
528 // the renaming operation has been canceled
534 // no destination exists, hence just move the file to
536 ok
= KIO::NetAccess::file_move(source
, dest
);
539 const QString destFileName
= dest
.fileName();
541 m_statusBar
->setMessage(i18n("Renamed file '%1' to '%2'.",source
.fileName(), destFileName
),
542 DolphinStatusBar::OperationCompleted
);
544 KonqOperations::rename(this, source
, destFileName
);
547 m_statusBar
->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source
.fileName(), destFileName
),
548 DolphinStatusBar::Error
);
553 void DolphinView::reload()
555 startDirLister(m_urlNavigator
->url(), true);
558 void DolphinView::declareViewActive()
560 mainWindow()->setActiveView( this );
563 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
565 QWidget::mouseReleaseEvent(event
);
566 mainWindow()->setActiveView(this);
569 DolphinMainWindow
* DolphinView::mainWindow() const
574 void DolphinView::loadDirectory(const KUrl
& url
)
576 const ViewProperties
props(url
);
578 const Mode mode
= props
.viewMode();
579 if (m_mode
!= mode
) {
585 const bool showHiddenFiles
= props
.showHiddenFiles();
586 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
587 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
588 emit
showHiddenFilesChanged();
591 const DolphinView::Sorting sorting
= props
.sorting();
592 if (sorting
!= m_proxyModel
->sorting()) {
593 m_proxyModel
->setSorting(sorting
);
594 emit
sortingChanged(sorting
);
597 const Qt::SortOrder sortOrder
= props
.sortOrder();
598 if (sortOrder
!= m_proxyModel
->sortOrder()) {
599 m_proxyModel
->setSortOrder(sortOrder
);
600 emit
sortOrderChanged(sortOrder
);
603 // TODO: handle previews (props.showPreview())
606 emit
urlChanged(url
);
608 m_statusBar
->clear();
611 void DolphinView::triggerItem(const QModelIndex
& index
)
613 KFileItem
* item
= m_dirModel
->itemForIndex(m_proxyModel
->mapToSource(index
));
619 // Prefer the local path over the Url. This assures that the
620 // volume space information is correct. Assuming that the Url is media:/sda1,
621 // and the local path is /windows/C: For the Url the space info is related
622 // to the root partition (and hence wrong) and for the local path the space
623 // info is related to the windows partition (-> correct).
624 const QString
localPath(item
->localPath());
625 if (localPath
.isEmpty()) {
629 setUrl(KUrl(localPath
));
637 void DolphinView::updateProgress(int percent
)
639 if (m_showProgress
) {
640 m_statusBar
->setProgress(percent
);
644 void DolphinView::updateItemCount()
646 if (m_showProgress
) {
647 m_statusBar
->setProgressText(QString::null
);
648 m_statusBar
->setProgress(100);
649 m_showProgress
= false;
652 KFileItemList
items(m_dirLister
->items());
653 KFileItemList::const_iterator it
= items
.begin();
654 const KFileItemList::const_iterator end
= items
.end();
660 KFileItem
* item
= *it
;
673 void DolphinView::showInfoMessage(const QString
& msg
)
675 m_statusBar
->setMessage(msg
, DolphinStatusBar::Information
);
678 void DolphinView::showErrorMessage(const QString
& msg
)
680 m_statusBar
->setMessage(msg
, DolphinStatusBar::Error
);
683 void DolphinView::emitSelectionChangedSignal()
685 emit
selectionChanged();
688 void DolphinView::closeFilterBar()
691 emit
showFilterBarChanged(false);
694 void DolphinView::startDirLister(const KUrl
& url
, bool reload
)
696 if (!url
.isValid()) {
697 const QString
location(url
.pathOrUrl());
698 if (location
.isEmpty()) {
699 m_statusBar
->setMessage(i18n("The location is empty."), DolphinStatusBar::Error
);
702 m_statusBar
->setMessage(i18n("The location '%1' is invalid.",location
),
703 DolphinStatusBar::Error
);
708 // Only show the directory loading progress if the status bar does
709 // not contain another progress information. This means that
710 // the directory loading progress information has the lowest priority.
711 const QString
progressText(m_statusBar
->progressText());
712 m_showProgress
= progressText
.isEmpty() ||
713 (progressText
== i18n("Loading directory..."));
714 if (m_showProgress
) {
715 m_statusBar
->setProgressText(i18n("Loading directory..."));
716 m_statusBar
->setProgress(0);
720 m_dirLister
->openUrl(url
, false, reload
);
723 QString
DolphinView::defaultStatusBarText() const
725 // TODO: the following code is not suitable for languages where multiple forms
726 // of plurals are given (e. g. in Poland three forms of plurals exist).
727 const int itemCount
= m_folderCount
+ m_fileCount
;
730 if (itemCount
== 1) {
731 text
= i18n("1 Item");
734 text
= i18n("%1 Items",itemCount
);
739 if (m_folderCount
== 1) {
740 text
+= i18n("1 Folder");
743 text
+= i18n("%1 Folders",m_folderCount
);
748 if (m_fileCount
== 1) {
749 text
+= i18n("1 File");
752 text
+= i18n("%1 Files",m_fileCount
);
760 QString
DolphinView::selectionStatusBarText() const
762 // TODO: the following code is not suitable for languages where multiple forms
763 // of plurals are given (e. g. in Poland three forms of plurals exist).
765 const KFileItemList list
= selectedItems();
766 if (list
.isEmpty()) {
767 // TODO: assert(!list.isEmpty()) should be used, as this method is only invoked if
768 // DolphinView::hasSelection() is true. Inconsistent behavior?
774 KIO::filesize_t byteSize
= 0;
775 KFileItemList::const_iterator it
= list
.begin();
776 const KFileItemList::const_iterator end
= list
.end();
778 KFileItem
* item
= *it
;
784 byteSize
+= item
->size();
789 if (folderCount
== 1) {
790 text
= i18n("1 Folder selected");
792 else if (folderCount
> 1) {
793 text
= i18n("%1 Folders selected",folderCount
);
796 if ((fileCount
> 0) && (folderCount
> 0)) {
800 const QString
sizeText(KIO::convertSize(byteSize
));
801 if (fileCount
== 1) {
802 text
+= i18n("1 File selected (%1)",sizeText
);
804 else if (fileCount
> 1) {
805 text
+= i18n("%1 Files selected (%1)",fileCount
,sizeText
);
811 void DolphinView::showFilterBar(bool show
)
813 assert(m_filterBar
!= 0);
822 void DolphinView::updateStatusBar()
824 // As the item count information is less important
825 // in comparison with other messages, it should only
827 // - the status bar is empty or
828 // - shows already the item count information or
829 // - shows only a not very important information
830 // - if any progress is given don't show the item count info at all
831 const QString
msg(m_statusBar
->message());
832 const bool updateStatusBarMsg
= (msg
.isEmpty() ||
833 (msg
== m_statusBar
->defaultText()) ||
834 (m_statusBar
->type() == DolphinStatusBar::Information
)) &&
835 (m_statusBar
->progress() == 100);
837 const QString
text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
838 m_statusBar
->setDefaultText(text
);
840 if (updateStatusBarMsg
) {
841 m_statusBar
->setMessage(text
, DolphinStatusBar::Default
);
845 void DolphinView::changeNameFilter(const QString
& nameFilter
)
847 // The name filter of KDirLister does a 'hard' filtering, which
848 // means that only the items are shown where the names match
849 // exactly the filter. This is non-transparent for the user, which
850 // just wants to have a 'soft' filtering: does the name contain
851 // the filter string?
852 QString
adjustedFilter(nameFilter
);
853 adjustedFilter
.insert(0, '*');
854 adjustedFilter
.append('*');
856 // Use the ProxyModel to filter:
857 // This code is #ifdefed as setNameFilter behaves
858 // slightly different than the QSortFilterProxyModel
859 // as it will not remove directories. I will ask
860 // our beloved usability experts for input
863 m_dirLister
->setNameFilter(adjustedFilter
);
864 m_dirLister
->emitChanges();
866 m_proxyModel
->setFilterRegExp( nameFilter
);
870 void DolphinView::createView()
872 // delete current view
873 QAbstractItemView
* view
= itemView();
875 m_topLayout
->removeWidget(view
);
882 assert(m_iconsView
== 0);
883 assert(m_detailsView
== 0);
885 // ... and recreate it representing the current mode
888 m_iconsView
= new DolphinIconsView(this);
889 m_iconsView
->setViewMode(QListView::IconMode
);
890 m_iconsView
->setSpacing(32);
892 // TODO: read out view settings
896 m_detailsView
= new DolphinDetailsView(this);
897 view
= m_detailsView
;
898 // TODO: read out view settings
902 view
->setModel(m_proxyModel
);
904 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
906 KFileItemDelegate
* delegate
= new KFileItemDelegate(this);
907 delegate
->setAdditionalInformation(KFileItemDelegate::FriendlyMimeType
);
908 view
->setItemDelegate(delegate
);
910 new KMimeTypeResolver(view
, m_dirModel
);
912 connect(view
, SIGNAL(clicked(const QModelIndex
&)),
913 this, SLOT(triggerItem(const QModelIndex
&)));
914 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
915 this, SLOT(emitSelectionChangedSignal()));
917 m_topLayout
->insertWidget(1, view
);
920 int DolphinView::columnIndex(Sorting sorting
) const
924 case SortByName
: index
= KDirModel::Name
; break;
925 case SortBySize
: index
= KDirModel::Size
; break;
926 case SortByDate
: index
= KDirModel::ModifiedTime
; break;
927 default: assert(false);
932 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags
)
934 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
935 const QAbstractItemModel
* itemModel
= selectionModel
->model();
937 const QModelIndex topLeft
= itemModel
->index(0, 0);
938 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
939 itemModel
->columnCount() - 1);
941 QItemSelection
selection(topLeft
, bottomRight
);
942 selectionModel
->select(selection
, flags
);
945 QAbstractItemView
* DolphinView::itemView() const
947 assert((m_iconsView
== 0) || (m_detailsView
== 0));
948 if (m_detailsView
!= 0) {
949 return m_detailsView
;
954 #include "dolphinview.moc"