File indexing completed on 2024-04-28 08:50:19

0001 /* This file is part of the KDE project
0002 
0003     SPDX-FileCopyrightText: 2004 Gary Cramblitt <garycramblitt@comcast.net>
0004 
0005     Adapted from kdeutils/ark/konqplugin by:
0006     SPDX-FileCopyrightText: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "akregatorplugin.h"
0012 
0013 #include "pluginutil.h"
0014 #include "akregatorplugindebug.h"
0015 
0016 #include <klocalizedstring.h>
0017 #include <kpluginfactory.h>
0018 #include <kfileitem.h>
0019 #include <kfileitemlistproperties.h>
0020 
0021 #include <QDebug>
0022 #include <QAction>
0023 
0024 using namespace Akregator;
0025 
0026 K_PLUGIN_FACTORY_WITH_JSON(AkregatorMenuFactory, "akregator_konqplugin.json", registerPlugin<AkregatorMenu>();)
0027 
0028 AkregatorMenu::AkregatorMenu(QObject *parent, const QVariantList &args)
0029   : KAbstractFileItemActionPlugin(parent)
0030 {
0031     Q_UNUSED(args);
0032 }
0033 
0034 
0035 QList<QAction *> AkregatorMenu::actions(const KFileItemListProperties &fileItemInfos, QWidget *parent)
0036 {
0037     Q_UNUSED(parent);
0038 
0039     QList<QAction *> acts;
0040     const KFileItemList items = fileItemInfos.items();
0041     for (const KFileItem &item : items ) {
0042         if (isFeedUrl(item)) {
0043             qCDebug(AKREGATORPLUGIN_LOG) << "found feed" << item.url();
0044 
0045             QAction *action = new QAction(this);
0046             action->setText(i18nc("@action:inmenu", "Add Feed to Akregator"));
0047             action->setIcon(QIcon::fromTheme("akregator"));
0048             QString url = item.url().url();
0049             connect(action, &QAction::triggered, this, [url, this](){addFeed(url);});
0050             acts.append(action);
0051         }
0052     }
0053 
0054     return acts;
0055 }
0056 
0057 static bool isFeedUrl(const QString &urlPath)
0058 {
0059     // If URL ends in .htm or .html, it is not a feed url.
0060     if (urlPath.endsWith(".htm", Qt::CaseInsensitive) || urlPath.endsWith(".html", Qt::CaseInsensitive)) {
0061         return false;
0062     }
0063     if (urlPath.contains("rss", Qt::CaseInsensitive)) {
0064         return true;
0065     }
0066     if (urlPath.contains("rdf", Qt::CaseInsensitive)) {
0067         return true;
0068     }
0069     return false;
0070 }
0071 
0072 bool AkregatorMenu::isFeedUrl(const KFileItem &item) const
0073 {
0074     if (m_feedMimeTypes.contains(item.mimetype())) {
0075         return true;
0076     } else {
0077         return ::isFeedUrl(item.url().path());
0078     }
0079     return false;
0080 }
0081 
0082 void Akregator::AkregatorMenu::addFeed(const QString& url)
0083 {
0084     PluginUtil::addFeeds({url});
0085 }
0086 
0087 
0088 #include "akregatorplugin.moc"