dolphinviewactionhandler.cpp 38.6 KB
Newer Older
1
2
3
4
5
6
/*
 * SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
 * SPDX-FileCopyrightText: 2012 Peter Penz <peter.penz19@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */
7
8
9

#include "dolphinviewactionhandler.h"

10
#include "kitemviews/kfileitemlisttostring.h"
Roman Inflianskas's avatar
Roman Inflianskas committed
11
#include "kitemviews/kfileitemmodel.h"
Felix Ernst's avatar
Felix Ernst committed
12
#include "selectionmode/actiontexthelper.h"
13
#include "settings/viewpropertiesdialog.h"
14
#include "views/zoomlevelinfo.h"
15

Ahmad Samir's avatar
Ahmad Samir committed
16
#if HAVE_BALOO
Roman Inflianskas's avatar
Roman Inflianskas committed
17
18
#include <Baloo/IndexerConfig>
#endif
Peter Penz's avatar
Peter Penz committed
19
20
#include <KActionCollection>
#include <KActionMenu>
21
#include <KFileItemListProperties>
Laurent Montel's avatar
Laurent Montel committed
22
#include <KLocalizedString>
23
#include <KNewFileMenu>
Peter Penz's avatar
Peter Penz committed
24
#include <KPropertiesDialog>
25
#include <KProtocolManager>
Nicolas Fella's avatar
Nicolas Fella committed
26
27

#include <QActionGroup>
Roman Inflianskas's avatar
Roman Inflianskas committed
28
29
#include <QMenu>
#include <QPointer>
Vishesh Handa's avatar
Vishesh Handa committed
30

31
32
33
34
35
36
DolphinViewActionHandler::DolphinViewActionHandler(KActionCollection *collection, SelectionMode::ActionTextHelper *actionTextHelper, QObject *parent)
    : QObject(parent)
    , m_actionCollection(collection)
    , m_currentView(nullptr)
    , m_sortByActions()
    , m_visibleRoles()
37
38
{
    Q_ASSERT(m_actionCollection);
Felix Ernst's avatar
Felix Ernst committed
39
    createActions(actionTextHelper);
40
41
}

42
void DolphinViewActionHandler::setCurrentView(DolphinView *view)
43
44
45
{
    Q_ASSERT(view);

46
    if (m_currentView) {
Kevin Funk's avatar
Kevin Funk committed
47
        disconnect(m_currentView, nullptr, this, nullptr);
48
    }
49
50
51

    m_currentView = view;

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    connect(view, &DolphinView::modeChanged, this, &DolphinViewActionHandler::updateViewActions);
    connect(view, &DolphinView::previewsShownChanged, this, &DolphinViewActionHandler::slotPreviewsShownChanged);
    connect(view, &DolphinView::sortOrderChanged, this, &DolphinViewActionHandler::slotSortOrderChanged);
    connect(view, &DolphinView::sortFoldersFirstChanged, this, &DolphinViewActionHandler::slotSortFoldersFirstChanged);
    connect(view, &DolphinView::sortHiddenLastChanged, this, &DolphinViewActionHandler::slotSortHiddenLastChanged);
    connect(view, &DolphinView::visibleRolesChanged, this, &DolphinViewActionHandler::slotVisibleRolesChanged);
    connect(view, &DolphinView::groupedSortingChanged, this, &DolphinViewActionHandler::slotGroupedSortingChanged);
    connect(view, &DolphinView::hiddenFilesShownChanged, this, &DolphinViewActionHandler::slotHiddenFilesShownChanged);
    connect(view, &DolphinView::sortRoleChanged, this, &DolphinViewActionHandler::slotSortRoleChanged);
    connect(view, &DolphinView::zoomLevelChanged, this, &DolphinViewActionHandler::slotZoomLevelChanged);
    connect(view, &DolphinView::writeStateChanged, this, &DolphinViewActionHandler::slotWriteStateChanged);
    connect(view, &DolphinView::selectionModeChangeRequested, this, [this](bool enabled) {
        Q_EMIT selectionModeChangeTriggered(enabled);
    });
    connect(view, &DolphinView::selectionChanged, this, &DolphinViewActionHandler::slotSelectionChanged);
67
    slotSelectionChanged(m_currentView->selectedItems());
68
69
}

70
DolphinView *DolphinViewActionHandler::currentView()
Rahman Duran's avatar
Rahman Duran committed
71
72
73
74
{
    return m_currentView;
}

