1 /***************************************************************************
2 * Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
22 #include "archiveorg.h"
24 #include <QPushButton>
26 #include <QListWidget>
27 #include <QDomDocument>
28 #include <QApplication>
31 #include "kdenlivesettings.h"
36 #include <qjson/parser.h>
39 ArchiveOrg::ArchiveOrg(QListWidget *listWidget, QObject *parent) :
40 AbstractService(listWidget, parent),
41 m_previewProcess(new QProcess)
43 serviceType = ARCHIVEORG;
46 inlineDownload = true;
47 //connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
50 ArchiveOrg::~ArchiveOrg()
52 delete m_previewProcess;
55 void ArchiveOrg::slotStartSearch(const QString &searchText, int page)
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");
66 KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
67 connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
71 void ArchiveOrg::slotShowResults(KJob* job)
73 if (job->error() != 0 ) return;
74 m_listWidget->blockSignals(true);
75 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
79 //kDebug()<<"// GOT RESULT: "<<m_result;
80 QVariant data = parser.parse(storedQueryJob->data(), &ok);
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") {
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();
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());
118 m_listWidget->blockSignals(false);
119 m_listWidget->setCurrentRow(0);
124 OnlineItemInfo ArchiveOrg::displayItemDetails(QListWidgetItem *item)
131 info.itemPreview = item->data(previewRole).toString();
132 info.itemDownload = item->data(downloadRole).toString();
133 info.itemId = item->data(idRole).toInt();
134 info.itemName = item->text();
135 info.infoUrl = item->data(infoUrl).toString();
136 info.author = item->data(authorRole).toString();
137 info.authorUrl = item->data(authorUrl).toString();
138 info.license = item->data(licenseRole).toString();
139 info.description = item->data(descriptionRole).toString();
141 m_metaInfo.insert("url", info.itemDownload);
142 m_metaInfo.insert("id", info.itemId);
144 QString extraInfoUrl = item->data(downloadRole).toString();
145 if (!extraInfoUrl.isEmpty()) {
146 KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
147 resolveJob->setProperty("id", info.itemId);
148 connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotParseResults(KJob*)) );
154 void ArchiveOrg::slotParseResults(KJob* job)
156 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
158 doc.setContent(QString::fromUtf8(storedQueryJob->data()));
159 QDomNodeList links = doc.elementsByTagName("a");
160 QString html = QString("<style type=\"text/css\">tr.cellone {background-color: %1;}").arg(qApp->palette().alternateBase().color().name());
161 html += "</style><table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">";
164 m_thumbsPath.clear();
165 for (int i = 0; i < links.count(); ++i) {
166 QString href = links.at(i).toElement().attribute("href");
167 if (href.endsWith(".thumbs/")) {
168 // sub folder contains image thumbs, display one.
169 m_thumbsPath = m_metaInfo.value("url") + '/' + href;
170 KJob* thumbJob = KIO::storedGet( KUrl(m_thumbsPath), KIO::NoReload, KIO::HideProgressInfo );
171 thumbJob->setProperty("id", m_metaInfo.value("id"));
172 connect( thumbJob, SIGNAL(result(KJob*)), this, SLOT(slotParseThumbs(KJob*)) );
174 else if (!href.contains('/') && !href.endsWith(".xml")) {
175 link = m_metaInfo.value("url") + '/' + href;
178 html += "<tr class=\"cellone\">";
181 html += "<td>" + KUrl(link).fileName() + QString("</td><td><a href=\"%1\">%2</a></td><td><a href=\"%3\">%4</a></td></tr>").arg(link).arg(i18n("Preview")).arg(link + "_import").arg(i18n("Import"));
185 if (m_metaInfo.value("id") == job->property("id").toString()) emit gotMetaInfo(html);
189 bool ArchiveOrg::startItemPreview(QListWidgetItem *item)
191 if (!item) return false;
192 QString url = item->data(previewRole).toString();
193 if (url.isEmpty()) return false;
194 if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
195 m_previewProcess->close();
197 m_previewProcess->start(KdenliveSettings::ffplaypath(), QStringList() << url << "-nodisp");
202 void ArchiveOrg::stopItemPreview(QListWidgetItem */*item*/)
204 if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
205 m_previewProcess->close();
209 QString ArchiveOrg::getExtension(QListWidgetItem *item)
211 if (!item) return QString();
212 return QString("*.") + item->text().section('.', -1);
216 QString ArchiveOrg::getDefaultDownloadName(QListWidgetItem *item)
218 if (!item) return QString();
222 void ArchiveOrg::slotParseThumbs(KJob* job)
224 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
226 doc.setContent(QString::fromUtf8(storedQueryJob->data()));
227 QDomNodeList links = doc.elementsByTagName("a");
228 if (links.isEmpty()) return;
229 for (int i = 0; i < links.count(); ++i) {
230 QString href = links.at(i).toElement().attribute("href");
231 if (!href.contains('/') && i >= links.count() / 2) {
232 QString thumbUrl = m_thumbsPath + href;
233 if (m_metaInfo.value("id") == job->property("id").toString())
234 emit gotThumb(thumbUrl);
240 #include "archiveorg.moc"