Warning, file /plasma/kdeplasma-addons/applets/comic/engine/comicproviderwrapper.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 * SPDX-FileCopyrightText: 2008 Petri Damstén <damu@iki.fi> 0003 * SPDX-FileCopyrightText: 2010 Matthias Fuchs <mat69@gmx.net> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #ifndef COMICPROVIDERWRAPPER_H 0009 #define COMICPROVIDERWRAPPER_H 0010 0011 #include "comicprovider.h" 0012 #include "types.h" 0013 0014 #include <QBuffer> 0015 #include <QByteArray> 0016 #include <QImage> 0017 #include <QImageReader> 0018 #include <QJSValue> 0019 0020 namespace KPackage 0021 { 0022 class Package; 0023 } 0024 class ComicProviderKross; 0025 class QJSEngine; 0026 0027 class ImageWrapper : public QObject 0028 { 0029 Q_OBJECT 0030 Q_PROPERTY(QImage image READ image WRITE setImage) 0031 Q_PROPERTY(QByteArray rawData READ rawData WRITE setRawData) 0032 public: 0033 explicit ImageWrapper(QObject *parent = nullptr, const QByteArray &image = QByteArray()); 0034 0035 QImage image() const; 0036 /** 0037 * Sets the image, rawData is changed to the new set image 0038 */ 0039 void setImage(const QImage &image); 0040 QByteArray rawData() const; 0041 0042 /** 0043 * Sets the rawData, image is changed to the new rawData 0044 */ 0045 void setRawData(const QByteArray &rawData); 0046 0047 public Q_SLOTS: 0048 /** 0049 * Returns the numbers of images contained in the image 0050 * 0 if there is just one image, > 0 if the image format supports animation (the number of frames), 0051 * -1 if there was an error 0052 * @since 4600 0053 */ 0054 int imageCount() const; 0055 0056 /** 0057 * Returns an image, if it did not work an null image is returned 0058 * For animations returns the next frame upon each call, if there are no frames left returns a null image 0059 * @see imageCount() 0060 * @since 4600 0061 */ 0062 QImage read(); 0063 0064 private: 0065 void resetImageReader(); 0066 0067 private: 0068 QImage mImage; 0069 mutable QByteArray mRawData; 0070 QBuffer mBuffer; 0071 QImageReader mImageReader; 0072 }; 0073 0074 class DateWrapper 0075 { 0076 Q_GADGET 0077 Q_PROPERTY(QDate date READ date WRITE setDate) 0078 public: 0079 explicit DateWrapper(const QDate &date = QDate()); 0080 0081 QDate date() const; 0082 void setDate(const QDate &date); 0083 static QDate fromVariant(const QVariant &variant); 0084 0085 Q_INVOKABLE QString toString(const QString &format) const; 0086 Q_INVOKABLE QString toString(int format = 0) const; 0087 0088 public Q_SLOTS: 0089 DateWrapper addDays(int ndays); 0090 DateWrapper addMonths(int nmonths); 0091 DateWrapper addYears(int nyears); 0092 int day() const; 0093 int dayOfWeek() const; 0094 int dayOfYear() const; 0095 int daysInMonth() const; 0096 int daysInYear() const; 0097 int daysTo(const QVariant d) const; 0098 bool isNull() const; 0099 bool isValid() const; 0100 int month() const; 0101 bool setDate(int year, int month, int day); 0102 int toJulianDay() const; 0103 int weekNumber() const; 0104 int year() const; 0105 0106 private: 0107 QDate mDate; 0108 }; 0109 0110 class StaticDateWrapper : public QObject 0111 { 0112 Q_OBJECT 0113 public: 0114 explicit StaticDateWrapper(QObject *parent = nullptr); 0115 0116 public Q_SLOTS: 0117 DateWrapper currentDate(); 0118 DateWrapper fromJulianDay(int jd); 0119 DateWrapper fromString(const QString &string, int format = Qt::TextDate); 0120 DateWrapper fromString(const QString &string, const QString &format); 0121 bool isLeapYear(int year); 0122 bool isValid(int year, int month, int day); 0123 QString longDayName(int weekday); 0124 QString longMonthName(int month); 0125 QString shortDayName(int weekday); 0126 QString shortMonthName(int month); 0127 }; 0128 0129 class ComicProviderWrapper : public QObject 0130 { 0131 Q_OBJECT 0132 Q_PROPERTY(bool identifierSpecified READ identifierSpecified) 0133 Q_PROPERTY(QString textCodec READ textCodec WRITE setTextCodec) 0134 Q_PROPERTY(QString comicAuthor READ comicAuthor WRITE setComicAuthor) 0135 Q_PROPERTY(QString websiteUrl READ websiteUrl WRITE setWebsiteUrl) 0136 Q_PROPERTY(QString shopUrl READ shopUrl WRITE setShopUrl) 0137 Q_PROPERTY(QString title READ title WRITE setTitle) 0138 Q_PROPERTY(QString additionalText READ additionalText WRITE setAdditionalText) 0139 Q_PROPERTY(QJSValue identifier READ identifier WRITE setIdentifier) 0140 Q_PROPERTY(QJSValue nextIdentifier READ nextIdentifier WRITE setNextIdentifier) 0141 Q_PROPERTY(QJSValue previousIdentifier READ previousIdentifier WRITE setPreviousIdentifier) 0142 Q_PROPERTY(QJSValue firstIdentifier READ firstIdentifier WRITE setFirstIdentifier) 0143 Q_PROPERTY(QJSValue lastIdentifier READ lastIdentifier WRITE setLastIdentifier) 0144 Q_PROPERTY(bool isLeftToRight READ isLeftToRight WRITE setLeftToRight) 0145 Q_PROPERTY(bool isTopToBottom READ isTopToBottom WRITE setTopToBottom) 0146 Q_PROPERTY(int apiVersion READ apiVersion) 0147 public: 0148 enum PositionType { 0149 Left = 0, 0150 Top, 0151 Right, 0152 Bottom, 0153 }; 0154 Q_ENUM(PositionType) 0155 0156 enum RedirectedUrlType { 0157 PreviousUrl = 0, 0158 CurrentUrl = 1, 0159 NextUrl = 2, 0160 FirstUrl = 3, 0161 LastUrl = 4, 0162 UserUrl = 10, 0163 }; 0164 Q_ENUM(RedirectedUrlType) 0165 0166 explicit ComicProviderWrapper(ComicProviderKross *parent); 0167 ~ComicProviderWrapper() override; 0168 0169 int apiVersion() const 0170 { 0171 return 4600; 0172 } 0173 0174 Q_INVOKABLE void print(const QJSValue &str) 0175 { 0176 qWarning() << str.toString(); 0177 } 0178 0179 IdentifierType identifierType() const; 0180 QImage comicImage(); 0181 void pageRetrieved(int id, const QByteArray &data); 0182 void pageError(int id, const QString &message); 0183 void redirected(int id, const QUrl &newUrl); 0184 0185 bool identifierSpecified() const; 0186 QString textCodec() const; 0187 void setTextCodec(const QString &textCodec); 0188 QString comicAuthor() const; 0189 void setComicAuthor(const QString &author); 0190 QString websiteUrl() const; 0191 void setWebsiteUrl(const QString &websiteUrl); 0192 QString shopUrl() const; 0193 void setShopUrl(const QString &shopUrl); 0194 QString title() const; 0195 void setTitle(const QString &title); 0196 QString additionalText() const; 0197 void setAdditionalText(const QString &additionalText); 0198 QJSValue identifier(); 0199 void setIdentifier(const QJSValue &identifier); 0200 QJSValue nextIdentifier(); 0201 void setNextIdentifier(const QJSValue &nextIdentifier); 0202 QJSValue previousIdentifier(); 0203 void setPreviousIdentifier(const QJSValue &previousIdentifier); 0204 QJSValue firstIdentifier(); 0205 void setFirstIdentifier(const QJSValue &firstIdentifier); 0206 QJSValue lastIdentifier(); 0207 void setLastIdentifier(const QJSValue &lastIdentifier); 0208 bool isLeftToRight() const; 0209 void setLeftToRight(bool ltr); 0210 bool isTopToBottom() const; 0211 void setTopToBottom(bool ttb); 0212 0213 QVariant identifierVariant() const; 0214 QVariant firstIdentifierVariant() const; 0215 QVariant lastIdentifierVariant() const; 0216 QVariant nextIdentifierVariant() const; 0217 QVariant previousIdentifierVariant() const; 0218 0219 public Q_SLOTS: 0220 void finished() const; 0221 void error() const; 0222 0223 void requestPage(const QString &url, int id, const QVariantMap &infos = QVariantMap()); 0224 void requestRedirectedUrl(const QString &url, int id, const QVariantMap &infos = QVariantMap()); 0225 void combine(const QVariant &image, PositionType position = Top); 0226 QObject *image(); 0227 0228 void init(); 0229 0230 protected: 0231 QVariant callFunction(const QString &name, const QJSValueList &args = {}); 0232 bool functionCalled() const; 0233 QJSValue identifierToScript(const QVariant &identifier); 0234 QVariant identifierFromScript(const QJSValue &identifier) const; 0235 void setIdentifierToDefault(); 0236 void checkIdentifier(QVariant *identifier); 0237 0238 private: 0239 QJSEngine *m_engine = nullptr; 0240 ComicProviderKross *mProvider; 0241 QStringList mFunctions; 0242 bool mFuncFound; 0243 ImageWrapper *mKrossImage; 0244 static QStringList mExtensions; 0245 KPackage::Package *mPackage; 0246 0247 QByteArray mTextCodec; 0248 QString mWebsiteUrl; 0249 QString mShopUrl; 0250 QString mTitle; 0251 QString mAdditionalText; 0252 QVariant mIdentifier; 0253 QVariant mNextIdentifier; 0254 QVariant mPreviousIdentifier; 0255 QVariant mFirstIdentifier; 0256 QVariant mLastIdentifier; 0257 int mRequests; 0258 bool mIdentifierSpecified; 0259 bool mIsLeftToRight; 0260 bool mIsTopToBottom; 0261 }; 0262 0263 #endif