File indexing completed on 2024-04-21 04:57:56
0001 /* 0002 This file is part of Akregator. 0003 0004 SPDX-FileCopyrightText: 2004 Teemu Rytilahti <tpr@d5k.net> 0005 0006 SPDX-License-Identifier: GPL-2.0-or-later WITH LicenseRef-Qt-exception 0007 */ 0008 0009 #include "pluginutil.h" 0010 0011 #include <kprocess.h> 0012 #include <KLocalizedString> 0013 #include <KMessageBox> 0014 0015 #include "akregatorplugindebug.h" 0016 0017 #include <QStringList> 0018 #include <QUrl> 0019 #include <QDBusConnection> 0020 #include <QDBusConnectionInterface> 0021 #include <QDBusInterface> 0022 #include <QDBusReply> 0023 0024 using namespace Akregator; 0025 0026 static bool isAkregatorRunning() 0027 { 0028 //Laurent if akregator is registered into dbus so akregator is running 0029 return QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.akregator")); 0030 } 0031 0032 static void addFeedsViaDBUS(const QStringList &urls) 0033 { 0034 QDBusInterface akregator(QStringLiteral("org.kde.akregator"), QStringLiteral("/Akregator"), QStringLiteral("org.kde.akregator.part")); 0035 QDBusReply<void> reply = akregator.call(QStringLiteral("addFeedsToGroup"), urls, i18n("Imported Feeds")); 0036 if (!reply.isValid()) { 0037 KMessageBox::error(nullptr, i18n("Unable to contact Akregator via D-Bus"), 0038 i18nc("@title:window", "D-Bus call failed")); 0039 } 0040 } 0041 0042 static void addFeedsViaCmdLine(const QStringList &urls) 0043 { 0044 KProcess proc; 0045 proc << QStringLiteral("akregator") << QStringLiteral("-g") << i18n("Imported Feeds"); 0046 for (const QString &url: urls) { 0047 proc << QStringLiteral("-a") << url; 0048 } 0049 proc.startDetached(); 0050 } 0051 0052 void PluginUtil::addFeeds(const QStringList &urls) 0053 { 0054 if (isAkregatorRunning()) { 0055 qCDebug(AKREGATORPLUGIN_LOG) << "adding" << urls.count() << "feeds via DBus"; 0056 addFeedsViaDBUS(urls); 0057 } else { 0058 qCDebug(AKREGATORPLUGIN_LOG) << "adding" << urls.count() << "feeds via command line"; 0059 addFeedsViaCmdLine(urls); 0060 } 0061 } 0062 0063 // handle all the wild stuff that QUrl doesn't handle 0064 QString PluginUtil::fixRelativeURL(const QString &s, const QUrl &baseurl) 0065 { 0066 QString s2 = s; 0067 QUrl u; 0068 if (QUrl(s2).isRelative()) { 0069 if (s2.startsWith(QLatin1String("//"))) { 0070 s2.prepend(baseurl.scheme() + ':'); 0071 u.setUrl(s2); 0072 } else if (s2.startsWith(QLatin1String("/"))) { 0073 // delete path/query/fragment, so that only protocol://host remains 0074 QUrl b2(baseurl.adjusted(QUrl::RemovePath|QUrl::RemoveQuery|QUrl::RemoveFragment)); 0075 u = b2.resolved(QUrl(s2.mid(1))); // remove leading "/" 0076 } else { 0077 u = baseurl.resolved(QUrl(s2)); 0078 } 0079 } else { 0080 u.setUrl(s2); 0081 } 0082 0083 u = u.adjusted(QUrl::NormalizePathSegments); 0084 //qCDebug(AKREGATORPLUGIN_LOG) << "url=" << s << " baseurl=" << baseurl << "fixed=" << u; 0085 return u.url(); 0086 }