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 "tinyarro_ws.h"
0010 
0011 #include <KIO/StoredTransferJob>
0012 #include <KLocalizedString>
0013 #include <KPluginFactory>
0014 
0015 #include "notifymanager.h"
0016 
0017 #include "tinyarro_ws_settings.h"
0018 
0019 K_PLUGIN_CLASS_WITH_JSON(Tinyarro_ws, "choqok_tinyarro_ws.json")
0020 
0021 Tinyarro_ws::Tinyarro_ws(QObject *parent, const QVariantList &)
0022     : Choqok::Shortener(QLatin1String("choqok_tinyarro_ws"), parent)
0023 {
0024 }
0025 
0026 Tinyarro_ws::~Tinyarro_ws()
0027 {
0028 }
0029 
0030 QString Tinyarro_ws::shorten(const QString &url)
0031 {
0032     QUrl reqUrl(QLatin1String("http://tinyarro.ws/api-create.php"));
0033     QUrlQuery reqQuery;
0034 
0035     Tinyarro_ws_Settings::self()->load();
0036 
0037     if (!Tinyarro_ws_Settings::tinyarro_ws_host_punny().isEmpty() ||
0038             Tinyarro_ws_Settings::tinyarro_ws_host_punny() != QLatin1String("Random")) {
0039         reqQuery.addQueryItem(QLatin1String("host"), Tinyarro_ws_Settings::tinyarro_ws_host_punny());
0040     }
0041     reqQuery.addQueryItem(QLatin1String("utfpure"), QLatin1String("1"));
0042     reqQuery.addQueryItem(QLatin1String("url"), QUrl(url).url());
0043     reqUrl.setQuery(reqQuery);
0044 
0045     KIO::StoredTransferJob *job = KIO::storedGet(reqUrl, KIO::Reload, KIO::HideProgressInfo);
0046     job->exec();
0047 
0048     if (!job->error()) {
0049         QString output = QString::fromUtf8(job->data());
0050 
0051         if (!output.isEmpty()) {
0052             if (output.startsWith(QLatin1String("http://"))) {
0053                 return output;
0054             }
0055         }
0056         Choqok::NotifyManager::error(output, i18n("Tinyarro.ws error"));
0057     } else {
0058         Choqok::NotifyManager::error(i18n("Cannot create a short URL.\n%1", job->errorString()));
0059     }
0060     return url;
0061 }
0062 
0063 #include "moc_tinyarro_ws.cpp"
0064 #include "tinyarro_ws.moc"