1/*
2 SPDX-FileCopyrightText: 2003 Jason Keirstead <jason@keirstead.org>
3 SPDX-FileCopyrightText: 2006 Michel Hermier <michel.hermier@gmail.com>
4 SPDX-FileCopyrightText: 2007 Nick Shaforostoff <shafff@ukr.net>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kcodecaction.h"
10#include "kconfigwidgets_debug.h"
11
12#include <KCharsets>
13#include <KEncodingProber>
14#include <KLocalizedString>
15
16#include <QMenu>
17#include <QVariant>
18
19class KCodecActionPrivate
20{
21public:
22 KCodecActionPrivate(KCodecAction *parent)
23 : q(parent)
24 {
25 }
26
27 void init(bool);
28
29 void subActionTriggered(QAction *);
30
31 KCodecAction *const q;
32 QAction *defaultAction = nullptr;
33 QAction *currentSubAction = nullptr;
34};
35
36KCodecAction::KCodecAction(QObject *parent, bool showAutoOptions)
37 : KSelectAction(parent)
38 , d(new KCodecActionPrivate(this))
39{
40 d->init(showAutoOptions);
41}
42
43KCodecAction::KCodecAction(const QString &text, QObject *parent, bool showAutoOptions)
44 : KSelectAction(text, parent)
45 , d(new KCodecActionPrivate(this))
46{
47 d->init(showAutoOptions);
48}
49
50KCodecAction::KCodecAction(const QIcon &icon, const QString &text, QObject *parent, bool showAutoOptions)
51 : KSelectAction(icon, text, parent)
52 , d(new KCodecActionPrivate(this))
53{
54 d->init(showAutoOptions);
55}
56
57KCodecAction::~KCodecAction() = default;
58
59void KCodecActionPrivate::init(bool showAutoOptions)
60{
61 q->setToolBarMode(KSelectAction::MenuMode);
62 defaultAction = q->addAction(i18nc("Encodings menu", "Default"));
63
64 const auto lstEncodings = KCharsets::charsets()->encodingsByScript();
65 for (const QStringList &encodingsForScript : lstEncodings) {
66 KSelectAction *tmp = new KSelectAction(encodingsForScript.at(i: 0), q);
67 if (showAutoOptions) {
68 KEncodingProber::ProberType scri = KEncodingProber::proberTypeForName(lang: encodingsForScript.at(i: 0));
69 if (scri != KEncodingProber::None) {
70 tmp->addAction(i18nc("Encodings menu", "Autodetect"))->setData(QVariant((uint)scri));
71 tmp->menu()->addSeparator();
72 }
73 }
74 for (int i = 1; i < encodingsForScript.size(); ++i) {
75 tmp->addAction(text: encodingsForScript.at(i));
76 }
77 q->connect(sender: tmp, signal: &KSelectAction::actionTriggered, context: q, slot: [this](QAction *action) {
78 subActionTriggered(action);
79 });
80 tmp->setCheckable(true);
81 q->addAction(action: tmp);
82 }
83 q->setCurrentItem(0);
84}
85
86void KCodecAction::slotActionTriggered(QAction *action)
87{
88 // we don't want to emit any signals from top-level items
89 // except for the default one
90 if (action == d->defaultAction) {
91 Q_EMIT defaultItemTriggered();
92 }
93}
94
95void KCodecActionPrivate::subActionTriggered(QAction *action)
96{
97 if (currentSubAction == action) {
98 return;
99 }
100 currentSubAction = action;
101 Q_EMIT q->textTriggered(text: action->text());
102 Q_EMIT q->codecNameTriggered(name: action->text().toUtf8());
103}
104
105QString KCodecAction::currentCodecName() const
106{
107 return d->currentSubAction->text();
108}
109
110bool KCodecAction::setCurrentCodec(const QString &codecName)
111{
112 if (codecName.isEmpty()) {
113 return false;
114 }
115
116 for (int i = 0; i < actions().size(); ++i) {
117 if (actions().at(i)->menu()) {
118 for (int j = 0; j < actions().at(i)->menu()->actions().size(); ++j) {
119 if (!j && !actions().at(i)->menu()->actions().at(i: j)->data().isNull()) {
120 continue;
121 }
122 if (codecName == actions().at(i)->menu()->actions().at(i: j)->text()) {
123 d->currentSubAction = actions().at(i)->menu()->actions().at(i: j);
124 d->currentSubAction->trigger();
125 return true;
126 }
127 }
128 }
129 }
130 return false;
131}
132
133#include "moc_kcodecaction.cpp"
134

source code of kconfigwidgets/src/kcodecaction.cpp