dolphincontextmenu.cpp 18 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

    installEventFilter(this);
66
67
}

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

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

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

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

    // open the corresponding popup for the context
    if (m_context & TrashContext) {
        if (m_context & ItemContext) {
            openTrashItemContextMenu();
102
        } else {
103
104
            openTrashContextMenu();
        }
105
    } else if (m_context & ItemContext) {
106
        openItemContextMenu();
107
    } else {
108
109
110
        Q_ASSERT(m_context == NoContext);
        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
190
191
192
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")));
193
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
194
195
196
197
198
199
200
201
202

    // 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();
203
     newFileMenu->setPopupFiles(QList<QUrl>() << m_fileInfo.url());
204
205
206
207
208
209
210
     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")));
211
     menu->setParent(this, Qt::Popup);
212
213
214
215
216
     addMenu(menu);

     addSeparator();
}

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

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

226
    KFileItemActions fileItemActions;
227
    fileItemActions.setParentWidget(this);
228
229
    fileItemActions.setItemListProperties(selectedItemsProps);

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

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

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

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

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

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

283
    insertDefaultItemActions(selectedItemsProps);
284

285
    addAdditionalActions(fileItemActions, selectedItemsProps);
286

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

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

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

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

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

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

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

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

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

343
344
345
346
    // Insert 'Sort By' and 'View Mode'
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));

347
    addAdditionalActions(fileItemActions, baseUrlProperties);
348
    addCustomActions();
349

350
351
    addSeparator();

352
    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
353
    addAction(propertiesAction);
354

355
    addShowMenuBarAction();
356
357

    exec(m_pos);
358
359
}

360
void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
361
{
362
    const KActionCollection* collection = m_mainWindow->actionCollection();
363

Nikita Churaev's avatar
Nikita Churaev committed
364
    // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
365
366
    addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
    addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
367
368
369
    QAction* copyPathAction = collection->action(QString("copy_location"));
    copyPathAction->setEnabled(m_selectedItems.size() == 1);
    addAction(copyPathAction);
370
371
372
373
    QAction* pasteAction = createPasteAction();
    if (pasteAction) {
        addAction(pasteAction);
    }
Nate Graham's avatar
Nate Graham committed
374
    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
375

376
    // Insert 'Rename'
377
    addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
378

379
380
381
382
383
384
385
386
387
388
389
    // 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")));
            }
        }
    }

    addSeparator();

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::addCustomActions()
488
{
489
490
491
492
493
494
495
496
497
498
499
    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"));
    }
500
    fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
501

502
    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
503
504
    const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
    if (!versionControlActions.isEmpty()) {
505
        addActions(versionControlActions);
506
        addSeparator();
507
508
509
    }
}