diff --git a/src/web/UBOEmbedParser.cpp b/src/web/UBOEmbedParser.cpp new file mode 100644 index 00000000..f4247bd0 --- /dev/null +++ b/src/web/UBOEmbedParser.cpp @@ -0,0 +1,258 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "UBOEmbedParser.h" + +UBOEmbedParser::UBOEmbedParser(QObject *parent, const char* name) +{ + setObjectName(name); + mParsedTitles.clear(); + connect(this, SIGNAL(parseContent(QString)), this, SLOT(onParseContent(QString))); +} + +UBOEmbedParser::~UBOEmbedParser() +{ + +} + +void UBOEmbedParser::setNetworkAccessManager(QNetworkAccessManager *nam) +{ + mpNam = nam; + connect(mpNam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*))); +} + +void UBOEmbedParser::parse(const QString& html) +{ + mContents.clear(); + QString query = "]*)>"; + QRegExp exp(query); + QStringList results; + int count = 0; + int pos = 0; + while ((pos = exp.indexIn(html, pos)) != -1) { + ++count; + pos += exp.matchedLength(); + QStringList res = exp.capturedTexts(); + if("" != res.at(1)){ + results << res.at(1); + } + } + + QVector oembedUrls; + + if(2 <= results.size()){ + for(int i=1; i").arg(results.at(i)); + QDomDocument domDoc; + domDoc.setContent(qsNode); + QDomNode linkNode = domDoc.documentElement(); + + // At this point, we have a node that is the element. Now we have to parse its attributes + // in order to check if it is a oEmbed node or not + QDomAttr typeAttribute = linkNode.toElement().attributeNode("type"); + if(typeAttribute.value().contains("oembed")){ + // The node is an oembed one! We have to get the url and the type of oembed encoding + QDomAttr hrefAttribute = linkNode.toElement().attributeNode("href"); + QString url = hrefAttribute.value(); + oembedUrls.append(url); + } + } + } + } + + mPending = oembedUrls.size(); + + if(0 == mPending){ + emit oembedParsed(mContents); + }else{ + // Here we start the parsing (finally...)! + for(int i=0; i")){ + // XML ! + crntContent = getXMLInfos(receivedDatas); + }else if(receivedDatas.contains("{\"provider_url")){ + // JSON ! + crntContent = getJSONInfos(receivedDatas); + } + + // As we don't want duplicates, we have to check if the content title has already + // been parsed. + if("" != crntContent.title && !mParsedTitles.contains(crntContent.title)){ + mParsedTitles << crntContent.title; + mContents << crntContent; + } + + }else{ + // We decided to not handle the error case here. If there is a problem with + // getting the oembed content information, we just don't handle it: the content + // will not be available for importation. + } + + // Decrement the number of content to analyze + mPending--; + if(0 == mPending){ + // All the oembed contents have been parsed. We notify it! + emit oembedParsed(mContents); + } +} diff --git a/src/web/UBOEmbedParser.h b/src/web/UBOEmbedParser.h new file mode 100644 index 00000000..2c0c0549 --- /dev/null +++ b/src/web/UBOEmbedParser.h @@ -0,0 +1,93 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UBOEMBEDPARSER_H +#define UBOEMBEDPARSER_H + +#include +#include +#include +#include +#include +#include +#include +#include + +/********************************************************************************** + ---------------------------------------------------------------- + Here is an example of an embed content in an XML representation + ---------------------------------------------------------------- + + http://www.youtube.com/ + EPISODE 36 Traditional Mediums + + + + FZDSCHOOL + 270 + 480 + 480 + 1.0 + http://www.youtube.com/user/FZDSCHOOL + YouTube + http://i4.ytimg.com/vi/C3lApsNmdwM/hqdefault.jpg + video + 360 + +***********************************************************************************/ +typedef struct{ + QString providerUrl; + QString title; + QString author; + int height; + int width; + int thumbWidth; + float version; + QString authorUrl; + QString providerName; + QString thumbUrl; + QString type; + QString thumbHeight; + QString html; + QString url; +}sOEmbedContent; + +class UBOEmbedParser : public QObject +{ + Q_OBJECT +public: + UBOEmbedParser(QObject* parent=0, const char* name="UBOEmbedParser"); + ~UBOEmbedParser(); + void parse(const QString& html); + void setNetworkAccessManager(QNetworkAccessManager* nam); + +signals: + void parseContent(QString url); + void oembedParsed(QVector contents); + +private slots: + void onFinished(QNetworkReply* reply); + void onParseContent(QString url); + +private: + sOEmbedContent getJSONInfos(const QString& json); + sOEmbedContent getXMLInfos(const QString& xml); + QVector mContents; + QVector mParsedTitles; + QNetworkAccessManager* mpNam; + int mPending; +}; + +#endif // UBOEMBEDPARSER_H diff --git a/src/web/UBWebController.cpp b/src/web/UBWebController.cpp index 08c5e8ab..1cacd63b 100644 --- a/src/web/UBWebController.cpp +++ b/src/web/UBWebController.cpp @@ -14,6 +14,8 @@ */ #include +#include +#include #include "frameworks/UBPlatformUtils.h" @@ -60,7 +62,6 @@ UBWebController::UBWebController(UBMainWindow* mainWindow) // , mKeyboardCurrentPalette(0) , mToolsPalettePositionned(false) , mDownloadViewIsVisible(false) - { connect(mMainWindow->actionWebTools, SIGNAL(toggled(bool)), this, SLOT(toggleWebToolsPalette(bool))); @@ -72,11 +73,13 @@ UBWebController::UBWebController(UBMainWindow* mainWindow) mToolsPalettePositionnedList[i] = false; } + connect(&mOEmbedParser, SIGNAL(oembedParsed(QVector)), this, SLOT(onOEmbedParsed(QVector))); + + // TODO : Comment the next line to continue the Youtube button bugfix initialiazemOEmbedProviders(); } - UBWebController::~UBWebController() { // NOOP @@ -93,7 +96,6 @@ void UBWebController::initialiazemOEmbedProviders() mOEmbedProviders << "metacafe.com"; mOEmbedProviders << "qik.com"; mOEmbedProviders << "slideshare"; - mOEmbedProviders << "5min.com"; mOEmbedProviders << "twitpic.com"; mOEmbedProviders << "viddler.com"; mOEmbedProviders << "vimeo.com"; @@ -324,16 +326,53 @@ void UBWebController::activePageChanged() mTrapFlashController->updateTrapFlashFromPage((*mCurrentWebBrowser)->currentTabWebView()->page()->currentFrame()); } + + mMainWindow->actionWebTrap->setChecked(false); QUrl latestUrl = (*mCurrentWebBrowser)->currentTabWebView()->url(); + + // TODO : Uncomment the next line to continue the youtube button bugfix + //UBApplication::mainWindow->actionWebOEmbed->setEnabled(hasEmbeddedContent()); + // And remove this line once the previous one is uncommented UBApplication::mainWindow->actionWebOEmbed->setEnabled(isOEmbedable(latestUrl)); + UBApplication::mainWindow->actionEduMedia->setEnabled(isEduMedia(latestUrl)); emit activeWebPageChanged((*mCurrentWebBrowser)->currentTabWebView()); } } +bool UBWebController::hasEmbeddedContent() +{ + bool bHasContent = false; + if(mCurrentWebBrowser){ + QString html = (*mCurrentWebBrowser)->currentTabWebView()->webPage()->mainFrame()->toHtml(); + + // search the presence of "+oembed" + QString query = "\\+oembed([^>]*)>"; + QRegExp exp(query); + exp.indexIn(html); + QStringList results = exp.capturedTexts(); + if(2 <= results.size() && "" != results.at(1)){ + // An embedded content has been found, no need to check the other ones + bHasContent = true; + }else{ + QList contentUrls; + lookForEmbedContent(&html, "embed", "src", &contentUrls); + lookForEmbedContent(&html, "video", "src", &contentUrls); + lookForEmbedContent(&html, "object", "data", &contentUrls); + + // TODO: check the hidden iFrame + + if(!contentUrls.empty()){ + bHasContent = true; + } + } + } + + return bHasContent; +} QPixmap UBWebController::captureCurrentPage() { @@ -539,9 +578,12 @@ void UBWebController::showTabAtTop(bool attop) void UBWebController::captureoEmbed() { - if ( mCurrentWebBrowser && (*mCurrentWebBrowser) - && (*mCurrentWebBrowser)->currentTabWebView()) - { + if ( mCurrentWebBrowser && (*mCurrentWebBrowser) && (*mCurrentWebBrowser)->currentTabWebView()){ + // TODO : Uncomment the next lines to continue the youtube button bugfix + // getEmbeddableContent(); + + // And comment from here + QWebView* webView = (*mCurrentWebBrowser)->currentTabWebView(); QUrl currentUrl = webView->url(); @@ -555,9 +597,56 @@ void UBWebController::captureoEmbed() UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Selector); } } + // --> Until here } } +void UBWebController::lookForEmbedContent(QString* pHtml, QString tag, QString attribute, QList *pList) +{ + if(NULL != pHtml && NULL != pList){ + QVector urlsFound; + // Check for content + QRegExp exp(QString("<%0(.*)").arg(tag)); + exp.indexIn(*pHtml); + QStringList strl = exp.capturedTexts(); + if(2 <= strl.size() && strl.at(1) != ""){ + // Here we call this regular expression: + // src\s?=\s?['"]([^'"]*)['"] + // It says: give me all characters that are after src=" (or src = ") + QRegExp src(QString("%0\\s?=\\s?['\"]([^'\"]*)['\"]").arg(attribute)); + for(int i=1; iappend(QUrl(urls.at(1))); + } + } + } + } +} + +void UBWebController::checkForOEmbed(QString *pHtml) +{ + mOEmbedParser.parse(*pHtml); +} + +void UBWebController::getEmbeddableContent() +{ + // Get the source code of the page + if(mCurrentWebBrowser){ + QNetworkAccessManager* pNam = (*mCurrentWebBrowser)->currentTabWebView()->webPage()->networkAccessManager(); + if(NULL != pNam){ + QString html = (*mCurrentWebBrowser)->currentTabWebView()->webPage()->mainFrame()->toHtml(); + mOEmbedParser.setNetworkAccessManager(pNam); + + // First, we have to check if there is some oembed content + checkForOEmbed(&html); + + // Note: The other contents will be verified once the oembed ones have been checked + } + } +} void UBWebController::captureEduMedia() { @@ -718,3 +807,38 @@ void UBWebController::cut() act->trigger(); } } + +void UBWebController::onOEmbedParsed(QVector contents) +{ + QList urls; + + foreach(sOEmbedContent cnt, contents){ + urls << QUrl(cnt.url); + } + + // TODO : Implement this + //lookForEmbedContent(&html, "embed", "src", &urls); + //lookForEmbedContent(&html, "video", "src", &contentUrls); + //lookForEmbedContent(&html, "object", "data", &contentUrls); + + // TODO: check the hidden iFrame + + if(!urls.empty()){ + QUrl contentUrl; // The selected content url + + if(1 == urls.size()){ + contentUrl = urls.at(0); + }else{ + // TODO : Display a dialog box asking the user which content to get and set contentUrl to the selected content + + } + + UBGraphicsW3CWidgetItem * widget = UBApplication::boardController->activeScene()->addOEmbed(contentUrl); + + if(widget) + { + UBApplication::applicationController->showBoard(); + UBDrawingController::drawingController()->setStylusTool(UBStylusTool::Selector); + } + } +} diff --git a/src/web/UBWebController.h b/src/web/UBWebController.h index 29640cbe..8faa752d 100644 --- a/src/web/UBWebController.h +++ b/src/web/UBWebController.h @@ -19,6 +19,7 @@ #include #include +#include "web/UBOEmbedParser.h" class WBBrowserWindow; class UBApplication; @@ -30,7 +31,6 @@ class WBWebView; class UBServerXMLHttpRequest; //class UBKeyboardPalette; - class UBWebController : public QObject { Q_OBJECT; @@ -88,6 +88,9 @@ class UBWebController : public QObject void captureEduMedia(); bool isOEmbedable(const QUrl& pUrl); + bool hasEmbeddedContent(); + void getEmbeddableContent(); + bool isEduMedia(const QUrl& pUrl); void copy(); @@ -95,41 +98,36 @@ class UBWebController : public QObject void cut(); private: + void initialiazemOEmbedProviders(); + void tutorialWebInstance(); + void webBrowserInstance(); + void paraschoolWebInstance(); + void lookForEmbedContent(QString* pHtml, QString tag, QString attribute, QList* pList); + void checkForOEmbed(QString* pHtml); - QStackedWidget mStackedWidget[TotalNumberOfWebInstances]; + QStackedWidget mStackedWidget[TotalNumberOfWebInstances]; UBMainWindow *mMainWindow; - WBBrowserWindow* mWebBrowserList[TotalNumberOfWebInstances]; WBBrowserWindow** mCurrentWebBrowser; - QWidget* mBrowserWidget; UBTrapFlashController* mTrapFlashController; UBWebToolsPalette** mToolsCurrentPalette; UBWebToolsPalette* mToolsPaletteList[TotalNumberOfWebInstances]; // UBKeyboardPalette** mKeyboardCurrentPalette; // UBKeyboardPalette* mKeyboardPaletteList[TotalNumberOfWebInstances]; - bool mToolsPalettePositionned; bool mToolsPalettePositionnedList[TotalNumberOfWebInstances]; - bool mDownloadViewIsVisible; - QStringList mOEmbedProviders; - - void initialiazemOEmbedProviders(); - - void tutorialWebInstance(); - void webBrowserInstance(); - void paraschoolWebInstance(); + UBOEmbedParser mOEmbedParser; private slots: void activePageChanged(); void trapFlash(); - void toggleWebTrap(bool checked); - + void onOEmbedParsed(QVector contents); // void showKeyboard(bool checked); signals: @@ -139,7 +137,6 @@ class UBWebController : public QObject * @param pCapturedPixmap QPixmap corresponding to the capture. */ void imageCaptured(const QPixmap& pCapturedPixmap, bool pageMode, const QUrl& source); - void activeWebPageChanged(WBWebView* pWebView); }; diff --git a/src/web/browser/UBOEmbedParser.cpp b/src/web/browser/UBOEmbedParser.cpp new file mode 100644 index 00000000..9b2a7d6e --- /dev/null +++ b/src/web/browser/UBOEmbedParser.cpp @@ -0,0 +1,91 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#include "UBOEmbedParser.h" + +/********************************************************************************** + ---------------------------------------------------------------- + Here is an example of an embed content in an XML representation + ---------------------------------------------------------------- + + http://www.youtube.com/ + EPISODE 36 Traditional Mediums + + + + FZDSCHOOL + 270 + 480 + 480 + 1.0 + http://www.youtube.com/user/FZDSCHOOL + YouTube + http://i4.ytimg.com/vi/C3lApsNmdwM/hqdefault.jpg + video + 360 + +***********************************************************************************/ +typedef struct{ + QString providerUrl; + QString title; + QString author; + int height; + int thumbWidth; + float version; + QString authorUrl; + QString providerName; + QString thumbUrl; + QString type; + QString thumbHeight; + QString sourceUrl; +}sOEmbedContent; + +UBOEmbedParser::UBOEmbedParser(QObject *parent, const char* name) +{ + setObjectName(name); + mParsedTitles.clear(); +} + +UBOEmbedParser::~UBOEmbedParser() +{ + +} + +void UBOEmbedParser::parse(const QString& html, QList *pList) +{ + // get all the oembed links and parse their informations + QString query = ""; + +} + +/** + /brief Extract the oembed infos from the JSON + @param jsonUrl as the url of the JSON file + */ +void UBOEmbedParser::getJSONInfos(const QString &jsonUrl) +{ + +} + +/** + /brief Extract the oembed infos from the XML + @param xmlUrl as the url of the XML file + */ +void UBOEmbedParser::getXMLInfos(const QString &xmlUrl) +{ + +} diff --git a/src/web/web.pri b/src/web/web.pri index 0e38d0c0..8d89a572 100644 --- a/src/web/web.pri +++ b/src/web/web.pri @@ -26,7 +26,8 @@ HEADERS += src/web/UBWebController.h \ src/web/browser/WBUrlLineEdit.h \ src/web/browser/WBWebView.h \ src/web/browser/WBHistory.h \ - src/web/browser/WBWebTrapWebView.h + src/web/browser/WBWebTrapWebView.h \ + src/web/UBOEmbedParser.h SOURCES += src/web/UBWebController.cpp \ src/web/UBTrapFlashController.cpp \ @@ -46,8 +47,7 @@ SOURCES += src/web/UBWebController.cpp \ src/web/browser/WBUrlLineEdit.cpp \ src/web/browser/WBWebView.cpp \ src/web/browser/WBHistory.cpp \ - src/web/browser/WBWebTrapWebView.cpp + src/web/browser/WBWebTrapWebView.cpp \ + src/web/UBOEmbedParser.cpp - -