Felix Ernst's avatar
Felix Ernst committed
75
void DolphinViewActionHandler::createActions(SelectionMode::ActionTextHelper *actionTextHelper)
76
77
{
    // This action doesn't appear in the GUI, it's for the shortcut only.
78
    // KNewFileMenu takes care of the GUI stuff.
79
    QAction *newDirAction = m_actionCollection->addAction(QStringLiteral("create_dir"));
80
    newDirAction->setText(i18nc("@action", "Create Folder..."));
81
82
    m_actionCollection->setDefaultShortcuts(newDirAction, KStandardShortcut::createFolder());
    newDirAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
83
    newDirAction->setEnabled(false); // Will be enabled in slotWriteStateChanged(bool) if the current URL is writable
84
    connect(newDirAction, &QAction::triggered, this, &DolphinViewActionHandler::createDirectoryTriggered);
85

86
    // File menu
87

88
    auto renameAction = KStandardAction::renameFile(this, &DolphinViewActionHandler::slotRename, m_actionCollection);
89
90
91
92
    renameAction->setWhatsThis(xi18nc("@info:whatsthis",
                                      "This renames the "
                                      "items in your current selection.<nl/>Renaming multiple items "
                                      "at once amounts to their new names differing only in a number."));
93

94
95
    auto trashAction = KStandardAction::moveToTrash(this, &DolphinViewActionHandler::slotTrashActivated, m_actionCollection);
    auto trashShortcuts = trashAction->shortcuts();
96
    trashAction->setAutoRepeat(false);
97
98
99
100
    if (!trashShortcuts.contains(QKeySequence::Delete)) {
        trashShortcuts.append(QKeySequence::Delete);
        m_actionCollection->setDefaultShortcuts(trashAction, trashShortcuts);
    }
101
102
103
104
105
    trashAction->setWhatsThis(xi18nc("@info:whatsthis",
                                     "This moves the "
                                     "items in your current selection to the <filename>Trash"
                                     "</filename>.<nl/>The trash is a temporary storage where "
                                     "items can be deleted from if disk space is needed."));
106

107
108
    auto deleteAction = KStandardAction::deleteFile(this, &DolphinViewActionHandler::slotDeleteItems, m_actionCollection);
    auto deleteShortcuts = deleteAction->shortcuts();
109
    deleteAction->setAutoRepeat(false);
110
111
112
113
    if (!deleteShortcuts.contains(Qt::SHIFT | Qt::Key_Delete)) {
        deleteShortcuts.append(Qt::SHIFT | Qt::Key_Delete);
        m_actionCollection->setDefaultShortcuts(deleteAction, deleteShortcuts);
    }
114
115
116
117
    deleteAction->setWhatsThis(xi18nc("@info:whatsthis",
                                      "This deletes "
                                      "the items in your current selection completely. They can "
                                      "not be recovered by normal means."));
118

119
    // This action is useful for being enabled when KStandardAction::MoveToTrash should be
120
    // disabled and KStandardAction::DeleteFile is enabled (e.g. non-local files), so that Key_Del
121
122
    // can be used for deleting the file (#76016). It needs to be a separate action
    // so that the Edit menu isn't affected.
123
    QAction *deleteWithTrashShortcut = m_actionCollection->addAction(QStringLiteral("delete_shortcut"));
124
    // The descriptive text is just for the shortcuts editor.
125
    deleteWithTrashShortcut->setText(i18nc("@action \"Move to Trash\" for non-local files, etc.", "Delete (using shortcut for Trash)"));
126
    m_actionCollection->setDefaultShortcuts(deleteWithTrashShortcut, KStandardShortcut::moveToTrash());
127
    deleteWithTrashShortcut->setEnabled(false);
128
    connect(deleteWithTrashShortcut, &QAction::triggered, this, &DolphinViewActionHandler::slotDeleteItems);
129

130
    QAction *duplicateAction = m_actionCollection->addAction(QStringLiteral("duplicate"));
Nate Graham's avatar
Nate Graham committed
131
132
133
134
135
136
    duplicateAction->setText(i18nc("@action:inmenu File", "Duplicate Here"));
    duplicateAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-duplicate")));
    m_actionCollection->setDefaultShortcut(duplicateAction, Qt::CTRL | Qt::Key_D);
    duplicateAction->setEnabled(false);
    connect(duplicateAction, &QAction::triggered, this, &DolphinViewActionHandler::slotDuplicate);

137
    QAction *propertiesAction = m_actionCollection->addAction(QStringLiteral("properties"));
138
    // Well, it's the File menu in dolphinmainwindow and the Edit menu in dolphinpart... :)
139
    propertiesAction->setText(i18nc("@action:inmenu File", "Properties"));
140
    propertiesAction->setWhatsThis(xi18nc("@info:whatsthis properties",
141
142
143
144
145
                                          "This shows a complete list of properties of the currently "
                                          "selected items in a new window.<nl/>If nothing is selected the "
                                          "window will be about the currently viewed folder instead.<nl/>"
                                          "You can configure advanced options there like managing "
                                          "read- and write-permissions."));
146
    propertiesAction->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
147
    m_actionCollection->setDefaultShortcuts(propertiesAction, {Qt::ALT | Qt::Key_Return, Qt::ALT | Qt::Key_Enter});
148
    connect(propertiesAction, &QAction::triggered, this, &DolphinViewActionHandler::slotProperties);
149

150
    QAction *copyPathAction = m_actionCollection->addAction(QStringLiteral("copy_location"));
Nikita Churaev's avatar
Nikita Churaev committed
151
    copyPathAction->setText(i18nc("@action:incontextmenu", "Copy Location"));
152
    copyPathAction->setWhatsThis(i18nc("@info:whatsthis copy_location", "This will copy the path of the first selected item into the clipboard."));
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
153

154
    copyPathAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy-path")));
155
    m_actionCollection->setDefaultShortcuts(copyPathAction, {Qt::CTRL | Qt::ALT | Qt::Key_C});
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
156
157
    connect(copyPathAction, &QAction::triggered, this, &DolphinViewActionHandler::slotCopyPath);

Felix Ernst's avatar
Felix Ernst committed
158
    if (actionTextHelper) {
Felix Ernst's avatar
Felix Ernst committed
159
        // The "…" at the end make clear that they won't trigger their respective actions directly.
Felix Ernst's avatar
Felix Ernst committed
160
161
162
163
164
165
        actionTextHelper->registerTextWhenNothingIsSelected(trashAction, i18nc("@action:inmenu File", "Move to Trash…"));
        actionTextHelper->registerTextWhenNothingIsSelected(deleteAction, i18nc("@action:inmenu File", "Delete…"));
        actionTextHelper->registerTextWhenNothingIsSelected(duplicateAction, i18nc("@action:inmenu File", "Duplicate Here…"));
        actionTextHelper->registerTextWhenNothingIsSelected(copyPathAction, i18nc("@action:incontextmenu", "Copy Location…"));
    }

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    // This menu makes sure that users who don't know how to open a context menu and haven't
    // figured out how to enable the menu bar can still perform basic file manipulation.
    // This only works if they know how to select a file.
    // The text when nothing is selected at least implies that a selection can /somehow/ be made.
    // This menu is by default only used in the hamburger menu but created here so users can put
    // it on their toolbar.
    KActionMenu *basicActionsMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("basic_actions"), this);
    // The text is set later depending on the selection in the currently active view.
    basicActionsMenu->setPopupMode(QToolButton::InstantPopup);
    basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::Cut)));
    basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::Copy)));
    basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::Paste)));
    basicActionsMenu->addSeparator();
    basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::RenameFile)));
    basicActionsMenu->addAction(m_actionCollection->action(KStandardAction::name(KStandardAction::MoveToTrash)));
    basicActionsMenu->addSeparator();
    basicActionsMenu->addAction(m_actionCollection->action(QStringLiteral("properties")));
    basicActionsMenu->addSeparator(); // We add one more separator because we sometimes add contextual
                                      // actions in slotSelectionChanged() after the static ones above.
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
185

