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

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2008-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #include "imagepreview.h"
0010 
0011 #include <QTimer>
0012 
0013 #include <KPluginFactory>
0014 
0015 #include "choqokuiglobal.h"
0016 #include "mediamanager.h"
0017 #include "postwidget.h"
0018 #include "textbrowser.h"
0019 
0020 K_PLUGIN_CLASS_WITH_JSON(ImagePreview, "choqok_imagepreview.json")
0021 
0022 const QRegExp ImagePreview::mImgLyRegExp(QLatin1String("(http://img.ly/[^\\s<>\"]+[^!,\\.\\s<>'\"\\]])"));
0023 const QRegExp ImagePreview::mTwitgooRegExp(QLatin1String("(http://(([a-zA-Z0-9]+\\.)?)twitgoo.com/[^\\s<>\"]+[^!,\\.\\s<>'\"\\]])"));
0024 const QRegExp ImagePreview::mPumpIORegExp(QLatin1String("(https://([a-zA-Z0-9]+\\.)?[a-zA-Z0-9]+\\.[a-zA-Z]+/uploads/\\w+/\\d{4}/\\d{1,2}/\\d{1,2}/\\w+)(\\.[a-zA-Z]{3,4})"));
0025 
0026 ImagePreview::ImagePreview(QObject *parent, const QList< QVariant > &)
0027     : Choqok::Plugin(QLatin1String("choqok_imagepreview"), parent), state(Stopped)
0028 {
0029     connect(Choqok::UI::Global::SessionManager::self(), &Choqok::UI::Global::SessionManager::newPostWidgetAdded,
0030             this, &ImagePreview::slotAddNewPostWidget);
0031 }
0032 
0033 ImagePreview::~ImagePreview()
0034 {
0035 
0036 }
0037 
0038 void ImagePreview::slotAddNewPostWidget(Choqok::UI::PostWidget *newWidget)
0039 {
0040     postsQueue.enqueue(newWidget);
0041     if (state == Stopped) {
0042         state = Running;
0043         QTimer::singleShot(1000, this, SLOT(startParsing()));
0044     }
0045 }
0046 
0047 void ImagePreview::startParsing()
0048 {
0049     int i = 8;
0050     while (!postsQueue.isEmpty() && i > 0) {
0051         parse(postsQueue.dequeue());
0052         --i;
0053     }
0054 
0055     if (postsQueue.isEmpty()) {
0056         state = Stopped;
0057     } else {
0058         QTimer::singleShot(500, this, SLOT(startParsing()));
0059     }
0060 }
0061 
0062 void ImagePreview::parse(Choqok::UI::PostWidget *postToParse)
0063 {
0064     if (!postToParse) {
0065         return;
0066     }
0067     int pos = 0;
0068     QStringList ImgLyRedirectList;
0069     QStringList TwitgooRedirectList;
0070     QStringList PumpIORedirectList;
0071     QString content = postToParse->currentPost()->content;
0072 
0073     //Img.ly; http://img.ly/api/docs
0074     pos = 0;
0075     while ((pos = mImgLyRegExp.indexIn(content, pos)) != -1) {
0076         pos += mImgLyRegExp.matchedLength();
0077         ImgLyRedirectList << mImgLyRegExp.cap(0);
0078     }
0079     for (const QString &url: ImgLyRedirectList) {
0080         connect(Choqok::MediaManager::self(),&Choqok::MediaManager::imageFetched,
0081                 this, &ImagePreview::slotImageFetched);
0082         QUrl ImgLyUrl = QUrl::fromUserInput(QStringLiteral("http://img.ly/show/thumb%1").arg(QString(url).remove(QLatin1String("http://img.ly"))));
0083         mParsingList.insert(ImgLyUrl, postToParse);
0084         mBaseUrlMap.insert(ImgLyUrl, url);
0085         Choqok::MediaManager::self()->fetchImage(ImgLyUrl, Choqok::MediaManager::Async);
0086     }
0087 
0088     //Twitgoo; http://twitgoo.com/docs/TwitgooHelp.htm
0089     pos = 0;
0090     while ((pos = mTwitgooRegExp.indexIn(content, pos)) != -1) {
0091         pos += mTwitgooRegExp.matchedLength();
0092         TwitgooRedirectList << mTwitgooRegExp.cap(0);
0093     }
0094     for (const QString &url: TwitgooRedirectList) {
0095         connect(Choqok::MediaManager::self(), &Choqok::MediaManager::imageFetched,
0096                 this, &ImagePreview::slotImageFetched);
0097         QUrl TwitgooUrl = QUrl::fromUserInput(url + QLatin1String("/thumb"));
0098         mParsingList.insert(TwitgooUrl, postToParse);
0099         mBaseUrlMap.insert(TwitgooUrl, url);
0100         Choqok::MediaManager::self()->fetchImage(TwitgooUrl, Choqok::MediaManager::Async);
0101     }
0102 
0103     //PumpIO
0104     pos = 0;
0105     QString baseUrl;
0106     QString imageExtension;
0107     while ((pos = mPumpIORegExp.indexIn(content, pos)) != -1) {
0108         pos += mPumpIORegExp.matchedLength();
0109         PumpIORedirectList << mPumpIORegExp.cap(0);
0110         baseUrl = mPumpIORegExp.cap(1);
0111         imageExtension = mPumpIORegExp.cap(mPumpIORegExp.capturedTexts().length() - 1);
0112     }
0113     for (const QString &url: PumpIORedirectList) {
0114         connect(Choqok::MediaManager::self(), &Choqok::MediaManager::imageFetched,
0115                 this, &ImagePreview::slotImageFetched);
0116         const QUrl pumpIOUrl = QUrl::fromUserInput(baseUrl + QLatin1String("_thumb") + imageExtension);
0117         mParsingList.insert(pumpIOUrl, postToParse);
0118         mBaseUrlMap.insert(pumpIOUrl, url);
0119         Choqok::MediaManager::self()->fetchImage(pumpIOUrl, Choqok::MediaManager::Async);
0120     }
0121 }
0122 
0123 void ImagePreview::slotImageFetched(const QUrl &remoteUrl, const QPixmap &pixmap)
0124 {
0125     Choqok::UI::PostWidget *postToParse = mParsingList.take(remoteUrl);
0126     QString baseUrl = mBaseUrlMap.take(remoteUrl);
0127     if (!postToParse) {
0128         return;
0129     }
0130     QString content = postToParse->content();
0131     QUrl imgU(remoteUrl);
0132     imgU.setScheme(QLatin1String("img"));
0133 //     imgUrl.replace("http://","img://");
0134     QPixmap pix = pixmap;
0135     if (pixmap.width() > 200) {
0136         pix = pixmap.scaledToWidth(200);
0137     } else if (pixmap.height() > 200) {
0138         pix = pixmap.scaledToHeight(200);
0139     }
0140     postToParse->mainWidget()->document()->addResource(QTextDocument::ImageResource, imgU, pix);
0141     content.replace(QRegExp(QLatin1Char('>') + baseUrl + QLatin1Char('<')), QStringLiteral("><img align='left' src='")
0142                     + imgU.toDisplayString() + QStringLiteral("' /><"));
0143     postToParse->setContent(content);
0144 }
0145 
0146 #include "imagepreview.moc"
0147 #include "moc_imagepreview.cpp"