┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/itemactions/setfoldericonitemaction.cpp
blob: feb6adc2b8891149af99c0acc2e4b4fe39d9ea50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * SPDX-FileCopyrightText: 2025 Méven Car <[email protected]>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "setfoldericonitemaction.h"
#include "../dolphindebug.h"

#include <KConfigGroup>
#include <KDesktopFile>
#include <KFileItem>
#include <KLocalizedString>
#include <KPluginFactory>
#ifdef QT_DBUS_LIB
#include <KDirNotify>
#endif

#include <QActionGroup>
#include <QBoxLayout>
#include <QEvent>
#include <QFocusEvent>
#include <QKeyEvent>
#include <QMenu>
#include <QPushButton>
#include <QUrl>
#include <QWidgetAction>

K_PLUGIN_CLASS_WITH_JSON(SetFolderIconItemAction, "setfoldericonitemaction.json")

namespace
{
bool isDefaultFolderIcon(const QString &iconName)
{
    return iconName.isEmpty() || iconName == QLatin1String("folder") || iconName == QLatin1String("inode-directory");
}
}

SetFolderIconItemAction::SetFolderIconItemAction(QObject *parent)
    : KAbstractFileItemActionPlugin(parent)
{
}

void SetFolderIconItemAction::setFolderIcon(bool check)
{
    QAction *action = qobject_cast<QAction *>(sender());
    Q_ASSERT(action);

    action->setChecked(check);

    auto iconName = action->icon().name();

    // Apply custom folder icon, if applicable.
    const QString fileName = m_localUrl.toLocalFile() + QLatin1String("/.directory");
    KDesktopFile desktopFile{fileName};

    if (check && !isDefaultFolderIcon(iconName)) {
        desktopFile.desktopGroup().writeEntry(QStringLiteral("Icon"), iconName);
    } else {
        desktopFile.desktopGroup().deleteEntry(QStringLiteral("Icon"));
        if (desktopFile.desktopGroup().entryMap().isEmpty() && QFile::exists(fileName)) {
            // clean file
            QFile::remove(fileName);
        }
    }

#ifdef QT_DBUS_LIB
    org::kde::KDirNotify::emitFilesChanged({m_url});
#endif
}

class ButtonsWithSubMenuWidgetAction : public QWidgetAction
{
public:
    ButtonsWithSubMenuWidgetAction(QMenu *subMenu, QWidget *parentWidget)
        : QWidgetAction(parentWidget)
        , m_subMenu(subMenu)
    {
    }

    void setActions(const QList<QAction *> &actions)
    {
        m_actions = actions;
    }

    bool eventFilter(QObject *object, QEvent *event) override
    {
        if (event->type() == QEvent::KeyPress) {
            const QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            auto widget = qobject_cast<QWidget *>(object);

            if (keyEvent->keyCombination() == QKeyCombination(Qt::Modifier::SHIFT, Qt::Key_Backtab) || keyEvent->key() == Qt::Key_Left
                || keyEvent->key() == Qt::Key_Up) {
                auto previous = widget->previousInFocusChain();
                if (previous == widget->parentWidget()) {
                    // the next object is the parent, let the focus bubble up
                    return false;
                }

                previous->setFocus(Qt::BacktabFocusReason);
                event->accept();
                return true;
            }

            if (keyEvent->keyCombination() == QKeyCombination(Qt::Key_Tab) || keyEvent->key() == Qt::Key_Right || keyEvent->key() == Qt::Key_Down) {
                auto next = widget->nextInFocusChain();
                if (next->parentWidget() != widget->parentWidget()) {
                    // the next object is not a sibling, let the focus bubble up
                    return false;
                }

                next->setFocus(Qt::TabFocusReason);
                event->accept();
                return true;
            }
        }

        // TODO implement proper SHIFT+TAB
        // See https://bugreports.qt.io/browse/QTBUG-137298

        return false;
    }

    QWidget *createWidget(QWidget *parent) override
    {
        QWidget *widget = new QWidget(parent);
        auto layout = new QHBoxLayout(widget);

        bool firstAction = false;
        for (const auto action : std::as_const(m_actions)) {
            if (!action->parent()) {
                action->setParent(widget);
            }

            auto p = new QPushButton(widget);

            p->setIcon(action->icon());
            p->setCheckable(true);
            p->setChecked(action->isChecked());
            p->setToolTip(action->toolTip());
            p->installEventFilter(this);

            connect(p, &QPushButton::clicked, action, &QAction::triggered);
            connect(action, &QAction::toggled, p, &QPushButton::setChecked);

            layout->addWidget(p);

            if (!firstAction) {
                widget->setFocusProxy(p);
                firstAction = true;
            }
        }

        auto p = new QPushButton(widget);
        p->setText(i18nc("@action open a submenu with additional entries", "Other"));
        p->setToolTip(i18nc("@label", "Other folder icon options"));
        p->setMenu(m_subMenu);
        layout->addWidget(p);
        p->installEventFilter(this);

        widget->setFocusPolicy(Qt::StrongFocus);

        return widget;
    }