186
    // View menu
187
188
189
    KToggleAction *iconsAction = iconsModeAction();
    KToggleAction *compactAction = compactModeAction();
    KToggleAction *detailsAction = detailsModeAction();
190

191
    iconsAction->setWhatsThis(xi18nc("@info:whatsthis Icons view mode",
192
193
194
195
196
197
                                     "<para>This switches to a view mode that focuses on the folder "
                                     "and file icons. This mode makes it easy to distinguish folders "
                                     "from files and to detect items with distinctive <emphasis>"
                                     "file types</emphasis>.</para><para> This mode is handy to "
                                     "browse through pictures when the <interface>Preview"
                                     "</interface> option is enabled.</para>"));
198
    compactAction->setWhatsThis(xi18nc("@info:whatsthis Compact view mode",
199
200
201
                                       "<para>This switches to a compact view mode that lists the folders "
                                       "and files in columns with the names beside the icons.</para><para>"
                                       "This helps to keep the overview in folders with many items.</para>"));
202
    detailsAction->setWhatsThis(xi18nc("@info:whatsthis Details view mode",
203
204
205
206
207
208
209
210
211
212
                                       "<para>This switches to a list view mode that focuses on folder "
                                       "and file details.</para><para>Click on a detail in the column "
                                       "header to sort the items by it. Click again to sort the other "
                                       "way around. To select which details should be displayed click "
                                       "the header with the right mouse button.</para><para>You can "
                                       "view the contents of a folder without leaving the current "
                                       "location by clicking to the left of it. This way you can view "
                                       "the contents of multiple folders in the same list.</para>"));

    KSelectAction *viewModeActions = m_actionCollection->add<KSelectAction>(QStringLiteral("view_mode"));
213
214
    viewModeActions->setText(i18nc("@action:intoolbar", "View Mode"));
    viewModeActions->addAction(iconsAction);
215
    viewModeActions->addAction(compactAction);
216
217
    viewModeActions->addAction(detailsAction);
    viewModeActions->setToolBarMode(KSelectAction::MenuMode);
218
    connect(viewModeActions, &KSelectAction::triggered, this, &DolphinViewActionHandler::slotViewModeActionTriggered);
219

220
    QAction *zoomInAction = KStandardAction::zoomIn(this, &DolphinViewActionHandler::zoomIn, m_actionCollection);
221
    zoomInAction->setWhatsThis(i18nc("@info:whatsthis zoom in", "This increases the icon size."));
222

223
    QAction *zoomResetAction = m_actionCollection->addAction(QStringLiteral("view_zoom_reset"));
224
225
226
227
    zoomResetAction->setText(i18nc("@action:inmenu View", "Reset Zoom Level"));
    zoomResetAction->setToolTip(i18n("Zoom To Default"));
    zoomResetAction->setWhatsThis(i18nc("@info:whatsthis zoom reset", "This resets the icon size to default."));
    zoomResetAction->setIcon(QIcon::fromTheme(QStringLiteral("zoom-original")));
228
    m_actionCollection->setDefaultShortcuts(zoomResetAction, {Qt::CTRL | Qt::Key_0});
229
230
    connect(zoomResetAction, &QAction::triggered, this, &DolphinViewActionHandler::zoomReset);

231
    QAction *zoomOutAction = KStandardAction::zoomOut(this, &DolphinViewActionHandler::zoomOut, m_actionCollection);
232
    zoomOutAction->setWhatsThis(i18nc("@info:whatsthis zoom out", "This reduces the icon size."));
233

234
    KActionMenu *zoomMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("zoom"));
235
236
237
238
239
240
241
    zoomMenu->setText(i18nc("@action:inmenu menu of zoom actions", "Zoom"));
    zoomMenu->setIcon(QIcon::fromTheme(QStringLiteral("zoom")));
    zoomMenu->setPopupMode(QToolButton::InstantPopup);
    zoomMenu->addAction(zoomInAction);
    zoomMenu->addAction(zoomResetAction);
    zoomMenu->addAction(zoomOutAction);

