File indexing completed on 2024-04-28 03:50:26

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2008 Shashank Singh <shashank.personal@gmail.com>
0004 //
0005 
0006 #include "twitterPlugin.h"
0007 
0008 #include <QColor>
0009 #include <QSize>
0010 #include <QRegExp>
0011 
0012 using namespace Marble;
0013 
0014 /**
0015 Right now this plugin displays public twit from Twitter , and gecocodes [i.e getting lat lon from a given street address] using Google Map API , i plan to extend it to use OSM GeoCoding in net few days :) [The API key has been taken for  my personal website , please don't misuse it :) ]
0016 */
0017 
0018 twitterPlugin::~twitterPlugin()
0019 {
0020 //    delete m_storagePolicy;
0021 }
0022 
0023 QStringList twitterPlugin::backendTypes() const
0024 {
0025     return QStringList(QStringLiteral("twitter"));
0026 }
0027 
0028 QString twitterPlugin::renderPolicy() const
0029 {
0030     return QStringLiteral("ALWAYS");
0031 }
0032 
0033 QStringList twitterPlugin::renderPosition() const
0034 {
0035     return QStringList(QStringLiteral("ALWAYS_ON_TOP"));
0036 }
0037 
0038 QString twitterPlugin::name() const
0039 {
0040     return tr("twitter ");
0041 }
0042 
0043 QString twitterPlugin::guiString() const
0044 {
0045     return tr("&twitter");
0046 }
0047 
0048 QString twitterPlugin::nameId() const
0049 {
0050     return QStringLiteral("twitter");
0051 }
0052 
0053 QString twitterPlugin::description() const
0054 {
0055     return tr("show public twitts in their places");
0056 }
0057 
0058 QIcon twitterPlugin::icon() const
0059 {
0060     return QIcon();
0061 }
0062 
0063 
0064 void twitterPlugin::initialize()
0065 {
0066     privateFlagForRenderingTwitts = 0;
0067     m_storagePolicy = new CacheStoragePolicy(MarbleDirs::localPath() + QLatin1String("/cache/"));
0068     m_downloadManager = new HttpDownloadManager(QUrl("http://twiter.com"), m_storagePolicy);
0069     downloadtwitter(0, 0, 0.0, 0.0, 0.0, 0.0);
0070      mDebug() << "twitter plugin was started";
0071 }
0072 
0073 bool twitterPlugin::isInitialized() const
0074 {
0075     return true;
0076 }
0077 
0078 bool twitterPlugin::render(GeoPainter *painter, ViewportParams *viewport,
0079                const QString& renderPos, GeoSceneLayer * layer)
0080 {
0081     QBrush brush(QColor(99, 198, 198, 80));
0082     painter->setPen(QColor(198, 99, 99, 255));
0083     brush.setColor(QColor(255, 255, 255, 200));
0084     brush.setStyle(Qt::SolidPattern);
0085     painter->setBrush(brush);
0086 
0087     if (privateFlagForRenderingTwitts >= 1) {
0088         for (int counter = 0;counter < 4;counter++)
0089 //painter->drawAnnotation(GeoDataCoordinates(0.0,0.0),"hiiiiiiiiii");            
0090 
0091 painter->drawAnnotation(twitsWithLocation[counter].location,
0092                     parsedData[counter].user + QLatin1String(" said \n")
0093                     + parsedData[counter].text,
0094                     QSize(140, 140)) ;
0095     } else {
0096         painter->drawAnnotation(GeoDataCoordinates(0.0, 0.0, 0.0,
0097                            GeoDataCoordinates::Degree),
0098                 "Twitts are being Downlaoded @Twitter/Identi.CA Plugin");
0099     }
0100     return true;
0101 }
0102 
0103 void twitterPlugin::slotJsonDownloadComplete(QString relativeUrlString, QString id)
0104 {
0105 static int counter=0;
0106 twitterStructure temp;
0107 //mDebug()<<"::::"<<temp.;
0108 //temp.twit = parsedData[counter].text ;
0109  parsedData = twitterJsonParser.parseAllObjects(QString::fromUtf8(m_storagePolicy->data(id)), 20);
0110 mDebug()<<"::::::::::::::::slot"<<parsedData[0].text;
0111     disconnect(m_downloadManager, SIGNAL(downloadComplete(QString,QString)), this, SLOT(slotJsonDownloadComplete(QString,QString)) );
0112 
0113     connect(m_downloadManager, SIGNAL(downloadComplete(QString,QString)), this, SLOT(slotGeoCodingReplyRecieved(QString,QString)) );
0114     for (int counter = 0;counter < 10;counter++) {
0115        if (parsedData[counter].location != QLatin1String("null")) {
0116            parsedData[counter].location.replace(QRegExp("[?,:!/\\s]+"), "+");//remove whitespace and replace it with + for query api
0117             findLatLonOfStreetAddress(parsedData [ counter ].location) ;   //this will set temp
0118        }
0119     }
0120 }
0121 
0122 
0123 void twitterPlugin::downloadtwitter(int rangeFrom , int rangeTo , qreal east , qreal west , qreal north , qreal south)
0124 {
0125 mDebug()<<"::::::downloading"<<rangeFrom ;
0126 /** to use identi.ca line also */
0127     m_downloadManager->addJob(QUrl("http://twitter.com/statuses/public_timeline.json"), "twitter", "twitter");
0128 //    m_downloadManager->addJob(QUrl("http://identi.ca/api/statuses/public_timeline.json"), "identica", "identica");
0129 
0130     connect(m_downloadManager, SIGNAL(downloadComplete(QString,QString)), this, SLOT(slotJsonDownloadComplete(QString,QString)) );
0131 
0132 }
0133 
0134 
0135 void twitterPlugin::findLatLonOfStreetAddress(QString streetAddress)
0136 {
0137     m_downloadManager->addJob(QLatin1String("http://maps.google.com/maps/geo?q=") + streetAddress + QLatin1String("&output=json&key=ABQIAAAASD_v8YRzG0tBD18730KjmRTxoHoIpYL45xcSRJH0O7cH64DuXRT7rQeRcgCLAhjkteQ8vkWAATM_JQ"), streetAddress, streetAddress);
0138     mDebug() << "twitter added Geo Coding job for " << streetAddress;
0139 }
0140 
0141 void twitterPlugin::slotGeoCodingReplyRecieved(QString relativeUrlString, QString id)
0142 {
0143     static int localCountOfTwitts = 0;
0144     twitterStructure twitterData;
0145     googleMapDataStructure geoCodedData;
0146 
0147    geoCodedData = twitterJsonParser.geoCodingAPIparseObject(QString::fromUtf8(m_storagePolicy->data(id))) ;
0148      twitterData.twit = "hi" ;
0149     twitterData.location = GeoDataCoordinates(geoCodedData.lat, geoCodedData.lon, 1.0, GeoDataCoordinates::Degree);
0150    twitsWithLocation.append(twitterData);
0151   localCountOfTwitts ++;
0152 mDebug()<<"::::::::::::::::::::twitter count has value == " << localCountOfTwitts;
0153     if (localCountOfTwitts >= 1)
0154         privateFlagForRenderingTwitts = 1;//1 means unblock
0155 }
0156 
0157  #include "moc_twitterPlugin.cpp"