dolphincontextmenu.cpp 18.7 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"
10
#include "dolphin_contextmenusettings.h"
Peter Penz's avatar
Peter Penz committed
11
#include "dolphinmainwindow.h"
Peter Penz's avatar
Peter Penz committed
12
#include "dolphinnewfilemenu.h"
13
#include "dolphinplacesmodelsingleton.h"
14
#include "dolphinremoveaction.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
15
16
17
#include "dolphinviewcontainer.h"
#include "panels/places/placesitem.h"
#include "panels/places/placesitemmodel.h"
18
#include "trash/dolphintrash.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
19
20
#include "views/dolphinview.h"
#include "views/viewmodecontroller.h"
Peter Penz's avatar
Peter Penz committed
21

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

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

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

67
    QApplication::instance()->installEventFilter(this);
68
69
70

    static_cast<KHamburgerMenu *>(m_mainWindow->actionCollection()->
                action(QStringLiteral("hamburger_menu")))->addToMenu(this);
71
72
}

73
DolphinContextMenu::~DolphinContextMenu()
Peter Penz's avatar
Peter Penz committed
74
{
75
76
    delete m_baseFileItem;
    m_baseFileItem = nullptr;
77
    delete m_selectedItemsProperties;
78
    m_selectedItemsProperties = nullptr;
Peter Penz's avatar
Peter Penz committed
79
}
80

81
82
83
84
85
void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
{
    m_customActions = actions;
}

86
DolphinContextMenu::Command DolphinContextMenu::open()
87
{
88
    // get the context information
89
90
    const auto scheme = m_baseUrl.scheme();
    if (scheme == QLatin1String("trash")) {
91
        m_context |= TrashContext;
92
    } else if (scheme.contains(QLatin1String("search"))) {
93
        m_context |= SearchContext;
94
    } else if (scheme.contains(QLatin1String("timeline"))) {
95
        m_context |= TimelineContext;
96
    }
97

98
    if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
99
100
        m_context |= ItemContext;
        // TODO: handle other use cases like devices + desktop files
101
    }
102
103
104
105
106

    // open the corresponding popup for the context
    if (m_context & TrashContext) {
        if (m_context & ItemContext) {
            openTrashItemContextMenu();
107
        } else {
108
109
            openTrashContextMenu();
        }
110
    } else if (m_context & ItemContext) {
111
        openItemContextMenu();
112
    } else {
113
114
        openViewportContextMenu();
    }
115
116

    return m_command;
117
118
}

119
bool DolphinContextMenu::eventFilter(QObject* object, QEvent* event)
120
{
121
    Q_UNUSED(object)
122

123
124
    if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
        QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
125
126
127

        if (m_removeAction && keyEvent->key() == Qt::Key_Shift) {
            if (event->type() == QEvent::KeyPress) {
128
129
130
131
132
                m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
            } else {
                m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
            }
        }
133
    }
134
135

    return false;
136
137
}

138
139
140
141
void DolphinContextMenu::openTrashContextMenu()
{
    Q_ASSERT(m_context & TrashContext);

142
    QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
143
    emptyTrashAction->setEnabled(!Trash::isEmpty());
144
    addAction(emptyTrashAction);
145

146
    addCustomActions();
147

148
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
149
    addAction(propertiesAction);
150

151
    if (exec(m_pos) == emptyTrashAction) {
152
        Trash::empty(m_mainWindow);
153
154
155
156
157
158
159
160
    }
}

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

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

164
    QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
165
    addAction(deleteAction);
166

167
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
168
    addAction(propertiesAction);
169

170
    if (exec(m_pos) == restoreAction) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
171
        QList<QUrl> selectedUrls;
172
        selectedUrls.reserve(m_selectedItems.count());
Alexander Lohnau's avatar
Alexander Lohnau committed
173
        for (const KFileItem &item : qAsConst(m_selectedItems)) {
174
175
176
            selectedUrls.append(item.url());
        }

177
178
179
        KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
        KJobWidgets::setWindow(job, m_mainWindow);
        job->uiDelegate()->setAutoErrorHandlingEnabled(true);
180
181
182
    }
}