242
    KToggleAction *showPreview = m_actionCollection->add<KToggleAction>(QStringLiteral("show_preview"));
243
    showPreview->setText(i18nc("@action:intoolbar", "Show Previews"));
244
    showPreview->setToolTip(i18nc("@info", "Show preview of files and folders"));
245
246
247
248
249
    showPreview->setWhatsThis(xi18nc("@info:whatsthis",
                                     "When this is "
                                     "enabled, the icons are based on the actual file or folder "
                                     "contents.<nl/>For example the icons of images become scaled "
                                     "down versions of the images."));
250
    showPreview->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
251
    connect(showPreview, &KToggleAction::triggered, this, &DolphinViewActionHandler::togglePreview);
252

253
    KToggleAction *sortFoldersFirst = m_actionCollection->add<KToggleAction>(QStringLiteral("folders_first"));
254
    sortFoldersFirst->setText(i18nc("@action:inmenu Sort", "Folders First"));
255
    connect(sortFoldersFirst, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortFoldersFirst);
256

257
    KToggleAction *sortHiddenLast = m_actionCollection->add<KToggleAction>(QStringLiteral("hidden_last"));
258
259
260
    sortHiddenLast->setText(i18nc("@action:inmenu Sort", "Hidden Files Last"));
    connect(sortHiddenLast, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleSortHiddenLast);

261
    // View -> Sort By
262
    QActionGroup *sortByActionGroup = createFileItemRolesActionGroup(QStringLiteral("sort_by_"));
263

264
    KActionMenu *sortByActionMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("sort"));
265
    sortByActionMenu->setIcon(QIcon::fromTheme(QStringLiteral("view-sort")));
266
    sortByActionMenu->setText(i18nc("@action:inmenu View", "Sort By"));
267
    sortByActionMenu->setPopupMode(QToolButton::InstantPopup);
268

Alexander Lohnau's avatar
Alexander Lohnau committed
269
    const auto sortByActionGroupActions = sortByActionGroup->actions();
270
    for (QAction *action : sortByActionGroupActions) {
271
272
        sortByActionMenu->addAction(action);
    }
273
274
275

    sortByActionMenu->addSeparator();

276
    QActionGroup *group = new QActionGroup(sortByActionMenu);
277
278
    group->setExclusive(true);

279
    KToggleAction *ascendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("ascending"));
280
281
282
283
284
    ascendingAction->setActionGroup(group);
    connect(ascendingAction, &QAction::triggered, this, [this] {
        m_currentView->setSortOrder(Qt::AscendingOrder);
    });

285
    KToggleAction *descendingAction = m_actionCollection->add<KToggleAction>(QStringLiteral("descending"));
286
287
288
289
290
291
292
    descendingAction->setActionGroup(group);
    connect(descendingAction, &QAction::triggered, this, [this] {
        m_currentView->setSortOrder(Qt::DescendingOrder);
    });

    sortByActionMenu->addAction(ascendingAction);
    sortByActionMenu->addAction(descendingAction);
293
294
    sortByActionMenu->addSeparator();
    sortByActionMenu->addAction(sortFoldersFirst);
295
    sortByActionMenu->addAction(sortHiddenLast);
296
297

    // View -> Additional Information
298
    QActionGroup *visibleRolesGroup = createFileItemRolesActionGroup(QStringLiteral("show_"));
299

300
    KActionMenu *visibleRolesMenu = m_actionCollection->add<KActionMenu>(QStringLiteral("additional_info"));
301
302
    visibleRolesMenu->setText(i18nc("@action:inmenu View", "Show Additional Information"));
    visibleRolesMenu->setIcon(QIcon::fromTheme(QStringLiteral("documentinfo")));
303
    visibleRolesMenu->setPopupMode(QToolButton::InstantPopup);
304

Alexander Lohnau's avatar
Alexander Lohnau committed
305
    const auto visibleRolesGroupActions = visibleRolesGroup->actions();
306
    for (QAction *action : visibleRolesGroupActions) {
307
        visibleRolesMenu->addAction(action);
308
    }
309

310
    KToggleAction *showInGroups = m_actionCollection->add<KToggleAction>(QStringLiteral("show_in_groups"));
311
    showInGroups->setIcon(QIcon::fromTheme(QStringLiteral("view-group")));
312
    showInGroups->setText(i18nc("@action:inmenu View", "Show in Groups"));
313
    showInGroups->setWhatsThis(i18nc("@info:whatsthis", "This groups files and folders by their first letter."));
314
    connect(showInGroups, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleGroupedSorting);
315

316
    KToggleAction *showHiddenFiles = m_actionCollection->add<KToggleAction>(QStringLiteral("show_hidden_files"));
317
    showHiddenFiles->setIcon(QIcon::fromTheme(QStringLiteral("view-visible")));
318
    showHiddenFiles->setText(i18nc("@action:inmenu View", "Show Hidden Files"));
319
320
321
322
323
324
325
    showHiddenFiles->setWhatsThis(xi18nc("@info:whatsthis",
                                         "<para>When "
                                         "this is enabled <emphasis>hidden</emphasis> files and folders "
                                         "are visible. They will be displayed semi-transparent.</para>"
                                         "<para>Hidden items only differ from other ones in that their "
                                         "name starts with a \".\". In general there is no need for "
                                         "users to access them which is why they are hidden.</para>"));