    QList<QAction *> m_actions;
    QMenu *m_subMenu;
};

QList<QAction *> SetFolderIconItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
{
    if (fileItemInfos.items().count() != 1) {
        return {};
    }

    auto fileItem = fileItemInfos.items().at(0);
    m_url = fileItem.url();

    bool local;
    m_localUrl = fileItem.mostLocalUrl(&local);
    if (!local || !fileItemInfos.supportsWriting() || !fileItem.isWritable()) {
        return {};
    }
    const short s_numberOfEntriesVisible = 5;

    using StringPair = QPair<KLocalizedString, QString>;
    // keep in sync with kio/src/filewidgets/knewfilemenu.cpp
    // default folder icon goes here.
    const QList<StringPair> icons = {// colors.
                                     StringPair{ki18nc("@label as in default folder color", "Red"), QStringLiteral("folder-red")},
                                     StringPair{ki18nc("@label as in default folder color", "Yellow"), QStringLiteral("folder-yellow")},
                                     StringPair{ki18nc("@label as in default folder color", "Orange"), QStringLiteral("folder-orange")},
                                     StringPair{ki18nc("@label as in default folder color", "Green"), QStringLiteral("folder-green")},
                                     StringPair{ki18nc("@label as in default folder color", "Cyan"), QStringLiteral("folder-cyan")},
                                     // must match s_numberOfEntriesVisible
                                     StringPair{ki18nc("@label: as in default folder icon", "Default"), QStringLiteral("inode-directory")},

                                     StringPair{ki18nc("@label as in default folder color", "Blue"), QStringLiteral("folder-blue")},
                                     StringPair{ki18nc("@label as in default folder color", "Violet"), QStringLiteral("folder-violet")},
                                     StringPair{ki18nc("@label as in default folder color", "Brown"), QStringLiteral("folder-brown")},
                                     StringPair{ki18nc("@label as in default folder color", "Grey"), QStringLiteral("folder-grey")},

                                     // emblems.
                                     StringPair{ki18nc("@label as in default folder color", "Bookmark"), QStringLiteral("folder-bookmark")},
                                     StringPair{ki18nc("@label as in default folder color", "Cloud"), QStringLiteral("folder-cloud")},
                                     StringPair{ki18nc("@label as in default folder color", "Development"), QStringLiteral("folder-development")},
                                     StringPair{ki18nc("@label as in default folder color", "Games"), QStringLiteral("folder-games")},
                                     StringPair{ki18nc("@label as in default folder color", "Mail"), QStringLiteral("folder-mail")},
                                     StringPair{ki18nc("@label as in default folder color", "Music"), QStringLiteral("folder-music")},
                                     StringPair{ki18nc("@label as in default folder color", "Print"), QStringLiteral("folder-print")},
                                     StringPair{ki18nc("@label as in default folder color", "Compressed"), QStringLiteral("folder-tar")},
                                     StringPair{ki18nc("@label as in default folder color", "Temporary"), QStringLiteral("folder-temp")},
                                     StringPair{ki18nc("@label as in default folder color", "Important"), QStringLiteral("folder-important")}};

    QActionGroup *actiongroup = new QActionGroup(this);
    actiongroup->setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional);

    QMenu *subMenu = new QMenu();
    auto action = new ButtonsWithSubMenuWidgetAction(subMenu, parentWidget);

    int i = 0;
    QList<QAction *> actions;
    const auto fileIconName = fileItem.iconName();
    for (const auto &[name, iconName] : icons) {
        auto icon = QIcon::fromTheme(iconName);
        if (icon.isNull()) {
            qCWarning(DolphinDebug) << "SetFolderIconItemAction Missing icon:" << iconName;
            continue;
        }

        QAction *folderIconAction = new QAction(KLocalizedString(name).toString(), parentWidget);
        folderIconAction->setIcon(icon);
        folderIconAction->setCheckable(true);
        folderIconAction->setChecked(fileIconName == iconName);
        folderIconAction->setToolTip(i18nc("@label %1 is a folder icon name (Red, Music...) etc", "Set folder icon to %1", folderIconAction->iconText()));
        actiongroup->addAction(folderIconAction);

        connect(folderIconAction, &QAction::triggered, this, &SetFolderIconItemAction::setFolderIcon);
        connect(folderIconAction, &QAction::triggered, action, &QAction::triggered);

        ++i;
        if (i < s_numberOfEntriesVisible + 1) {
            actions.append(folderIconAction);
        } else {
            folderIconAction->setParent(subMenu);
            subMenu->addAction(folderIconAction);
        }
    }
    action->setActions(actions);

    return {action};
}

#include "moc_setfoldericonitemaction.cpp"
#include "setfoldericonitemaction.moc"