File indexing completed on 2025-01-05 04:49:28
0001 /* 0002 SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "insertshorturlplugineditorinterface.h" 0008 #include "kmail_insertshorturl_debug.h" 0009 #include "shorturlengineplugin/shorturlengineinterface.h" 0010 #include "shorturlengineplugin/shorturlengineplugin.h" 0011 #include "shorturlengineplugin/shorturlenginepluginmanager.h" 0012 #include <KActionCollection> 0013 #include <KConfigGroup> 0014 #include <KLocalizedString> 0015 #include <KSharedConfig> 0016 #include <PimCommon/NetworkManager> 0017 #include <QAction> 0018 #include <TextCustomEditor/RichTextEditor> 0019 0020 InsertShorturlPluginEditorInterface::InsertShorturlPluginEditorInterface(QObject *parent) 0021 : MessageComposer::PluginEditorInterface(parent) 0022 { 0023 setNeedSelectedText(true); 0024 initializePlugins(); 0025 loadEngine(); 0026 } 0027 0028 InsertShorturlPluginEditorInterface::~InsertShorturlPluginEditorInterface() = default; 0029 0030 void InsertShorturlPluginEditorInterface::initializePlugins() 0031 { 0032 const QList<ShortUrlEnginePlugin *> lstPlugin = ShortUrlEnginePluginManager::self()->pluginsList(); 0033 for (ShortUrlEnginePlugin *plugin : lstPlugin) { 0034 ShortUrlEngineInterface *interface = plugin->createInterface(this); 0035 if (interface) { 0036 mLstInterface.insert(interface->engineName(), interface); 0037 } 0038 } 0039 } 0040 0041 void InsertShorturlPluginEditorInterface::loadEngine() 0042 { 0043 if (mCurrentEngine) { 0044 disconnect(mCurrentEngine, &ShortUrlEngineInterface::shortUrlFailed, this, &InsertShorturlPluginEditorInterface::slotShortUrlFailed); 0045 } 0046 0047 KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("ShortUrl")); 0048 const QString engineName = grp.readEntry("EngineName"); 0049 mCurrentEngine = mLstInterface.value(engineName); 0050 if (!mCurrentEngine && !mLstInterface.isEmpty()) { 0051 mCurrentEngine = mLstInterface.cbegin().value(); 0052 } 0053 0054 if (mCurrentEngine) { 0055 connect(mCurrentEngine, &ShortUrlEngineInterface::shortUrlFailed, this, &InsertShorturlPluginEditorInterface::slotShortUrlFailed); 0056 } 0057 } 0058 0059 void InsertShorturlPluginEditorInterface::createAction(KActionCollection *ac) 0060 { 0061 auto action = new QAction(i18n("Insert Short URL..."), this); 0062 ac->addAction(QStringLiteral("insert_shortulr"), action); 0063 connect(action, &QAction::triggered, this, &InsertShorturlPluginEditorInterface::slotActivated); 0064 MessageComposer::PluginActionType type(action, MessageComposer::PluginActionType::PopupMenu); 0065 setActionType(type); 0066 } 0067 0068 void InsertShorturlPluginEditorInterface::slotActivated() 0069 { 0070 Q_EMIT emitPluginActivated(this); 0071 } 0072 0073 void InsertShorturlPluginEditorInterface::exec() 0074 { 0075 QTextCursor textCursor = richTextEditor()->textCursor(); 0076 const QString urlStr = textCursor.selectedText(); 0077 0078 if (urlStr.startsWith(QLatin1StringView("http:")) || urlStr.startsWith(QLatin1StringView("https:")) || urlStr.startsWith(QLatin1StringView("ftp:")) 0079 || urlStr.startsWith(QLatin1StringView("ftps:"))) { 0080 if (!mCurrentEngine) { 0081 qCWarning(KMAIL_INSERTSHORTURL_LOG) << "Current Engine not defined"; 0082 return; 0083 } 0084 if (!PimCommon::NetworkManager::self()->isOnline()) { 0085 Q_EMIT message(i18n("No network connection detected, we cannot shorten URL.")); 0086 return; 0087 } 0088 mCurrentEngine->setTextCursor(textCursor); 0089 mCurrentEngine->setShortUrl(urlStr); 0090 mCurrentEngine->generateShortUrl(); 0091 } else { 0092 Q_EMIT message(i18n("String doesn't seem to be a URL")); 0093 } 0094 } 0095 0096 void InsertShorturlPluginEditorInterface::slotShortUrlFailed(const QString &errMsg) 0097 { 0098 Q_EMIT message(i18n("An error occurred: \"%1\"", errMsg)); 0099 } 0100 0101 #include "moc_insertshorturlplugineditorinterface.cpp"