]> git.sesse.net Git - kdenlive/blob - src/utils/archiveorg.cpp
Merge branch 'master' of git://anongit.kde.org/kdenlive
[kdenlive] / src / utils / archiveorg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21
22 #include "archiveorg.h"
23
24 #include <QPushButton>
25 #include <QSpinBox>
26 #include <QListWidget>
27 #include <QDomDocument>
28 #include <QApplication>
29
30 #include <KDebug>
31 #include "kdenlivesettings.h"
32 #include <kio/job.h>
33 #include <KLocale>
34
35 #ifdef USE_QJSON
36 #include <qjson/parser.h>
37 #endif
38
39 ArchiveOrg::ArchiveOrg(QListWidget *listWidget, QObject *parent) :
40         AbstractService(listWidget, parent),
41         m_previewProcess(new QProcess)
42 {
43     serviceType = ARCHIVEORG;
44     hasPreview = false;
45     hasMetadata = true;
46     inlineDownload = true;
47     //connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
48 }
49
50 ArchiveOrg::~ArchiveOrg()
51 {
52     if (m_previewProcess) delete m_previewProcess;
53 }
54
55 void ArchiveOrg::slotStartSearch(const QString searchText, int page)
56 {
57     m_listWidget->clear();
58     QString uri = "http://www.archive.org/advancedsearch.php?q=";
59     uri.append(searchText);
60     uri.append("%20AND%20mediatype:movies");//MovingImage");
61     uri.append("&fl%5B%5D=creator&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=licenseurl&fl%5B%5D=title");
62     uri.append("&rows=30");
63     if (page > 1) uri.append("&page=" + QString::number(page));
64     uri.append("&output=json"); //&callback=callback&save=yes#raw");
65
66     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
67     connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotShowResults( KJob* ) ) );
68 }
69
70
71 void ArchiveOrg::slotShowResults(KJob* job)
72 {
73     if (job->error() != 0 ) return;
74     m_listWidget->blockSignals(true);
75     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
76 #ifdef USE_QJSON
77     QJson::Parser parser;
78     bool ok;
79     //kDebug()<<"// GOT RESULT: "<<m_result;
80     QVariant data = parser.parse(storedQueryJob->data(), &ok);
81     QVariant sounds;
82     if (data.canConvert(QVariant::Map)) {
83         QMap <QString, QVariant> map = data.toMap();
84         QMap<QString, QVariant>::const_iterator i = map.constBegin();
85         while (i != map.constEnd()) {
86             if (i.key() == "response") {
87                 sounds = i.value();
88                 if (sounds.canConvert(QVariant::Map)) {
89                     QMap <QString, QVariant> soundsList = sounds.toMap();
90                     if (soundsList.contains("numFound")) emit searchInfo(i18np("Found %1 result", "Found %1 results", soundsList.value("numFound").toInt()));
91                     QList <QVariant> resultsList;
92                     if (soundsList.contains("docs")) {
93                         resultsList = soundsList.value("docs").toList();
94                     }
95                     
96                     for (int j = 0; j < resultsList.count(); j++) {
97                         if (resultsList.at(j).canConvert(QVariant::Map)) {
98                             QMap <QString, QVariant> soundmap = resultsList.at(j).toMap();
99                             if (soundmap.contains("title")) {
100                                 QListWidgetItem *item = new   QListWidgetItem(soundmap.value("title").toString(), m_listWidget);
101                                 item->setData(descriptionRole, soundmap.value("description").toString());
102                                 item->setData(idRole, soundmap.value("identifier").toString());
103                                 QString author = soundmap.value("creator").toString();
104                                 item->setData(authorRole, author);
105                                 if (author.startsWith("http")) item->setData(authorUrl, author);
106                                 item->setData(infoUrl, "http://archive.org/details/" + soundmap.value("identifier").toString());
107                                 item->setData(downloadRole, "http://archive.org/download/" + soundmap.value("identifier").toString());
108                                 item->setData(licenseRole, soundmap.value("licenseurl").toString());                        
109                             }
110                         }
111                     }
112                 }
113             }
114             ++i;
115         }
116     }
117 #endif  
118     m_listWidget->blockSignals(false);
119     m_listWidget->setCurrentRow(0);
120 }
121     
122
123 OnlineItemInfo ArchiveOrg::displayItemDetails(QListWidgetItem *item)
124 {
125     OnlineItemInfo info;
126     m_metaInfo.clear();
127     if (!item) {
128         return info;
129     }
130     info.itemPreview = item->data(previewRole).toString();
131     info.itemDownload = item->data(downloadRole).toString();
132     info.itemId = item->data(idRole).toInt();
133     info.itemName = item->text();
134     info.infoUrl = item->data(infoUrl).toString();
135     info.author = item->data(authorRole).toString();
136     info.authorUrl = item->data(authorUrl).toString();
137     info.license = item->data(licenseRole).toString();
138     info.description = item->data(descriptionRole).toString();
139     
140     m_metaInfo.insert("url", info.itemDownload);
141     
142     QString extraInfoUrl = item->data(downloadRole).toString();
143     if (!extraInfoUrl.isEmpty()) {
144         KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
145         connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotParseResults( KJob* ) ) );
146     }
147     return info;
148 }
149
150
151 void ArchiveOrg::slotParseResults(KJob* job)
152 {
153     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
154     QDomDocument doc;
155     doc.setContent(storedQueryJob->data());
156     QDomNodeList links = doc.elementsByTagName("a");
157     QString html = QString("<style type=\"text/css\">tr.cellone {background-color: %1;}").arg(qApp->palette().alternateBase().color().name());
158     html += "</style><table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">";
159     QString link;
160     int ct = 0;
161     m_thumbsPath.clear();
162     for (int i = 0; i < links.count(); i++) {
163         QString href = links.at(i).toElement().attribute("href");
164         if (href.endsWith(".thumbs/")) {
165             // sub folder contains image thumbs, display one.
166             m_thumbsPath = m_metaInfo.value("url") + "/" + href;
167             KJob* thumbJob = KIO::storedGet( KUrl(m_thumbsPath), KIO::NoReload, KIO::HideProgressInfo );
168             connect( thumbJob, SIGNAL( result( KJob* ) ), this, SLOT( slotParseThumbs( KJob* ) ) );
169         }
170         else if (!href.contains('/') && !href.endsWith(".xml")) {
171             link = m_metaInfo.value("url") + "/" + href;
172             ct++;
173             if (ct %2 == 0) {
174                 html += "<tr class=\"cellone\">";
175             }
176             else html += "<tr>";
177             html += "<td>" + KUrl(link).fileName() + QString("</td><td><a href=\"%1\">preview</a></td><td><a href=\"%2\">download</a></td></tr>").arg(link + "_preview").arg(link);
178         }
179     }
180     html += "</table>";
181     emit gotMetaInfo(html);
182 }
183
184
185 bool ArchiveOrg::startItemPreview(QListWidgetItem *item)
186 {    
187     if (!item) return false;
188     QString url = item->data(previewRole).toString();
189     if (url.isEmpty()) return false;
190     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
191         m_previewProcess->close();
192     }
193     m_previewProcess->start("ffplay", QStringList() << url << "-nodisp");
194     return true;
195 }
196
197
198 void ArchiveOrg::stopItemPreview(QListWidgetItem *item)
199 {    
200     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
201         m_previewProcess->close();
202     }
203 }
204
205 QString ArchiveOrg::getExtension(QListWidgetItem *item)
206 {
207     if (!item) return QString();
208     return QString("*.") + item->text().section('.', -1);
209 }
210
211
212 QString ArchiveOrg::getDefaultDownloadName(QListWidgetItem *item)
213 {
214     if (!item) return QString();
215     return item->text();
216 }
217
218 void ArchiveOrg::slotParseThumbs(KJob* job)
219 {
220     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
221     QDomDocument doc;
222     doc.setContent(storedQueryJob->data());
223     QDomNodeList links = doc.elementsByTagName("a");
224     if (links.isEmpty()) return;
225     for (int i = 0; i < links.count(); i++) {
226         QString href = links.at(i).toElement().attribute("href");
227         if (!href.contains('/') && i >= links.count() / 2) {
228             QString thumbUrl = m_thumbsPath + href;
229             emit gotThumb(thumbUrl);
230             break;
231         }
232     }
233 }