File indexing completed on 2024-05-05 03:50:48

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2008 Shashan Singh <shashank.personal@gmail.com>
0004 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0005 //
0006 
0007 #include "PanoramioParser.h"
0008 
0009 #include <QScriptValue>
0010 
0011 PanoramioParser::PanoramioParser() :
0012     dataStorage()
0013 {
0014   myEngine.setProcessEventsInterval(10);//this lets the gui remain responsive
0015 }
0016 
0017 PanoramioParser::~PanoramioParser()
0018 {
0019 }
0020 
0021 panoramioDataStructure PanoramioParser::parseObjectOnPosition(const QString &content, int requiredObjectPosition)
0022 {
0023     QString temp = QLatin1String("var myJSONObject =") + content;
0024     myEngine.evaluate(temp);
0025     myEngine.evaluate(QString("function count(){ return myJSONObject.count };"));
0026     myEngine.evaluate(QString("function height(x){return myJSONObject.photos[x].height};"));
0027     myEngine.evaluate(QString("function latitude(x){return myJSONObject.photos[x].latitude};"));
0028     myEngine.evaluate(QString("function longitude (x){return myJSONObject.photos[x].longitude};"));
0029     myEngine.evaluate(QString("function owner_id(x){return myJSONObject.photos[x].owner_id};"));
0030     myEngine.evaluate(QString("function photo_url(x){return myJSONObject.photos[x].photo_url};"));
0031     myEngine.evaluate(QString("function photo_file_url(x){return myJSONObject.photos[x].photo_file_url};"));
0032     myEngine.evaluate(QString("function photo_title(x){return myJSONObject.photos[x].photo_title};"));
0033     myEngine.evaluate(QString("function photo_id(x){return myJSONObject.photos[x].photo_id};"));
0034     myEngine.evaluate(QString("function upload_date(x){return myJSONObject.photos[x].upload_date};"));
0035     dataStorage.count = myEngine.evaluate("count();").toInteger();
0036     myEngine.evaluate(QString("var x="+QString::number(requiredObjectPosition)));
0037     dataStorage.longitude=myEngine.evaluate(QString("longitude(x)")).toNumber();
0038     dataStorage.latitude=myEngine.evaluate(QString("latitude(x)")).toNumber();
0039     dataStorage.photo_url=myEngine.evaluate("photo_url(x)").toString();
0040     dataStorage.photo_file_url=myEngine.evaluate(QString("photo_file_url(x)")).toString();
0041     dataStorage.photo_title=myEngine.evaluate(QString("photo_title(x)")).toString();
0042     dataStorage.photo_id=myEngine.evaluate(QString("photo_id(x)")).toNumber();
0043     
0044     // Getting the upload date of the image.
0045     QString upload_date_string=myEngine.evaluate(QString("upload_date(x)")).toString();
0046     QStringList date = upload_date_string.split(QLatin1Char(' '));
0047     if( date.size() == 3 ) {
0048         unsigned int day = date.at( 0 ).toUInt();
0049         const QString &monthString = date.at(1);
0050         unsigned int month = 1;
0051         if (monthString.contains(QLatin1String("January"))) {
0052             month = 1;
0053         } else if (monthString.contains(QLatin1String("February"))) {
0054             month = 2;
0055         } else if (monthString.contains(QLatin1String("March"))) {
0056             month = 3;
0057         } else if (monthString.contains(QLatin1String("April"))) {
0058             month = 4;
0059         } else if (monthString.contains(QLatin1String("May"))) {
0060             month = 5;
0061         } else if (monthString.contains(QLatin1String("June"))) {
0062             month = 6;
0063         } else if (monthString.contains(QLatin1String("July"))) {
0064             month = 7;
0065         } else if (monthString.contains(QLatin1String("August"))) {
0066             month = 8;
0067         } else if (monthString.contains(QLatin1String("September"))) {
0068             month = 9;
0069         } else if (monthString.contains(QLatin1String("October"))) {
0070             month = 10;
0071         } else if (monthString.contains(QLatin1String("November"))) {
0072             month = 11;
0073         } else if (monthString.contains(QLatin1String("December"))) {
0074             month = 12;
0075         }
0076 
0077         unsigned int year = date.at( 2 ).toUInt();
0078         
0079         dataStorage.upload_date = QDate( year, month, day );
0080     }
0081     else {
0082         dataStorage.upload_date = QDate( 1970,     1,   1 );
0083     }
0084 
0085 //     mDebug()<<":::::::"<<__func__<<myEngine.evaluate("longitude(x)").toNumber();
0086     return dataStorage;
0087 }
0088 
0089 QList<panoramioDataStructure> PanoramioParser::parseAllObjects(const QString &content, int number)
0090 {
0091     QList <panoramioDataStructure> returnStructure;
0092     
0093     for( int i = 0; i < number; i++ ) {
0094         returnStructure.append( parseObjectOnPosition( content, i ) );
0095     }
0096     
0097     return returnStructure;
0098 }