]> git.sur5r.net Git - minitube/blob - src/youtubesuggest.cpp
Imported Upstream version 1.4.1
[minitube] / src / youtubesuggest.cpp
1 #include "youtubesuggest.h"
2 #include <QtXml>
3 #include "networkaccess.h"
4
5 #define GSUGGEST_URL "http://suggestqueries.google.com/complete/search?ds=yt&output=toolbar&hl=%1&q=%2"
6
7 namespace The {
8     NetworkAccess* http();
9 }
10
11 YouTubeSuggest::YouTubeSuggest(QObject *parent) : Suggester() {
12
13 }
14
15 void YouTubeSuggest::suggest(QString query) {
16     QString locale = QLocale::system().name().replace("_", "-");
17     // case for system locales such as "C"
18     if (locale.length() < 2) {
19         locale = "en-US";
20     }
21
22     QString url = QString(GSUGGEST_URL).arg(locale, query);
23
24     QObject *reply = The::http()->get(url);
25     connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
26 }
27
28 void YouTubeSuggest::handleNetworkData(QByteArray response) {
29     QStringList choices;
30
31     QXmlStreamReader xml(response);
32     while (!xml.atEnd()) {
33         xml.readNext();
34         if (xml.tokenType() == QXmlStreamReader::StartElement) {
35             if (xml.name() == "suggestion") {
36                 QStringRef str = xml.attributes().value("data");
37                 choices << str.toString();
38             }
39         }
40     }
41     emit ready(choices);
42 }