File indexing completed on 2024-04-14 03:51:12

0001 /*
0002     SPDX-FileCopyrightText: 2003 Jason Keirstead <jason@keirstead.org>
0003     SPDX-FileCopyrightText: 2006 Michel Hermier <michel.hermier@gmail.com>
0004     SPDX-FileCopyrightText: 2007 Nick Shaforostoff <shafff@ukr.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kcodecaction.h"
0010 #include "kconfigwidgets_debug.h"
0011 
0012 #include <KCharsets>
0013 #include <KEncodingProber>
0014 #include <KLocalizedString>
0015 
0016 #include <QMenu>
0017 #include <QVariant>
0018 
0019 class KCodecActionPrivate
0020 {
0021 public:
0022     KCodecActionPrivate(KCodecAction *parent)
0023         : q(parent)
0024     {
0025     }
0026 
0027     void init(bool);
0028 
0029     void subActionTriggered(QAction *);
0030 
0031     KCodecAction *const q;
0032     QAction *defaultAction = nullptr;
0033     QAction *currentSubAction = nullptr;
0034 };
0035 
0036 KCodecAction::KCodecAction(QObject *parent, bool showAutoOptions)
0037     : KSelectAction(parent)
0038     , d(new KCodecActionPrivate(this))
0039 {
0040     d->init(showAutoOptions);
0041 }
0042 
0043 KCodecAction::KCodecAction(const QString &text, QObject *parent, bool showAutoOptions)
0044     : KSelectAction(text, parent)
0045     , d(new KCodecActionPrivate(this))
0046 {
0047     d->init(showAutoOptions);
0048 }
0049 
0050 KCodecAction::KCodecAction(const QIcon &icon, const QString &text, QObject *parent, bool showAutoOptions)
0051     : KSelectAction(icon, text, parent)
0052     , d(new KCodecActionPrivate(this))
0053 {
0054     d->init(showAutoOptions);
0055 }
0056 
0057 KCodecAction::~KCodecAction() = default;
0058 
0059 void KCodecActionPrivate::init(bool showAutoOptions)
0060 {
0061     q->setToolBarMode(KSelectAction::MenuMode);
0062     defaultAction = q->addAction(i18nc("Encodings menu", "Default"));
0063 
0064     const auto lstEncodings = KCharsets::charsets()->encodingsByScript();
0065     for (const QStringList &encodingsForScript : lstEncodings) {
0066         KSelectAction *tmp = new KSelectAction(encodingsForScript.at(0), q);
0067         if (showAutoOptions) {
0068             KEncodingProber::ProberType scri = KEncodingProber::proberTypeForName(encodingsForScript.at(0));
0069             if (scri != KEncodingProber::None) {
0070                 tmp->addAction(i18nc("Encodings menu", "Autodetect"))->setData(QVariant((uint)scri));
0071                 tmp->menu()->addSeparator();
0072             }
0073         }
0074         for (int i = 1; i < encodingsForScript.size(); ++i) {
0075             tmp->addAction(encodingsForScript.at(i));
0076         }
0077         q->connect(tmp, &KSelectAction::actionTriggered, q, [this](QAction *action) {
0078             subActionTriggered(action);
0079         });
0080         tmp->setCheckable(true);
0081         q->addAction(tmp);
0082     }
0083     q->setCurrentItem(0);
0084 }
0085 
0086 void KCodecAction::slotActionTriggered(QAction *action)
0087 {
0088     // we don't want to emit any signals from top-level items
0089     // except for the default one
0090     if (action == d->defaultAction) {
0091         Q_EMIT defaultItemTriggered();
0092     }
0093 }
0094 
0095 void KCodecActionPrivate::subActionTriggered(QAction *action)
0096 {
0097     if (currentSubAction == action) {
0098         return;
0099     }
0100     currentSubAction = action;
0101     Q_EMIT q->textTriggered(action->text());
0102     Q_EMIT q->codecNameTriggered(action->text().toUtf8());
0103 }
0104 
0105 QString KCodecAction::currentCodecName() const
0106 {
0107     return d->currentSubAction->text();
0108 }
0109 
0110 bool KCodecAction::setCurrentCodec(const QString &codecName)
0111 {
0112     if (codecName.isEmpty()) {
0113         return false;
0114     }
0115 
0116     for (int i = 0; i < actions().size(); ++i) {
0117         if (actions().at(i)->menu()) {
0118             for (int j = 0; j < actions().at(i)->menu()->actions().size(); ++j) {
0119                 if (!j && !actions().at(i)->menu()->actions().at(j)->data().isNull()) {
0120                     continue;
0121                 }
0122                 if (codecName == actions().at(i)->menu()->actions().at(j)->text()) {
0123                     d->currentSubAction = actions().at(i)->menu()->actions().at(j);
0124                     d->currentSubAction->trigger();
0125                     return true;
0126                 }
0127             }
0128         }
0129     }
0130     return false;
0131 }
0132 
0133 #include "moc_kcodecaction.cpp"