File indexing completed on 2024-03-24 04:58:21
0001 /* 0002 This file is part of Choqok, the KDE micro-blogging client 0003 0004 SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@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 "shortenmanager.h" 0010 0011 #include <QApplication> 0012 0013 #include <KSharedConfig> 0014 0015 #include "choqokbehaviorsettings.h" 0016 #include "libchoqokdebug.h" 0017 #include "pluginmanager.h" 0018 #include "notifymanager.h" 0019 0020 namespace Choqok 0021 { 0022 0023 class ShortenManagerPrivate 0024 { 0025 public: 0026 Shortener *backend; 0027 ShortenManager instance; 0028 QRegExp findUrlRegExp; 0029 QRegExp removeUrlRegExp; 0030 0031 ShortenManagerPrivate() 0032 : backend(nullptr) 0033 { 0034 findUrlRegExp.setPattern(QLatin1String("(ftps?|https?)://")); 0035 removeUrlRegExp.setPattern(QLatin1String("^(https?)://")); 0036 reloadConfig(); 0037 } 0038 void reloadConfig() 0039 { 0040 const QString pluginId = Choqok::BehaviorSettings::shortenerPlugin(); 0041 if (backend) { 0042 if (backend->pluginId() == pluginId) { 0043 return;//Already loaded 0044 } else { 0045 qCDebug(CHOQOK) << backend->pluginId(); 0046 PluginManager::self()->unloadPlugin(backend->pluginId()); 0047 backend = nullptr; 0048 } 0049 } 0050 if (pluginId.isEmpty()) { 0051 return; 0052 } 0053 Plugin *plugin = PluginManager::self()->loadPlugin(pluginId); 0054 backend = qobject_cast<Shortener *>(plugin); 0055 if (!backend) { 0056 qCDebug(CHOQOK) << "Could not load a Shortener plugin. Shortening Disabled"; 0057 } 0058 } 0059 }; 0060 0061 Q_GLOBAL_STATIC(ShortenManagerPrivate, _smp) 0062 0063 QString shorten(const QString &url) 0064 { 0065 return _smp->backend->shorten(url); 0066 } 0067 0068 ShortenManager::ShortenManager(QObject *parent) 0069 : QObject(parent) 0070 { 0071 } 0072 0073 ShortenManager::~ShortenManager() 0074 {} 0075 0076 ShortenManager *ShortenManager::self() 0077 { 0078 return &_smp->instance; 0079 } 0080 0081 QString ShortenManager::shortenUrl(const QString &url) 0082 { 0083 if (_smp->backend) { 0084 qCDebug(CHOQOK) << "Shortening:" << url; 0085 NotifyManager::shortening(url); 0086 QString shortUrl = shorten(url); 0087 if (BehaviorSettings::removeHttp() && url != shortUrl) { 0088 shortUrl.remove(_smp->removeUrlRegExp); 0089 } 0090 return shortUrl; 0091 } else { 0092 qCDebug(CHOQOK) << "There isn't any Shortener plugin."; 0093 return url; 0094 } 0095 } 0096 0097 void ShortenManager::reloadConfig() 0098 { 0099 _smp->reloadConfig(); 0100 } 0101 0102 QString ShortenManager::parseText(const QString &text) 0103 { 0104 qCDebug(CHOQOK); 0105 QString t; 0106 int i = 0, j = 0; 0107 while ((j = text.indexOf(_smp->findUrlRegExp, i)) != -1) { 0108 t += text.mid(i, j - i); 0109 int k = text.indexOf(QLatin1Char(' '), j); 0110 if (k == -1) { 0111 k = text.length(); 0112 } 0113 QString baseUrl = text.mid(j, k - j); 0114 if (baseUrl.count() > 30) { 0115 QString tmp = Choqok::ShortenManager::self()->shortenUrl(baseUrl); 0116 if (BehaviorSettings::removeHttp() && tmp != baseUrl) { 0117 tmp.remove(_smp->removeUrlRegExp); 0118 } 0119 t += tmp; 0120 } else { 0121 t += baseUrl; 0122 } 0123 i = k; 0124 } 0125 t += text.mid(i); 0126 return t; 0127 } 0128 0129 void ShortenManager::emitNewUnshortenedUrl(Choqok::UI::PostWidget *widget, const QUrl &fromUrl, const QUrl &toUrl) 0130 { 0131 Q_EMIT newUnshortenedUrl(widget, fromUrl, toUrl); 0132 } 0133 0134 } 0135 0136 #include "moc_shortenmanager.cpp"