dolphincontextmenu.cpp 17.4 KB
Newer Older
1 2 3 4 5
/*
 * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Cvetoslav Ludmiloff
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
6 7 8

#include "dolphincontextmenu.h"

Roman Inflianskas's avatar
Roman Inflianskas committed
9
#include "dolphin_generalsettings.h"
Peter Penz's avatar
Peter Penz committed
10
#include "dolphinmainwindow.h"
Peter Penz's avatar
Peter Penz committed
11
#include "dolphinnewfilemenu.h"
12
#include "dolphinplacesmodelsingleton.h"
13
#include "dolphinremoveaction.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
14 15 16
#include "dolphinviewcontainer.h"
#include "panels/places/placesitem.h"
#include "panels/places/placesitemmodel.h"
17
#include "trash/dolphintrash.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
18 19
#include "views/dolphinview.h"
#include "views/viewmodecontroller.h"
Peter Penz's avatar
Peter Penz committed
20

Roman Inflianskas's avatar
Roman Inflianskas committed
21
#include <KActionCollection>
22 23
#include <KFileItemActions>
#include <KFileItemListProperties>
24 25
#include <KIO/EmptyTrashJob>
#include <KIO/JobUiDelegate>
26
#include <KIO/Paste>
Roman Inflianskas's avatar
Roman Inflianskas committed
27
#include <KIO/RestoreJob>
28
#include <KJobWidgets>
Roman Inflianskas's avatar
Roman Inflianskas committed
29
#include <KLocalizedString>
30
#include <KNewFileMenu>
31
#include <KPluginMetaData>
32 33
#include <KService>
#include <KStandardAction>
34
#include <KToolBar>
35

36 37
#include <QApplication>
#include <QClipboard>
David Faure's avatar
David Faure committed
38
#include <QKeyEvent>
Roman Inflianskas's avatar
Roman Inflianskas committed
39
#include <QMenuBar>
40
#include <QMimeDatabase>
41

42
DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
Peter Penz's avatar
Peter Penz committed
43
                                       const QPoint& pos,
44
                                       const KFileItem& fileInfo,
Lukáš Tinkl's avatar
Lukáš Tinkl committed
45
                                       const QUrl& baseUrl) :
46
    QMenu(parent),
Peter Penz's avatar
Peter Penz committed
47
    m_pos(pos),
48 49 50
    m_mainWindow(parent),
    m_fileInfo(fileInfo),
    m_baseUrl(baseUrl),
Kevin Funk's avatar
Kevin Funk committed
51
    m_baseFileItem(nullptr),
52
    m_selectedItems(),
53
    m_selectedItemsProperties(nullptr),
54
    m_context(NoContext),
55
    m_copyToMenu(parent),
56
    m_customActions(),
57
    m_command(None),
Kevin Funk's avatar
Kevin Funk committed
58
    m_removeAction(nullptr)
59
{
60 61
    // The context menu either accesses the URLs of the selected items
    // or the items itself. To increase the performance both lists are cached.
62
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
63
    m_selectedItems = view->selectedItems();
64 65
}

66
DolphinContextMenu::~DolphinContextMenu()
Peter Penz's avatar
Peter Penz committed
67
{
68 69
    delete m_baseFileItem;
    m_baseFileItem = nullptr;
70
    delete m_selectedItemsProperties;
71
    m_selectedItemsProperties = nullptr;
Peter Penz's avatar
Peter Penz committed
72
}
73

74 75 76 77 78
void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
{
    m_customActions = actions;
}

79
DolphinContextMenu::Command DolphinContextMenu::open()
80
{
81
    // get the context information
82 83
    const auto scheme = m_baseUrl.scheme();
    if (scheme == QLatin1String("trash")) {
84
        m_context |= TrashContext;
85
    } else if (scheme.contains(QLatin1String("search"))) {
86
        m_context |= SearchContext;
87
    } else if (scheme.contains(QLatin1String("timeline"))) {
88
        m_context |= TimelineContext;
89
    }
90

91
    if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
92 93
        m_context |= ItemContext;
        // TODO: handle other use cases like devices + desktop files
94
    }
95 96 97 98 99

    // open the corresponding popup for the context
    if (m_context & TrashContext) {
        if (m_context & ItemContext) {
            openTrashItemContextMenu();
100
        } else {
101 102
            openTrashContextMenu();
        }
103
    } else if (m_context & ItemContext) {
104
        openItemContextMenu();
105
    } else {
106 107
        openViewportContextMenu();
    }
108 109

    return m_command;
110 111
}

112
void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
113
{
114
    if (m_removeAction && ev->key() == Qt::Key_Shift) {
115
        m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
116
    }
117
    QMenu::keyPressEvent(ev);
118 119
}

120
void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
121
{
122
    if (m_removeAction && ev->key() == Qt::Key_Shift) {
123
        m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
124
    }
125
    QMenu::keyReleaseEvent(ev);
126 127
}

128 129 130 131
void DolphinContextMenu::openTrashContextMenu()
{
    Q_ASSERT(m_context & TrashContext);

132
    QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
133
    emptyTrashAction->setEnabled(!Trash::isEmpty());
134
    addAction(emptyTrashAction);
135

136
    addCustomActions();
137

138
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
139
    addAction(propertiesAction);
140

141 142
    addShowMenuBarAction();

143
    if (exec(m_pos) == emptyTrashAction) {
144
        Trash::empty(m_mainWindow);
145 146 147 148 149 150 151 152
    }
}

void DolphinContextMenu::openTrashItemContextMenu()
{
    Q_ASSERT(m_context & TrashContext);
    Q_ASSERT(m_context & ItemContext);

Shubham  .'s avatar
Shubham . committed
153
    QAction* restoreAction = new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow);
154
    addAction(restoreAction);
155

156
    QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
157
    addAction(deleteAction);
158

159
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
160
    addAction(propertiesAction);
161

162
    if (exec(m_pos) == restoreAction) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
163
        QList<QUrl> selectedUrls;
164
        selectedUrls.reserve(m_selectedItems.count());
Alexander Lohnau's avatar
Alexander Lohnau committed
165
        for (const KFileItem &item : qAsConst(m_selectedItems)) {
166 167 168
            selectedUrls.append(item.url());
        }

169 170 171
        KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
        KJobWidgets::setWindow(job, m_mainWindow);
        job->uiDelegate()->setAutoErrorHandlingEnabled(true);
172 173 174
    }
}

175 176 177 178 179 180 181
void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemActions)
{
    // insert 'Open in new window' and 'Open in new tab' entries

    const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();

    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
182
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
183 184 185 186 187 188 189 190 191

    // Insert 'Open With' entries
    addOpenWithActions(fileItemActions);

    // set up 'Create New' menu
     DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
     newFileMenu->checkUpToDate();
192
     newFileMenu->setPopupFiles(QList<QUrl>() << m_fileInfo.url());
193 194 195 196 197 198 199 200 201 202 203 204
     newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
     connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
     connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);

     QMenu* menu = newFileMenu->menu();
     menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
     menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
     addMenu(menu);

     addSeparator();
}

205 206
void DolphinContextMenu::openItemContextMenu()
{
207
    Q_ASSERT(!m_fileInfo.isNull());
208

209 210 211
    QAction* openParentAction = nullptr;
    QAction* openParentInNewWindowAction = nullptr;
    QAction* openParentInNewTabAction = nullptr;
212 213
    const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();

214 215 216 217
    KFileItemActions fileItemActions;
    fileItemActions.setParentWidget(m_mainWindow);
    fileItemActions.setItemListProperties(selectedItemsProps);

218
    if (m_selectedItems.count() == 1) {
219
        // single files
220
        if (m_fileInfo.isDir()) {
221 222
            addDirectoryItemContextMenu(fileItemActions);
        } else if (m_context & TimelineContext || m_context & SearchContext) {
223 224
            addOpenWithActions(fileItemActions);

225
            openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
226 227 228 229 230
                                           i18nc("@action:inmenu",
                                                 "Open Path"),
                                           this);
            addAction(openParentAction);

231
            openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
232
                                                    i18nc("@action:inmenu",
233
                                                          "Open Path in New Window"),
234
                                                    this);
235
            addAction(openParentInNewWindowAction);
236

237
            openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
238
                                                   i18nc("@action:inmenu",
239
                                                         "Open Path in New Tab"),
240
                                                   this);
241
            addAction(openParentInNewTabAction);
242

243 244 245 246 247 248 249
            addSeparator();
        } else {
            // Insert 'Open With" entries
            addOpenWithActions(fileItemActions);
        }
        if (m_fileInfo.isLink()) {
            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
250 251 252
            addSeparator();
        }
    } else {
253
        // multiple files
254
        bool selectionHasOnlyDirs = true;
255
        for (const auto &item : qAsConst(m_selectedItems)) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
256
            const QUrl& url = DolphinView::openItemAsFolderUrl(item);
257
            if (url.isEmpty()) {
258 259 260 261 262 263 264
                selectionHasOnlyDirs = false;
                break;
            }
        }

        if (selectionHasOnlyDirs) {
            // insert 'Open in new tab' entry
265
            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
Peter Penz's avatar
Peter Penz committed
266
        }
267 268
        // Insert 'Open With" entries
        addOpenWithActions(fileItemActions);
269
    }
270

271
    insertDefaultItemActions(selectedItemsProps);
272

273 274 275 276 277 278 279 280 281
    // insert 'Add to Places' entry if appropriate
    if (m_selectedItems.count() == 1) {
        if (m_fileInfo.isDir()) {
            if (!placeExists(m_fileInfo.url())) {
                addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
            }
        }
    }

282
    addSeparator();
283

284
    fileItemActions.addServiceActionsTo(this);
Elvis Angelaccio's avatar
Elvis Angelaccio committed
285
    fileItemActions.addPluginActionsTo(this);
286 287

    addVersionControlPluginActions();
288

289
    // insert 'Copy To' and 'Move To' sub menus
290
    if (GeneralSettings::showCopyMoveMenu()) {
291
        m_copyToMenu.setUrls(m_selectedItems.urlList());
292
        m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
293
        m_copyToMenu.setAutoErrorHandlingEnabled(true);
294
        m_copyToMenu.addActionsTo(this);
Peter Penz's avatar
Peter Penz committed
295 296
    }

297
    // insert 'Properties...' entry
298
    addSeparator();
299
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
300
    addAction(propertiesAction);
301

302
    QAction* activatedAction = exec(m_pos);
303
    if (activatedAction) {
304
        if (activatedAction == openParentAction) {
305
            m_command = OpenParentFolder;
306 307 308 309
        } else if (activatedAction == openParentInNewWindowAction) {
            m_command = OpenParentFolderInNewWindow;
        } else if (activatedAction == openParentInNewTabAction) {
            m_command = OpenParentFolderInNewTab;
310 311 312 313
        }
    }
}

314 315
void DolphinContextMenu::openViewportContextMenu()
{
316
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
Peter Penz's avatar
Peter Penz committed
317

318
    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
319 320 321
    KFileItemActions fileItemActions;
    fileItemActions.setParentWidget(m_mainWindow);
    fileItemActions.setItemListProperties(baseUrlProperties);
322

323 324 325 326
    // Set up and insert 'Create New' menu
    KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
    newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
    newFileMenu->checkUpToDate();
327
    newFileMenu->setPopupFiles(QList<QUrl>() << m_baseUrl);
328 329
    addMenu(newFileMenu->menu());

330 331 332
    // Show "open with" menu items even if the dir is empty, because there are legitimate
    // use cases for this, such as opening an empty dir in Kate or VSCode or something
    addOpenWithActions(fileItemActions);
333

334
    QAction* pasteAction = createPasteAction();
335 336 337
    if (pasteAction) {
        addAction(pasteAction);
    }
338

339
    // Insert 'Add to Places' entry if it's not already in the places panel
340
    if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
341
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
342
    }
343
    addSeparator();
344

345 346 347 348 349 350
    // Insert 'Sort By' and 'View Mode'
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));

    addSeparator();

351
    // Insert service actions
352
    fileItemActions.addServiceActionsTo(this);
Elvis Angelaccio's avatar
Elvis Angelaccio committed
353
    fileItemActions.addPluginActionsTo(this);
354 355

    addVersionControlPluginActions();
356

357
    addCustomActions();
358

359 360
    addSeparator();

361
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
362
    addAction(propertiesAction);
363

364
    addShowMenuBarAction();
365 366

    exec(m_pos);
367 368
}

369
void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
370
{
371
    const KActionCollection* collection = m_mainWindow->actionCollection();
372

Nikita Churaev's avatar
Nikita Churaev committed
373
    // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
374 375
    addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
    addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
376 377 378
    QAction* copyPathAction = collection->action(QString("copy_location"));
    copyPathAction->setEnabled(m_selectedItems.size() == 1);
    addAction(copyPathAction);
379 380 381 382
    QAction* pasteAction = createPasteAction();
    if (pasteAction) {
        addAction(pasteAction);
    }
Nate Graham's avatar
Nate Graham committed
383
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
384

385
    addSeparator();
386 387

    // Insert 'Rename'
388
    addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
389

390
    // Insert 'Move to Trash' and/or 'Delete'
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
                                    !properties.isLocal());
    const bool showMoveToTrashAction = (properties.isLocal() &&
                                        properties.supportsMoving());

    if (showDeleteAction && showMoveToTrashAction) {
        delete m_removeAction;
        m_removeAction = nullptr;
        addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
        addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
    } else if (showDeleteAction && !showMoveToTrashAction) {
        addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
    } else {
        if (!m_removeAction) {
            m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
406
        }
407 408
        addAction(m_removeAction);
        m_removeAction->update();
409 410 411
    }
}

412 413 414 415 416
void DolphinContextMenu::addShowMenuBarAction()
{
    const KActionCollection* ac = m_mainWindow->actionCollection();
    QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
    if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
417 418
        addSeparator();
        addAction(showMenuBar);
419 420 421
    }
}

Lukáš Tinkl's avatar
Lukáš Tinkl committed
422
bool DolphinContextMenu::placeExists(const QUrl& url) const
Harsh J Chouraria's avatar
Harsh J Chouraria committed
423
{
424 425 426 427 428
    const KFilePlacesModel* placesModel = DolphinPlacesModelSingleton::instance().placesModel();

    const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);

    return !matchedPlaces.isEmpty();
Harsh J Chouraria's avatar
Harsh J Chouraria committed
429 430
}

431 432
QAction* DolphinContextMenu::createPasteAction()
{
Kevin Funk's avatar
Kevin Funk committed
433
    QAction* action = nullptr;
434
    KFileItem destItem;
435
    if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) {
436
        destItem = m_fileInfo;
437
    } else {
438 439 440 441
        destItem = baseFileItem();
    }

    if (!destItem.isNull() && destItem.isDir()) {
442 443 444 445 446 447 448
        const QMimeData *mimeData = QApplication::clipboard()->mimeData();
        bool canPaste;
        const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem);
        if (canPaste) {
            if (destItem == m_fileInfo) {
                // if paste destination is a selected folder
                action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
449 450
                connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
            } else {
451
                action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
452 453
            }
        }
454 455 456 457 458
    }

    return action;
}

459
KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
460
{
461
    if (!m_selectedItemsProperties) {
462 463 464 465 466 467
        m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
    }
    return *m_selectedItemsProperties;
}

KFileItem DolphinContextMenu::baseFileItem()
468
{
469
    if (!m_baseFileItem) {
470 471 472 473 474 475 476
        const DolphinView* view = m_mainWindow->activeViewContainer()->view();
        KFileItem baseItem = view->rootItem();
        if (baseItem.isNull() || baseItem.url() != m_baseUrl) {
            m_baseFileItem = new KFileItem(m_baseUrl);
        } else {
            m_baseFileItem = new KFileItem(baseItem);
        }
477
    }
478
    return *m_baseFileItem;
479 480
}

481
void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
Peter Penz's avatar
Peter Penz committed
482 483
{
    // insert 'Open With...' action or sub menu
484
    fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
485 486 487
}

void DolphinContextMenu::addVersionControlPluginActions()
488 489
{
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
490 491
    const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
    if (!versionControlActions.isEmpty()) {
492
        addActions(versionControlActions);
493
        addSeparator();
494 495 496
    }
}

497
void DolphinContextMenu::addCustomActions()
498
{
499
    addActions(m_customActions);
500 501
}