326
    m_actionCollection->setDefaultShortcuts(showHiddenFiles, KStandardShortcut::showHideHiddenFiles());
327
    connect(showHiddenFiles, &KToggleAction::triggered, this, &DolphinViewActionHandler::toggleShowHiddenFiles);
328

329
    QAction *adjustViewProps = m_actionCollection->addAction(QStringLiteral("view_properties"));
330
    adjustViewProps->setText(i18nc("@action:inmenu View", "Adjust View Display Style..."));
331
    adjustViewProps->setIcon(QIcon::fromTheme(QStringLiteral("view-choose")));
332
333
334
    adjustViewProps->setWhatsThis(i18nc("@info:whatsthis",
                                        "This opens a window "
                                        "in which all folder view properties can be adjusted."));
335
    connect(adjustViewProps, &QAction::triggered, this, &DolphinViewActionHandler::slotAdjustViewProperties);
336
337
}

338
QActionGroup *DolphinViewActionHandler::createFileItemRolesActionGroup(const QString &groupPrefix)
339
{
340
    const bool isSortGroup = (groupPrefix == QLatin1String("sort_by_"));
Antonio Prcela's avatar
Antonio Prcela committed
341
    Q_ASSERT(isSortGroup || groupPrefix == QLatin1String("show_"));
342

343
    QActionGroup *rolesActionGroup = new QActionGroup(m_actionCollection);
344
345
    rolesActionGroup->setExclusive(isSortGroup);
    if (isSortGroup) {
346
        connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
347
    } else {
348
        connect(rolesActionGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
349
    }
350

351
    QString groupName;
352
353
    KActionMenu *groupMenu = nullptr;
    QActionGroup *groupMenuGroup = nullptr;
354

355
    bool indexingEnabled = false;
Ahmad Samir's avatar
Ahmad Samir committed
356
#if HAVE_BALOO
Vishesh Handa's avatar
Vishesh Handa committed
357
358
    Baloo::IndexerConfig config;
    indexingEnabled = config.fileIndexingEnabled();
359
360
#endif

Peter Penz's avatar
Peter Penz committed
361
    const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
362
    for (const KFileItemModel::RoleInfo &info : rolesInfo) {
363
364
        if (!isSortGroup && info.role == "text") {
            // It should not be possible to hide the "text" role
365
366
367
            continue;
        }

368
        KToggleAction *action = nullptr;
369
370
371
372
373
374
375
376
377
378
379
380
381
382
        const QString name = groupPrefix + info.role;
        if (info.group.isEmpty()) {
            action = m_actionCollection->add<KToggleAction>(name);
            action->setActionGroup(rolesActionGroup);
        } else {
            if (!groupMenu || info.group != groupName) {
                groupName = info.group;
                groupMenu = m_actionCollection->add<KActionMenu>(groupName);
                groupMenu->setText(groupName);
                groupMenu->setActionGroup(rolesActionGroup);

                groupMenuGroup = new QActionGroup(groupMenu);
                groupMenuGroup->setExclusive(isSortGroup);
                if (isSortGroup) {
383
                    connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::slotSortTriggered);
384
                } else {
385
                    connect(groupMenuGroup, &QActionGroup::triggered, this, &DolphinViewActionHandler::toggleVisibleRole);
386
387
388
389
390
391
392
                }
            }

            action = new KToggleAction(groupMenu);
            action->setActionGroup(groupMenuGroup);
            groupMenu->addAction(action);
        }
Peter Penz's avatar
Peter Penz committed
393
394
        action->setText(info.translation);
        action->setData(info.role);
395

396
        const bool enable = (!info.requiresBaloo && !info.requiresIndexer) || (info.requiresBaloo) || (info.requiresIndexer && indexingEnabled);
397
398
        action->setEnabled(enable);

399
400
401
402
403
        if (isSortGroup) {
            m_sortByActions.insert(info.role, action);
        } else {
            m_visibleRoles.insert(info.role, action);
        }
404
    }
405

406
    return rolesActionGroup;
407
408
}

409
void DolphinViewActionHandler::slotViewModeActionTriggered(QAction *action)
410
411
{
    const DolphinView::Mode mode = action->data().value<DolphinView::Mode>();
Felix Ernst's avatar
Felix Ernst committed
412
    m_currentView->setViewMode(mode);
413

414
    QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
415
    viewModeMenu->setIcon(action->icon());
416
417
}

418
419
void DolphinViewActionHandler::slotRename()
{
Felix Ernst's avatar
Felix Ernst committed
420
    if (m_currentView->selectedItemsCount() == 0) {
421
        Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::RenameContents);
Felix Ernst's avatar
Felix Ernst committed
422
423
424
    } else {
        Q_EMIT actionBeingHandled();
        m_currentView->renameSelectedItems();
Felix Ernst's avatar
Felix Ernst committed
425
        // We don't exit selectionMode here because users might want to rename more items.
Felix Ernst's avatar
Felix Ernst committed
426
    }
427
428
}

429
void DolphinViewActionHandler::slotTrashActivated()
430
{
Felix Ernst's avatar
Felix Ernst committed
431
    if (m_currentView->selectedItemsCount() == 0) {
432
        Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::MoveToTrashContents);
Felix Ernst's avatar
Felix Ernst committed
433
434
435
    } else {
        Q_EMIT actionBeingHandled();
        m_currentView->trashSelectedItems();
436
        Q_EMIT selectionModeChangeTriggered(false);
Felix Ernst's avatar
Felix Ernst committed
437
    }
