File indexing completed on 2024-04-21 15:07:31

0001 /*
0002     This file is part of LibSyndication.
0003     SPDX-FileCopyrightText: 2005 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0006 */
0007 
0008 #include "testloader.h"
0009 
0010 #include "atom/parser.h"
0011 #include "dataretriever.h"
0012 #include "feed.h"
0013 #include "rdf/parser.h"
0014 #include "rss2/parser.h"
0015 #include "specificdocument.h"
0016 #include "syndication_version.h"
0017 
0018 #include <QCoreApplication>
0019 
0020 #include <QUrl>
0021 
0022 #include <QCommandLineOption>
0023 #include <QCommandLineParser>
0024 #include <QNetworkAccessManager>
0025 #include <QNetworkReply>
0026 #include <QNetworkRequest>
0027 #include <QString>
0028 
0029 #include <cstdlib>
0030 #include <iostream>
0031 
0032 using namespace Syndication;
0033 
0034 class SimpleRetriever : public Syndication::DataRetriever
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit SimpleRetriever()
0039         : mMgr(new QNetworkAccessManager)
0040         , mReply(nullptr)
0041     {
0042     }
0043     ~SimpleRetriever() override
0044     {
0045         delete mReply;
0046         delete mMgr;
0047     }
0048 
0049     void retrieveData(const QUrl &url) override
0050     {
0051         mReply = mMgr->get(QNetworkRequest(url));
0052         connect(mReply, &QNetworkReply::finished, this, [this]() {
0053             if (mReply->error() == QNetworkReply::NoError) {
0054                 Q_EMIT dataRetrieved(mReply->readAll(), true);
0055             } else {
0056                 Q_EMIT dataRetrieved({}, false);
0057             }
0058         });
0059     }
0060     void abort() override
0061     {
0062         if (mReply) {
0063             mReply->abort();
0064         }
0065     }
0066     int errorCode() const override
0067     {
0068         if (mReply) {
0069             return mReply->error();
0070         } else {
0071             return 0;
0072         }
0073     }
0074 
0075 private:
0076     QNetworkAccessManager *mMgr;
0077     QNetworkReply *mReply;
0078 };
0079 
0080 TestLibSyndication::TestLibSyndication(const QString &url)
0081 {
0082     QUrl kurl = QUrl::fromUserInput(url);
0083 
0084     std::cerr << kurl.url().toLocal8Bit().data() << std::endl;
0085     Loader *loader = Loader::create(this, SLOT(slotLoadingComplete(Syndication::Loader *, Syndication::FeedPtr, Syndication::ErrorCode)));
0086     loader->loadFrom(kurl, new SimpleRetriever());
0087 }
0088 
0089 void TestLibSyndication::slotLoadingComplete(Syndication::Loader *loader, Syndication::FeedPtr feed, Syndication::ErrorCode error)
0090 {
0091     Q_UNUSED(loader)
0092     Q_UNUSED(error)
0093 
0094     if (feed) {
0095         std::cout << feed->debugInfo().toUtf8().data() << std::endl;
0096         exit(0);
0097     } else {
0098         std::cerr << "error" << std::endl;
0099         exit(1);
0100     }
0101 }
0102 
0103 int main(int argc, char **argv)
0104 {
0105     if (argc < 2) {
0106         std::cerr << "filename expected" << std::endl;
0107         return 1;
0108     }
0109 
0110     QCoreApplication app(argc, argv);
0111 
0112     QCommandLineParser parser;
0113     app.setApplicationVersion(QStringLiteral(SYNDICATION_VERSION_STRING));
0114     parser.addVersionOption();
0115     parser.addHelpOption();
0116     parser.addOption(QCommandLineOption(QStringList() << QStringLiteral("+url"), QStringLiteral("URL of feed")));
0117     parser.process(app);
0118 
0119     if (parser.positionalArguments().count() != 1) {
0120         parser.showHelp();
0121     }
0122 
0123     TestLibSyndication *tester = new TestLibSyndication(parser.positionalArguments().at(0));
0124     Q_UNUSED(tester)
0125 
0126     return app.exec();
0127 }
0128 
0129 #include "moc_testloader.cpp"
0130 #include "testloader.moc"