File indexing completed on 2024-04-28 04:55:51

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2014 Eugene Shalygin <eugene.shalygin@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #ifndef CHOQOK_LONGURL_H
0010 #define CHOQOK_LONGURL_H
0011 
0012 #include <QPointer>
0013 #include <QQueue>
0014 #include <QSharedPointer>
0015 #include <QUrlQuery>
0016 
0017 #include <KIO/Job>
0018 
0019 #include "plugin.h"
0020 
0021 class QUrl;
0022 
0023 namespace Choqok
0024 {
0025 namespace UI
0026 {
0027 class PostWidget;
0028 }
0029 }
0030 
0031 class LongUrl : public Choqok::Plugin
0032 {
0033     typedef Choqok::Plugin base;
0034     Q_OBJECT
0035 public:
0036     LongUrl(QObject *parent, const QList< QVariant > &args);
0037     ~LongUrl();
0038 
0039 protected Q_SLOTS:
0040     void slotAddNewPostWidget(Choqok::UI::PostWidget *newWidget);
0041     void startParsing();
0042     void dataReceived(KIO::Job *job, QByteArray data);
0043     void jobResult(KJob *job);
0044     virtual void aboutToUnload() override;
0045     void servicesDataReceived(KIO::Job *job, QByteArray data);
0046     void servicesJobResult(KJob *job);
0047 private:
0048     enum ParserState { Running = 0, Stopped };
0049     ParserState state;
0050 
0051     typedef QPointer<Choqok::UI::PostWidget> PostWidgetPointer;
0052 
0053     void sheduleSupportedServicesFetch();
0054     bool isServiceSupported(const QString &host);
0055     void processJobResults(KJob *job);
0056 
0057     void parse(PostWidgetPointer postToParse);
0058     KJob *sheduleParsing(const QString &shortUrl);
0059     void suspendJobs();
0060 
0061     void replaceUrl(PostWidgetPointer post, const QUrl &fromUrl, const QUrl &toUrl);
0062 
0063     PostWidgetPointer takeJob(KJob *job)
0064     {
0065         return mParsingList.take(job);
0066     }
0067 
0068     void insertJob(KJob *job, PostWidgetPointer post)
0069     {
0070         mParsingList.insert(job, post);
0071     }
0072 
0073     QQueue< PostWidgetPointer > postsQueue;
0074     QMap<KJob *, PostWidgetPointer > mParsingList;
0075     QStringList supportedServices;
0076     typedef QMap<KJob *, QByteArray> DataMap;
0077     DataMap mData;
0078     typedef QMap<KJob *, QString> UrlsMap;
0079     UrlsMap mShortUrls;
0080     QSharedPointer<QByteArray> mServicesData;
0081     bool mServicesAreFetched;
0082 };
0083 
0084 #endif //CHOQOK_LONGURL_H