/* * Copyright (C) 2013 Open Education Foundation * * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour * l'Education Numérique en Afrique (GIP ENA) * * This file is part of OpenBoard. * * OpenBoard 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, version 3 of the License, * with a specific linking exception for the OpenSSL project's * "OpenSSL" library (or with modified versions of it that use the * same license as the "OpenSSL" library). * * OpenBoard 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 OpenBoard. If not, see . */ #include #include #include #include #include #include #include #include #include "UBOEmbedParser.h" #include "core/memcheck.h" UBOEmbedParser::UBOEmbedParser(QObject *parent, const char* name) { Q_UNUSED(parent); 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); } }