]> git.sur5r.net Git - minitube/blob - src/playlistsuggest.cpp
Imported Upstream version 2.0
[minitube] / src / playlistsuggest.cpp
1 #include "playlistsuggest.h"
2 #include <QtXml>
3 #include "networkaccess.h"
4
5 namespace The {
6 NetworkAccess* http();
7 }
8
9 struct Playlist {
10     QString id;
11     QString title;
12     QString summary;
13     QString author;
14     int videoCount;
15 };
16
17 PlaylistSuggest::PlaylistSuggest(QObject *parent) : Suggester() {
18
19 }
20
21 void PlaylistSuggest::suggest(QString query) {
22     QUrl url("http://gdata.youtube.com/feeds/api/playlists/snippets");
23     url.addQueryItem("v", "2");
24     url.addQueryItem("q", query);
25     QObject *reply = The::http()->get(url);
26     connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
27 }
28
29 void PlaylistSuggest::handleNetworkData(QByteArray data) {
30     QList<Playlist> playlists;
31
32     QXmlStreamReader xml(data);
33     while (!xml.atEnd()) {
34         xml.readNext();
35         if (xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == "entry") {
36
37             Playlist playlist = {};
38
39             while (xml.readNextStartElement()) {
40                 if (xml.name() == "title") {
41                     playlist.title = xml.readElementText();
42                 }
43                 else if (xml.name() == "summary") {
44                     playlist.summary = xml.readElementText();
45                 }
46                 else if (xml.name() == "author") {
47                     while (xml.readNextStartElement()) {
48                         if (xml.name() == "name") {
49                             playlist.author = xml.readElementText();
50                             break;
51                         }
52                     }
53                 }
54                 else if (xml.name() == "playlistId") {
55                     playlist.id = xml.readElementText();
56                 }
57                 else if (xml.name() == "countHint") {
58                     playlist.videoCount = xml.readElementText().toInt();
59                 }
60             }
61
62             playlists << playlist;
63
64         }
65     }
66
67     // emit ready(choices);
68 }
69
70 /* model */
71
72 class PlaylistSuggestModel : public QAbstractListModel {
73
74     Q_OBJECT
75
76 public:
77     PlaylistSuggestModel(QWidget *parent) : QAbstractListModel(parent) { }
78     int rowCount(const QModelIndex &parent = QModelIndex()) const {
79         Q_UNUSED(parent);
80         return list.size();
81     }
82     int columnCount( const QModelIndex& parent = QModelIndex() ) const {
83         Q_UNUSED(parent);
84         return 1;
85     }
86     QVariant data(const QModelIndex &index, int role) const {
87         Q_UNUSED(index);
88         Q_UNUSED(role);
89         return QVariant();
90     }
91     QList<Playlist> list;
92
93 };
94
95 /* delegate */
96
97 class PlaylistSuggestDelegate : public QStyledItemDelegate {
98
99     Q_OBJECT
100
101 public:
102     PlaylistSuggestDelegate(QObject* parent);
103     QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const {
104         return QSize(0, 100);
105     }
106     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
107         QStyleOptionViewItemV4 opt = QStyleOptionViewItemV4(option);
108         initStyleOption(&opt, index);
109         opt.text = "";
110         opt.widget->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);
111
112         painter->save();
113         painter->translate(option.rect.topLeft());
114
115         QRect line(0, 0, option.rect.width(), option.rect.height());
116
117         painter->restore();
118
119     }
120
121 };