438
439
440
441
}

void DolphinViewActionHandler::slotDeleteItems()
{
Felix Ernst's avatar
Felix Ernst committed
442
    if (m_currentView->selectedItemsCount() == 0) {
443
        Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DeleteContents);
Felix Ernst's avatar
Felix Ernst committed
444
445
446
    } else {
        Q_EMIT actionBeingHandled();
        m_currentView->deleteSelectedItems();
447
        Q_EMIT selectionModeChangeTriggered(false);
Felix Ernst's avatar
Felix Ernst committed
448
    }
449
450
451
452
}

void DolphinViewActionHandler::togglePreview(bool show)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
453
    Q_EMIT actionBeingHandled();
454
    m_currentView->setPreviewsShown(show);
455
456
}

457
void DolphinViewActionHandler::slotPreviewsShownChanged(bool shown)
458
{
459
    Q_UNUSED(shown)
460
    // It is not enough to update the 'Show Preview' action, also
461
    // the 'Zoom In', 'Zoom Out' and 'Zoom Reset' actions must be adapted.
462
463
464
    updateViewActions();
}

465
466
QString DolphinViewActionHandler::currentViewModeActionName() const
{
Felix Ernst's avatar
Felix Ernst committed
467
    switch (m_currentView->viewMode()) {
468
    case DolphinView::IconsView:
469
        return QStringLiteral("icons");
470
    case DolphinView::DetailsView:
471
        return QStringLiteral("details");
472
    case DolphinView::CompactView:
473
        return QStringLiteral("compact");
474
475
476
    default:
        Q_ASSERT(false);
        break;
477
478
479
480
    }
    return QString(); // can't happen
}

481
KActionCollection *DolphinViewActionHandler::actionCollection()
Rahman Duran's avatar
Rahman Duran committed
482
483
484
485
{
    return m_actionCollection;
}

486
487
void DolphinViewActionHandler::updateViewActions()
{
488
    QAction *viewModeAction = m_actionCollection->action(currentViewModeActionName());
489
    if (viewModeAction) {
490
        viewModeAction->setChecked(true);
491

492
        QAction *viewModeMenu = m_actionCollection->action(QStringLiteral("view_mode"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
493
        viewModeMenu->setIcon(viewModeAction->icon());
494
495
    }

496
    QAction *showPreviewAction = m_actionCollection->action(QStringLiteral("show_preview"));
497
    showPreviewAction->setChecked(m_currentView->previewsShown());
498
499

    slotSortOrderChanged(m_currentView->sortOrder());
500
    slotSortFoldersFirstChanged(m_currentView->sortFoldersFirst());
501
    slotSortHiddenLastChanged(m_currentView->sortHiddenLast());
502
    slotVisibleRolesChanged(m_currentView->visibleRoles(), QList<QByteArray>());
503
    slotGroupedSortingChanged(m_currentView->groupedSorting());
504
    slotSortRoleChanged(m_currentView->sortRole());
505
    slotZoomLevelChanged(m_currentView->zoomLevel(), -1);
506

Roman Gilg's avatar
Roman Gilg committed
507
508
    // Updates the "show_hidden_files" action state and icon
    slotHiddenFilesShownChanged(m_currentView->hiddenFilesShown());
509
510
511
512
}

void DolphinViewActionHandler::zoomIn()
{
513
514
    const int level = m_currentView->zoomLevel();
    m_currentView->setZoomLevel(level + 1);
515
516
517
518
519
    updateViewActions();
}

void DolphinViewActionHandler::zoomOut()
{
520
521
    const int level = m_currentView->zoomLevel();
    m_currentView->setZoomLevel(level - 1);
522
523
524
    updateViewActions();
}

525
526
527
528
529
530
void DolphinViewActionHandler::zoomReset()
{
    m_currentView->resetZoomLevel();
    updateViewActions();
}

531
532
void DolphinViewActionHandler::toggleSortFoldersFirst()
{
533
534
    const bool sortFirst = m_currentView->sortFoldersFirst();
    m_currentView->setSortFoldersFirst(!sortFirst);
535
536
}

537
538
539
540
541
542
void DolphinViewActionHandler::toggleSortHiddenLast()
{
    const bool sortHiddenLast = m_currentView->sortHiddenLast();
    m_currentView->setSortHiddenLast(!sortHiddenLast);
}

543
544
void DolphinViewActionHandler::slotSortOrderChanged(Qt::SortOrder order)
{
545
546
    QAction *descending = m_actionCollection->action(QStringLiteral("descending"));
    QAction *ascending = m_actionCollection->action(QStringLiteral("ascending"));
547
548
    const bool sortDescending = (order == Qt::DescendingOrder);
    descending->setChecked(sortDescending);
549
    ascending->setChecked(!sortDescending);
550
551
}

552
553
void DolphinViewActionHandler::slotSortFoldersFirstChanged(bool foldersFirst)
{
554
    m_actionCollection->action(QStringLiteral("folders_first"))->setChecked(foldersFirst);
555
556
}

557
558
559
560
561
void DolphinViewActionHandler::slotSortHiddenLastChanged(bool hiddenLast)
{
    m_actionCollection->action(QStringLiteral("hidden_last"))->setChecked(hiddenLast);
}

562
void DolphinViewActionHandler::toggleVisibleRole(QAction *action)
563
{
Alexander Lohnau's avatar
Alexander Lohnau committed
564
    Q_EMIT actionBeingHandled();
565

566
    const QByteArray toggledRole = action->data().toByteArray();
567

568
    QList<QByteArray> roles = m_currentView->visibleRoles();
569
570
571

    const bool show = action->isChecked();

572
    const int index = roles.indexOf(toggledRole);
573
574
    const bool containsInfo = (index >= 0);
    if (show && !containsInfo) {
575
576
        roles.append(toggledRole);
        m_currentView->setVisibleRoles(roles);
577
    } else if (!show && containsInfo) {
578
579
580
        roles.removeAt(index);
        m_currentView->setVisibleRoles(roles);
        Q_ASSERT(roles.indexOf(toggledRole) < 0);
581
    }
582
583
}

584
void DolphinViewActionHandler::slotVisibleRolesChanged(const QList<QByteArray> &current, const QList<QByteArray> &previous)
585
{
586
    Q_UNUSED(previous)
587

588
    const auto checkedRoles = QSet<QByteArray>(current.constBegin(), current.constEnd());
589
    QHashIterator<QByteArray, KToggleAction *> it(m_visibleRoles);
590
591
    while (it.hasNext()) {
        it.next();
592
593
        const QByteArray &role = it.key();
        KToggleAction *action = it.value();
594
        action->setChecked(checkedRoles.contains(role));
595
    }
596
597
}

598
void DolphinViewActionHandler::toggleGroupedSorting(bool grouped)
599
{
600
    m_currentView->setGroupedSorting(grouped);
601
602
}

603
void DolphinViewActionHandler::slotGroupedSortingChanged(bool groupedSorting)
604
{
605
    QAction *showInGroupsAction = m_actionCollection->action(QStringLiteral("show_in_groups"));
606
    showInGroupsAction->setChecked(groupedSorting);
607
608
609
610
}

void DolphinViewActionHandler::toggleShowHiddenFiles(bool show)
{
Alexander Lohnau's avatar
Alexander Lohnau committed
611
    Q_EMIT actionBeingHandled();
612
    m_currentView->setHiddenFilesShown(show);
613
614
}

615
void DolphinViewActionHandler::slotHiddenFilesShownChanged(bool shown)
616
{
617
    QAction *showHiddenFilesAction = m_actionCollection->action(QStringLiteral("show_hidden_files"));
618
    showHiddenFilesAction->setChecked(shown);
619
620
}

621
622
void DolphinViewActionHandler::slotWriteStateChanged(bool isFolderWritable)
{
623
    m_actionCollection->action(QStringLiteral("create_dir"))->setEnabled(isFolderWritable && KProtocolManager::supportsMakeDir(currentView()->url()));
624
625
}

626
KToggleAction *DolphinViewActionHandler::iconsModeAction()
627
{
628
    KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("icons"));
629
    iconsView->setText(i18nc("@action:inmenu View Mode", "Icons"));
630
    iconsView->setToolTip(i18nc("@info", "Icons view mode"));
631
    m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_1);
632
    iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-icons")));
633
634
635
636
    iconsView->setData(QVariant::fromValue(DolphinView::IconsView));
    return iconsView;
}

