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

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2017 Sutirtha Ghosh <ghsutirtha@gmail.com>
0004 //
0005 
0006 //Self
0007 #include "DownloadOsmDialog.h"
0008 
0009 //Qt
0010 #include <QMessageBox>
0011 #include <QDialogButtonBox>
0012 #include  <QDir>
0013 
0014 //Marble
0015 #include "MarbleWidget.h"
0016 #include "ViewParams.h"
0017 #include "ViewportParams.h"
0018 #include "GeoDataLatLonAltBox.h"
0019 #include "GeoDataLatLonBox.h"
0020 #include "MarbleGlobal.h"
0021 #include "AnnotatePlugin.h"
0022 
0023 namespace Marble
0024 {
0025 DownloadOsmDialog::DownloadOsmDialog(MarbleWidget *parent,AnnotatePlugin *annotatePlugin) :
0026     QDialog(parent),
0027     m_marbleWidget(parent),
0028     m_latLonBoxWidget(new LatLonBoxWidget)
0029 {
0030     setupUi(this);
0031     horizontalLayout->addWidget(m_latLonBoxWidget);
0032     this->setWindowTitle(tr("Download"));
0033     connect(m_marbleWidget,
0034             SIGNAL(visibleLatLonAltBoxChanged(GeoDataLatLonAltBox)),
0035             this,
0036             SLOT(updateCoordinates(GeoDataLatLonAltBox))
0037             );
0038 
0039     m_downloadButton = new QPushButton(tr("Download"));
0040     m_downloadButton->setDefault(true);
0041 
0042     buttonBox->addButton(m_downloadButton,QDialogButtonBox::ActionRole);
0043 
0044     connect( m_downloadButton, SIGNAL(clicked(bool)), this, SLOT(downloadFile()) );
0045     connect( buttonBox, SIGNAL(rejected()), this, SLOT(close()) );
0046     connect( this, SIGNAL(openFile(QString)), annotatePlugin, SLOT(openAnnotationFile(QString)) );
0047 
0048     progressBar->hide();
0049     updateCoordinates();
0050 }
0051 
0052 DownloadOsmDialog::~DownloadOsmDialog()
0053 {
0054 
0055 }
0056 
0057 void DownloadOsmDialog::updateCoordinates()
0058 {
0059 
0060     m_latLonBoxWidget->setLatLonBox( m_marbleWidget->viewport()->viewLatLonAltBox() );
0061 }
0062 
0063 void DownloadOsmDialog::updateCoordinates( const GeoDataLatLonAltBox& boundingBox )
0064 {
0065     m_latLonBoxWidget->setLatLonBox( boundingBox );
0066 }
0067 
0068 void DownloadOsmDialog::downloadFile()
0069 {
0070     QString m_west;
0071     QString m_south;
0072     QString m_east;
0073     QString m_north;
0074     QString url;
0075     m_isDownloadSuccess=false;
0076     m_file = new QTemporaryFile(QDir::tempPath()+"/"+"XXXXXXosmdata.osm");
0077     if(!m_file->open())
0078     {
0079         QMessageBox::information(this, tr("ERROR"),
0080                                  tr("Unable to create temporary file to download OSM data to."));
0081         this->close();
0082     }
0083     m_downloadButton->setEnabled(false);
0084 
0085     m_west=QString::number( m_latLonBoxWidget->latLonBox().west()*RAD2DEG );
0086     m_south=QString::number( m_latLonBoxWidget->latLonBox().south()*RAD2DEG );
0087     m_east=QString::number( m_latLonBoxWidget->latLonBox().east()*RAD2DEG );
0088     m_north=QString::number( m_latLonBoxWidget->latLonBox().north()*RAD2DEG );
0089 
0090     url="http://api.openstreetmap.org/api/0.6/map?bbox=";
0091     url+=m_west+",";
0092     url+=m_south+",";
0093     url+=m_east+",";
0094     url+=m_north;
0095 
0096     m_reply = m_qnam.get(QNetworkRequest(QUrl(url)));
0097 
0098     connect( m_reply, SIGNAL(finished()), this, SLOT(httpFinished()) );
0099     connect( m_reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()) );
0100 
0101     progressBar->show();
0102     progressBar->setMinimum(0);
0103     progressBar->setMaximum(0);
0104 }
0105 
0106 void DownloadOsmDialog::httpReadyRead()
0107 {
0108     //Reads all the data present and writes it to the file whenever data is
0109     //available
0110     if ( m_file ){
0111         m_file->write(m_reply->readAll());
0112     }
0113 }
0114 
0115 void DownloadOsmDialog::httpFinished()
0116 {
0117     QVariant statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
0118     int status = statusCode.toInt();
0119     //in case download fails due to network error or server not available
0120     if ( m_reply->error()==QNetworkReply::NoError ){
0121         m_isDownloadSuccess=true;
0122     }
0123     else {
0124         switch(status)
0125         {
0126         case 400:
0127             QMessageBox::information(this, tr("ERROR"),
0128                                      tr("The selected region contains too much data. Please select a smaller region and try again."));
0129             m_downloadButton->setEnabled(true);
0130             m_isDownloadSuccess=false;
0131             break;
0132         case 509:
0133             QMessageBox::information(this, tr("ERROR"),
0134                                      tr("The bandwidth limit exceeded. Please try again later."));
0135             m_downloadButton->setEnabled(true);
0136             m_isDownloadSuccess=false;
0137             break;
0138         default:
0139             QMessageBox::information(this, tr("ERROR"),tr("Sorry, a network error occurred. Please check your internet connection"
0140                                                        " or try again later."));
0141             m_downloadButton->setEnabled(true);
0142             m_isDownloadSuccess=false;
0143             break;
0144         }
0145     }
0146 
0147     progressBar->hide();
0148     m_file->flush();
0149     m_file->close();
0150     if( m_isDownloadSuccess ) {
0151         emit openFile(m_file->fileName());
0152     }
0153     m_reply->deleteLater();
0154     m_reply=nullptr;
0155     delete m_file;
0156     m_file=nullptr;
0157     if( m_isDownloadSuccess ) {
0158         this->close();
0159     }
0160 }
0161 
0162 }
0163 #include "moc_DownloadOsmDialog.cpp"