183
184
185
186
void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemActions)
{
    // insert 'Open in new window' and 'Open in new tab' entries
    const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
187
188
189
190
191
192
    if (ContextMenuSettings::showOpenInNewTab()) {
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
    }
    if (ContextMenuSettings::showOpenInNewWindow()) {
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
    }
193
194
195
196
197

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

    // set up 'Create New' menu
198
199
200
201
202
203
204
205
206
207
208
    DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
    newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
    newFileMenu->checkUpToDate();
    newFileMenu->setPopupFiles(QList<QUrl>() << m_fileInfo.url());
    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"));
209
    menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
210
211
212
    addMenu(menu);

    addSeparator();
213
214
}

215
216
void DolphinContextMenu::openItemContextMenu()
{
217
    Q_ASSERT(!m_fileInfo.isNull());
218

219
220
221
    QAction* openParentAction = nullptr;
    QAction* openParentInNewWindowAction = nullptr;
    QAction* openParentInNewTabAction = nullptr;
222
223
    const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();

224
    KFileItemActions fileItemActions;
225
    fileItemActions.setParentWidget(m_mainWindow);
226
227
228
229
230
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
    connect(&fileItemActions, &KFileItemActions::error, this, [this](const QString &errorMessage) {
        m_mainWindow->activeViewContainer()->showMessage(errorMessage, DolphinViewContainer::Error);
    });
#endif
231
232
    fileItemActions.setItemListProperties(selectedItemsProps);

233
    if (m_selectedItems.count() == 1) {
234
        // single files
235
        if (m_fileInfo.isDir()) {
236
237
            addDirectoryItemContextMenu(fileItemActions);
        } else if (m_context & TimelineContext || m_context & SearchContext) {
238
239
            addOpenWithActions(fileItemActions);

240
            openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
241
242
243
244
245
                                           i18nc("@action:inmenu",
                                                 "Open Path"),
                                           this);
            addAction(openParentAction);

246
            openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
247
                                                    i18nc("@action:inmenu",
248
                                                          "Open Path in New Window"),
249
                                                    this);
250
            addAction(openParentInNewWindowAction);
251

252
            openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
253
                                                   i18nc("@action:inmenu",
254
                                                         "Open Path in New Tab"),
255
                                                   this);
256
            addAction(openParentInNewTabAction);
257

258
259
260
261
262
263
264
            addSeparator();
        } else {
            // Insert 'Open With" entries
            addOpenWithActions(fileItemActions);
        }
        if (m_fileInfo.isLink()) {
            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
265
266
267
            addSeparator();
        }
    } else {
268
        // multiple files
269
        bool selectionHasOnlyDirs = true;
270
        for (const auto &item : qAsConst(m_selectedItems)) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
271
            const QUrl& url = DolphinView::openItemAsFolderUrl(item);
272
            if (url.isEmpty()) {
273
274
275
276
277
                selectionHasOnlyDirs = false;
                break;
            }
        }

278
        if (selectionHasOnlyDirs && ContextMenuSettings::showOpenInNewTab()) {
279
            // insert 'Open in new tab' entry
280
            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
Peter Penz's avatar
Peter Penz committed
281
        }
282
283
        // Insert 'Open With" entries
        addOpenWithActions(fileItemActions);
284
    }
285

286
    insertDefaultItemActions(selectedItemsProps);
287

288
    addAdditionalActions(fileItemActions, selectedItemsProps);
289

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

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

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

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

319
    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
320
321
    KFileItemActions fileItemActions;
    fileItemActions.setParentWidget(m_mainWindow);
322
323
324
325
326
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
    connect(&fileItemActions, &KFileItemActions::error, this, [this](const QString &errorMessage) {
        m_mainWindow->activeViewContainer()->showMessage(errorMessage, DolphinViewContainer::Error);
    });
#endif
327
    fileItemActions.setItemListProperties(baseUrlProperties);
328

329
330
331
332
    // Set up and insert 'Create New' menu
    KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
    newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
    newFileMenu->checkUpToDate();
333
    newFileMenu->setPopupFiles(QList<QUrl>() << m_baseUrl);
334
335
    addMenu(newFileMenu->menu());

336
337
338
    // 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);
339

340
    QAction* pasteAction = createPasteAction();
