diff --git a/containments/desktop/plugins/folder/foldermodel.cpp b/containments/desktop/plugins/folder/foldermodel.cpp --- a/containments/desktop/plugins/folder/foldermodel.cpp +++ b/containments/desktop/plugins/folder/foldermodel.cpp @@ -1248,6 +1248,8 @@ delete m_dragImages.take(idx.row()); } } + + updateActions(); } bool FolderModel::isBlank(int row) const @@ -1604,19 +1606,52 @@ void FolderModel::updateActions() { + const QModelIndexList indexes = m_selectionModel->selectedIndexes(); + + KFileItemList items; + QList urls; + bool hasRemoteFiles = false; + bool isTrashLink = false; + const bool isTrash = (resolvedUrl().scheme() == QLatin1String("trash")); + + if (indexes.isEmpty()) { + items << rootItem(); + } else { + items.reserve(indexes.count()); + urls.reserve(indexes.count()); + for (const QModelIndex &index : indexes) { + KFileItem item = itemForIndex(index); + if (!item.isNull()) { + hasRemoteFiles |= item.localPath().isEmpty(); + items.append(item); + urls.append(item.url()); + } + } + } + + KFileItemListProperties itemProperties(items); + // Check if we're showing the menu for the trash link + if (items.count() == 1 && items.at(0).isDesktopFile()) { + KDesktopFile file(items.at(0).localPath()); + if (file.hasLinkType() && file.readUrl() == QLatin1String("trash:/")) { + isTrashLink = true; + } + } + if (m_newMenu) { m_newMenu->checkUpToDate(); m_newMenu->setPopupFiles(m_dirModel->dirLister()->url()); // we need to set here as well, when the menu is shown via AppletInterface::eventFilter m_menuPosition = QCursor::pos(); - } - - const bool isTrash = (resolvedUrl().scheme() == QLatin1String("trash")); - QAction *emptyTrash = m_actionCollection.action(QStringLiteral("emptyTrash")); + if (QAction *newMenuAction = m_actionCollection.action(QStringLiteral("newMenu"))) { + newMenuAction->setEnabled(itemProperties.supportsWriting()); + newMenuAction->setVisible(!isTrash); + } + } - if (emptyTrash) { - if (isTrash) { + if (QAction *emptyTrash = m_actionCollection.action(QStringLiteral("emptyTrash"))) { + if (isTrash || isTrashLink) { emptyTrash->setVisible(true); emptyTrash->setEnabled(!isTrashEmpty()); } else { @@ -1628,9 +1663,19 @@ restoreFromTrash->setVisible(isTrash); } - QAction *paste = m_actionCollection.action(QStringLiteral("paste")); + if (QAction *moveToTrash = m_actionCollection.action(QStringLiteral("trash"))) { + moveToTrash->setVisible(!hasRemoteFiles && itemProperties.supportsMoving()); + } - if (paste) { + if (QAction *del = m_actionCollection.action(QStringLiteral("del"))) { + del->setVisible(itemProperties.supportsDeleting()); + } + + if (QAction *cut = m_actionCollection.action(QStringLiteral("cut"))) { + cut->setEnabled(itemProperties.supportsDeleting()); + } + + if (QAction *paste = m_actionCollection.action(QStringLiteral("paste"))) { bool enable = false; const QString pasteText = KIO::pasteActionText(QApplication::clipboard()->mimeData(), @@ -1644,25 +1689,29 @@ paste->setEnabled(false); } - QAction* pasteTo = m_actionCollection.action(QStringLiteral("pasteto")); - - if (pasteTo) { + if (QAction *pasteTo = m_actionCollection.action(QStringLiteral("pasteto"))) { + pasteTo->setVisible(itemProperties.isDirectory() && itemProperties.supportsWriting()); pasteTo->setEnabled(paste->isEnabled()); pasteTo->setText(paste->text()); } } + + if (QAction *rename = m_actionCollection.action(QStringLiteral("rename"))) { + rename->setEnabled(itemProperties.supportsMoving()); + rename->setVisible(!isTrash); + } } void FolderModel::openContextMenu(QQuickItem *visualParent, Qt::KeyboardModifiers modifiers) { - QModelIndexList indexes = m_selectionModel->selectedIndexes(); - if (m_usedByContainment && !KAuthorized::authorize(QStringLiteral("action/kdesktop_rmb"))) { return; } updateActions(); + const QModelIndexList indexes = m_selectionModel->selectedIndexes(); + QMenu *menu = new QMenu(); if (!m_fileItemActions) { m_fileItemActions = new KFileItemActions(this); @@ -1685,70 +1734,45 @@ } else { KFileItemList items; QList urls; - bool hasRemoteFiles = false; - bool isTrashLink = false; items.reserve(indexes.count()); urls.reserve(indexes.count()); - foreach (const QModelIndex &index, indexes) { + for (const QModelIndex &index : indexes) { KFileItem item = itemForIndex(index); if (!item.isNull()) { - hasRemoteFiles |= item.localPath().isEmpty(); items.append(item); urls.append(item.url()); } } - KFileItemListProperties itemProperties(items); - // Check if we're showing the menu for the trash link - if (items.count() == 1 && items.at(0).isDesktopFile()) { - KDesktopFile file(items.at(0).localPath()); - if (file.hasLinkType() && file.readUrl() == QLatin1String("trash:/")) { - isTrashLink = true; - } - } + KFileItemListProperties itemProperties(items); // Start adding the actions: menu->addAction(m_actionCollection.action(QStringLiteral("open"))); menu->addSeparator(); - if (itemProperties.supportsDeleting()) { - menu->addAction(m_actionCollection.action(QStringLiteral("cut"))); - } + menu->addAction(m_actionCollection.action(QStringLiteral("cut"))); menu->addAction(m_actionCollection.action(QStringLiteral("copy"))); - - if (itemProperties.isDirectory() && itemProperties.supportsWriting()) { - menu->addAction(m_actionCollection.action(QStringLiteral("pasteto"))); - } - + menu->addAction(m_actionCollection.action(QStringLiteral("pasteto"))); menu->addAction(m_actionCollection.action(QStringLiteral("rename"))); menu->addAction(m_actionCollection.action(QStringLiteral("restoreFromTrash"))); KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig(QStringLiteral("kdeglobals"), KConfig::NoGlobals); KConfigGroup cg(globalConfig, "KDE"); bool showDeleteCommand = cg.readEntry("ShowDeleteCommand", false); - // When we're showing the menu for the trash link, offer "Empty Trash" instead - // of the "Move to Trash" action. - if (isTrashLink) { - QAction *emptyTrashAction = m_actionCollection.action(QStringLiteral("emptyTrash")); - if (emptyTrashAction) { - // We explicitly force the action visible here, as it relies on the KFileItemList - // we collected above. In updateActions() we don't have it and since this is always - // called before we open the menu, it would correct visibility again when opening - // the context menu for other items later. - emptyTrashAction->setVisible(true); - emptyTrashAction->setEnabled(!isTrashEmpty()); - menu->addAction(emptyTrashAction); - } + menu->addAction(m_actionCollection.action(QStringLiteral("emptyTrash"))); + if (modifiers.testFlag(Qt::ShiftModifier)) { + showDeleteCommand = true; } else { - if (!modifiers.testFlag(Qt::ShiftModifier) && !hasRemoteFiles && itemProperties.supportsMoving()) { - menu->addAction(m_actionCollection.action(QStringLiteral("trash"))); - } else { - showDeleteCommand = true; + if (QAction *trashAction = m_actionCollection.action(QStringLiteral("trash"))) { + menu->addAction(trashAction); + if (!trashAction->isVisible()) { + showDeleteCommand = true; + } } } - if (showDeleteCommand && itemProperties.supportsDeleting()) { + if (showDeleteCommand) { menu->addAction(m_actionCollection.action(QStringLiteral("del"))); } @@ -1776,7 +1800,6 @@ QObject::connect(act, &QAction::triggered, this, &FolderModel::openPropertiesDialog); menu->addAction(act); } - } if (visualParent) { @@ -1838,6 +1861,12 @@ return; } + if (QAction *action = m_actionCollection.action(QStringLiteral("copy"))) { + if (!action->isEnabled()) { + return; + } + } + QMimeData *mimeData = QSortFilterProxyModel::mimeData(m_selectionModel->selectedIndexes()); QApplication::clipboard()->setMimeData(mimeData); } @@ -1848,13 +1877,25 @@ return; } + if (QAction *action = m_actionCollection.action(QStringLiteral("cut"))) { + if (!action->isEnabled()) { + return; + } + } + QMimeData *mimeData = QSortFilterProxyModel::mimeData(m_selectionModel->selectedIndexes()); KIO::setClipboardDataCut(mimeData, true); QApplication::clipboard()->setMimeData(mimeData); } void FolderModel::paste() { + if (QAction *action = m_actionCollection.action(QStringLiteral("paste"))) { + if (!action->isEnabled()) { + return; + } + } + KIO::paste(QApplication::clipboard()->mimeData(), m_dirModel->dirLister()->url()); } @@ -1913,6 +1954,12 @@ return; } + if (QAction *action = m_actionCollection.action(QStringLiteral("trash"))) { + if (!action->isEnabled()) { + return; + } + } + const QList urls = selectedUrls(); KIO::JobUiDelegate uiDelegate; @@ -1929,6 +1976,12 @@ return; } + if (QAction *action = m_actionCollection.action(QStringLiteral("del"))) { + if (!action->isEnabled()) { + return; + } + } + const QList urls = selectedUrls(); KIO::JobUiDelegate uiDelegate;