File indexing completed on 2024-05-19 04:58:25
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2009-2010 Felix Rohrbach <fxrh@gmx.de> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "is_gd.h" 0010 0011 #include <QEventLoop> 0012 #include <QJsonDocument> 0013 0014 #include <KIO/StoredTransferJob> 0015 #include <KLocalizedString> 0016 #include <KPluginFactory> 0017 0018 #include "notifymanager.h" 0019 0020 #include "is_gd_settings.h" 0021 0022 K_PLUGIN_CLASS_WITH_JSON(Is_gd, "choqok_is_gd.json") 0023 0024 Is_gd::Is_gd(QObject *parent, const QVariantList &) 0025 : Choqok::Shortener(QLatin1String("choqok_is_gd"), parent) 0026 { 0027 } 0028 0029 Is_gd::~Is_gd() 0030 { 0031 } 0032 0033 QString Is_gd::shorten(const QString &url) 0034 { 0035 Is_gd_Settings::self()->load(); 0036 0037 QUrl reqUrl(QLatin1String("https://is.gd/create.php")); 0038 QUrlQuery reqQuery; 0039 reqQuery.addQueryItem(QLatin1String("format"), QLatin1String("json")); 0040 reqQuery.addQueryItem(QLatin1String("url"), QUrl(url).url()); 0041 if (Is_gd_Settings::logstats()) { 0042 reqQuery.addQueryItem(QLatin1String("logstats"), QLatin1String("true")); 0043 } 0044 0045 reqUrl.setQuery(reqQuery); 0046 QEventLoop loop; 0047 KIO::StoredTransferJob *job = KIO::storedGet(reqUrl, KIO::Reload, KIO::HideProgressInfo); 0048 connect(job, &KIO::StoredTransferJob::result, &loop, &QEventLoop::quit); 0049 job->start(); 0050 loop.exec(); 0051 0052 if (job->error() == KJob::NoError) { 0053 0054 const QJsonDocument json = QJsonDocument::fromJson(job->data()); 0055 if (!json.isNull()) { 0056 const QVariantMap map = json.toVariant().toMap(); 0057 0058 if (!map[ QLatin1String("errorcode") ].toString().isEmpty()) { 0059 Choqok::NotifyManager::error(map[ QLatin1String("errormessage") ].toString(), i18n("is.gd Error")); 0060 return url; 0061 } 0062 QString shorturl = map[ QLatin1String("shorturl") ].toString(); 0063 if (!shorturl.isEmpty()) { 0064 return shorturl; 0065 } 0066 } else { 0067 Choqok::NotifyManager::error(i18n("Malformed response"), i18n("is.gd Error")); 0068 } 0069 } else { 0070 Choqok::NotifyManager::error(i18n("Cannot create a short URL.\n%1", job->errorString()), i18n("is.gd Error")); 0071 } 0072 return url; 0073 } 0074 0075 #include "is_gd.moc" 0076 #include "moc_is_gd.cpp"