341
342
343
    if (pasteAction) {
        addAction(pasteAction);
    }
344

345
    // Insert 'Add to Places' entry if it's not already in the places panel
346
347
    if (ContextMenuSettings::showAddToPlaces() &&
            !placeExists(m_mainWindow->activeViewContainer()->url())) {
348
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
349
    }
350
    addSeparator();
351

352
    // Insert 'Sort By' and 'View Mode'
353
354
355
356
357
358
359
360
361
    if (ContextMenuSettings::showSortBy()) {
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
    }
    if (ContextMenuSettings::showViewMode()) {
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
    }
    if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) {
        addSeparator();
    }
362

363
    addAdditionalActions(fileItemActions, baseUrlProperties);
364
    addCustomActions();
365

366
367
    addSeparator();

368
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
369
    addAction(propertiesAction);
370

371
    exec(m_pos);
372
373
}

374
void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
375
{
376
    const KActionCollection* collection = m_mainWindow->actionCollection();
377

Nikita Churaev's avatar
Nikita Churaev committed
378
    // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
379
380
    addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
    addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
381
382
383
384
385
    if (ContextMenuSettings::showCopyLocation()) {
        QAction* copyPathAction = collection->action(QString("copy_location"));
        copyPathAction->setEnabled(m_selectedItems.size() == 1);
        addAction(copyPathAction);
    }
386
387
388
389
    QAction* pasteAction = createPasteAction();
    if (pasteAction) {
        addAction(pasteAction);
    }
390
391
392
393
394

    // Insert 'Duplicate Here'
    if (ContextMenuSettings::showDuplicateHere()) {
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
    }
395

396
    // Insert 'Rename'
397
    addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
398

399
400
401
402
403
404
    // Insert 'Add to Places' entry if appropriate
    if (ContextMenuSettings::showAddToPlaces() &&
            m_selectedItems.count() == 1 &&
            m_fileInfo.isDir() &&
            !placeExists(m_fileInfo.url())) {
        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places")));
405
406
407
408
    }

    addSeparator();

409
    // Insert 'Move to Trash' and/or 'Delete'
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    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());
425
        }
426
427
        addAction(m_removeAction);
        m_removeAction->update();
428
429
430
    }
}

Lukáš Tinkl's avatar
Lukáš Tinkl committed
431
bool DolphinContextMenu::placeExists(const QUrl& url) const
Harsh J Chouraria's avatar
Harsh J Chouraria committed
432
{
433
434
435
436
437
    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
438
439
}

440
441
QAction* DolphinContextMenu::createPasteAction()
{
Kevin Funk's avatar
Kevin Funk committed
442
    QAction* action = nullptr;
443
    KFileItem destItem;
444
    if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) {
445
        destItem = m_fileInfo;
446
    } else {
447
448
449
450
        destItem = baseFileItem();
    }

    if (!destItem.isNull() && destItem.isDir()) {
451
452
453
454
455
456
457
        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);
458
459
                connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
            } else {
460
                action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
461
462
            }
        }
463
464
465
466
467
    }

    return action;
}

468
KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
469
{
470
    if (!m_selectedItemsProperties) {
471
472
473
474
475
476
        m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
    }
    return *m_selectedItemsProperties;
}

KFileItem DolphinContextMenu::baseFileItem()
477
{
478
    if (!m_baseFileItem) {
479
480
481
482
483
484
485
        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);
        }
486
    }
487
    return *m_baseFileItem;
488
489
}

490
void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
Peter Penz's avatar
Peter Penz committed
491
492
{
    // insert 'Open With...' action or sub menu
493
    fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
494
495
}

496
void DolphinContextMenu::addCustomActions()
497
{
498
499
500
501
502
503
504
505
    addActions(m_customActions);
}

void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props)
{
    addSeparator();

    QList<QAction *> additionalActions;
506
    if (props.isDirectory() && props.isLocal() && ContextMenuSettings::showOpenTerminal()) {
507
508
        additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal"));
    }
509
    fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
510

511
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
512
513
    const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
    if (!versionControlActions.isEmpty()) {
514
        addActions(versionControlActions);
515
        addSeparator();
516
517
518
    }
}