]> git.sur5r.net Git - minitube/blob - src/youtubesearch.cpp
Keyboard shortcuts for the sortBar
[minitube] / src / youtubesearch.cpp
1 #include "youtubesearch.h"
2 #include "youtubestreamreader.h"
3 #include "Constants.h"
4 #include "networkaccess.h"
5
6 namespace The {
7     NetworkAccess* http();
8 }
9
10 YouTubeSearch::YouTubeSearch() : QObject() {}
11
12 void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
13     this->abortFlag = false;
14
15     QString urlString = QString(
16             "http://gdata.youtube.com/feeds/api/videos?q=%1&max-results=%2&start-index=%3")
17             .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
18
19     // Useful to test with a local webserver
20     urlString = QString("http://localhost/oringo/video.xml?q=%1&max-results=%2&start-index=%3")
21                 .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
22
23     switch (searchParams->sortBy()) {
24     case SearchParams::SortByNewest:
25         urlString.append("&orderby=published");
26         break;
27     case SearchParams::SortByViewCount:
28         urlString.append("&orderby=viewCount");
29         break;
30     }
31
32     QUrl url(urlString);
33
34     QObject *reply = The::http()->get(url);
35     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
36     connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(error(QNetworkReply*)));
37
38 }
39
40 void YouTubeSearch::error(QNetworkReply *reply) {
41     emit error(reply->errorString());
42 }
43
44 void YouTubeSearch::parseResults(QByteArray data) {
45
46     YouTubeStreamReader reader;
47     if (!reader.read(data)) {
48         qDebug() << "Error parsing XML";
49     }
50     videos = reader.getVideos();
51
52     foreach (Video *video, videos) {
53         // send it to the model
54         emit gotVideo(video);
55     }
56
57     foreach (Video *video, videos) {
58         // preload the thumb
59         if (abortFlag) return;
60         video->preloadThumbnail();
61     }
62
63     emit finished(videos.size());
64 }
65
66 QList<Video*> YouTubeSearch::getResults() {
67     return videos;
68 }
69
70 void YouTubeSearch::abort() {
71     this->abortFlag = true;
72 }