]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Provide KVersionControlPlugin version 2
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QAbstractItemView>
24 #include <QApplication>
25 #include <QBoxLayout>
26 #include <QClipboard>
27 #include <QDropEvent>
28 #include <QGraphicsSceneDragDropEvent>
29 #include <QKeyEvent>
30 #include <QItemSelection>
31 #include <QTimer>
32 #include <QScrollBar>
33
34 #include <KActionCollection>
35 #include <KColorScheme>
36 #include <KDirLister>
37 #include <KDirModel>
38 #include <KIconEffect>
39 #include <KFileItem>
40 #include <KFileItemListProperties>
41 #include <KLocale>
42 #include <kitemviews/kfileitemmodel.h>
43 #include <kitemviews/kfileitemlistview.h>
44 #include <kitemviews/kitemlistselectionmanager.h>
45 #include <kitemviews/kitemlistview.h>
46 #include <kitemviews/kitemlistcontroller.h>
47 #include <KIO/DeleteJob>
48 #include <KIO/NetAccess>
49 #include <KIO/PreviewJob>
50 #include <KJob>
51 #include <KMenu>
52 #include <KMessageBox>
53 #include <konq_fileitemcapabilities.h>
54 #include <konq_operations.h>
55 #include <konqmimedata.h>
56 #include <KToggleAction>
57 #include <KUrl>
58
59 #include "additionalinfoaccessor.h"
60 #include "dolphindirlister.h"
61 #include "dolphinnewfilemenuobserver.h"
62 #include "dolphin_detailsmodesettings.h"
63 #include "dolphin_generalsettings.h"
64 #include "dolphinitemlistcontainer.h"
65 #include "draganddrophelper.h"
66 #include "renamedialog.h"
67 #include "settings/dolphinsettings.h"
68 #include "versioncontrol/versioncontrolobserver.h"
69 #include "viewmodecontroller.h"
70 #include "viewproperties.h"
71 #include "views/tooltips/tooltipmanager.h"
72 #include "zoomlevelinfo.h"
73
74 namespace {
75 const int MaxModeEnum = DolphinView::CompactView;
76 const int MaxSortingEnum = DolphinView::SortByPath;
77 };
78
79 DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
80 QWidget(parent),
81 m_active(true),
82 m_tabsForFiles(false),
83 m_assureVisibleCurrentIndex(false),
84 m_isFolderWritable(true),
85 m_url(url),
86 m_mode(DolphinView::IconsView),
87 m_additionalInfoList(),
88 m_topLayout(0),
89 m_dirLister(0),
90 m_container(0),
91 m_toolTipManager(0),
92 m_selectionChangedTimer(0),
93 m_currentItemIndex(-1),
94 m_restoredContentsPosition(),
95 m_createdItemUrl(),
96 m_selectedItems(),
97 m_versionControlObserver(0)
98 {
99 m_topLayout = new QVBoxLayout(this);
100 m_topLayout->setSpacing(0);
101 m_topLayout->setMargin(0);
102
103 //m_dolphinViewController = new DolphinViewController(this);
104
105 //m_viewModeController = new ViewModeController(this);
106 //m_viewModeController->setUrl(url);
107
108 /*connect(m_viewModeController, SIGNAL(urlChanged(KUrl)),
109 this, SIGNAL(urlChanged(KUrl)));
110
111 connect(m_dolphinViewController, SIGNAL(requestContextMenu(QPoint,QList<QAction*>)),
112 this, SLOT(openContextMenu(QPoint,QList<QAction*>)));
113 connect(m_dolphinViewController, SIGNAL(urlsDropped(KFileItem,KUrl,QDropEvent*)),
114 this, SLOT(dropUrls(KFileItem,KUrl,QDropEvent*)));
115 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
116 this, SLOT(updateSorting(DolphinView::Sorting)));
117 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
118 this, SLOT(updateSortOrder(Qt::SortOrder)));
119 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
120 this, SLOT(updateSortFoldersFirst(bool)));
121 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(QList<DolphinView::AdditionalInfo>)),
122 this, SLOT(updateAdditionalInfo(QList<DolphinView::AdditionalInfo>)));*/
123 //connect(m_dolphinViewController, SIGNAL(itemActivated(KFileItem)),
124 // this, SLOT(triggerItem(KFileItem)));
125 //connect(m_dolphinViewController, SIGNAL(tabRequested(KUrl)),
126 // this, SIGNAL(tabRequested(KUrl)));
127 /*connect(m_dolphinViewController, SIGNAL(activated()),
128 this, SLOT(activate()));
129 connect(m_dolphinViewController, SIGNAL(itemEntered(KFileItem)),
130 this, SLOT(showHoverInformation(KFileItem)));
131 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
132 this, SLOT(clearHoverInformation()));
133 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
134 this, SLOT(slotUrlChangeRequested(KUrl)));*/
135
136 // When a new item has been created by the "Create New..." menu, the item should
137 // get selected and it must be assured that the item will get visible. As the
138 // creation is done asynchronously, several signals must be checked:
139 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl)),
140 this, SLOT(observeCreatedItem(KUrl)));
141
142 m_selectionChangedTimer = new QTimer(this);
143 m_selectionChangedTimer->setSingleShot(true);
144 m_selectionChangedTimer->setInterval(300);
145 connect(m_selectionChangedTimer, SIGNAL(timeout()),
146 this, SLOT(emitSelectionChangedSignal()));
147
148 m_dirLister = new DolphinDirLister(this);
149 m_dirLister->setAutoUpdate(true);
150 m_dirLister->setDelayedMimeTypes(true);
151
152 connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
153 connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl)));
154 connect(m_dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
155 connect(m_dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)),
156 this, SLOT(slotRefreshItems()));
157
158 connect(m_dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
159 connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged()));
160 connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
161 connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(infoMessage(QString)));
162 connect(m_dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
163 connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
164 connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SIGNAL(itemCountChanged()));
165
166 m_container = new DolphinItemListContainer(m_dirLister, this);
167 QHash<QByteArray, int> visibleRoles;
168 visibleRoles.insert("name", 0);
169 m_container->setVisibleRoles(visibleRoles);
170
171 KItemListController* controller = m_container->controller();
172 controller->setSelectionBehavior(KItemListController::MultiSelection);
173 connect(controller, SIGNAL(itemActivated(int)),
174 this, SLOT(slotItemActivated(int)));
175 connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
176 connect(controller, SIGNAL(contextMenuRequested(int,QPointF)), this, SLOT(slotContextMenuRequested(int,QPointF)));
177 connect(controller, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
178 connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
179 connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
180 connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
181 connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)), this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
182
183 KItemListSelectionManager* selectionManager = controller->selectionManager();
184 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
185 this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
186
187 m_toolTipManager = new ToolTipManager(this);
188
189 m_versionControlObserver = new VersionControlObserver(this);
190 m_versionControlObserver->setModel(fileItemModel());
191 connect(m_versionControlObserver, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
192 connect(m_versionControlObserver, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
193 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(QString)), this, SIGNAL(operationCompletedMessage(QString)));
194
195 applyViewProperties();
196 m_topLayout->addWidget(m_container);
197
198 loadDirectory(url);
199 }
200
201 DolphinView::~DolphinView()
202 {
203 }
204
205 KUrl DolphinView::url() const
206 {
207 return m_url;
208 }
209
210 void DolphinView::setActive(bool active)
211 {
212 if (active == m_active) {
213 return;
214 }
215
216 m_active = active;
217
218 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
219 if (!active) {
220 color.setAlpha(150);
221 }
222
223 /*QAbstractItemView* view = m_viewAccessor.itemView();
224 QWidget* viewport = view ? view->viewport() : 0;
225 if (viewport) {
226 QPalette palette;
227 palette.setColor(viewport->backgroundRole(), color);
228 viewport->setPalette(palette);
229 }*/
230
231 update();
232
233 if (active) {
234 //if (view) {
235 // view->setFocus();
236 //}
237 emit activated();
238 emit writeStateChanged(m_isFolderWritable);
239 }
240
241 //m_viewModeController->indicateActivationChange(active);
242 }
243
244 bool DolphinView::isActive() const
245 {
246 return m_active;
247 }
248
249 void DolphinView::setMode(Mode mode)
250 {
251 if (mode != m_mode) {
252 ViewProperties props(url());
253 props.setViewMode(mode);
254 props.save();
255
256 applyViewProperties();
257 }
258 }
259
260 DolphinView::Mode DolphinView::mode() const
261 {
262 return m_mode;
263 }
264
265 bool DolphinView::previewsShown() const
266 {
267 return m_container->previewsShown();
268 }
269
270 bool DolphinView::hiddenFilesShown() const
271 {
272 return m_dirLister->showingDotFiles();
273 }
274
275 bool DolphinView::categorizedSorting() const
276 {
277 return false; //m_storedCategorizedSorting;
278 }
279
280 KFileItemList DolphinView::items() const
281 {
282 return m_dirLister->items();
283 }
284
285 KFileItemList DolphinView::selectedItems() const
286 {
287 const KFileItemModel* model = fileItemModel();
288 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
289 const QSet<int> selectedIndexes = selectionManager->selectedItems();
290
291 KFileItemList selectedItems;
292 QSetIterator<int> it(selectedIndexes);
293 while (it.hasNext()) {
294 const int index = it.next();
295 selectedItems.append(model->fileItem(index));
296 }
297 return selectedItems;
298 }
299
300 int DolphinView::selectedItemsCount() const
301 {
302 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
303 return selectionManager->selectedItems().count();
304 }
305
306 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
307 {
308 foreach (const KUrl& url, urls) {
309 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
310 m_selectedItems.append(item);
311 }
312 }
313
314 void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled)
315 {
316 const KItemListSelectionManager::SelectionMode mode = enabled
317 ? KItemListSelectionManager::Select
318 : KItemListSelectionManager::Deselect;
319 const KFileItemModel* model = fileItemModel();
320 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
321
322 for (int index = 0; index < model->count(); index++) {
323 const KFileItem item = model->fileItem(index);
324 if (pattern.exactMatch(item.name())) {
325 // An alternative approach would be to store the matching items in a QSet<int> and
326 // select them in one go after the loop, but we'd need a new function
327 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
328 // for that.
329 selectionManager->setSelected(index, 1, mode);
330 }
331 }
332 }
333
334 void DolphinView::setZoomLevel(int level)
335 {
336 const int oldZoomLevel = zoomLevel();
337 m_container->setZoomLevel(level);
338 if (zoomLevel() != oldZoomLevel) {
339 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
340 }
341 }
342
343 int DolphinView::zoomLevel() const
344 {
345 return m_container->zoomLevel();
346 }
347
348 void DolphinView::setSorting(Sorting sorting)
349 {
350 if (sorting != this->sorting()) {
351 updateSorting(sorting);
352 }
353 }
354
355 DolphinView::Sorting DolphinView::sorting() const
356 {
357 return DolphinView::SortByName;
358 //return m_viewAccessor.proxyModel()->sorting();
359 }
360
361 void DolphinView::setSortOrder(Qt::SortOrder order)
362 {
363 if (sortOrder() != order) {
364 updateSortOrder(order);
365 }
366 }
367
368 Qt::SortOrder DolphinView::sortOrder() const
369 {
370 return Qt::AscendingOrder; // m_viewAccessor.proxyModel()->sortOrder();
371 }
372
373 void DolphinView::setSortFoldersFirst(bool foldersFirst)
374 {
375 if (sortFoldersFirst() != foldersFirst) {
376 updateSortFoldersFirst(foldersFirst);
377 }
378 }
379
380 bool DolphinView::sortFoldersFirst() const
381 {
382 return true; // m_viewAccessor.proxyModel()->sortFoldersFirst();
383 }
384
385 void DolphinView::setAdditionalInfoList(const QList<AdditionalInfo>& info)
386 {
387 const QList<AdditionalInfo> previousList = info;
388
389 ViewProperties props(url());
390 props.setAdditionalInfoList(info);
391
392 m_additionalInfoList = info;
393 applyAdditionalInfoListToView();
394
395 emit additionalInfoListChanged(m_additionalInfoList, previousList);
396 }
397
398 QList<DolphinView::AdditionalInfo> DolphinView::additionalInfoList() const
399 {
400 return m_additionalInfoList;
401 }
402
403 void DolphinView::reload()
404 {
405 QByteArray viewState;
406 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
407 saveState(saveStream);
408 m_selectedItems= selectedItems();
409
410 setUrl(url());
411 loadDirectory(url(), true);
412
413 QDataStream restoreStream(viewState);
414 restoreState(restoreStream);
415 }
416
417 void DolphinView::stopLoading()
418 {
419 m_dirLister->stop();
420 }
421
422 void DolphinView::refresh()
423 {
424 const bool oldActivationState = m_active;
425 const int oldZoomLevel = zoomLevel();
426 m_active = true;
427
428 applyViewProperties();
429 reload();
430
431 setActive(oldActivationState);
432 updateZoomLevel(oldZoomLevel);
433 }
434
435 void DolphinView::setNameFilter(const QString& nameFilter)
436 {
437 Q_UNUSED(nameFilter);
438 //m_viewModeController->setNameFilter(nameFilter);
439 }
440
441 QString DolphinView::nameFilter() const
442 {
443 return QString(); //m_viewModeController->nameFilter();
444 }
445
446 void DolphinView::calculateItemCount(int& fileCount,
447 int& folderCount,
448 KIO::filesize_t& totalFileSize) const
449 {
450 foreach (const KFileItem& item, m_dirLister->items()) {
451 if (item.isDir()) {
452 ++folderCount;
453 } else {
454 ++fileCount;
455 totalFileSize += item.size();
456 }
457 }
458 }
459
460 QString DolphinView::statusBarText() const
461 {
462 QString text;
463 int folderCount = 0;
464 int fileCount = 0;
465 KIO::filesize_t totalFileSize = 0;
466
467 if (hasSelection()) {
468 // give a summary of the status of the selected files
469 const KFileItemList list = selectedItems();
470 if (list.isEmpty()) {
471 // when an item is triggered, it is temporary selected but selectedItems()
472 // will return an empty list
473 return text;
474 }
475
476 KFileItemList::const_iterator it = list.begin();
477 const KFileItemList::const_iterator end = list.end();
478 while (it != end) {
479 const KFileItem& item = *it;
480 if (item.isDir()) {
481 ++folderCount;
482 } else {
483 ++fileCount;
484 totalFileSize += item.size();
485 }
486 ++it;
487 }
488
489 if (folderCount + fileCount == 1) {
490 // if only one item is selected, show the filename
491 const QString name = list.first().text();
492 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
493 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
494 name, KIO::convertSize(totalFileSize));
495 } else {
496 // at least 2 items are selected
497 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
498 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
499 if ((folderCount > 0) && (fileCount > 0)) {
500 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
501 foldersText, filesText, KIO::convertSize(totalFileSize));
502 } else if (fileCount > 0) {
503 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
504 } else {
505 Q_ASSERT(folderCount > 0);
506 text = foldersText;
507 }
508 }
509 } else {
510 calculateItemCount(fileCount, folderCount, totalFileSize);
511 text = KIO::itemsSummaryString(fileCount + folderCount,
512 fileCount, folderCount,
513 totalFileSize, true);
514 }
515
516 return text;
517 }
518
519 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
520 {
521 QList<QAction*> actions;
522
523 if (items.isEmpty()) {
524 const KFileItem item = fileItemModel()->rootItem();
525 actions = m_versionControlObserver->actions(KFileItemList() << item);
526 } else {
527 actions = m_versionControlObserver->actions(items);
528 }
529
530 return actions;
531 }
532
533 void DolphinView::setUrl(const KUrl& url)
534 {
535 if (url == m_url) {
536 return;
537 }
538
539 emit urlAboutToBeChanged(url);
540 m_url = url;
541
542 if (GeneralSettings::showToolTips()) {
543 m_toolTipManager->hideToolTip();
544 }
545
546 // It is important to clear the items from the model before
547 // applying the view properties, otherwise expensive operations
548 // might be done on the existing items although they get cleared
549 // anyhow afterwards by loadDirectory().
550 fileItemModel()->clear();
551 applyViewProperties();
552 loadDirectory(url);
553
554 emit urlChanged(url);
555 }
556
557 void DolphinView::selectAll()
558 {
559 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
560 selectionManager->setSelected(0, fileItemModel()->count());
561 }
562
563 void DolphinView::invertSelection()
564 {
565 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
566 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
567 }
568
569 void DolphinView::clearSelection()
570 {
571 m_container->controller()->selectionManager()->clearSelection();
572 }
573
574 void DolphinView::renameSelectedItems()
575 {
576 KFileItemList items = selectedItems();
577 const int itemCount = items.count();
578 if (itemCount < 1) {
579 return;
580 }
581
582 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
583 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
584 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
585 m_viewAccessor.itemView()->edit(proxyIndex);
586 } else {*/
587 RenameDialog* dialog = new RenameDialog(this, items);
588 dialog->setAttribute(Qt::WA_DeleteOnClose);
589 dialog->show();
590 dialog->raise();
591 dialog->activateWindow();
592 //}
593
594 // assure that the current index remains visible when KDirLister
595 // will notify the view about changed items
596 m_assureVisibleCurrentIndex = true;
597 }
598
599 void DolphinView::trashSelectedItems()
600 {
601 const KUrl::List list = simplifiedSelectedUrls();
602 KonqOperations::del(this, KonqOperations::TRASH, list);
603 }
604
605 void DolphinView::deleteSelectedItems()
606 {
607 const KUrl::List list = simplifiedSelectedUrls();
608 const bool del = KonqOperations::askDeleteConfirmation(list,
609 KonqOperations::DEL,
610 KonqOperations::DEFAULT_CONFIRMATION,
611 this);
612
613 if (del) {
614 KIO::Job* job = KIO::del(list);
615 connect(job, SIGNAL(result(KJob*)),
616 this, SLOT(slotDeleteFileFinished(KJob*)));
617 }
618 }
619
620 void DolphinView::cutSelectedItems()
621 {
622 QMimeData* mimeData = selectionMimeData();
623 KonqMimeData::addIsCutSelection(mimeData, true);
624 QApplication::clipboard()->setMimeData(mimeData);
625 }
626
627 void DolphinView::copySelectedItems()
628 {
629 QMimeData* mimeData = selectionMimeData();
630 QApplication::clipboard()->setMimeData(mimeData);
631 }
632
633 void DolphinView::paste()
634 {
635 pasteToUrl(url());
636 }
637
638 void DolphinView::pasteIntoFolder()
639 {
640 const KFileItemList items = selectedItems();
641 if ((items.count() == 1) && items.first().isDir()) {
642 pasteToUrl(items.first().url());
643 }
644 }
645
646 void DolphinView::setPreviewsShown(bool show)
647 {
648 if (previewsShown() == show) {
649 return;
650 }
651
652 ViewProperties props(url());
653 props.setPreviewsShown(show);
654
655 m_container->setPreviewsShown(show);
656 emit previewsShownChanged(show);
657 }
658
659 void DolphinView::setHiddenFilesShown(bool show)
660 {
661 if (m_dirLister->showingDotFiles() == show) {
662 return;
663 }
664
665 m_selectedItems = selectedItems();
666
667 ViewProperties props(url());
668 props.setHiddenFilesShown(show);
669
670 m_dirLister->setShowingDotFiles(show);
671 m_dirLister->emitChanges();
672 emit hiddenFilesShownChanged(show);
673 }
674
675 void DolphinView::setCategorizedSorting(bool categorized)
676 {
677 if (categorized == categorizedSorting()) {
678 return;
679 }
680
681 ViewProperties props(url());
682 props.setCategorizedSorting(categorized);
683 props.save();
684
685 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
686
687 emit categorizedSortingChanged(categorized);
688 }
689
690 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
691 {
692 QWidget::mouseReleaseEvent(event);
693 setActive(true);
694 }
695
696 void DolphinView::contextMenuEvent(QContextMenuEvent* event)
697 {
698 Q_UNUSED(event);
699
700 const QPoint pos = m_container->mapFromGlobal(QCursor::pos());
701 const KItemListView* view = m_container->controller()->view();
702 if (view->itemAt(pos) < 0) {
703 // Only open the context-menu if the cursor is above the viewport
704 // (the context-menu for items is handled in slotContextMenuRequested())
705 requestContextMenu(KFileItem(), url(), QList<QAction*>());
706 }
707 }
708
709 void DolphinView::wheelEvent(QWheelEvent* event)
710 {
711 if (event->modifiers().testFlag(Qt::ControlModifier)) {
712 const int numDegrees = event->delta() / 8;
713 const int numSteps = numDegrees / 15;
714
715 setZoomLevel(zoomLevel() + numSteps);
716 }
717 event->accept();
718 }
719
720 void DolphinView::activate()
721 {
722 setActive(true);
723 }
724
725 void DolphinView::slotItemActivated(int index)
726 {
727 Q_UNUSED(index);
728
729 const KFileItemList items = selectedItems();
730 if (items.isEmpty()) {
731 return;
732 }
733
734 if (items.count() == 1) {
735 emit itemActivated(items.at(0)); // caught by DolphinViewContainer or DolphinPart
736 } else {
737 foreach (const KFileItem& item, items) {
738 if (item.isDir()) {
739 emit tabRequested(item.url());
740 } else {
741 emit itemActivated(item);
742 }
743 }
744 }
745 }
746
747 void DolphinView::slotItemMiddleClicked(int index)
748 {
749 const KFileItem item = fileItemModel()->fileItem(index);
750 if (item.isDir() || isTabsForFilesEnabled()) {
751 emit tabRequested(item.url());
752 }
753 }
754
755 void DolphinView::slotContextMenuRequested(int index, const QPointF& pos)
756 {
757 Q_UNUSED(pos);
758 if (GeneralSettings::showToolTips()) {
759 m_toolTipManager->hideToolTip();
760 }
761 const KFileItem item = fileItemModel()->fileItem(index);
762 emit requestContextMenu(item, url(), QList<QAction*>());
763 }
764
765 void DolphinView::slotItemExpansionToggleClicked(int index)
766 {
767 // TODO: When doing a model->setExpanded(false) it should
768 // be checked here whether the current index is part of the
769 // closed sub-tree. If this is the case, the current index
770 // should be adjusted to the parent index.
771 KFileItemModel* model = fileItemModel();
772 const bool expanded = model->isExpanded(index);
773 model->setExpanded(index, !expanded);
774 }
775
776 void DolphinView::slotItemHovered(int index)
777 {
778 const KFileItem item = fileItemModel()->fileItem(index);
779
780 if (GeneralSettings::showToolTips()) {
781 QRectF itemRect = m_container->controller()->view()->itemBoundingRect(index);
782 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
783 itemRect.moveTo(pos);
784
785 m_toolTipManager->showToolTip(item, itemRect);
786 }
787
788 emit requestItemInfo(item);
789 }
790
791 void DolphinView::slotItemUnhovered(int index)
792 {
793 Q_UNUSED(index);
794 if (GeneralSettings::showToolTips()) {
795 m_toolTipManager->hideToolTip();
796 }
797 emit requestItemInfo(KFileItem());
798 }
799
800 void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
801 {
802 const KFileItem destItem = fileItemModel()->fileItem(index);
803
804 QDropEvent dropEvent(event->pos().toPoint(),
805 event->possibleActions(),
806 event->mimeData(),
807 event->buttons(),
808 event->modifiers());
809
810 DragAndDropHelper::dropUrls(destItem, url(), &dropEvent, this);
811 }
812
813 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
814 {
815 Q_UNUSED(previous);
816 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
817
818 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(current);
819 m_versionControlObserver->setModel(fileItemModel);
820 }
821
822 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
823 {
824 const int currentCount = current.count();
825 const int previousCount = previous.count();
826 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
827 (currentCount > 0 && previousCount == 0);
828
829 // If nothing has been selected before and something got selected (or if something
830 // was selected before and now nothing is selected) the selectionChangedSignal must
831 // be emitted asynchronously as fast as possible to update the edit-actions.
832 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
833 m_selectionChangedTimer->start();
834 }
835
836 void DolphinView::emitSelectionChangedSignal()
837 {
838 m_selectionChangedTimer->stop();
839 emit selectionChanged(selectedItems());
840 }
841
842 void DolphinView::openContextMenu(const QPoint& pos,
843 const QList<QAction*>& customActions)
844 {
845 KFileItem item;
846 const int index = m_container->controller()->view()->itemAt(pos);
847 if (index >= 0) {
848 item = fileItemModel()->fileItem(index);
849 }
850
851 emit requestContextMenu(item, url(), customActions);
852 }
853
854 void DolphinView::dropUrls(const KFileItem& destItem,
855 const KUrl& destPath,
856 QDropEvent* event)
857 {
858 Q_UNUSED(destItem);
859 Q_UNUSED(destPath);
860 markPastedUrlsAsSelected(event->mimeData());
861 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
862 }
863
864 void DolphinView::updateSorting(DolphinView::Sorting sorting)
865 {
866 ViewProperties props(url());
867 props.setSorting(sorting);
868
869 KItemModelBase* model = m_container->controller()->model();
870 model->setSortRole(sortRoleForSorting(sorting));
871
872 emit sortingChanged(sorting);
873 }
874
875 void DolphinView::updateSortOrder(Qt::SortOrder order)
876 {
877 ViewProperties props(url());
878 props.setSortOrder(order);
879
880 //m_viewAccessor.proxyModel()->setSortOrder(order);
881
882 emit sortOrderChanged(order);
883 }
884
885 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
886 {
887 ViewProperties props(url());
888 props.setSortFoldersFirst(foldersFirst);
889
890 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
891
892 emit sortFoldersFirstChanged(foldersFirst);
893 }
894
895 QPair<bool, QString> DolphinView::pasteInfo() const
896 {
897 return KonqOperations::pasteInfo(url());
898 }
899
900 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
901 {
902 m_tabsForFiles = tabsForFiles;
903 }
904
905 bool DolphinView::isTabsForFilesEnabled() const
906 {
907 return m_tabsForFiles;
908 }
909
910 bool DolphinView::itemsExpandable() const
911 {
912 return m_mode == DetailsView;
913 }
914
915 void DolphinView::restoreState(QDataStream& stream)
916 {
917 // Restore the current item that had the keyboard focus
918 stream >> m_currentItemIndex;
919
920 // Restore the view position
921 stream >> m_restoredContentsPosition;
922
923 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
924 QSet<KUrl> urlsToExpand;
925 stream >> urlsToExpand;
926 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
927 if (expander) {
928 m_expanderActive = true;
929 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
930 }
931 else {
932 m_expanderActive = false;
933 }*/
934 }
935
936 void DolphinView::saveState(QDataStream& stream)
937 {
938 // Save the current item that has the keyboard focus
939 stream << m_container->controller()->selectionManager()->currentItem();
940
941 // Save view position
942 const qreal x = m_container->horizontalScrollBar()->value();
943 const qreal y = m_container->verticalScrollBar()->value();
944 stream << QPoint(x, y);
945
946 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
947 //stream << m_viewAccessor.expandedUrls();
948 }
949
950 bool DolphinView::hasSelection() const
951 {
952 return m_container->controller()->selectionManager()->hasSelection();
953 }
954
955 KFileItem DolphinView::rootItem() const
956 {
957 return m_dirLister->rootItem();
958 }
959
960 void DolphinView::observeCreatedItem(const KUrl& url)
961 {
962 m_createdItemUrl = url;
963 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
964 // this, SLOT(selectAndScrollToCreatedItem()));
965 }
966
967 void DolphinView::selectAndScrollToCreatedItem()
968 {
969 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
970 if (dirIndex.isValid()) {
971 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
972 QAbstractItemView* view = m_viewAccessor.itemView();
973 if (view) {
974 view->setCurrentIndex(proxyIndex);
975 }
976 }
977
978 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
979 this, SLOT(selectAndScrollToCreatedItem()));*/
980 m_createdItemUrl = KUrl();
981 }
982
983 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
984 {
985 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
986 emit redirection(oldUrl, newUrl);
987 m_url = newUrl; // #186947
988 }
989 }
990
991 void DolphinView::updateViewState()
992 {
993 if (m_currentItemIndex >= 0) {
994 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
995 selectionManager->setCurrentItem(m_currentItemIndex);
996 m_currentItemIndex =-1;
997 }
998
999 if (!m_restoredContentsPosition.isNull()) {
1000 const int x = m_restoredContentsPosition.x();
1001 const int y = m_restoredContentsPosition.y();
1002 m_restoredContentsPosition = QPoint();
1003
1004 m_container->horizontalScrollBar()->setValue(x);
1005 m_container->verticalScrollBar()->setValue(y);
1006 }
1007
1008 if (!m_selectedItems.isEmpty()) {
1009 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1010 QSet<int> selectedItems = selectionManager->selectedItems();
1011 const KFileItemModel* model = fileItemModel();
1012
1013 foreach (const KFileItem& selectedItem, m_selectedItems) {
1014 const int index = model->index(selectedItem);
1015 if (index >= 0) {
1016 selectedItems.insert(index);
1017 }
1018 }
1019
1020 selectionManager->setSelectedItems(selectedItems);
1021 m_selectedItems.clear();
1022 }
1023 }
1024
1025 void DolphinView::showHoverInformation(const KFileItem& item)
1026 {
1027 emit requestItemInfo(item);
1028 }
1029
1030 void DolphinView::clearHoverInformation()
1031 {
1032 emit requestItemInfo(KFileItem());
1033 }
1034
1035 void DolphinView::slotDeleteFileFinished(KJob* job)
1036 {
1037 if (job->error() == 0) {
1038 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1039 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1040 emit errorMessage(job->errorString());
1041 }
1042 }
1043
1044 void DolphinView::slotDirListerStarted(const KUrl& url)
1045 {
1046 // Disable the writestate temporary until it can be determined in a fast way
1047 // in DolphinView::slotDirListerCompleted()
1048 if (m_isFolderWritable) {
1049 m_isFolderWritable = false;
1050 emit writeStateChanged(m_isFolderWritable);
1051 }
1052
1053 emit startedPathLoading(url);
1054 }
1055
1056 void DolphinView::slotDirListerCompleted()
1057 {
1058 // Update the view-state. This has to be done using a Qt::QueuedConnection
1059 // because the view might not be in its final state yet (the view also
1060 // listens to the completed()-signal from KDirLister and the order of
1061 // of slots is undefined).
1062 QTimer::singleShot(0, this, SLOT(updateViewState()));
1063
1064 emit finishedPathLoading(url());
1065
1066 updateWritableState();
1067 }
1068
1069 void DolphinView::slotRefreshItems()
1070 {
1071 if (m_assureVisibleCurrentIndex) {
1072 m_assureVisibleCurrentIndex = false;
1073 //QAbstractItemView* view = m_viewAccessor.itemView();
1074 //if (view) {
1075 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1076 //}
1077 }
1078 }
1079
1080 KFileItemModel* DolphinView::fileItemModel() const
1081 {
1082 return static_cast<KFileItemModel*>(m_container->controller()->model());
1083 }
1084
1085 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1086 {
1087 if (!url.isValid()) {
1088 const QString location(url.pathOrUrl());
1089 if (location.isEmpty()) {
1090 emit errorMessage(i18nc("@info:status", "The location is empty."));
1091 } else {
1092 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1093 }
1094 return;
1095 }
1096
1097 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1098 }
1099
1100 void DolphinView::applyViewProperties()
1101 {
1102 m_container->beginTransaction();
1103
1104 const ViewProperties props(url());
1105
1106 const Mode mode = props.viewMode();
1107 if (m_mode != mode) {
1108 const Mode previousMode = m_mode;
1109 m_mode = mode;
1110
1111 // Changing the mode might result in changing
1112 // the zoom level. Remember the old zoom level so
1113 // that zoomLevelChanged() can get emitted.
1114 const int oldZoomLevel = m_container->zoomLevel();
1115
1116 switch (m_mode) {
1117 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1118 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1119 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1120 default: Q_ASSERT(false); break;
1121 }
1122
1123 emit modeChanged(m_mode, previousMode);
1124
1125 if (m_container->zoomLevel() != oldZoomLevel) {
1126 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1127 }
1128 }
1129
1130 const bool hiddenFilesShown = props.hiddenFilesShown();
1131 if (hiddenFilesShown != m_dirLister->showingDotFiles()) {
1132 m_dirLister->setShowingDotFiles(hiddenFilesShown);
1133 m_dirLister->emitChanges();
1134 emit hiddenFilesShownChanged(hiddenFilesShown);
1135 }
1136
1137 /* m_storedCategorizedSorting = props.categorizedSorting();
1138 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1139 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1140 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1141 emit categorizedSortingChanged();
1142 }*/
1143
1144 const DolphinView::Sorting sorting = props.sorting();
1145 KItemModelBase* model = m_container->controller()->model();
1146 const QByteArray newSortRole = sortRoleForSorting(sorting);
1147 if (newSortRole != model->sortRole()) {
1148 model->setSortRole(newSortRole);
1149 emit sortingChanged(sorting);
1150 }
1151 /*
1152 const Qt::SortOrder sortOrder = props.sortOrder();
1153 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1154 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1155 emit sortOrderChanged(sortOrder);
1156 }
1157
1158 const bool sortFoldersFirst = props.sortFoldersFirst();
1159 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1160 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1161 emit sortFoldersFirstChanged(sortFoldersFirst);
1162 }
1163 */
1164 const QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
1165 if (infoList != m_additionalInfoList) {
1166 const QList<DolphinView::AdditionalInfo> previousList = m_additionalInfoList;
1167 m_additionalInfoList = infoList;
1168 applyAdditionalInfoListToView();
1169 emit additionalInfoListChanged(m_additionalInfoList, previousList);
1170 }
1171
1172 const bool previewsShown = props.previewsShown();
1173 if (previewsShown != m_container->previewsShown()) {
1174 const int oldZoomLevel = zoomLevel();
1175
1176 m_container->setPreviewsShown(previewsShown);
1177 emit previewsShownChanged(previewsShown);
1178
1179 // Changing the preview-state might result in a changed zoom-level
1180 if (oldZoomLevel != zoomLevel()) {
1181 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1182 }
1183 }
1184
1185 m_container->endTransaction();
1186 }
1187
1188 void DolphinView::applyAdditionalInfoListToView()
1189 {
1190 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
1191
1192 QHash<QByteArray, int> visibleRoles;
1193 visibleRoles.insert("name", 0);
1194
1195 int index = 1;
1196 foreach (AdditionalInfo info, m_additionalInfoList) {
1197 visibleRoles.insert(infoAccessor.role(info), index);
1198 ++index;
1199 }
1200
1201 m_container->setVisibleRoles(visibleRoles);
1202 }
1203
1204 void DolphinView::pasteToUrl(const KUrl& url)
1205 {
1206 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1207 KonqOperations::doPaste(this, url);
1208 }
1209
1210 void DolphinView::updateZoomLevel(int oldZoomLevel)
1211 {
1212 Q_UNUSED(oldZoomLevel);
1213 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1214 if (oldZoomLevel != newZoomLevel) {
1215 m_viewModeController->setZoomLevel(newZoomLevel);
1216 emit zoomLevelChanged(newZoomLevel);
1217 }*/
1218 }
1219
1220 KUrl::List DolphinView::simplifiedSelectedUrls() const
1221 {
1222 KUrl::List urls;
1223
1224 const KFileItemList items = selectedItems();
1225 foreach (const KFileItem &item, items) {
1226 urls.append(item.url());
1227 }
1228
1229 if (itemsExpandable()) {
1230 // TODO: Check if we still need KDirModel for this in KDE 5.0
1231 urls = KDirModel::simplifiedUrlList(urls);
1232 }
1233
1234 return urls;
1235 }
1236
1237 QMimeData* DolphinView::selectionMimeData() const
1238 {
1239 const KFileItemModel* model = fileItemModel();
1240 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1241 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1242
1243 return model->createMimeData(selectedIndexes);
1244 }
1245
1246 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1247 {
1248 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1249 markUrlsAsSelected(urls);
1250 }
1251
1252 void DolphinView::updateWritableState()
1253 {
1254 const bool wasFolderWritable = m_isFolderWritable;
1255 m_isFolderWritable = true;
1256
1257 const KFileItem item = m_dirLister->rootItem();
1258 if (!item.isNull()) {
1259 KFileItemListProperties capabilities(KFileItemList() << item);
1260 m_isFolderWritable = capabilities.supportsWriting();
1261 }
1262 if (m_isFolderWritable != wasFolderWritable) {
1263 emit writeStateChanged(m_isFolderWritable);
1264 }
1265 }
1266
1267 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1268 {
1269 switch (sorting) {
1270 case SortByName: return "name";
1271 case SortBySize: return "size";
1272 case SortByDate: return "date";
1273 case SortByPermissions: return "permissions";
1274 case SortByOwner: return "owner";
1275 case SortByGroup: return "group";
1276 case SortByType: return "type";
1277 case SortByDestination: return "destination";
1278 case SortByPath: return "path";
1279 default: break;
1280 }
1281
1282 return QByteArray();
1283 }
1284
1285 #include "dolphinview.moc"