File indexing completed on 2024-05-12 04:57:43

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Andrey Esin <gmlastik@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "bit_ly_config.h"
0010 
0011 #include <QComboBox>
0012 #include <QLineEdit>
0013 #include <QPushButton>
0014 #include <QVBoxLayout>
0015 
0016 #include <KAboutData>
0017 #include <KIO/StoredTransferJob>
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 #include <KPluginFactory>
0021 #include <KSharedConfig>
0022 
0023 #include "notifymanager.h"
0024 #include "passwordmanager.h"
0025 
0026 #include "bit_ly_settings.h"
0027 
0028 K_PLUGIN_CLASS_WITH_JSON(Bit_ly_Config, "choqok_bit_ly_config.json")
0029 
0030 Bit_ly_Config::Bit_ly_Config(QWidget *parent, const QVariantList &):
0031     KCModule(parent)
0032 {
0033     QVBoxLayout *layout = new QVBoxLayout(this);
0034     QWidget *wd = new QWidget(this);
0035     wd->setObjectName(QLatin1String("mBitLYCtl"));
0036     wd->setMinimumWidth(400);
0037     ui.setupUi(wd);
0038     addConfig(Bit_ly_Settings::self(), wd);
0039     layout->addWidget(wd);
0040 
0041     QRegExp rx(QLatin1String("([a-z0-9_]){4,32}"), Qt::CaseInsensitive);
0042     QValidator *val0 = new QRegExpValidator(rx, nullptr);
0043     ui.kcfg_login->setValidator(val0);
0044     rx.setPattern(QLatin1String("([a-z0-9_]){1,40}"));
0045     QValidator *val1 = new QRegExpValidator(rx, nullptr);
0046     ui.kcfg_api_key->setValidator(val1);
0047 
0048     ui.help_label->setTextFormat(Qt::RichText);
0049     ui.help_label->setText(i18nc("The your_api_key part of the URL is a fixed part of the URL "
0050                                  "and should probably not be changed in localization.",
0051                                  "You can find your API key <a href=\"http://bit.ly/a/your_api_key\">here</a>"));
0052 
0053     domains << QLatin1String("bit.ly") << QLatin1String("j.mp");
0054     ui.kcfg_domain->addItems(domains);
0055 
0056     connect(ui.kcfg_login, &QLineEdit::textChanged, this, &Bit_ly_Config::emitChanged);
0057     connect(ui.kcfg_api_key, &QLineEdit::textChanged, this, &Bit_ly_Config::emitChanged);
0058     connect(ui.kcfg_domain, (void (QComboBox::*)(int))&QComboBox::currentIndexChanged,
0059             this, &Bit_ly_Config::emitChanged);
0060     connect(ui.validate_button, &QPushButton::clicked, this, &Bit_ly_Config::slotValidate);
0061 }
0062 
0063 Bit_ly_Config::~Bit_ly_Config()
0064 {
0065 }
0066 
0067 void Bit_ly_Config::load()
0068 {
0069     KCModule::load();
0070     KConfigGroup grp(KSharedConfig::openConfig(), "Bit.ly Shortener");
0071     ui.kcfg_login->setText(grp.readEntry("login", ""));
0072     ui.kcfg_domain->setCurrentIndex(domains.indexOf(grp.readEntry("domain", "bit.ly")));
0073     ui.kcfg_api_key->setText(Choqok::PasswordManager::self()->readPassword(QStringLiteral("bitly_%1")
0074                              .arg(ui.kcfg_login->text())));
0075 }
0076 
0077 void Bit_ly_Config::save()
0078 {
0079     KCModule::save();
0080     KConfigGroup grp(KSharedConfig::openConfig(), "Bit.ly Shortener");
0081     grp.writeEntry("login", ui.kcfg_login->text());
0082     grp.writeEntry("domain", domains.at(ui.kcfg_domain->currentIndex()));
0083     Choqok::PasswordManager::self()->writePassword(QStringLiteral("bitly_%1").arg(ui.kcfg_login->text()),
0084             ui.kcfg_api_key->text());
0085 }
0086 
0087 void Bit_ly_Config::emitChanged()
0088 {
0089     Q_EMIT changed(true);
0090 }
0091 
0092 void Bit_ly_Config::slotValidate()
0093 {
0094     ui.validate_button->setEnabled(false);
0095     ui.validate_button->setText(i18n("Checking..."));
0096     QString login = QCoreApplication::applicationName();
0097     QString apiKey = QLatin1String("R_bdd1ae8b6191dd36e13fc77ca1d4f27f");
0098     QUrl reqUrl(QLatin1String("http://api.bit.ly/v3/validate"));
0099     QUrlQuery reqQuery;
0100 
0101     reqQuery.addQueryItem(QLatin1String("x_login"), ui.kcfg_login->text());
0102     reqQuery.addQueryItem(QLatin1String("x_apiKey"), ui.kcfg_api_key->text());
0103 
0104     if (Bit_ly_Settings::domain() == QLatin1String("j.mp")) {   //bit.ly is default domain
0105         reqQuery.addQueryItem(QLatin1String("domain"), QLatin1String("j.mp"));
0106     }
0107 
0108     reqQuery.addQueryItem(QLatin1String("login"), QLatin1String(login.toUtf8()));
0109     reqQuery.addQueryItem(QLatin1String("apiKey"), QLatin1String(apiKey.toUtf8()));
0110     reqQuery.addQueryItem(QLatin1String("format"), QLatin1String("txt"));
0111     reqUrl.setQuery(reqQuery);
0112 
0113     KIO::StoredTransferJob *job = KIO::storedGet(reqUrl, KIO::Reload, KIO::HideProgressInfo);
0114     job->exec();
0115 
0116     if (!job->error()) {
0117         QString output(QLatin1String(job->data()));
0118         if (output.startsWith(QLatin1Char('0')))
0119             KMessageBox::error(this, i18nc("The your_api_key part of the URL is a fixed part of the URL "
0120                                            "and should probably not be changed in localization.",
0121                                            "Provided data is invalid. Try another login or API key.\n"
0122                                            "You can find it on http://bit.ly/a/your_api_key"));
0123         if (output.startsWith(QLatin1Char('1'))) {
0124             KMessageBox::information(this, i18n("You entered valid information."));
0125         }
0126     } else {
0127         Choqok::NotifyManager::error(job->errorString(), i18n("bit.ly Config Error"));
0128     }
0129 
0130     ui.validate_button->setEnabled(true);
0131     ui.validate_button->setText(i18n("Validate"));
0132 }
0133 
0134 #include "bit_ly_config.moc"
0135 #include "moc_bit_ly_config.cpp"