MpcImportWindow.cpp   MpcImportWindow.cpp 
skipping to change at line 45 skipping to change at line 45
#include <QHash> #include <QHash>
#include <QList> #include <QList>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <QString> #include <QString>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QTimer> #include <QTimer>
#include <QUrl> #include <QUrl>
MpcImportWindow::MpcImportWindow() MpcImportWindow::MpcImportWindow() :
downloadReply(0),
queryReply(0),
downloadProgressBar(0),
queryProgressBar(0)
{ {
ui = new Ui_mpcImportWindow(); ui = new Ui_mpcImportWindow();
ssoManager = GETSTELMODULE(SolarSystemEditor); ssoManager = GETSTELMODULE(SolarSystemEditor);
networkManager = StelApp::getInstance().getNetworkAccessManager(); networkManager = StelApp::getInstance().getNetworkAccessManager();
downloadReply = NULL;
queryReply = NULL;
downloadProgressBar = NULL;
queryProgressBar = NULL;
countdownTimer = new QTimer(this); countdownTimer = new QTimer(this);
QHash<QString,QString> asteroidBookmarks; QHash<QString,QString> asteroidBookmarks;
QHash<QString,QString> cometBookmarks; QHash<QString,QString> cometBookmarks;
bookmarks.insert(MpcComets, cometBookmarks); bookmarks.insert(MpcComets, cometBookmarks);
bookmarks.insert(MpcMinorPlanets, asteroidBookmarks); bookmarks.insert(MpcMinorPlanets, asteroidBookmarks);
} }
MpcImportWindow::~MpcImportWindow() MpcImportWindow::~MpcImportWindow()
{ {
skipping to change at line 84 skipping to change at line 83
downloadProgressBar->deleteLater(); downloadProgressBar->deleteLater();
if (queryProgressBar) if (queryProgressBar)
queryProgressBar->deleteLater(); queryProgressBar->deleteLater();
} }
void MpcImportWindow::createDialogContent() void MpcImportWindow::createDialogContent()
{ {
ui->setupUi(dialog); ui->setupUi(dialog);
//Signals //Signals
connect(&StelApp::getInstance(), SIGNAL(languageChanged()), this, SL OT(languageChanged()));
connect(ui->closeStelWindow, SIGNAL(clicked()), this, SLOT(close())) ; connect(ui->closeStelWindow, SIGNAL(clicked()), this, SLOT(close())) ;
connect(ui->pushButtonAcquire, SIGNAL(clicked()), this, SLOT(acquire ObjectData())); connect(ui->pushButtonAcquire, SIGNAL(clicked()), this, SLOT(acquire ObjectData()));
connect(ui->pushButtonAbortDownload, SIGNAL(clicked()), this, SLOT(a bortDownload())); connect(ui->pushButtonAbortDownload, SIGNAL(clicked()), this, SLOT(a bortDownload()));
connect(ui->pushButtonAdd, SIGNAL(clicked()), this, SLOT(addObjects( ))); connect(ui->pushButtonAdd, SIGNAL(clicked()), this, SLOT(addObjects( )));
connect(ui->pushButtonDiscard, SIGNAL(clicked()), this, SLOT(discard Objects())); connect(ui->pushButtonDiscard, SIGNAL(clicked()), this, SLOT(discard Objects()));
connect(ui->pushButtonBrowse, SIGNAL(clicked()), this, SLOT(selectFi le())); connect(ui->pushButtonBrowse, SIGNAL(clicked()), this, SLOT(selectFi le()));
connect(ui->pushButtonPasteURL, SIGNAL(clicked()), this, SLOT(pasteC lipboardURL())); connect(ui->pushButtonPasteURL, SIGNAL(clicked()), this, SLOT(pasteC lipboardURL()));
connect(ui->comboBoxBookmarks, SIGNAL(currentIndexChanged(QString)), this, SLOT(bookmarkSelected(QString))); connect(ui->comboBoxBookmarks, SIGNAL(currentIndexChanged(QString)), this, SLOT(bookmarkSelected(QString)));
skipping to change at line 636 skipping to change at line 636
if (downloadProgressBar) if (downloadProgressBar)
{ {
downloadProgressBar->setVisible(false); downloadProgressBar->setVisible(false);
downloadProgressBar->deleteLater(); downloadProgressBar->deleteLater();
downloadProgressBar = NULL; downloadProgressBar = NULL;
} }
} }
void MpcImportWindow::sendQuery() void MpcImportWindow::sendQuery()
{ {
if (queryReply != NULL) if (queryReply != 0)
return; return;
QString query = ui->lineEditQuery->text().trimmed(); query = ui->lineEditQuery->text().trimmed();
if (query.isEmpty()) if (query.isEmpty())
return; return;
//Progress bar //Progress bar
queryProgressBar = StelApp::getInstance().getGui()->addProgressBar() ; queryProgressBar = StelApp::getInstance().getGui()->addProgressBar() ;
queryProgressBar->setValue(0); queryProgressBar->setValue(0);
queryProgressBar->setMaximum(0); queryProgressBar->setMaximum(0);
queryProgressBar->setFormat("Searching..."); queryProgressBar->setFormat("Searching...");
queryProgressBar->setVisible(true); queryProgressBar->setVisible(true);
//TODO: Better handling of the interface //TODO: Better handling of the interface
enableInterface(false); enableInterface(false);
ui->labelQueryMessage->setVisible(false); ui->labelQueryMessage->setVisible(false);
QUrl url; startCountdown();
ui->pushButtonAbortQuery->setVisible(true);
sendQueryToUrl(QUrl("http://stellarium.org/mpc-mpeph"));
//sendQueryToUrl(QUrl("http://scully.cfa.harvard.edu/cgi-bin/mpeph2.
cgi"));
}
void MpcImportWindow::sendQueryToUrl(QUrl url)
{
//QUrl url;
url.addQueryItem("ty","e");//Type: ephemerides url.addQueryItem("ty","e");//Type: ephemerides
url.addQueryItem("TextArea", query);//Object name query url.addQueryItem("TextArea", query);//Object name query
//url.addQueryItem("e", "-1");//Elements format: MPC 1-line //url.addQueryItem("e", "-1");//Elements format: MPC 1-line
//XEphem's format is used instead because it doesn't truncate object names.
url.addQueryItem("e", "3");//Elements format: XEphem url.addQueryItem("e", "3");//Elements format: XEphem
//Yes, all of the rest are necessary //Yes, all of the rest are necessary
url.addQueryItem("d",""); url.addQueryItem("d","");
url.addQueryItem("l",""); url.addQueryItem("l","");
url.addQueryItem("i",""); url.addQueryItem("i","");
url.addQueryItem("u","d"); url.addQueryItem("u","d");
url.addQueryItem("uto", "0"); url.addQueryItem("uto", "0");
url.addQueryItem("c", ""); url.addQueryItem("c", "");
url.addQueryItem("long", ""); url.addQueryItem("long", "");
url.addQueryItem("lat", ""); url.addQueryItem("lat", "");
skipping to change at line 681 skipping to change at line 691
url.addQueryItem("m", "m"); url.addQueryItem("m", "m");
url.addQueryItem("adir", "S"); url.addQueryItem("adir", "S");
url.addQueryItem("oed", ""); url.addQueryItem("oed", "");
url.addQueryItem("resoc", ""); url.addQueryItem("resoc", "");
url.addQueryItem("tit", ""); url.addQueryItem("tit", "");
url.addQueryItem("bu", ""); url.addQueryItem("bu", "");
url.addQueryItem("ch", "c"); url.addQueryItem("ch", "c");
url.addQueryItem("ce", "f"); url.addQueryItem("ce", "f");
url.addQueryItem("js", "f"); url.addQueryItem("js", "f");
QNetworkRequest request(QUrl("http://scully.cfa.harvard.edu/~cgi/MPE QNetworkRequest request(url);
ph2")); request.setHeader(QNetworkRequest::ContentTypeHeader,
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x- "application/x-www-form-urlencoded");//Is this rea
www-form-urlencoded");//Is this really necessary? lly necessary?
request.setHeader(QNetworkRequest::ContentLengthHeader, url.encodedQ request.setHeader(QNetworkRequest::ContentLengthHeader,
uery().length()); url.encodedQuery().length());
startCountdown(); connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT
ui->pushButtonAbortQuery->setVisible(true); (receiveQueryReply(QNetworkReply*)));
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT
(queryComplete(QNetworkReply*)));
queryReply = networkManager->post(request, url.encodedQuery()); queryReply = networkManager->post(request, url.encodedQuery());
connect(queryReply, SIGNAL(downloadProgress(qint64,qint64)), this, S LOT(updateQueryProgress(qint64,qint64))); connect(queryReply, SIGNAL(downloadProgress(qint64,qint64)), this, S LOT(updateQueryProgress(qint64,qint64)));
} }
void MpcImportWindow::abortQuery() void MpcImportWindow::abortQuery()
{ {
if (queryReply == NULL) if (queryReply == 0)
return; return;
disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, S LOT(queryComplete(QNetworkReply*))); disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, S LOT(receiveQueryReply(QNetworkReply*)));
deleteQueryProgressBar(); deleteQueryProgressBar();
queryReply->abort(); queryReply->abort();
queryReply->deleteLater(); queryReply->deleteLater();
queryReply = NULL; queryReply = 0;
//resetCountdown(); //resetCountdown();
enableInterface(true); enableInterface(true);
ui->pushButtonAbortQuery->setVisible(false); ui->pushButtonAbortQuery->setVisible(false);
} }
void MpcImportWindow::queryComplete(QNetworkReply *reply) void MpcImportWindow::receiveQueryReply(QNetworkReply *reply)
{ {
disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, S if (reply == 0)
LOT(queryComplete(QNetworkReply*))); return;
disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, S
LOT(receiveQueryReply(QNetworkReply*)));
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAtt
ribute).toInt();
if (statusCode == 301 || statusCode == 302 || statusCode == 307)
{
QUrl rawUrl = reply->attribute(QNetworkRequest::RedirectionT
argetAttribute).toUrl();
QUrl redirectUrl(rawUrl.toString(QUrl::RemoveQuery));
qDebug() << "The search query has been redirected to" << red
irectUrl.toString();
//TODO: Add counter and cycle check.
reply->deleteLater();
queryReply = 0;
sendQueryToUrl(redirectUrl);
return;
}
deleteQueryProgressBar(); deleteQueryProgressBar();
//Hide the abort button - a reply has been received //Hide the abort button - a reply has been received
ui->pushButtonAbortQuery->setVisible(false); ui->pushButtonAbortQuery->setVisible(false);
if (reply->error()) if (reply->error())
{ {
qWarning() << "Download error: While trying to access" qWarning() << "Download error: While trying to access"
<< reply->url().toString() << reply->url().toString()
<< "the following error occured:" << "the following error occured:"
<< reply->errorString(); << reply->errorString();
ui->labelQueryMessage->setText(reply->errorString());//TODO: Decide if this is a good idea ui->labelQueryMessage->setText(reply->errorString());//TODO: Decide if this is a good idea
ui->labelQueryMessage->setVisible(true); ui->labelQueryMessage->setVisible(true);
enableInterface(true); enableInterface(true);
reply->deleteLater(); reply->deleteLater();
queryReply = NULL; queryReply = 0;
return; return;
} }
if (reply->header(QNetworkRequest::ContentTypeHeader) != "text/ascii QString contentType = reply->header(QNetworkRequest::ContentTypeHead
" || er).toString();
reply->rawHeader(QByteArray("Content-disposition")) != "attachme QString contentDisposition = reply->rawHeader(QByteArray("Content-di
nt; filename=elements.txt") sposition"));
if (contentType == "text/ascii" &&
contentDisposition == "attachment; filename=elements.txt")
{
readQueryReply(reply);
}
else
{ {
ui->labelQueryMessage->setText("Object not found."); ui->labelQueryMessage->setText("Object not found.");
ui->labelQueryMessage->setVisible(true); ui->labelQueryMessage->setVisible(true);
enableInterface(true); enableInterface(true);
} }
else
reply->deleteLater();
queryReply = 0;
}
void MpcImportWindow::readQueryReply(QNetworkReply * reply)
{
Q_ASSERT(reply);
QList<SsoElements> objects;
QTemporaryFile file;
if (file.open())
{ {
QList<SsoElements> objects; file.write(reply->readAll());
QTemporaryFile file; file.close();
if (file.open())
{
file.write(reply->readAll());
file.close();
/*
//Try to read it as a comet first?
objects = readElementsFromFile(MpcComets, file.fileN
ame());
if (objects.isEmpty())
objects = readElementsFromFile(MpcMinorPlane
ts, file.fileName());
*/
objects = ssoManager->readXEphemOneLineElementsFromF
ile(file.fileName());
}
else
{
qWarning() << "Unable to open a temporary file. Abor
ting operation.";
}
/*
//Try to read it as a comet first?
objects = readElementsFromFile(MpcComets, file.fileName());
if (objects.isEmpty()) if (objects.isEmpty())
objects = readElementsFromFile(MpcMinorPlanets, file
.fileName());
*/
objects = ssoManager->readXEphemOneLineElementsFromFile(file
.fileName());
}
else
{
qWarning() << "Unable to open a temporary file. Aborting ope
ration.";
}
if (objects.isEmpty())
{
qWarning() << "No objects found in the file downloaded from"
<< reply->url().toString();
}
else
{
//The request has been successful: add the URL to bookmarks?
if (ui->checkBoxAddBookmark->isChecked())
{ {
qWarning() << "No objects found in the file download QString url = reply->url().toString();
ed from" if (!bookmarks.value(importType).values().contains(u
<< reply->url().toString(); rl))
}
else
{
//The request has been successful: add the URL to bo
okmarks?
if (ui->checkBoxAddBookmark->isChecked())
{ {
QString url = reply->url().toString(); //Use the URL as a title for now
if (!bookmarks.value(importType).values().co bookmarks[importType].insert(url, url);
ntains(url))
{
//Use the URL as a title for now
bookmarks[importType].insert(url, ur
l);
}
} }
//Temporary, until the slot/socket mechanism is read
y
populateCandidateObjects(objects);
ui->stackedWidget->setCurrentIndex(1);
} }
}
reply->deleteLater(); //Temporary, until the slot/socket mechanism is ready
queryReply = NULL; populateCandidateObjects(objects);
ui->stackedWidget->setCurrentIndex(1);
}
} }
void MpcImportWindow::deleteQueryProgressBar() void MpcImportWindow::deleteQueryProgressBar()
{ {
disconnect(this, SLOT(updateQueryProgress(qint64,qint64))); disconnect(this, SLOT(updateQueryProgress(qint64,qint64)));
if (queryProgressBar) if (queryProgressBar)
{ {
queryProgressBar->setVisible(false); queryProgressBar->setVisible(false);
queryProgressBar->deleteLater(); queryProgressBar->deleteLater();
queryProgressBar = NULL; queryProgressBar = NULL;
skipping to change at line 818 skipping to change at line 856
} }
void MpcImportWindow::resetCountdown() void MpcImportWindow::resetCountdown()
{ {
//Stop the timer //Stop the timer
if (countdownTimer->isActive()) if (countdownTimer->isActive())
{ {
countdownTimer->stop(); countdownTimer->stop();
//If the query is still active, kill it //If the query is still active, kill it
if (queryReply != NULL && queryReply->isRunning()) if (queryReply != 0 && queryReply->isRunning())
{ {
abortQuery(); abortQuery();
ui->labelQueryMessage->setText("The query timed out. You can try again, now or later."); ui->labelQueryMessage->setText("The query timed out. You can try again, now or later.");
ui->labelQueryMessage->setVisible(true); ui->labelQueryMessage->setVisible(true);
} }
} }
//Reset the counter //Reset the counter
countdown = 60; countdown = 60;
skipping to change at line 842 skipping to change at line 880
} }
void MpcImportWindow::updateCountdown() void MpcImportWindow::updateCountdown()
{ {
--countdown; --countdown;
if (countdown < 0) if (countdown < 0)
{ {
resetCountdown(); resetCountdown();
} }
//If there has been an answer //If there has been an answer
else if (countdown > 50 && queryReply == NULL) else if (countdown > 50 && queryReply == 0)
{ {
resetCountdown(); resetCountdown();
} }
} }
void MpcImportWindow::resetNotFound() void MpcImportWindow::resetNotFound()
{ {
ui->labelQueryMessage->setVisible(false); ui->labelQueryMessage->setVisible(false);
} }
skipping to change at line 880 skipping to change at line 918
//If nothing was read, continue //If nothing was read, continue
if (!bookmarks.value(MpcComets).isEmpty() && !bookma rks[MpcMinorPlanets].isEmpty()) if (!bookmarks.value(MpcComets).isEmpty() && !bookma rks[MpcMinorPlanets].isEmpty())
return; return;
} }
} }
qDebug() << "Bookmarks file can't be read. Hard-coded bookmarks will be used."; qDebug() << "Bookmarks file can't be read. Hard-coded bookmarks will be used.";
//Initialize with hard-coded values //Initialize with hard-coded values
bookmarks[MpcMinorPlanets].insert("MPC's list of bright minor planet bookmarks[MpcMinorPlanets].insert("MPC's list of bright minor planet
s at opposition", "http://www.minorplanetcenter.org/iau/Ephemerides/Bright/ s at opposition in 2011", "http://www.minorplanetcenter.net/iau/Ephemerides
2010/Soft00Bright.txt"); /Bright/2011/Soft00Bright.txt");
bookmarks[MpcMinorPlanets].insert("MPCORB: near-Earth asteroids (NEA bookmarks[MpcMinorPlanets].insert("MPCORB: near-Earth asteroids (NEA
s)", "http://www.minorplanetcenter.org/iau/MPCORB/NEA.txt"); s)", "http://www.minorplanetcenter.net/iau/MPCORB/NEA.txt");
bookmarks[MpcMinorPlanets].insert("MPCORB: potentially hazardous ast bookmarks[MpcMinorPlanets].insert("MPCORB: potentially hazardous ast
eroids (PHAs)", "http://www.minorplanetcenter.org/iau/MPCORB/PHA.txt"); eroids (PHAs)", "http://www.minorplanetcenter.net/iau/MPCORB/PHA.txt");
bookmarks[MpcComets].insert("MPC's list of observable comets", "http bookmarks[MpcComets].insert("MPC's list of observable comets", "http
://www.minorplanetcenter.org/iau/Ephemerides/Comets/Soft00Cmt.txt"); ://www.minorplanetcenter.net/iau/Ephemerides/Comets/Soft00Cmt.txt");
//Try to save them to a file //Try to save them to a file
saveBookmarks(); saveBookmarks();
} }
void MpcImportWindow::loadBookmarksGroup(QVariantMap source, Bookmarks & bo okmarkGroup) void MpcImportWindow::loadBookmarksGroup(QVariantMap source, Bookmarks & bo okmarkGroup)
{ {
if (source.isEmpty()) if (source.isEmpty())
return; return;
 End of changes. 28 change blocks. 
90 lines changed or deleted 125 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/