File indexing completed on 2025-01-05 04:49:28

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "insertshorturlconfigurewidget.h"
0008 #include "shorturlengineplugin/shorturlengineplugin.h"
0009 #include "shorturlengineplugin/shorturlenginepluginmanager.h"
0010 
0011 #include <KConfigGroup>
0012 #include <KLocalizedString>
0013 #include <KSharedConfig>
0014 #include <QComboBox>
0015 #include <QLabel>
0016 #include <QVBoxLayout>
0017 namespace
0018 {
0019 static const char myConfigGroupName[] = "ShortUrl";
0020 }
0021 InsertShorturlConfigureWidget::InsertShorturlConfigureWidget(QWidget *parent)
0022     : QWidget(parent)
0023     , mShortUrlServer(new QComboBox(this))
0024 {
0025     auto mainLayout = new QVBoxLayout(this);
0026     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0027     mainLayout->setContentsMargins({});
0028 
0029     auto lab = new QLabel(i18n("Select Short URL server:"), this);
0030     lab->setObjectName(QLatin1StringView("label"));
0031     mainLayout->addWidget(lab);
0032 
0033     mShortUrlServer->setObjectName(QLatin1StringView("shorturlserver"));
0034     mainLayout->addWidget(mShortUrlServer);
0035     const QList<ShortUrlEnginePlugin *> lstPlugin = ShortUrlEnginePluginManager::self()->pluginsList();
0036     for (ShortUrlEnginePlugin *plugin : lstPlugin) {
0037         mShortUrlServer->addItem(plugin->pluginName(), plugin->engineName());
0038     }
0039 
0040     connect(mShortUrlServer, &QComboBox::activated, this, &InsertShorturlConfigureWidget::slotChanged);
0041     loadConfig();
0042 }
0043 
0044 InsertShorturlConfigureWidget::~InsertShorturlConfigureWidget() = default;
0045 
0046 void InsertShorturlConfigureWidget::slotChanged()
0047 {
0048     mChanged = true;
0049 }
0050 
0051 void InsertShorturlConfigureWidget::loadConfig()
0052 {
0053     KConfigGroup grp(KSharedConfig::openConfig(), QLatin1StringView(myConfigGroupName));
0054     const QString engineName = grp.readEntry("EngineName");
0055     int index = mShortUrlServer->findData(engineName);
0056     if (index < 0) {
0057         index = 0;
0058     }
0059     mShortUrlServer->setCurrentIndex(index);
0060     mChanged = false;
0061 }
0062 
0063 void InsertShorturlConfigureWidget::writeConfig()
0064 {
0065     if (mChanged) {
0066         KConfigGroup grp(KSharedConfig::openConfig(), QLatin1StringView(myConfigGroupName));
0067         grp.writeEntry("EngineName", mShortUrlServer->itemData(mShortUrlServer->currentIndex()).toString());
0068         grp.sync();
0069     }
0070 }
0071 
0072 #include "moc_insertshorturlconfigurewidget.cpp"