]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Add missing includes
[dolphin.git] / src / views / dolphinview.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006-2009 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2006 Gregor Kališnik <gregor@podnapisi.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "dolphinview.h"
9
10 #include "dolphin_detailsmodesettings.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphinitemlistview.h"
13 #include "dolphinnewfilemenuobserver.h"
14 #include "draganddrophelper.h"
15 #include "kitemviews/kfileitemlistview.h"
16 #include "kitemviews/kfileitemmodel.h"
17 #include "kitemviews/kitemlistcontainer.h"
18 #include "kitemviews/kitemlistcontroller.h"
19 #include "kitemviews/kitemlistheader.h"
20 #include "kitemviews/kitemlistselectionmanager.h"
21 #include "versioncontrol/versioncontrolobserver.h"
22 #include "viewproperties.h"
23 #include "views/tooltips/tooltipmanager.h"
24 #include "zoomlevelinfo.h"
25
26 #ifdef HAVE_BALOO
27 #include <Baloo/IndexerConfig>
28 #endif
29 #include <KColorScheme>
30 #include <KDesktopFile>
31 #include <KDirModel>
32 #include <KFileItemListProperties>
33 #include <KFormat>
34 #include <KIO/CopyJob>
35 #include <KIO/DeleteJob>
36 #include <KIO/DropJob>
37 #include <KIO/JobUiDelegate>
38 #include <KIO/Paste>
39 #include <KIO/PasteJob>
40 #include <KIO/PreviewJob>
41 #include <KIO/RenameFileDialog>
42 #include <KJobWidgets>
43 #include <KLocalizedString>
44 #include <KMessageBox>
45 #include <KProtocolManager>
46
47 #include <QAbstractItemView>
48 #include <QActionGroup>
49 #include <QApplication>
50 #include <QClipboard>
51 #include <QDropEvent>
52 #include <QGraphicsOpacityEffect>
53 #include <QGraphicsSceneDragDropEvent>
54 #include <QLabel>
55 #include <QMenu>
56 #include <QMimeDatabase>
57 #include <QPixmapCache>
58 #include <QPointer>
59 #include <QScrollBar>
60 #include <QSize>
61 #include <QTimer>
62 #include <QVBoxLayout>
63
64 DolphinView::DolphinView(const QUrl& url, QWidget* parent) :
65 QWidget(parent),
66 m_active(true),
67 m_tabsForFiles(false),
68 m_assureVisibleCurrentIndex(false),
69 m_isFolderWritable(true),
70 m_dragging(false),
71 m_loading(false),
72 m_url(url),
73 m_viewPropertiesContext(),
74 m_mode(DolphinView::IconsView),
75 m_visibleRoles(),
76 m_topLayout(nullptr),
77 m_model(nullptr),
78 m_view(nullptr),
79 m_container(nullptr),
80 m_toolTipManager(nullptr),
81 m_selectionChangedTimer(nullptr),
82 m_currentItemUrl(),
83 m_scrollToCurrentItem(false),
84 m_restoredContentsPosition(),
85 m_selectedUrls(),
86 m_clearSelectionBeforeSelectingNewItems(false),
87 m_markFirstNewlySelectedItemAsCurrent(false),
88 m_versionControlObserver(nullptr),
89 m_twoClicksRenamingTimer(nullptr),
90 m_placeholderLabel(nullptr)
91 {
92 m_topLayout = new QVBoxLayout(this);
93 m_topLayout->setSpacing(0);
94 m_topLayout->setContentsMargins(0, 0, 0, 0);
95
96 // When a new item has been created by the "Create New..." menu, the item should
97 // get selected and it must be assured that the item will get visible. As the
98 // creation is done asynchronously, several signals must be checked:
99 connect(&DolphinNewFileMenuObserver::instance(), &DolphinNewFileMenuObserver::itemCreated,
100 this, &DolphinView::observeCreatedItem);
101
102 m_selectionChangedTimer = new QTimer(this);
103 m_selectionChangedTimer->setSingleShot(true);
104 m_selectionChangedTimer->setInterval(300);
105 connect(m_selectionChangedTimer, &QTimer::timeout,
106 this, &DolphinView::emitSelectionChangedSignal);
107
108 m_model = new KFileItemModel(this);
109 m_view = new DolphinItemListView();
110 m_view->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle());
111 m_view->setVisibleRoles({"text"});
112 applyModeToView();
113
114 KItemListController* controller = new KItemListController(m_model, m_view, this);
115 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
116 controller->setAutoActivationDelay(delay);
117
118 // The EnlargeSmallPreviews setting can only be changed after the model
119 // has been set in the view by KItemListController.
120 m_view->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews());
121
122 m_container = new KItemListContainer(controller, this);
123 m_container->installEventFilter(this);
124 setFocusProxy(m_container);
125 connect(m_container->horizontalScrollBar(), &QScrollBar::valueChanged, this, [=] { hideToolTip(); });
126 connect(m_container->verticalScrollBar(), &QScrollBar::valueChanged, this, [=] { hideToolTip(); });
127
128 // Show some placeholder text for empty folders
129 // This is made using a heavily-modified QLabel rather than a KTitleWidget
130 // because KTitleWidget can't be told to turn off mouse-selectable text
131 m_placeholderLabel = new QLabel(this);
132 QFont placeholderLabelFont;
133 // To match the size of a level 2 Heading/KTitleWidget
134 placeholderLabelFont.setPointSize(qRound(placeholderLabelFont.pointSize() * 1.3));
135 m_placeholderLabel->setFont(placeholderLabelFont);
136 m_placeholderLabel->setTextInteractionFlags(Qt::NoTextInteraction);
137 m_placeholderLabel->setWordWrap(true);
138 m_placeholderLabel->setAlignment(Qt::AlignCenter);
139 // Match opacity of QML placeholder label component
140 auto *effect = new QGraphicsOpacityEffect(m_placeholderLabel);
141 effect->setOpacity(0.5);
142 m_placeholderLabel->setGraphicsEffect(effect);
143 // Set initial text and visibility
144 updatePlaceholderLabel();
145
146 auto *centeringLayout = new QVBoxLayout(m_container);
147 centeringLayout->addWidget(m_placeholderLabel);
148 centeringLayout->setAlignment(m_placeholderLabel, Qt::AlignCenter);
149
150 controller->setSelectionBehavior(KItemListController::MultiSelection);
151 connect(controller, &KItemListController::itemActivated, this, &DolphinView::slotItemActivated);
152 connect(controller, &KItemListController::itemsActivated, this, &DolphinView::slotItemsActivated);
153 connect(controller, &KItemListController::itemMiddleClicked, this, &DolphinView::slotItemMiddleClicked);
154 connect(controller, &KItemListController::itemContextMenuRequested, this, &DolphinView::slotItemContextMenuRequested);
155 connect(controller, &KItemListController::viewContextMenuRequested, this, &DolphinView::slotViewContextMenuRequested);
156 connect(controller, &KItemListController::headerContextMenuRequested, this, &DolphinView::slotHeaderContextMenuRequested);
157 connect(controller, &KItemListController::mouseButtonPressed, this, &DolphinView::slotMouseButtonPressed);
158 connect(controller, &KItemListController::itemHovered, this, &DolphinView::slotItemHovered);
159 connect(controller, &KItemListController::itemUnhovered, this, &DolphinView::slotItemUnhovered);
160 connect(controller, &KItemListController::itemDropEvent, this, &DolphinView::slotItemDropEvent);
161 connect(controller, &KItemListController::escapePressed, this, &DolphinView::stopLoading);
162 connect(controller, &KItemListController::modelChanged, this, &DolphinView::slotModelChanged);
163 connect(controller, &KItemListController::selectedItemTextPressed, this, &DolphinView::slotSelectedItemTextPressed);
164 connect(controller, &KItemListController::increaseZoom, this, &DolphinView::slotIncreaseZoom);
165 connect(controller, &KItemListController::decreaseZoom, this, &DolphinView::slotDecreaseZoom);
166 connect(controller, &KItemListController::swipeUp, this, &DolphinView::slotSwipeUp);
167
168 connect(m_model, &KFileItemModel::directoryLoadingStarted, this, &DolphinView::slotDirectoryLoadingStarted);
169 connect(m_model, &KFileItemModel::directoryLoadingCompleted, this, &DolphinView::slotDirectoryLoadingCompleted);
170 connect(m_model, &KFileItemModel::directoryLoadingCanceled, this, &DolphinView::slotDirectoryLoadingCanceled);
171 connect(m_model, &KFileItemModel::directoryLoadingProgress, this, &DolphinView::directoryLoadingProgress);
172 connect(m_model, &KFileItemModel::directorySortingProgress, this, &DolphinView::directorySortingProgress);
173 connect(m_model, &KFileItemModel::itemsChanged,
174 this, &DolphinView::slotItemsChanged);
175 connect(m_model, &KFileItemModel::itemsRemoved, this, &DolphinView::itemCountChanged);
176 connect(m_model, &KFileItemModel::itemsInserted, this, &DolphinView::itemCountChanged);
177 connect(m_model, &KFileItemModel::infoMessage, this, &DolphinView::infoMessage);
178 connect(m_model, &KFileItemModel::errorMessage, this, &DolphinView::errorMessage);
179 connect(m_model, &KFileItemModel::directoryRedirection, this, &DolphinView::slotDirectoryRedirection);
180 connect(m_model, &KFileItemModel::urlIsFileError, this, &DolphinView::urlIsFileError);
181
182 connect(this, &DolphinView::itemCountChanged,
183 this, &DolphinView::updatePlaceholderLabel);
184
185 m_view->installEventFilter(this);
186 connect(m_view, &DolphinItemListView::sortOrderChanged,
187 this, &DolphinView::slotSortOrderChangedByHeader);
188 connect(m_view, &DolphinItemListView::sortRoleChanged,
189 this, &DolphinView::slotSortRoleChangedByHeader);
190 connect(m_view, &DolphinItemListView::visibleRolesChanged,
191 this, &DolphinView::slotVisibleRolesChangedByHeader);
192 connect(m_view, &DolphinItemListView::roleEditingCanceled,
193 this, &DolphinView::slotRoleEditingCanceled);
194 connect(m_view->header(), &KItemListHeader::columnWidthChangeFinished,
195 this, &DolphinView::slotHeaderColumnWidthChangeFinished);
196
197 KItemListSelectionManager* selectionManager = controller->selectionManager();
198 connect(selectionManager, &KItemListSelectionManager::selectionChanged,
199 this, &DolphinView::slotSelectionChanged);
200
201 #ifdef HAVE_BALOO
202 m_toolTipManager = new ToolTipManager(this);
203 connect(m_toolTipManager, &ToolTipManager::urlActivated, this, &DolphinView::urlActivated);
204 #endif
205
206 m_versionControlObserver = new VersionControlObserver(this);
207 m_versionControlObserver->setView(this);
208 m_versionControlObserver->setModel(m_model);
209 connect(m_versionControlObserver, &VersionControlObserver::infoMessage, this, &DolphinView::infoMessage);
210 connect(m_versionControlObserver, &VersionControlObserver::errorMessage, this, &DolphinView::errorMessage);
211 connect(m_versionControlObserver, &VersionControlObserver::operationCompletedMessage, this, &DolphinView::operationCompletedMessage);
212
213 m_twoClicksRenamingTimer = new QTimer(this);
214 m_twoClicksRenamingTimer->setSingleShot(true);
215 connect(m_twoClicksRenamingTimer, &QTimer::timeout, this, &DolphinView::slotTwoClicksRenamingTimerTimeout);
216
217 applyViewProperties();
218 m_topLayout->addWidget(m_container);
219
220 loadDirectory(url);
221 }
222
223 DolphinView::~DolphinView()
224 {
225 }
226
227 QUrl DolphinView::url() const
228 {
229 return m_url;
230 }
231
232 void DolphinView::setActive(bool active)
233 {
234 if (active == m_active) {
235 return;
236 }
237
238 m_active = active;
239
240 updatePalette();
241
242 if (active) {
243 m_container->setFocus();
244 Q_EMIT activated();
245 Q_EMIT writeStateChanged(m_isFolderWritable);
246 }
247 }
248
249 bool DolphinView::isActive() const
250 {
251 return m_active;
252 }
253
254 void DolphinView::setMode(Mode mode)
255 {
256 if (mode != m_mode) {
257 ViewProperties props(viewPropertiesUrl());
258 props.setViewMode(mode);
259
260 // We pass the new ViewProperties to applyViewProperties, rather than
261 // storing them on disk and letting applyViewProperties() read them
262 // from there, to prevent that changing the view mode fails if the
263 // .directory file is not writable (see bug 318534).
264 applyViewProperties(props);
265 }
266 }
267
268 DolphinView::Mode DolphinView::mode() const
269 {
270 return m_mode;
271 }
272
273 void DolphinView::setPreviewsShown(bool show)
274 {
275 if (previewsShown() == show) {
276 return;
277 }
278
279 ViewProperties props(viewPropertiesUrl());
280 props.setPreviewsShown(show);
281
282 const int oldZoomLevel = m_view->zoomLevel();
283 m_view->setPreviewsShown(show);
284 Q_EMIT previewsShownChanged(show);
285
286 const int newZoomLevel = m_view->zoomLevel();
287 if (newZoomLevel != oldZoomLevel) {
288 Q_EMIT zoomLevelChanged(newZoomLevel, oldZoomLevel);
289 }
290 }
291
292 bool DolphinView::previewsShown() const
293 {
294 return m_view->previewsShown();
295 }
296
297 void DolphinView::setHiddenFilesShown(bool show)
298 {
299 if (m_model->showHiddenFiles() == show) {
300 return;
301 }
302
303 const KFileItemList itemList = selectedItems();
304 m_selectedUrls.clear();
305 m_selectedUrls = itemList.urlList();
306
307 ViewProperties props(viewPropertiesUrl());
308 props.setHiddenFilesShown(show);
309
310 m_model->setShowHiddenFiles(show);
311 Q_EMIT hiddenFilesShownChanged(show);
312 }
313
314 bool DolphinView::hiddenFilesShown() const
315 {
316 return m_model->showHiddenFiles();
317 }
318
319 void DolphinView::setGroupedSorting(bool grouped)
320 {
321 if (grouped == groupedSorting()) {
322 return;
323 }
324
325 ViewProperties props(viewPropertiesUrl());
326 props.setGroupedSorting(grouped);
327 props.save();
328
329 m_container->controller()->model()->setGroupedSorting(grouped);
330
331 Q_EMIT groupedSortingChanged(grouped);
332 }
333
334 bool DolphinView::groupedSorting() const
335 {
336 return m_model->groupedSorting();
337 }
338
339 KFileItemList DolphinView::items() const
340 {
341 KFileItemList list;
342 const int itemCount = m_model->count();
343 list.reserve(itemCount);
344
345 for (int i = 0; i < itemCount; ++i) {
346 list.append(m_model->fileItem(i));
347 }
348
349 return list;
350 }
351
352 int DolphinView::itemsCount() const
353 {
354 return m_model->count();
355 }
356
357 KFileItemList DolphinView::selectedItems() const
358 {
359 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
360
361 KFileItemList selectedItems;
362 const auto items = selectionManager->selectedItems();
363 selectedItems.reserve(items.count());
364 for (int index : items) {
365 selectedItems.append(m_model->fileItem(index));
366 }
367 return selectedItems;
368 }
369
370 int DolphinView::selectedItemsCount() const
371 {
372 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
373 return selectionManager->selectedItems().count();
374 }
375
376 void DolphinView::markUrlsAsSelected(const QList<QUrl>& urls)
377 {
378 m_selectedUrls = urls;
379 }
380
381 void DolphinView::markUrlAsCurrent(const QUrl &url)
382 {
383 m_currentItemUrl = url;
384 m_scrollToCurrentItem = true;
385 }
386
387 void DolphinView::selectItems(const QRegularExpression &regexp, bool enabled)
388 {
389 const KItemListSelectionManager::SelectionMode mode = enabled
390 ? KItemListSelectionManager::Select
391 : KItemListSelectionManager::Deselect;
392 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
393
394 for (int index = 0; index < m_model->count(); index++) {
395 const KFileItem item = m_model->fileItem(index);
396 if (regexp.match(item.text()).hasMatch()) {
397 // An alternative approach would be to store the matching items in a KItemSet and
398 // select them in one go after the loop, but we'd need a new function
399 // KItemListSelectionManager::setSelected(KItemSet, SelectionMode mode)
400 // for that.
401 selectionManager->setSelected(index, 1, mode);
402 }
403 }
404 }
405
406 void DolphinView::setZoomLevel(int level)
407 {
408 const int oldZoomLevel = zoomLevel();
409 m_view->setZoomLevel(level);
410 if (zoomLevel() != oldZoomLevel) {
411 hideToolTip();
412 Q_EMIT zoomLevelChanged(zoomLevel(), oldZoomLevel);
413 }
414 }
415
416 int DolphinView::zoomLevel() const
417 {
418 return m_view->zoomLevel();
419 }
420
421 void DolphinView::setSortRole(const QByteArray& role)
422 {
423 if (role != sortRole()) {
424 updateSortRole(role);
425 }
426 }
427
428 QByteArray DolphinView::sortRole() const
429 {
430 const KItemModelBase* model = m_container->controller()->model();
431 return model->sortRole();
432 }
433
434 void DolphinView::setSortOrder(Qt::SortOrder order)
435 {
436 if (sortOrder() != order) {
437 updateSortOrder(order);
438 }
439 }
440
441 Qt::SortOrder DolphinView::sortOrder() const
442 {
443 return m_model->sortOrder();
444 }
445
446 void DolphinView::setSortFoldersFirst(bool foldersFirst)
447 {
448 if (sortFoldersFirst() != foldersFirst) {
449 updateSortFoldersFirst(foldersFirst);
450 }
451 }
452
453 bool DolphinView::sortFoldersFirst() const
454 {
455 return m_model->sortDirectoriesFirst();
456 }
457
458 void DolphinView::setVisibleRoles(const QList<QByteArray>& roles)
459 {
460 const QList<QByteArray> previousRoles = roles;
461
462 ViewProperties props(viewPropertiesUrl());
463 props.setVisibleRoles(roles);
464
465 m_visibleRoles = roles;
466 m_view->setVisibleRoles(roles);
467
468 Q_EMIT visibleRolesChanged(m_visibleRoles, previousRoles);
469 }
470
471 QList<QByteArray> DolphinView::visibleRoles() const
472 {
473 return m_visibleRoles;
474 }
475
476 void DolphinView::reload()
477 {
478 QByteArray viewState;
479 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
480 saveState(saveStream);
481
482 setUrl(url());
483 loadDirectory(url(), true);
484
485 QDataStream restoreStream(viewState);
486 restoreState(restoreStream);
487 }
488
489 void DolphinView::readSettings()
490 {
491 const int oldZoomLevel = m_view->zoomLevel();
492
493 GeneralSettings::self()->load();
494 m_view->readSettings();
495 applyViewProperties();
496
497 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
498 m_container->controller()->setAutoActivationDelay(delay);
499
500 const int newZoomLevel = m_view->zoomLevel();
501 if (newZoomLevel != oldZoomLevel) {
502 Q_EMIT zoomLevelChanged(newZoomLevel, oldZoomLevel);
503 }
504 }
505
506 void DolphinView::writeSettings()
507 {
508 GeneralSettings::self()->save();
509 m_view->writeSettings();
510 }
511
512 void DolphinView::setNameFilter(const QString& nameFilter)
513 {
514 m_model->setNameFilter(nameFilter);
515 }
516
517 QString DolphinView::nameFilter() const
518 {
519 return m_model->nameFilter();
520 }
521
522 void DolphinView::setMimeTypeFilters(const QStringList& filters)
523 {
524 return m_model->setMimeTypeFilters(filters);
525 }
526
527 QStringList DolphinView::mimeTypeFilters() const
528 {
529 return m_model->mimeTypeFilters();
530 }
531
532 void DolphinView::requestStatusBarText()
533 {
534 if (m_statJobForStatusBarText) {
535 // Kill the pending request.
536 m_statJobForStatusBarText->kill();
537 }
538
539 if (m_container->controller()->selectionManager()->hasSelection()) {
540 int folderCount = 0;
541 int fileCount = 0;
542 KIO::filesize_t totalFileSize = 0;
543
544 // Give a summary of the status of the selected files
545 const KFileItemList list = selectedItems();
546 for (const KFileItem& item : list) {
547 if (item.isDir()) {
548 ++folderCount;
549 } else {
550 ++fileCount;
551 totalFileSize += item.size();
552 }
553 }
554
555 if (folderCount + fileCount == 1) {
556 // If only one item is selected, show info about it
557 Q_EMIT statusBarTextChanged(list.first().getStatusBarInfo());
558 } else {
559 // At least 2 items are selected
560 emitStatusBarText(folderCount, fileCount, totalFileSize, HasSelection);
561 }
562 } else { // has no selection
563 if (!m_model->rootItem().url().isValid()) {
564 return;
565 }
566
567 m_statJobForStatusBarText = KIO::statDetails(m_model->rootItem().url(),
568 KIO::StatJob::SourceSide, KIO::StatRecursiveSize, KIO::HideProgressInfo);
569 connect(m_statJobForStatusBarText, &KJob::result,
570 this, &DolphinView::slotStatJobResult);
571 m_statJobForStatusBarText->start();
572 }
573 }
574
575 void DolphinView::emitStatusBarText(const int folderCount, const int fileCount,
576 KIO::filesize_t totalFileSize, const Selection selection)
577 {
578 QString foldersText;
579 QString filesText;
580 QString summary;
581
582 if (selection == HasSelection) {
583 // At least 2 items are selected because the case of 1 selected item is handled in
584 // DolphinView::requestStatusBarText().
585 foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
586 filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
587 } else {
588 foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount);
589 filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount);
590 }
591
592 if (fileCount > 0 && folderCount > 0) {
593 summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
594 foldersText, filesText,
595 KFormat().formatByteSize(totalFileSize));
596 } else if (fileCount > 0) {
597 summary = i18nc("@info:status files (size)", "%1 (%2)",
598 filesText,
599 KFormat().formatByteSize(totalFileSize));
600 } else if (folderCount > 0) {
601 summary = foldersText;
602 } else {
603 summary = i18nc("@info:status", "0 Folders, 0 Files");
604 }
605 Q_EMIT statusBarTextChanged(summary);
606 }
607
608 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
609 {
610 QList<QAction*> actions;
611
612 if (items.isEmpty()) {
613 const KFileItem item = m_model->rootItem();
614 if (!item.isNull()) {
615 actions = m_versionControlObserver->actions(KFileItemList() << item);
616 }
617 } else {
618 actions = m_versionControlObserver->actions(items);
619 }
620
621 return actions;
622 }
623
624 void DolphinView::setUrl(const QUrl& url)
625 {
626 if (url == m_url) {
627 return;
628 }
629
630 clearSelection();
631
632 m_url = url;
633
634 hideToolTip();
635
636 disconnect(m_view, &DolphinItemListView::roleEditingFinished,
637 this, &DolphinView::slotRoleEditingFinished);
638
639 // It is important to clear the items from the model before
640 // applying the view properties, otherwise expensive operations
641 // might be done on the existing items although they get cleared
642 // anyhow afterwards by loadDirectory().
643 m_model->clear();
644 applyViewProperties();
645 loadDirectory(url);
646
647 Q_EMIT urlChanged(url);
648 }
649
650 void DolphinView::selectAll()
651 {
652 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
653 selectionManager->setSelected(0, m_model->count());
654 }
655
656 void DolphinView::invertSelection()
657 {
658 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
659 selectionManager->setSelected(0, m_model->count(), KItemListSelectionManager::Toggle);
660 }
661
662 void DolphinView::clearSelection()
663 {
664 m_selectedUrls.clear();
665 m_container->controller()->selectionManager()->clearSelection();
666 }
667
668 void DolphinView::renameSelectedItems()
669 {
670 const KFileItemList items = selectedItems();
671 if (items.isEmpty()) {
672 return;
673 }
674
675 if (items.count() == 1 && GeneralSettings::renameInline()) {
676 const int index = m_model->index(items.first());
677 m_view->editRole(index, "text");
678
679 hideToolTip();
680
681 connect(m_view, &DolphinItemListView::roleEditingFinished,
682 this, &DolphinView::slotRoleEditingFinished);
683 } else {
684 KIO::RenameFileDialog* dialog = new KIO::RenameFileDialog(items, this);
685 connect(dialog, &KIO::RenameFileDialog::renamingFinished,
686 this, &DolphinView::slotRenameDialogRenamingFinished);
687
688 dialog->open();
689 }
690
691 // Assure that the current index remains visible when KFileItemModel
692 // will notify the view about changed items (which might result in
693 // a changed sorting).
694 m_assureVisibleCurrentIndex = true;
695 }
696
697 void DolphinView::trashSelectedItems()
698 {
699 const QList<QUrl> list = simplifiedSelectedUrls();
700 KIO::JobUiDelegate uiDelegate;
701 uiDelegate.setWindow(window());
702 if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Trash, KIO::JobUiDelegate::DefaultConfirmation)) {
703 KIO::Job* job = KIO::trash(list);
704 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Trash, list, QUrl(QStringLiteral("trash:/")), job);
705 KJobWidgets::setWindow(job, this);
706 connect(job, &KIO::Job::result,
707 this, &DolphinView::slotTrashFileFinished);
708 }
709 }
710
711 void DolphinView::deleteSelectedItems()
712 {
713 const QList<QUrl> list = simplifiedSelectedUrls();
714
715 KIO::JobUiDelegate uiDelegate;
716 uiDelegate.setWindow(window());
717 if (uiDelegate.askDeleteConfirmation(list, KIO::JobUiDelegate::Delete, KIO::JobUiDelegate::DefaultConfirmation)) {
718 KIO::Job* job = KIO::del(list);
719 KJobWidgets::setWindow(job, this);
720 connect(job, &KIO::Job::result,
721 this, &DolphinView::slotDeleteFileFinished);
722 }
723 }
724
725 void DolphinView::cutSelectedItemsToClipboard()
726 {
727 QMimeData* mimeData = selectionMimeData();
728 KIO::setClipboardDataCut(mimeData, true);
729 QApplication::clipboard()->setMimeData(mimeData);
730 }
731
732 void DolphinView::copySelectedItemsToClipboard()
733 {
734 QMimeData* mimeData = selectionMimeData();
735 QApplication::clipboard()->setMimeData(mimeData);
736 }
737
738 void DolphinView::copySelectedItems(const KFileItemList &selection, const QUrl &destinationUrl)
739 {
740 KIO::CopyJob* job = KIO::copy(selection.urlList(), destinationUrl, KIO::DefaultFlags);
741 KJobWidgets::setWindow(job, this);
742
743 connect(job, &KIO::DropJob::result, this, &DolphinView::slotJobResult);
744 connect(job, &KIO::CopyJob::copyingDone, this, &DolphinView::slotCopyingDone);
745 KIO::FileUndoManager::self()->recordCopyJob(job);
746 }
747
748 void DolphinView::moveSelectedItems(const KFileItemList &selection, const QUrl &destinationUrl)
749 {
750 KIO::CopyJob* job = KIO::move(selection.urlList(), destinationUrl, KIO::DefaultFlags);
751 KJobWidgets::setWindow(job, this);
752
753 connect(job, &KIO::DropJob::result, this, &DolphinView::slotJobResult);
754 connect(job, &KIO::CopyJob::copyingDone, this, &DolphinView::slotCopyingDone);
755 KIO::FileUndoManager::self()->recordCopyJob(job);
756
757 }
758
759 void DolphinView::paste()
760 {
761 pasteToUrl(url());
762 }
763
764 void DolphinView::pasteIntoFolder()
765 {
766 const KFileItemList items = selectedItems();
767 if ((items.count() == 1) && items.first().isDir()) {
768 pasteToUrl(items.first().url());
769 }
770 }
771
772 void DolphinView::duplicateSelectedItems()
773 {
774 const KFileItemList itemList = selectedItems();
775 if (itemList.isEmpty()) {
776 return;
777 }
778
779 const QMimeDatabase db;
780
781 // Duplicate all selected items and append "copy" to the end of the file name
782 // but before the filename extension, if present
783 QList<QUrl> newSelection;
784 for (const auto &item : itemList) {
785 const QUrl originalURL = item.url();
786 const QString originalDirectoryPath = originalURL.adjusted(QUrl::RemoveFilename).path();
787 const QString originalFileName = item.name();
788
789 QString extension = db.suffixForFileName(originalFileName);
790
791 QUrl duplicateURL = originalURL;
792
793 // No extension; new filename is "<oldfilename> copy"
794 if (extension.isEmpty()) {
795 duplicateURL.setPath(originalDirectoryPath + i18nc("<filename> copy", "%1 copy", originalFileName));
796 // There's an extension; new filename is "<oldfilename> copy.<extension>"
797 } else {
798 // Need to add a dot since QMimeDatabase::suffixForFileName() doesn't include it
799 extension = QLatin1String(".") + extension;
800 const QString originalFilenameWithoutExtension = originalFileName.chopped(extension.size());
801 // Preserve file's original filename extension in case the casing differs
802 // from what QMimeDatabase::suffixForFileName() returned
803 const QString originalExtension = originalFileName.right(extension.size());
804 duplicateURL.setPath(originalDirectoryPath + i18nc("<filename> copy", "%1 copy", originalFilenameWithoutExtension) + originalExtension);
805 }
806
807 KIO::CopyJob* job = KIO::copyAs(originalURL, duplicateURL);
808 KJobWidgets::setWindow(job, this);
809
810 if (job) {
811 newSelection << duplicateURL;
812 KIO::FileUndoManager::self()->recordCopyJob(job);
813 }
814 }
815
816 forceUrlsSelection(newSelection.first(), newSelection);
817 }
818
819 void DolphinView::stopLoading()
820 {
821 m_model->cancelDirectoryLoading();
822 }
823
824 void DolphinView::updatePalette()
825 {
826 QColor color = KColorScheme(isActiveWindow() ? QPalette::Active : QPalette::Inactive, KColorScheme::View).background().color();
827 if (!m_active) {
828 color.setAlpha(150);
829 }
830
831 QWidget* viewport = m_container->viewport();
832 if (viewport) {
833 QPalette palette;
834 palette.setColor(viewport->backgroundRole(), color);
835 viewport->setPalette(palette);
836 }
837
838 update();
839 }
840
841 void DolphinView::abortTwoClicksRenaming()
842 {
843 m_twoClicksRenamingItemUrl.clear();
844 m_twoClicksRenamingTimer->stop();
845 }
846
847 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
848 {
849 switch (event->type()) {
850 case QEvent::PaletteChange:
851 updatePalette();
852 QPixmapCache::clear();
853 break;
854
855 case QEvent::WindowActivate:
856 case QEvent::WindowDeactivate:
857 updatePalette();
858 break;
859
860 case QEvent::KeyPress:
861 hideToolTip(ToolTipManager::HideBehavior::Instantly);
862 if (GeneralSettings::useTabForSwitchingSplitView()) {
863 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
864 if (keyEvent->key() == Qt::Key_Tab && keyEvent->modifiers() == Qt::NoModifier) {
865 Q_EMIT toggleActiveViewRequested();
866 return true;
867 }
868 }
869 break;
870 case QEvent::FocusIn:
871 if (watched == m_container) {
872 setActive(true);
873 }
874 break;
875
876 case QEvent::GraphicsSceneDragEnter:
877 if (watched == m_view) {
878 m_dragging = true;
879 abortTwoClicksRenaming();
880 }
881 break;
882
883 case QEvent::GraphicsSceneDragLeave:
884 if (watched == m_view) {
885 m_dragging = false;
886 }
887 break;
888
889 case QEvent::GraphicsSceneDrop:
890 if (watched == m_view) {
891 m_dragging = false;
892 }
893 default:
894 break;
895 }
896
897 return QWidget::eventFilter(watched, event);
898 }
899
900 void DolphinView::wheelEvent(QWheelEvent* event)
901 {
902 if (event->modifiers().testFlag(Qt::ControlModifier)) {
903 const QPoint numDegrees = event->angleDelta() / 8;
904 const QPoint numSteps = numDegrees / 15;
905
906 setZoomLevel(zoomLevel() + numSteps.y());
907 event->accept();
908 } else {
909 event->ignore();
910 }
911 }
912
913 void DolphinView::hideEvent(QHideEvent* event)
914 {
915 hideToolTip();
916 QWidget::hideEvent(event);
917 }
918
919 bool DolphinView::event(QEvent* event)
920 {
921 if (event->type() == QEvent::WindowDeactivate) {
922 /* See Bug 297355
923 * Dolphin leaves file preview tooltips open even when is not visible.
924 *
925 * Hide tool-tip when Dolphin loses focus.
926 */
927 hideToolTip();
928 abortTwoClicksRenaming();
929 }
930
931 return QWidget::event(event);
932 }
933
934 void DolphinView::activate()
935 {
936 setActive(true);
937 }
938
939 void DolphinView::slotItemActivated(int index)
940 {
941 abortTwoClicksRenaming();
942
943 const KFileItem item = m_model->fileItem(index);
944 if (!item.isNull()) {
945 Q_EMIT itemActivated(item);
946 }
947 }
948
949 void DolphinView::slotItemsActivated(const KItemSet& indexes)
950 {
951 Q_ASSERT(indexes.count() >= 2);
952
953 abortTwoClicksRenaming();
954
955 if (indexes.count() > 5) {
956 QString question = i18np("Are you sure you want to open 1 item?", "Are you sure you want to open %1 items?", indexes.count());
957 const int answer = KMessageBox::warningYesNo(this, question);
958 if (answer != KMessageBox::Yes) {
959 return;
960 }
961 }
962
963 KFileItemList items;
964 items.reserve(indexes.count());
965
966 for (int index : indexes) {
967 KFileItem item = m_model->fileItem(index);
968 const QUrl& url = openItemAsFolderUrl(item);
969
970 if (!url.isEmpty()) { // Open folders in new tabs
971 Q_EMIT tabRequested(url);
972 } else {
973 items.append(item);
974 }
975 }
976
977 if (items.count() == 1) {
978 Q_EMIT itemActivated(items.first());
979 } else if (items.count() > 1) {
980 Q_EMIT itemsActivated(items);
981 }
982 }
983
984 void DolphinView::slotItemMiddleClicked(int index)
985 {
986 const KFileItem& item = m_model->fileItem(index);
987 const QUrl& url = openItemAsFolderUrl(item);
988 if (!url.isEmpty()) {
989 Q_EMIT tabRequested(url);
990 } else if (isTabsForFilesEnabled()) {
991 Q_EMIT tabRequested(item.url());
992 }
993 }
994
995 void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
996 {
997 // Force emit of a selection changed signal before we request the
998 // context menu, to update the edit-actions first. (See Bug 294013)
999 if (m_selectionChangedTimer->isActive()) {
1000 emitSelectionChangedSignal();
1001 }
1002
1003 const KFileItem item = m_model->fileItem(index);
1004 Q_EMIT requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
1005 }
1006
1007 void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
1008 {
1009 Q_EMIT requestContextMenu(pos.toPoint(), KFileItem(), url(), QList<QAction*>());
1010 }
1011
1012 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
1013 {
1014 ViewProperties props(viewPropertiesUrl());
1015
1016 QPointer<QMenu> menu = new QMenu(QApplication::activeWindow());
1017
1018 KItemListView* view = m_container->controller()->view();
1019 const QList<QByteArray> visibleRolesSet = view->visibleRoles();
1020
1021 bool indexingEnabled = false;
1022 #ifdef HAVE_BALOO
1023 Baloo::IndexerConfig config;
1024 indexingEnabled = config.fileIndexingEnabled();
1025 #endif
1026
1027 QString groupName;
1028 QMenu* groupMenu = nullptr;
1029
1030 // Add all roles to the menu that can be shown or hidden by the user
1031 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
1032 for (const KFileItemModel::RoleInfo& info : rolesInfo) {
1033 if (info.role == "text") {
1034 // It should not be possible to hide the "text" role
1035 continue;
1036 }
1037
1038 const QString text = m_model->roleDescription(info.role);
1039 QAction* action = nullptr;
1040 if (info.group.isEmpty()) {
1041 action = menu->addAction(text);
1042 } else {
1043 if (!groupMenu || info.group != groupName) {
1044 groupName = info.group;
1045 groupMenu = menu->addMenu(groupName);
1046 }
1047
1048 action = groupMenu->addAction(text);
1049 }
1050
1051 action->setCheckable(true);
1052 action->setChecked(visibleRolesSet.contains(info.role));
1053 action->setData(info.role);
1054
1055 const bool enable = (!info.requiresBaloo && !info.requiresIndexer) ||
1056 (info.requiresBaloo) ||
1057 (info.requiresIndexer && indexingEnabled);
1058 action->setEnabled(enable);
1059 }
1060
1061 menu->addSeparator();
1062
1063 QActionGroup* widthsGroup = new QActionGroup(menu);
1064 const bool autoColumnWidths = props.headerColumnWidths().isEmpty();
1065
1066 QAction* autoAdjustWidthsAction = menu->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
1067 autoAdjustWidthsAction->setCheckable(true);
1068 autoAdjustWidthsAction->setChecked(autoColumnWidths);
1069 autoAdjustWidthsAction->setActionGroup(widthsGroup);
1070
1071 QAction* customWidthsAction = menu->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
1072 customWidthsAction->setCheckable(true);
1073 customWidthsAction->setChecked(!autoColumnWidths);
1074 customWidthsAction->setActionGroup(widthsGroup);
1075
1076 QAction* action = menu->exec(pos.toPoint());
1077 if (menu && action) {
1078 KItemListHeader* header = view->header();
1079
1080 if (action == autoAdjustWidthsAction) {
1081 // Clear the column-widths from the viewproperties and turn on
1082 // the automatic resizing of the columns
1083 props.setHeaderColumnWidths(QList<int>());
1084 header->setAutomaticColumnResizing(true);
1085 } else if (action == customWidthsAction) {
1086 // Apply the current column-widths as custom column-widths and turn
1087 // off the automatic resizing of the columns
1088 QList<int> columnWidths;
1089 const auto visibleRoles = view->visibleRoles();
1090 columnWidths.reserve(visibleRoles.count());
1091 for (const QByteArray& role : visibleRoles) {
1092 columnWidths.append(header->columnWidth(role));
1093 }
1094 props.setHeaderColumnWidths(columnWidths);
1095 header->setAutomaticColumnResizing(false);
1096 } else {
1097 // Show or hide the selected role
1098 const QByteArray selectedRole = action->data().toByteArray();
1099
1100 QList<QByteArray> visibleRoles = view->visibleRoles();
1101 if (action->isChecked()) {
1102 visibleRoles.append(selectedRole);
1103 } else {
1104 visibleRoles.removeOne(selectedRole);
1105 }
1106
1107 view->setVisibleRoles(visibleRoles);
1108 props.setVisibleRoles(visibleRoles);
1109
1110 QList<int> columnWidths;
1111 if (!header->automaticColumnResizing()) {
1112 const auto visibleRoles = view->visibleRoles();
1113 columnWidths.reserve(visibleRoles.count());
1114 for (const QByteArray& role : visibleRoles) {
1115 columnWidths.append(header->columnWidth(role));
1116 }
1117 }
1118 props.setHeaderColumnWidths(columnWidths);
1119 }
1120 }
1121
1122 delete menu;
1123 }
1124
1125 void DolphinView::slotHeaderColumnWidthChangeFinished(const QByteArray& role, qreal current)
1126 {
1127 const QList<QByteArray> visibleRoles = m_view->visibleRoles();
1128
1129 ViewProperties props(viewPropertiesUrl());
1130 QList<int> columnWidths = props.headerColumnWidths();
1131 if (columnWidths.count() != visibleRoles.count()) {
1132 columnWidths.clear();
1133 columnWidths.reserve(visibleRoles.count());
1134 const KItemListHeader* header = m_view->header();
1135 for (const QByteArray& role : visibleRoles) {
1136 const int width = header->columnWidth(role);
1137 columnWidths.append(width);
1138 }
1139 }
1140
1141 const int roleIndex = visibleRoles.indexOf(role);
1142 Q_ASSERT(roleIndex >= 0 && roleIndex < columnWidths.count());
1143 columnWidths[roleIndex] = current;
1144
1145 props.setHeaderColumnWidths(columnWidths);
1146 }
1147
1148 void DolphinView::slotItemHovered(int index)
1149 {
1150 const KFileItem item = m_model->fileItem(index);
1151
1152 if (GeneralSettings::showToolTips() && !m_dragging) {
1153 QRectF itemRect = m_container->controller()->view()->itemContextRect(index);
1154 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
1155 itemRect.moveTo(pos);
1156
1157 #ifdef HAVE_BALOO
1158 m_toolTipManager->showToolTip(item, itemRect, nativeParentWidget()->windowHandle());
1159 #endif
1160 }
1161
1162 Q_EMIT requestItemInfo(item);
1163 }
1164
1165 void DolphinView::slotItemUnhovered(int index)
1166 {
1167 Q_UNUSED(index)
1168 hideToolTip();
1169 Q_EMIT requestItemInfo(KFileItem());
1170 }
1171
1172 void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
1173 {
1174 QUrl destUrl;
1175 KFileItem destItem = m_model->fileItem(index);
1176 if (destItem.isNull() || (!destItem.isDir() && !destItem.isDesktopFile())) {
1177 // Use the URL of the view as drop target if the item is no directory
1178 // or desktop-file
1179 destItem = m_model->rootItem();
1180 destUrl = url();
1181 } else {
1182 // The item represents a directory or desktop-file
1183 destUrl = destItem.mostLocalUrl();
1184 }
1185
1186 QDropEvent dropEvent(event->pos().toPoint(),
1187 event->possibleActions(),
1188 event->mimeData(),
1189 event->buttons(),
1190 event->modifiers());
1191 dropUrls(destUrl, &dropEvent, this);
1192
1193 setActive(true);
1194 }
1195
1196 void DolphinView::dropUrls(const QUrl &destUrl, QDropEvent *dropEvent, QWidget *dropWidget)
1197 {
1198 KIO::DropJob* job = DragAndDropHelper::dropUrls(destUrl, dropEvent, dropWidget);
1199
1200 if (job) {
1201 connect(job, &KIO::DropJob::result, this, &DolphinView::slotJobResult);
1202
1203 if (destUrl == url()) {
1204 // Mark the dropped urls as selected.
1205 m_clearSelectionBeforeSelectingNewItems = true;
1206 m_markFirstNewlySelectedItemAsCurrent = true;
1207 connect(job, &KIO::DropJob::itemCreated, this, &DolphinView::slotItemCreated);
1208 }
1209 }
1210 }
1211
1212 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
1213 {
1214 if (previous != nullptr) {
1215 Q_ASSERT(qobject_cast<KFileItemModel*>(previous));
1216 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(previous);
1217 disconnect(fileItemModel, &KFileItemModel::directoryLoadingCompleted, this, &DolphinView::slotDirectoryLoadingCompleted);
1218 m_versionControlObserver->setModel(nullptr);
1219 }
1220
1221 if (current) {
1222 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
1223 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(current);
1224 connect(fileItemModel, &KFileItemModel::directoryLoadingCompleted, this, &DolphinView::slotDirectoryLoadingCompleted);
1225 m_versionControlObserver->setModel(fileItemModel);
1226 }
1227 }
1228
1229 void DolphinView::slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons)
1230 {
1231 Q_UNUSED(itemIndex)
1232
1233 hideToolTip();
1234
1235 if (buttons & Qt::BackButton) {
1236 Q_EMIT goBackRequested();
1237 } else if (buttons & Qt::ForwardButton) {
1238 Q_EMIT goForwardRequested();
1239 }
1240 }
1241
1242 void DolphinView::slotSelectedItemTextPressed(int index)
1243 {
1244 if (GeneralSettings::renameInline() && !m_view->style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) {
1245 const KFileItem item = m_model->fileItem(index);
1246 const KFileItemListProperties capabilities(KFileItemList() << item);
1247 if (capabilities.supportsMoving()) {
1248 m_twoClicksRenamingItemUrl = item.url();
1249 m_twoClicksRenamingTimer->start(QApplication::doubleClickInterval());
1250 }
1251 }
1252 }
1253
1254 void DolphinView::slotCopyingDone(KIO::Job *, const QUrl &, const QUrl &to)
1255 {
1256 slotItemCreated(to);
1257 }
1258
1259 void DolphinView::slotItemCreated(const QUrl& url)
1260 {
1261 if (m_markFirstNewlySelectedItemAsCurrent) {
1262 markUrlAsCurrent(url);
1263 m_markFirstNewlySelectedItemAsCurrent = false;
1264 }
1265 m_selectedUrls << url;
1266 }
1267
1268 void DolphinView::slotJobResult(KJob *job)
1269 {
1270 if (job->error()) {
1271 Q_EMIT errorMessage(job->errorString());
1272 }
1273 if (!m_selectedUrls.isEmpty()) {
1274 m_selectedUrls = KDirModel::simplifiedUrlList(m_selectedUrls);
1275 }
1276 }
1277
1278 void DolphinView::slotSelectionChanged(const KItemSet& current, const KItemSet& previous)
1279 {
1280 const int currentCount = current.count();
1281 const int previousCount = previous.count();
1282 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
1283 (currentCount > 0 && previousCount == 0);
1284
1285 // If nothing has been selected before and something got selected (or if something
1286 // was selected before and now nothing is selected) the selectionChangedSignal must
1287 // be emitted asynchronously as fast as possible to update the edit-actions.
1288 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
1289 m_selectionChangedTimer->start();
1290 }
1291
1292 void DolphinView::emitSelectionChangedSignal()
1293 {
1294 m_selectionChangedTimer->stop();
1295 Q_EMIT selectionChanged(selectedItems());
1296 }
1297
1298 void DolphinView::slotStatJobResult(KJob *job)
1299 {
1300 int folderCount = 0;
1301 int fileCount = 0;
1302 KIO::filesize_t totalFileSize = 0;
1303 bool countFileSize = true;
1304
1305 const auto entry = static_cast<KIO::StatJob *>(job)->statResult();
1306 if (entry.contains(KIO::UDSEntry::UDS_RECURSIVE_SIZE)) {
1307 // We have a precomputed value.
1308 totalFileSize = static_cast<KIO::filesize_t>(
1309 entry.numberValue(KIO::UDSEntry::UDS_RECURSIVE_SIZE));
1310 countFileSize = false;
1311 }
1312
1313 const int itemCount = m_model->count();
1314 for (int i = 0; i < itemCount; ++i) {
1315 const KFileItem item = m_model->fileItem(i);
1316 if (item.isDir()) {
1317 ++folderCount;
1318 } else {
1319 ++fileCount;
1320 if (countFileSize) {
1321 totalFileSize += item.size();
1322 }
1323 }
1324 }
1325 emitStatusBarText(folderCount, fileCount, totalFileSize, NoSelection);
1326 }
1327
1328 void DolphinView::updateSortRole(const QByteArray& role)
1329 {
1330 ViewProperties props(viewPropertiesUrl());
1331 props.setSortRole(role);
1332
1333 KItemModelBase* model = m_container->controller()->model();
1334 model->setSortRole(role);
1335
1336 Q_EMIT sortRoleChanged(role);
1337 }
1338
1339 void DolphinView::updateSortOrder(Qt::SortOrder order)
1340 {
1341 ViewProperties props(viewPropertiesUrl());
1342 props.setSortOrder(order);
1343
1344 m_model->setSortOrder(order);
1345
1346 Q_EMIT sortOrderChanged(order);
1347 }
1348
1349 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
1350 {
1351 ViewProperties props(viewPropertiesUrl());
1352 props.setSortFoldersFirst(foldersFirst);
1353
1354 m_model->setSortDirectoriesFirst(foldersFirst);
1355
1356 Q_EMIT sortFoldersFirstChanged(foldersFirst);
1357 }
1358
1359 QPair<bool, QString> DolphinView::pasteInfo() const
1360 {
1361 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
1362 QPair<bool, QString> info;
1363 info.second = KIO::pasteActionText(mimeData, &info.first, rootItem());
1364 return info;
1365 }
1366
1367 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1368 {
1369 m_tabsForFiles = tabsForFiles;
1370 }
1371
1372 bool DolphinView::isTabsForFilesEnabled() const
1373 {
1374 return m_tabsForFiles;
1375 }
1376
1377 bool DolphinView::itemsExpandable() const
1378 {
1379 return m_mode == DetailsView;
1380 }
1381
1382 void DolphinView::restoreState(QDataStream& stream)
1383 {
1384 // Read the version number of the view state and check if the version is supported.
1385 quint32 version = 0;
1386 stream >> version;
1387 if (version != 1) {
1388 // The version of the view state isn't supported, we can't restore it.
1389 return;
1390 }
1391
1392 // Restore the current item that had the keyboard focus
1393 stream >> m_currentItemUrl;
1394
1395 // Restore the previously selected items
1396 stream >> m_selectedUrls;
1397
1398 // Restore the view position
1399 stream >> m_restoredContentsPosition;
1400
1401 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1402 QSet<QUrl> urls;
1403 stream >> urls;
1404 m_model->restoreExpandedDirectories(urls);
1405 }
1406
1407 void DolphinView::saveState(QDataStream& stream)
1408 {
1409 stream << quint32(1); // View state version
1410
1411 // Save the current item that has the keyboard focus
1412 const int currentIndex = m_container->controller()->selectionManager()->currentItem();
1413 if (currentIndex != -1) {
1414 KFileItem item = m_model->fileItem(currentIndex);
1415 Q_ASSERT(!item.isNull()); // If the current index is valid a item must exist
1416 QUrl currentItemUrl = item.url();
1417 stream << currentItemUrl;
1418 } else {
1419 stream << QUrl();
1420 }
1421
1422 // Save the selected urls
1423 stream << selectedItems().urlList();
1424
1425 // Save view position
1426 const qreal x = m_container->horizontalScrollBar()->value();
1427 const qreal y = m_container->verticalScrollBar()->value();
1428 stream << QPoint(x, y);
1429
1430 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1431 stream << m_model->expandedDirectories();
1432 }
1433
1434 KFileItem DolphinView::rootItem() const
1435 {
1436 return m_model->rootItem();
1437 }
1438
1439 void DolphinView::setViewPropertiesContext(const QString& context)
1440 {
1441 m_viewPropertiesContext = context;
1442 }
1443
1444 QString DolphinView::viewPropertiesContext() const
1445 {
1446 return m_viewPropertiesContext;
1447 }
1448
1449 QUrl DolphinView::openItemAsFolderUrl(const KFileItem& item, const bool browseThroughArchives)
1450 {
1451 if (item.isNull()) {
1452 return QUrl();
1453 }
1454
1455 QUrl url = item.targetUrl();
1456
1457 if (item.isDir()) {
1458 return url;
1459 }
1460
1461 if (item.isMimeTypeKnown()) {
1462 const QString& mimetype = item.mimetype();
1463
1464 if (browseThroughArchives && item.isFile() && url.isLocalFile()) {
1465 // Generic mechanism for redirecting to tar:/<path>/ when clicking on a tar file,
1466 // zip:/<path>/ when clicking on a zip file, etc.
1467 // The .protocol file specifies the mimetype that the kioslave handles.
1468 // Note that we don't use mimetype inheritance since we don't want to
1469 // open OpenDocument files as zip folders...
1470 const QString& protocol = KProtocolManager::protocolForArchiveMimetype(mimetype);
1471 if (!protocol.isEmpty()) {
1472 url.setScheme(protocol);
1473 return url;
1474 }
1475 }
1476
1477 if (mimetype == QLatin1String("application/x-desktop")) {
1478 // Redirect to the URL in Type=Link desktop files, unless it is a http(s) URL.
1479 KDesktopFile desktopFile(url.toLocalFile());
1480 if (desktopFile.hasLinkType()) {
1481 const QString linkUrl = desktopFile.readUrl();
1482 if (!linkUrl.startsWith(QLatin1String("http"))) {
1483 return QUrl::fromUserInput(linkUrl);
1484 }
1485 }
1486 }
1487 }
1488
1489 return QUrl();
1490 }
1491
1492 void DolphinView::resetZoomLevel()
1493 {
1494 ViewModeSettings::ViewMode mode;
1495
1496 switch (m_mode) {
1497 case IconsView: mode = ViewModeSettings::IconsMode; break;
1498 case CompactView: mode = ViewModeSettings::CompactMode; break;
1499 case DetailsView: mode = ViewModeSettings::DetailsMode; break;
1500 }
1501 const ViewModeSettings settings(mode);
1502 const QSize iconSize = QSize(settings.iconSize(), settings.iconSize());
1503 setZoomLevel(ZoomLevelInfo::zoomLevelForIconSize(iconSize));
1504 }
1505
1506 void DolphinView::observeCreatedItem(const QUrl& url)
1507 {
1508 if (m_active) {
1509 forceUrlsSelection(url, {url});
1510 }
1511 }
1512
1513 void DolphinView::slotDirectoryRedirection(const QUrl& oldUrl, const QUrl& newUrl)
1514 {
1515 if (oldUrl.matches(url(), QUrl::StripTrailingSlash)) {
1516 Q_EMIT redirection(oldUrl, newUrl);
1517 m_url = newUrl; // #186947
1518 }
1519 }
1520
1521 void DolphinView::updateViewState()
1522 {
1523 if (m_currentItemUrl != QUrl()) {
1524 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1525
1526 // if there is a selection already, leave it that way
1527 if (!selectionManager->hasSelection()) {
1528 const int currentIndex = m_model->index(m_currentItemUrl);
1529 if (currentIndex != -1) {
1530 selectionManager->setCurrentItem(currentIndex);
1531
1532 // scroll to current item and reset the state
1533 if (m_scrollToCurrentItem) {
1534 m_view->scrollToItem(currentIndex);
1535 m_scrollToCurrentItem = false;
1536 }
1537 } else {
1538 selectionManager->setCurrentItem(0);
1539 }
1540 }
1541
1542 m_currentItemUrl = QUrl();
1543 }
1544
1545 if (!m_restoredContentsPosition.isNull()) {
1546 const int x = m_restoredContentsPosition.x();
1547 const int y = m_restoredContentsPosition.y();
1548 m_restoredContentsPosition = QPoint();
1549
1550 m_container->horizontalScrollBar()->setValue(x);
1551 m_container->verticalScrollBar()->setValue(y);
1552 }
1553
1554 if (!m_selectedUrls.isEmpty()) {
1555 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1556
1557 // if there is a selection already, leave it that way
1558 if (!selectionManager->hasSelection()) {
1559 if (m_clearSelectionBeforeSelectingNewItems) {
1560 selectionManager->clearSelection();
1561 m_clearSelectionBeforeSelectingNewItems = false;
1562 }
1563
1564 KItemSet selectedItems = selectionManager->selectedItems();
1565
1566 QList<QUrl>::iterator it = m_selectedUrls.begin();
1567 while (it != m_selectedUrls.end()) {
1568 const int index = m_model->index(*it);
1569 if (index >= 0) {
1570 selectedItems.insert(index);
1571 it = m_selectedUrls.erase(it);
1572 } else {
1573 ++it;
1574 }
1575 }
1576
1577 selectionManager->beginAnchoredSelection(selectionManager->currentItem());
1578 selectionManager->setSelectedItems(selectedItems);
1579 }
1580 }
1581 }
1582
1583 void DolphinView::hideToolTip(const ToolTipManager::HideBehavior behavior)
1584 {
1585 #ifdef HAVE_BALOO
1586 if (GeneralSettings::showToolTips()) {
1587 m_toolTipManager->hideToolTip(behavior);
1588 }
1589 #else
1590 Q_UNUSED(behavior)
1591 #endif
1592 }
1593
1594 void DolphinView::slotTwoClicksRenamingTimerTimeout()
1595 {
1596 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1597
1598 // verify that only one item is selected
1599 if (selectionManager->selectedItems().count() == 1) {
1600 const int index = selectionManager->currentItem();
1601 const QUrl fileItemUrl = m_model->fileItem(index).url();
1602
1603 // check if the selected item was the same item that started the twoClicksRenaming
1604 if (fileItemUrl.isValid() && m_twoClicksRenamingItemUrl == fileItemUrl) {
1605 renameSelectedItems();
1606 }
1607 }
1608 }
1609
1610 void DolphinView::slotTrashFileFinished(KJob* job)
1611 {
1612 if (job->error() == 0) {
1613 Q_EMIT operationCompletedMessage(i18nc("@info:status", "Trash operation completed."));
1614 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1615 Q_EMIT errorMessage(job->errorString());
1616 }
1617 }
1618
1619 void DolphinView::slotDeleteFileFinished(KJob* job)
1620 {
1621 if (job->error() == 0) {
1622 Q_EMIT operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1623 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1624 Q_EMIT errorMessage(job->errorString());
1625 }
1626 }
1627
1628 void DolphinView::slotRenamingResult(KJob* job)
1629 {
1630 if (job->error()) {
1631 KIO::CopyJob *copyJob = qobject_cast<KIO::CopyJob *>(job);
1632 Q_ASSERT(copyJob);
1633 const QUrl newUrl = copyJob->destUrl();
1634 const int index = m_model->index(newUrl);
1635 if (index >= 0) {
1636 QHash<QByteArray, QVariant> data;
1637 const QUrl oldUrl = copyJob->srcUrls().at(0);
1638 data.insert("text", oldUrl.fileName());
1639 m_model->setData(index, data);
1640 }
1641 }
1642 }
1643
1644 void DolphinView::slotDirectoryLoadingStarted()
1645 {
1646 m_loading = true;
1647 updatePlaceholderLabel();
1648
1649 // Disable the writestate temporary until it can be determined in a fast way
1650 // in DolphinView::slotDirectoryLoadingCompleted()
1651 if (m_isFolderWritable) {
1652 m_isFolderWritable = false;
1653 Q_EMIT writeStateChanged(m_isFolderWritable);
1654 }
1655
1656 Q_EMIT directoryLoadingStarted();
1657 }
1658
1659 void DolphinView::slotDirectoryLoadingCompleted()
1660 {
1661 m_loading = false;
1662
1663 // Update the view-state. This has to be done asynchronously
1664 // because the view might not be in its final state yet.
1665 QTimer::singleShot(0, this, &DolphinView::updateViewState);
1666
1667 // Update the placeholder label in case we found that the folder was empty
1668 // after loading it
1669
1670 Q_EMIT directoryLoadingCompleted();
1671
1672 updatePlaceholderLabel();
1673 updateWritableState();
1674 }
1675
1676 void DolphinView::slotDirectoryLoadingCanceled()
1677 {
1678 m_loading = false;
1679
1680 updatePlaceholderLabel();
1681
1682 Q_EMIT directoryLoadingCanceled();
1683 }
1684
1685 void DolphinView::slotItemsChanged()
1686 {
1687 m_assureVisibleCurrentIndex = false;
1688 }
1689
1690 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous)
1691 {
1692 Q_UNUSED(previous)
1693 Q_ASSERT(m_model->sortOrder() == current);
1694
1695 ViewProperties props(viewPropertiesUrl());
1696 props.setSortOrder(current);
1697
1698 Q_EMIT sortOrderChanged(current);
1699 }
1700
1701 void DolphinView::slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous)
1702 {
1703 Q_UNUSED(previous)
1704 Q_ASSERT(m_model->sortRole() == current);
1705
1706 ViewProperties props(viewPropertiesUrl());
1707 props.setSortRole(current);
1708
1709 Q_EMIT sortRoleChanged(current);
1710 }
1711
1712 void DolphinView::slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
1713 const QList<QByteArray>& previous)
1714 {
1715 Q_UNUSED(previous)
1716 Q_ASSERT(m_container->controller()->view()->visibleRoles() == current);
1717
1718 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1719
1720 m_visibleRoles = current;
1721
1722 ViewProperties props(viewPropertiesUrl());
1723 props.setVisibleRoles(m_visibleRoles);
1724
1725 Q_EMIT visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1726 }
1727
1728 void DolphinView::slotRoleEditingCanceled()
1729 {
1730 disconnect(m_view, &DolphinItemListView::roleEditingFinished,
1731 this, &DolphinView::slotRoleEditingFinished);
1732 }
1733
1734 void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
1735 {
1736 disconnect(m_view, &DolphinItemListView::roleEditingFinished,
1737 this, &DolphinView::slotRoleEditingFinished);
1738
1739 if (index < 0 || index >= m_model->count()) {
1740 return;
1741 }
1742
1743 if (role == "text") {
1744 const KFileItem oldItem = m_model->fileItem(index);
1745 const QString newName = value.toString();
1746 if (!newName.isEmpty() && newName != oldItem.text() && newName != QLatin1Char('.') && newName != QLatin1String("..")) {
1747 const QUrl oldUrl = oldItem.url();
1748
1749 QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename);
1750 newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName));
1751
1752 #ifndef Q_OS_WIN
1753 //Confirm hiding file/directory by renaming inline
1754 if (!hiddenFilesShown() && newName.startsWith(QLatin1Char('.')) && !oldItem.name().startsWith(QLatin1Char('.'))) {
1755 KGuiItem yesGuiItem(KStandardGuiItem::yes());
1756 yesGuiItem.setText(i18nc("@action:button", "Rename and Hide"));
1757
1758 const auto code = KMessageBox::questionYesNo(this,
1759 oldItem.isFile() ? i18n("Adding a dot to the beginning of this file's name will hide it from view.\n"
1760 "Do you still want to rename it?")
1761 : i18n("Adding a dot to the beginning of this folder's name will hide it from view.\n"
1762 "Do you still want to rename it?"),
1763 oldItem.isFile() ? i18n("Hide this File?") : i18n("Hide this Folder?"),
1764 yesGuiItem,
1765 KStandardGuiItem::cancel(),
1766 QStringLiteral("ConfirmHide")
1767 );
1768
1769 if (code == KMessageBox::No) {
1770 return;
1771 }
1772 }
1773 #endif
1774
1775 const bool newNameExistsAlready = (m_model->index(newUrl) >= 0);
1776 if (!newNameExistsAlready) {
1777 // Only change the data in the model if no item with the new name
1778 // is in the model yet. If there is an item with the new name
1779 // already, calling KIO::CopyJob will open a dialog
1780 // asking for a new name, and KFileItemModel will update the
1781 // data when the dir lister signals that the file name has changed.
1782 QHash<QByteArray, QVariant> data;
1783 data.insert(role, value);
1784 m_model->setData(index, data);
1785 }
1786
1787 KIO::Job * job = KIO::moveAs(oldUrl, newUrl);
1788 KJobWidgets::setWindow(job, this);
1789 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job);
1790 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
1791
1792 forceUrlsSelection(newUrl, {newUrl});
1793
1794 if (!newNameExistsAlready) {
1795 // Only connect the result signal if there is no item with the new name
1796 // in the model yet, see bug 328262.
1797 connect(job, &KJob::result, this, &DolphinView::slotRenamingResult);
1798 }
1799 }
1800 }
1801 }
1802
1803 void DolphinView::loadDirectory(const QUrl& url, bool reload)
1804 {
1805 if (!url.isValid()) {
1806 const QString location(url.toDisplayString(QUrl::PreferLocalFile));
1807 if (location.isEmpty()) {
1808 Q_EMIT errorMessage(i18nc("@info:status", "The location is empty."));
1809 } else {
1810 Q_EMIT errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1811 }
1812 return;
1813 }
1814
1815 if (reload) {
1816 m_model->refreshDirectory(url);
1817 } else {
1818 m_model->loadDirectory(url);
1819 }
1820 }
1821
1822 void DolphinView::applyViewProperties()
1823 {
1824 const ViewProperties props(viewPropertiesUrl());
1825 applyViewProperties(props);
1826 }
1827
1828 void DolphinView::applyViewProperties(const ViewProperties& props)
1829 {
1830 m_view->beginTransaction();
1831
1832 const Mode mode = props.viewMode();
1833 if (m_mode != mode) {
1834 const Mode previousMode = m_mode;
1835 m_mode = mode;
1836
1837 // Changing the mode might result in changing
1838 // the zoom level. Remember the old zoom level so
1839 // that zoomLevelChanged() can get emitted.
1840 const int oldZoomLevel = m_view->zoomLevel();
1841 applyModeToView();
1842
1843 Q_EMIT modeChanged(m_mode, previousMode);
1844
1845 if (m_view->zoomLevel() != oldZoomLevel) {
1846 Q_EMIT zoomLevelChanged(m_view->zoomLevel(), oldZoomLevel);
1847 }
1848 }
1849
1850 const bool hiddenFilesShown = props.hiddenFilesShown();
1851 if (hiddenFilesShown != m_model->showHiddenFiles()) {
1852 m_model->setShowHiddenFiles(hiddenFilesShown);
1853 Q_EMIT hiddenFilesShownChanged(hiddenFilesShown);
1854 }
1855
1856 const bool groupedSorting = props.groupedSorting();
1857 if (groupedSorting != m_model->groupedSorting()) {
1858 m_model->setGroupedSorting(groupedSorting);
1859 Q_EMIT groupedSortingChanged(groupedSorting);
1860 }
1861
1862 const QByteArray sortRole = props.sortRole();
1863 if (sortRole != m_model->sortRole()) {
1864 m_model->setSortRole(sortRole);
1865 Q_EMIT sortRoleChanged(sortRole);
1866 }
1867
1868 const Qt::SortOrder sortOrder = props.sortOrder();
1869 if (sortOrder != m_model->sortOrder()) {
1870 m_model->setSortOrder(sortOrder);
1871 Q_EMIT sortOrderChanged(sortOrder);
1872 }
1873
1874 const bool sortFoldersFirst = props.sortFoldersFirst();
1875 if (sortFoldersFirst != m_model->sortDirectoriesFirst()) {
1876 m_model->setSortDirectoriesFirst(sortFoldersFirst);
1877 Q_EMIT sortFoldersFirstChanged(sortFoldersFirst);
1878 }
1879
1880 const QList<QByteArray> visibleRoles = props.visibleRoles();
1881 if (visibleRoles != m_visibleRoles) {
1882 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1883 m_visibleRoles = visibleRoles;
1884 m_view->setVisibleRoles(visibleRoles);
1885 Q_EMIT visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1886 }
1887
1888 const bool previewsShown = props.previewsShown();
1889 if (previewsShown != m_view->previewsShown()) {
1890 const int oldZoomLevel = zoomLevel();
1891
1892 m_view->setPreviewsShown(previewsShown);
1893 Q_EMIT previewsShownChanged(previewsShown);
1894
1895 // Changing the preview-state might result in a changed zoom-level
1896 if (oldZoomLevel != zoomLevel()) {
1897 Q_EMIT zoomLevelChanged(zoomLevel(), oldZoomLevel);
1898 }
1899 }
1900
1901 KItemListView* itemListView = m_container->controller()->view();
1902 if (itemListView->isHeaderVisible()) {
1903 KItemListHeader* header = itemListView->header();
1904 const QList<int> headerColumnWidths = props.headerColumnWidths();
1905 const int rolesCount = m_visibleRoles.count();
1906 if (headerColumnWidths.count() == rolesCount) {
1907 header->setAutomaticColumnResizing(false);
1908
1909 QHash<QByteArray, qreal> columnWidths;
1910 for (int i = 0; i < rolesCount; ++i) {
1911 columnWidths.insert(m_visibleRoles[i], headerColumnWidths[i]);
1912 }
1913 header->setColumnWidths(columnWidths);
1914 } else {
1915 header->setAutomaticColumnResizing(true);
1916 }
1917 }
1918
1919 m_view->endTransaction();
1920 }
1921
1922 void DolphinView::applyModeToView()
1923 {
1924 switch (m_mode) {
1925 case IconsView: m_view->setItemLayout(KFileItemListView::IconsLayout); break;
1926 case CompactView: m_view->setItemLayout(KFileItemListView::CompactLayout); break;
1927 case DetailsView: m_view->setItemLayout(KFileItemListView::DetailsLayout); break;
1928 default: Q_ASSERT(false); break;
1929 }
1930 }
1931
1932 void DolphinView::pasteToUrl(const QUrl& url)
1933 {
1934 KIO::PasteJob *job = KIO::paste(QApplication::clipboard()->mimeData(), url);
1935 KJobWidgets::setWindow(job, this);
1936 m_clearSelectionBeforeSelectingNewItems = true;
1937 m_markFirstNewlySelectedItemAsCurrent = true;
1938 connect(job, &KIO::PasteJob::itemCreated, this, &DolphinView::slotItemCreated);
1939 connect(job, &KIO::PasteJob::result, this, &DolphinView::slotJobResult);
1940 }
1941
1942 QList<QUrl> DolphinView::simplifiedSelectedUrls() const
1943 {
1944 QList<QUrl> urls;
1945
1946 const KFileItemList items = selectedItems();
1947 urls.reserve(items.count());
1948 for (const KFileItem& item : items) {
1949 urls.append(item.url());
1950 }
1951
1952 if (itemsExpandable()) {
1953 // TODO: Check if we still need KDirModel for this in KDE 5.0
1954 urls = KDirModel::simplifiedUrlList(urls);
1955 }
1956
1957 return urls;
1958 }
1959
1960 QMimeData* DolphinView::selectionMimeData() const
1961 {
1962 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1963 const KItemSet selectedIndexes = selectionManager->selectedItems();
1964
1965 return m_model->createMimeData(selectedIndexes);
1966 }
1967
1968 void DolphinView::updateWritableState()
1969 {
1970 const bool wasFolderWritable = m_isFolderWritable;
1971 m_isFolderWritable = false;
1972
1973 KFileItem item = m_model->rootItem();
1974 if (item.isNull()) {
1975 // Try to find out if the URL is writable even if the "root item" is
1976 // null, see https://bugs.kde.org/show_bug.cgi?id=330001
1977 item = KFileItem(url());
1978 item.setDelayedMimeTypes(true);
1979 }
1980
1981 KFileItemListProperties capabilities(KFileItemList() << item);
1982 m_isFolderWritable = capabilities.supportsWriting();
1983
1984 if (m_isFolderWritable != wasFolderWritable) {
1985 Q_EMIT writeStateChanged(m_isFolderWritable);
1986 }
1987 }
1988
1989 QUrl DolphinView::viewPropertiesUrl() const
1990 {
1991 if (m_viewPropertiesContext.isEmpty()) {
1992 return m_url;
1993 }
1994
1995 QUrl url;
1996 url.setScheme(m_url.scheme());
1997 url.setPath(m_viewPropertiesContext);
1998 return url;
1999 }
2000
2001 void DolphinView::slotRenameDialogRenamingFinished(const QList<QUrl>& urls)
2002 {
2003 forceUrlsSelection(urls.first(), urls);
2004 }
2005
2006 void DolphinView::forceUrlsSelection(const QUrl& current, const QList<QUrl>& selected)
2007 {
2008 clearSelection();
2009 m_clearSelectionBeforeSelectingNewItems = true;
2010 markUrlAsCurrent(current);
2011 markUrlsAsSelected(selected);
2012 }
2013
2014 void DolphinView::copyPathToClipboard()
2015 {
2016 const KFileItemList list = selectedItems();
2017 if (list.isEmpty()) {
2018 return;
2019 }
2020 const KFileItem& item = list.at(0);
2021 QString path = item.localPath();
2022 if (path.isEmpty()) {
2023 path = item.url().toDisplayString();
2024 }
2025 QClipboard* clipboard = QApplication::clipboard();
2026 if (clipboard == nullptr) {
2027 return;
2028 }
2029 clipboard->setText(path);
2030 }
2031
2032 void DolphinView::slotIncreaseZoom()
2033 {
2034 setZoomLevel(zoomLevel() + 1);
2035 }
2036
2037 void DolphinView::slotDecreaseZoom()
2038 {
2039 setZoomLevel(zoomLevel() - 1);
2040 }
2041
2042 void DolphinView::slotSwipeUp()
2043 {
2044 Q_EMIT goUpRequested();
2045 }
2046
2047 void DolphinView::updatePlaceholderLabel()
2048 {
2049 if (m_loading || itemsCount() > 0) {
2050 m_placeholderLabel->setVisible(false);
2051 return;
2052 }
2053
2054 if (!nameFilter().isEmpty()) {
2055 m_placeholderLabel->setText(i18n("No items matching the filter"));
2056 } else if (m_url.scheme() == QLatin1String("baloosearch") || m_url.scheme() == QLatin1String("filenamesearch")) {
2057 m_placeholderLabel->setText(i18n("No items matching the search"));
2058 } else if (m_url.scheme() == QLatin1String("trash")) {
2059 m_placeholderLabel->setText(i18n("Trash is empty"));
2060 } else if (m_url.scheme() == QLatin1String("tags")) {
2061 m_placeholderLabel->setText(i18n("No tags"));
2062 } else if (m_url.scheme() == QLatin1String("recentlyused")) {
2063 m_placeholderLabel->setText(i18n("No recently used items"));
2064 } else if (m_url.scheme() == QLatin1String("smb")) {
2065 m_placeholderLabel->setText(i18n("No shared folders found"));
2066 } else if (m_url.scheme() == QLatin1String("network")) {
2067 m_placeholderLabel->setText(i18n("No relevant network resources found"));
2068 } else if (m_url.scheme() == QLatin1String("mtp")) {
2069 m_placeholderLabel->setText(i18n("No MTP-compatible devices found"));
2070 } else if (m_url.scheme() == QLatin1String("bluetooth")) {
2071 m_placeholderLabel->setText(i18n("No Bluetooth devices found"));
2072 } else {
2073 m_placeholderLabel->setText(i18n("Folder is empty"));
2074 }
2075
2076 m_placeholderLabel->setVisible(true);
2077 }