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 "goo_gl.h" 0010 0011 #include <QJsonDocument> 0012 #include <QUrl> 0013 0014 #include <KIO/StoredTransferJob> 0015 #include <KLocalizedString> 0016 #include <KPluginFactory> 0017 0018 #include "notifymanager.h" 0019 0020 K_PLUGIN_CLASS_WITH_JSON(Goo_gl, "choqok_goo_gl.json") 0021 0022 Goo_gl::Goo_gl(QObject *parent, const QVariantList &) 0023 : Choqok::Shortener(QLatin1String("choqok_goo_gl"), parent) 0024 { 0025 } 0026 0027 Goo_gl::~Goo_gl() 0028 { 0029 } 0030 0031 QString Goo_gl::shorten(const QString &url) 0032 { 0033 QVariantMap req; 0034 req.insert(QLatin1String("longUrl"), url); 0035 const QByteArray json = QJsonDocument::fromVariant(req).toJson(); 0036 KIO::StoredTransferJob *job = KIO::storedHttpPost(json, QUrl::fromUserInput(QLatin1String("https://www.googleapis.com/urlshortener/v1/url")), KIO::HideProgressInfo) ; 0037 if (!job) { 0038 Choqok::NotifyManager::error(i18n("Error when creating job"), i18n("Goo.gl Error")); 0039 return url; 0040 } 0041 job->addMetaData(QLatin1String("content-type"), QLatin1String("Content-Type: application/json")); 0042 job->exec(); 0043 0044 if (!job->error()) { 0045 const QJsonDocument json = QJsonDocument::fromJson(job->data()); 0046 if (!json.isNull()) { 0047 const QVariantMap map = json.toVariant().toMap(); 0048 const QVariantMap error = map[QLatin1String("error")].toMap(); 0049 if (!error.isEmpty()) { 0050 Choqok::NotifyManager::error(error[QLatin1String("message")].toString(), i18n("Goo.gl Error")); 0051 return url; 0052 } 0053 return map[ QLatin1String("id") ].toString(); 0054 } 0055 Choqok::NotifyManager::error(i18n("Malformed response"), i18n("Goo.gl Error")); 0056 } else { 0057 Choqok::NotifyManager::error(i18n("Cannot create a short URL.\n%1", job->errorString()), i18n("Goo.gl Error")); 0058 } 0059 return url; 0060 } 0061 0062 #include "goo_gl.moc" 0063 #include "moc_goo_gl.cpp"