dolphincontextmenu.cpp 18.6 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
26
#include <KIO/EmptyTrashJob>
#include <KIO/JobUiDelegate>
27
#include <KIO/Paste>
Roman Inflianskas's avatar
Roman Inflianskas committed
28
#include <KIO/RestoreJob>
29
#include <KJobWidgets>
Roman Inflianskas's avatar
Roman Inflianskas committed
30
#include <KLocalizedString>
31
#include <KNewFileMenu>
32
#include <KPluginMetaData>
33
34
#include <KService>
#include <KStandardAction>
35
#include <KToolBar>
36

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

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

    installEventFilter(this);
67
68
}

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

77
78
79
80
81
void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
{
    m_customActions = actions;
}

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

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

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

    return m_command;
113
114
}

115
void DolphinContextMenu::childEvent(QChildEvent* event)
116
{
117
118
    if(event->added()) {
        event->child()->installEventFilter(this);
119
    }
120
    QMenu::childEvent(event);
121
122
}

123
bool DolphinContextMenu::eventFilter(QObject* dest, QEvent* event)
124
{
125
126
127
128
129
130
131
132
133
134
    if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
        QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
        if(m_removeAction && keyEvent->key() == Qt::Key_Shift) {
            if(event->type() == QEvent::KeyPress) {
                m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
            } else {
                m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
            }
            return true;
        }
135
    }
136
    return QMenu::eventFilter(dest, event);
137
138
}

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

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

147
    addCustomActions();
148

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

152
153
    addShowMenuBarAction();

154
    if (exec(m_pos) == emptyTrashAction) {
155
        Trash::empty(m_mainWindow);
156
157
158
159
160
161
162
163
    }
}

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

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

167
    QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
168
    addAction(deleteAction);
169

170
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
171
    addAction(propertiesAction);
172

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

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

186
187
188
189
void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemActions)
{
    // insert 'Open in new window' and 'Open in new tab' entries
    const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
190
191
192
193
194
195
    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")));
    }
196
197
198
199
200

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

    // set up 'Create New' menu
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    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"));
    menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
    addMenu(menu);

    addSeparator();
216
217
}

218
219
void DolphinContextMenu::openItemContextMenu()
{
220
    Q_ASSERT(!m_fileInfo.isNull());
221

222
223
224
    QAction* openParentAction = nullptr;
    QAction* openParentInNewWindowAction = nullptr;
    QAction* openParentInNewTabAction = nullptr;
225
226
    const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();

227
    KFileItemActions fileItemActions;
228
    fileItemActions.setParentWidget(m_mainWindow);
229
230
    fileItemActions.setItemListProperties(selectedItemsProps);

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

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

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

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

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

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

284
    insertDefaultItemActions(selectedItemsProps);
285

286
    addAdditionalActions(fileItemActions, selectedItemsProps);
287

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

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

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

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

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

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

329
330
331
    // 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);
332

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

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

345
    // Insert 'Sort By' and 'View Mode'
346
347
348
349
350
351
352
353
354
    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();
    }
355

356
    addAdditionalActions(fileItemActions, baseUrlProperties);
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)));
376
377
378
379
380
    if (ContextMenuSettings::showCopyLocation()) {
        QAction* copyPathAction = collection->action(QString("copy_location"));
        copyPathAction->setEnabled(m_selectedItems.size() == 1);
        addAction(copyPathAction);
    }
381
382
383
384
    QAction* pasteAction = createPasteAction();
    if (pasteAction) {
        addAction(pasteAction);
    }
385
386
387
388
389

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

391
    // Insert 'Rename'
392
    addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
393

394
395
396
397
398
399
    // 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")));
400
401
402
403
    }

    addSeparator();

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

426
427
428
429
430
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()) {
431
432
        addSeparator();
        addAction(showMenuBar);
433
434
435
    }
}

Lukáš Tinkl's avatar
Lukáš Tinkl committed
436
bool DolphinContextMenu::placeExists(const QUrl& url) const
Harsh J Chouraria's avatar
Harsh J Chouraria committed
437
{
438
439
440
441
442
    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
443
444
}

445
446
QAction* DolphinContextMenu::createPasteAction()
{
Kevin Funk's avatar
Kevin Funk committed
447
    QAction* action = nullptr;
448
    KFileItem destItem;
449
    if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) {
450
        destItem = m_fileInfo;
451
    } else {
452
453
454
455
        destItem = baseFileItem();
    }

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

    return action;
}

473
KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
474
{
475
    if (!m_selectedItemsProperties) {
476
477
478
479
480
481
        m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
    }
    return *m_selectedItemsProperties;
}

KFileItem DolphinContextMenu::baseFileItem()
482
{
483
    if (!m_baseFileItem) {
484
485
486
487
488
489
490
        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);
        }
491
    }
492
    return *m_baseFileItem;
493
494
}

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

501
void DolphinContextMenu::addCustomActions()
502
{
503
504
505
506
507
508
509
510
511
512
513
    addActions(m_customActions);
}

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

    QList<QAction *> additionalActions;
    if (props.isDirectory() && props.isLocal()) {
        additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal"));
    }
514
    fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
515

516
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
517
518
    const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
    if (!versionControlActions.isEmpty()) {
519
        addActions(versionControlActions);
520
        addSeparator();
521
522
523
    }
}