3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "ytcategories.h"
22 #include "networkaccess.h"
26 NetworkAccess* http();
29 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
31 void YTCategories::loadCategories(QString language) {
32 if (language.isEmpty())
33 language = QLocale::system().uiLanguages().first();
34 lastLanguage = language;
36 QString url = "http://gdata.youtube.com/schemas/2007/categories.cat?hl=" + language;
37 QObject *reply = The::http()->get(url);
38 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseCategories(QByteArray)));
39 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
42 void YTCategories::parseCategories(QByteArray bytes) {
43 QList<YTCategory> categories;
45 QXmlStreamReader xml(bytes);
46 while (!xml.atEnd()) {
48 if (xml.isStartElement() && xml.name() == QLatin1String("category")) {
49 QString term = xml.attributes().value("term").toString();
50 QString label = xml.attributes().value("label").toString();
51 while(xml.readNextStartElement())
52 if (xml.name() == QLatin1String("assignable")) {
55 category.label = label;
56 categories << category;
57 } else xml.skipCurrentElement();
62 emit error(xml.errorString());
66 emit categoriesLoaded(categories);
69 void YTCategories::requestError(QNetworkReply *reply) {
70 if (lastLanguage != "en") loadCategories("en");
71 else emit error(reply->errorString());