1 /*************************************************************************** 2 * Copyright (C) 2008 by Łukasz Jernaś * 3 * deejay1@srem.org * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License as published by * 7 * the Free Software Foundation; either version 2 of the License, or * 8 * (at your option) any later version. * 9 * * 10 * This program is distributed in the hope that it will be useful, * 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 * GNU General Public License for more details. * 14 * * 15 * You should have received a copy of the GNU General Public License * 16 * along with this program; if not, write to the * 17 * Free Software Foundation, Inc., * 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 ***************************************************************************/ 20 #include "httpquery.h" 21 #include <QtDebug> 22 #include "Global.h" 23 24 #include "MerkaartorPreferences.h" 25 26 namespace NameFinder 27 { 28 HttpQuery(QObject * parent)29 HttpQuery::HttpQuery ( QObject *parent ) : QObject ( parent ) 30 { 31 myService = M_PREFS->getNominatimUrl(); 32 } HttpQuery(QObject * parent,QUrl service)33 HttpQuery::HttpQuery ( QObject *parent, QUrl service ) : QObject ( parent ) 34 { 35 myService = service; 36 } 37 38 ~HttpQuery()39 HttpQuery::~HttpQuery() 40 { 41 } 42 startSearch(QString question)43 bool HttpQuery::startSearch ( QString question ) 44 { 45 connect(&connection,SIGNAL(finished(QNetworkReply*)),this,SLOT(on_requestFinished(QNetworkReply*))); 46 47 #ifdef QT5 48 QUrlQuery theQuery(myService); 49 #define theQuery theQuery 50 #else 51 #define theQuery myService 52 #endif 53 theQuery.addQueryItem ( "q",question ); 54 theQuery.addQueryItem ( "format","xml" ); 55 #ifdef QT5 56 myService.setQuery(theQuery); 57 #endif 58 #undef theQuery 59 60 QUrl url("http://"+myService.host()); 61 url.setPath(myService.path()); 62 #ifdef QT5 63 url.setQuery(myService.query()); 64 #else 65 url.setEncodedQuery(myService.encodedQuery()); 66 #endif 67 68 qDebug() << "HttpQuery: getting" << url; 69 QNetworkRequest req(url); 70 71 req.setRawHeader(QByteArray("User-Agent"), USER_AGENT.toLatin1()); 72 73 connection.setProxy(M_PREFS->getProxy(myService)); 74 75 myReply = connection.get( req ); 76 return true; 77 } 78 on_requestFinished(QNetworkReply * reply)79 void HttpQuery::on_requestFinished ( QNetworkReply *reply ) 80 { 81 if (reply == myReply) { 82 if (!reply->error()) { 83 qDebug() << "HttpQuery: request completed without error."; 84 emit done(reply); 85 } else { 86 qDebug() << "HttpQuery: request returned with error" << reply->error() << ":" << reply->errorString(); 87 emit doneWithError(reply->error()); 88 } 89 } else { 90 qDebug() << "HttpQuery: reply received to unknown request"; 91 } 92 } 93 } 94