637
KToggleAction *DolphinViewActionHandler::compactModeAction()
638
{
639
    KToggleAction *iconsView = m_actionCollection->add<KToggleAction>(QStringLiteral("compact"));
640
641
    iconsView->setText(i18nc("@action:inmenu View Mode", "Compact"));
    iconsView->setToolTip(i18nc("@info", "Compact view mode"));
642
    m_actionCollection->setDefaultShortcut(iconsView, Qt::CTRL | Qt::Key_2);
643
    iconsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-details"))); // TODO: discuss with Oxygen-team the wrong (?) name
644
645
646
647
    iconsView->setData(QVariant::fromValue(DolphinView::CompactView));
    return iconsView;
}

648
KToggleAction *DolphinViewActionHandler::detailsModeAction()
649
{
650
    KToggleAction *detailsView = m_actionCollection->add<KToggleAction>(QStringLiteral("details"));
651
    detailsView->setText(i18nc("@action:inmenu View Mode", "Details"));
652
    detailsView->setToolTip(i18nc("@info", "Details view mode"));
653
    m_actionCollection->setDefaultShortcut(detailsView, Qt::CTRL | Qt::Key_3);
654
    detailsView->setIcon(QIcon::fromTheme(QStringLiteral("view-list-tree")));
655
656
657
658
    detailsView->setData(QVariant::fromValue(DolphinView::DetailsView));
    return detailsView;
}

659
void DolphinViewActionHandler::slotSortRoleChanged(const QByteArray &role)
660
{
661
    KToggleAction *action = m_sortByActions.value(role);
662
    if (action) {
663
        action->setChecked(true);
664

665
        if (!action->icon().isNull()) {
666
            QAction *sortByMenu = m_actionCollection->action(QStringLiteral("sort"));
Emmanuel Pescosta's avatar
Emmanuel Pescosta committed
667
            sortByMenu->setIcon(action->icon());
668
        }
669
    }
670

671
672
    QAction *descending = m_actionCollection->action(QStringLiteral("descending"));
    QAction *ascending = m_actionCollection->action(QStringLiteral("ascending"));
673

674
    if (role == "text" || role == "type" || role == "extension" || role == "tags" || role == "comment") {
675
676
677
        descending->setText(i18nc("Sort descending", "Z-A"));
        ascending->setText(i18nc("Sort ascending", "A-Z"));
    } else if (role == "size") {
Nikita Churaev's avatar
Nikita Churaev committed
678
679
        descending->setText(i18nc("Sort descending", "Largest First"));
        ascending->setText(i18nc("Sort ascending", "Smallest First"));
680
    } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
Nikita Churaev's avatar
Nikita Churaev committed
681
682
        descending->setText(i18nc("Sort descending", "Newest First"));
        ascending->setText(i18nc("Sort ascending", "Oldest First"));
683
    } else if (role == "rating") {
Nikita Churaev's avatar
Nikita Churaev committed
684
685
        descending->setText(i18nc("Sort descending", "Highest First"));
        ascending->setText(i18nc("Sort ascending", "Lowest First"));
686
687
688
689
690
691
    } else {
        descending->setText(i18nc("Sort descending", "Descending"));
        ascending->setText(i18nc("Sort ascending", "Ascending"));
    }

    slotSortOrderChanged(m_currentView->sortOrder());
