Skip to content

Commit

Permalink
Prevent dragging on non-writable directories
Browse files Browse the repository at this point in the history
KFileItemModel::supportsDroppin now returns the rootItem when -1 is passed and checks for write access.
  • Loading branch information
Méven Car authored and Felix Ernst committed Mar 16, 2023
1 parent 43c2963 commit 9967a5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
9 changes: 7 additions & 2 deletions src/kitemviews/kfileitemmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,13 @@ int KFileItemModel::indexForKeyboardSearch(const QString &text, int startFromInd

bool KFileItemModel::supportsDropping(int index) const
{
const KFileItem item = fileItem(index);
return !item.isNull() && (item.isDir() || item.isDesktopFile());
KFileItem item;
if (index == -1) {
item = rootItem();
} else {
item = fileItem(index);
}
return !item.isNull() && ((item.isDir() && item.isWritable()) || item.isDesktopFile());
}

QString KFileItemModel::roleDescription(const QByteArray &role) const
Expand Down
34 changes: 19 additions & 15 deletions src/kitemviews/kitemlistcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ void KItemListController::slotAutoActivationTimeout()
*
* See Bug 293200 and 305783
*/
if (m_model->supportsDropping(index) && m_view->isUnderMouse()) {
if (m_view->isUnderMouse()) {
if (m_view->supportsItemExpanding() && m_model->isExpandable(index)) {
const bool expanded = m_model->isExpanded(index);
m_model->setExpanded(index, !expanded);
Expand Down Expand Up @@ -738,6 +738,7 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent *event, cons

const QPointF pos = transform.map(event->pos());
KItemListWidget *newHoveredWidget = widgetForDropPos(pos);
int index = -1;

if (oldHoveredWidget != newHoveredWidget) {
m_autoActivationTimer->stop();
Expand All @@ -755,25 +756,23 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent *event, cons
droppingBetweenItems = (m_view->showDropIndicator(pos) >= 0);
}

const int index = newHoveredWidget->index();
index = newHoveredWidget->index();

if (m_model->isDir(index)) {
hoveredDir = m_model->url(index);
}

if (!droppingBetweenItems) {
if (m_model->supportsDropping(index)) {
// Something has been dragged on an item.
m_view->hideDropIndicator();
if (!newHoveredWidget->isHovered()) {
newHoveredWidget->setHovered(true);
Q_EMIT itemHovered(index);
}
// Something has been dragged on an item.
m_view->hideDropIndicator();
if (!newHoveredWidget->isHovered()) {
newHoveredWidget->setHovered(true);
Q_EMIT itemHovered(index);
}

if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0) {
m_autoActivationTimer->setProperty("index", index);
m_autoActivationTimer->start();
}
if (!m_autoActivationTimer->isActive() && m_autoActivationTimer->interval() >= 0) {
m_autoActivationTimer->setProperty("index", index);
m_autoActivationTimer->start();
}
} else {
m_autoActivationTimer->stop();
Expand All @@ -790,8 +789,13 @@ bool KItemListController::dragMoveEvent(QGraphicsSceneDragDropEvent *event, cons
event->setDropAction(Qt::IgnoreAction);
event->ignore();
} else {
event->setDropAction(event->proposedAction());
event->accept();
if (m_model->supportsDropping(index)) {
event->setDropAction(event->proposedAction());
event->accept();
} else {
event->setDropAction(Qt::IgnoreAction);
event->ignore();
}
}
return false;
}
Expand Down

0 comments on commit 9967a5c

Please sign in to comment.