]> git.sur5r.net Git - minitube/blob - src/ytcategories.cpp
Imported Upstream version 2.1.3
[minitube] / src / ytcategories.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "ytcategories.h"
22 #include "networkaccess.h"
23 #include <QtXml>
24
25 namespace The {
26 NetworkAccess* http();
27 }
28
29 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
30
31 void YTCategories::loadCategories(QString language) {
32     if (language.isEmpty())
33         language = QLocale::system().uiLanguages().first();
34     lastLanguage = language;
35
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*)));
40 }
41
42 void YTCategories::parseCategories(QByteArray bytes) {
43     QList<YTCategory> categories;
44
45     QXmlStreamReader xml(bytes);
46     while (!xml.atEnd()) {
47         xml.readNext();
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")) {
53                     YTCategory category;
54                     category.term = term;
55                     category.label = label;
56                     categories << category;
57                 } else xml.skipCurrentElement();
58         }
59     }
60
61     if (xml.hasError()) {
62         emit error(xml.errorString());
63         return;
64     }
65
66     emit categoriesLoaded(categories);
67 }
68
69 void YTCategories::requestError(QNetworkReply *reply) {
70     if (lastLanguage != "en") loadCategories("en");
71     else emit error(reply->errorString());
72 }