692
693
}

694
void DolphinViewActionHandler::slotZoomLevelChanged(int current, int previous)
695
{
696
    Q_UNUSED(previous)
697

698
    QAction *zoomInAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomIn));
699
    if (zoomInAction) {
700
        zoomInAction->setEnabled(current < ZoomLevelInfo::maximumLevel());
701
702
    }

703
    QAction *zoomOutAction = m_actionCollection->action(KStandardAction::name(KStandardAction::ZoomOut));
704
    if (zoomOutAction) {
705
        zoomOutAction->setEnabled(current > ZoomLevelInfo::minimumLevel());
706
707
708
    }
}

709
void DolphinViewActionHandler::slotSortTriggered(QAction *action)
710
{
711
712
713
714
    // The radiobuttons of the "Sort By"-menu are split between the main-menu
    // and several sub-menus. Because of this they don't have a common
    // action-group that assures an exclusive toggle-state between the main-menu
    // actions and the sub-menu-actions. If an action gets checked, it must
715
716
717
    // be assured that all other actions get unchecked, except the ascending/
    // descending actions
    for (QAction *groupAction : qAsConst(m_sortByActions)) {
718
        KActionMenu *actionMenu = qobject_cast<KActionMenu *>(groupAction);
719
        if (actionMenu) {
Alexander Lohnau's avatar
Alexander Lohnau committed
720
            const auto actions = actionMenu->menu()->actions();
721
            for (QAction *subAction : actions) {
722
723
724
725
726
727
728
729
730
                subAction->setChecked(false);
            }
        } else if (groupAction->actionGroup()) {
            groupAction->setChecked(false);
        }
    }
    action->setChecked(true);

    // Apply the activated sort-role to the view
731
732
    const QByteArray role = action->data().toByteArray();
    m_currentView->setSortRole(role);
733
}
734
735
736

void DolphinViewActionHandler::slotAdjustViewProperties()
{
Alexander Lohnau's avatar
Alexander Lohnau committed
737
    Q_EMIT actionBeingHandled();
738
739
740
    QPointer<ViewPropertiesDialog> dialog = new ViewPropertiesDialog(m_currentView);
    dialog->exec();
    delete dialog;
741
}
742

Nate Graham's avatar
Nate Graham committed
743
744
void DolphinViewActionHandler::slotDuplicate()
{
Felix Ernst's avatar
Felix Ernst committed
745
    if (m_currentView->selectedItemsCount() == 0) {
746
        Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::DuplicateContents);
Felix Ernst's avatar
Felix Ernst committed
747
748
749
    } else {
        Q_EMIT actionBeingHandled();
        m_currentView->duplicateSelectedItems();
750
        Q_EMIT selectionModeChangeTriggered(false);
Felix Ernst's avatar
Felix Ernst committed
751
    }
Nate Graham's avatar
Nate Graham committed
752
753
}

754
755
void DolphinViewActionHandler::slotProperties()
{
756
    KPropertiesDialog *dialog = nullptr;
757
758
    const KFileItemList list = m_currentView->selectedItems();
    if (list.isEmpty()) {
Lukáš Tinkl's avatar
Lukáš Tinkl committed
759
        const QUrl url = m_currentView->url();
760
761
762
763
764
765
766
767
768
769
        dialog = new KPropertiesDialog(url, m_currentView);
    } else {
        dialog = new KPropertiesDialog(list, m_currentView);
    }

    dialog->setAttribute(Qt::WA_DeleteOnClose);
    dialog->show();
    dialog->raise();
    dialog->activateWindow();
}
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
770
771
772

void DolphinViewActionHandler::slotCopyPath()
{
Felix Ernst's avatar
Felix Ernst committed
773
    if (m_currentView->selectedItemsCount() == 0) {
774
        Q_EMIT selectionModeChangeTriggered(true, SelectionMode::BottomBar::Contents::CopyLocationContents);
Felix Ernst's avatar
Felix Ernst committed
775
776
    } else {
        m_currentView->copyPathToClipboard();
777
        Q_EMIT selectionModeChangeTriggered(false);
Felix Ernst's avatar
Felix Ernst committed
778
    }
Yann Holme-Nielsen's avatar
Yann Holme-Nielsen committed
779
}
780

781
void DolphinViewActionHandler::slotSelectionChanged(const KFileItemList &selection)
782
783
{
    QString basicActionsMenuText;
784
    if (selection.isEmpty()) {
785
786
        basicActionsMenuText = i18nc("@action:inmenu menu with actions like copy, paste, rename. The user's selection is empty when this text is shown.",
                                     "Actions for Current View");
787
    } else {
788
        // clang-format off