1 #include "ytcategories.h"
2 #include "networkaccess.h"
9 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
11 void YTCategories::loadCategories(QString language) {
12 if (language.isEmpty())
13 language = QLocale::system().uiLanguages().first();
14 lastLanguage = language;
16 QString url = "http://gdata.youtube.com/schemas/2007/categories.cat?hl=" + language;
17 QObject *reply = The::http()->get(url);
18 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseCategories(QByteArray)));
19 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
22 void YTCategories::parseCategories(QByteArray bytes) {
23 QList<YTCategory> categories;
25 QXmlStreamReader xml(bytes);
26 while (!xml.atEnd()) {
28 if (xml.isStartElement() && xml.name() == QLatin1String("category")) {
29 QString term = xml.attributes().value("term").toString();
30 QString label = xml.attributes().value("label").toString();
31 while(xml.readNextStartElement())
32 if (xml.name() == QLatin1String("assignable")) {
35 category.label = label;
36 categories << category;
37 } else xml.skipCurrentElement();
42 emit error(xml.errorString());
46 emit categoriesLoaded(categories);
49 void YTCategories::requestError(QNetworkReply *reply) {
50 if (lastLanguage != "en") loadCategories("en");
51 else emit error(reply->errorString());