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 "freesound.h"
24 #include <QPushButton>
26 #include <QListWidget>
27 #include <QDomDocument>
28 #include <QApplication>
31 #include "kdenlivesettings.h"
33 #include <KLocalizedString>
36 #include <qjson/parser.h>
39 FreeSound::FreeSound(QListWidget *listWidget, QObject *parent) :
40 AbstractService(listWidget, parent),
41 m_previewProcess(new QProcess)
43 serviceType = FREESOUND;
48 FreeSound::~FreeSound()
50 delete m_previewProcess;
53 void FreeSound::slotStartSearch(const QString &searchText, int page)
55 m_listWidget->clear();
56 QString uri = "http://www.freesound.org/api/sounds/search/?q=";
57 uri.append(searchText);
59 uri.append("&p=" + QString::number(page));
60 uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
62 KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
63 connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
67 void FreeSound::slotShowResults(KJob* job)
69 if (job->error() != 0 ) return;
70 m_listWidget->blockSignals(true);
71 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
75 //kDebug()<<"// GOT RESULT: "<<m_result;
76 QVariant data = parser.parse(storedQueryJob->data(), &ok);
78 if (data.canConvert(QVariant::Map)) {
79 QMap <QString, QVariant> map = data.toMap();
80 QMap<QString, QVariant>::const_iterator i = map.constBegin();
81 while (i != map.constEnd()) {
82 if (i.key() == "num_results") emit searchInfo(i18np("Found %1 result", "Found %1 results", i.value().toInt()));
83 else if (i.key() == "num_pages") {
84 emit maxPages(i.value().toInt());
86 else if (i.key() == "sounds") {
88 if (sounds.canConvert(QVariant::List)) {
89 QList <QVariant> soundsList = sounds.toList();
90 for (int j = 0; j < soundsList.count(); j++) {
91 if (soundsList.at(j).canConvert(QVariant::Map)) {
92 QMap <QString, QVariant> soundmap = soundsList.at(j).toMap();
93 if (soundmap.contains("original_filename")) {
94 QListWidgetItem *item = new QListWidgetItem(soundmap.value("original_filename").toString(), m_listWidget);
95 item->setData(imageRole, soundmap.value("waveform_m").toString());
96 item->setData(infoUrl, soundmap.value("url").toString());
97 item->setData(infoData, soundmap.value("ref").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
98 item->setData(durationRole, soundmap.value("duration").toDouble());
99 item->setData(idRole, soundmap.value("id").toInt());
100 item->setData(previewRole, soundmap.value("preview-hq-mp3").toString());
101 item->setData(downloadRole, soundmap.value("serve").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
102 QVariant authorInfo = soundmap.value("user");
103 if (authorInfo.canConvert(QVariant::Map)) {
104 QMap <QString, QVariant> authorMap = authorInfo.toMap();
105 if (authorMap.contains("username")) {
106 item->setData(authorRole, authorMap.value("username").toString());
107 item->setData(authorUrl, authorMap.value("url").toString());
119 m_listWidget->blockSignals(false);
120 m_listWidget->setCurrentRow(0);
125 OnlineItemInfo FreeSound::displayItemDetails(QListWidgetItem *item)
132 info.itemPreview = item->data(previewRole).toString();
133 info.itemDownload = item->data(downloadRole).toString();
134 info.itemId = item->data(idRole).toInt();
135 info.itemName = item->text();
136 info.infoUrl = item->data(infoUrl).toString();
137 info.author = item->data(authorRole).toString();
138 info.authorUrl = item->data(authorUrl).toString();
139 m_metaInfo.insert(i18n("Duration"), item->data(durationRole).toString());
140 info.license = item->data(licenseRole).toString();
141 info.description = item->data(descriptionRole).toString();
143 QString extraInfoUrl = item->data(infoData).toString();
144 if (!extraInfoUrl.isEmpty()) {
145 KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
146 connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotParseResults(KJob*)) );
148 emit gotThumb(item->data(imageRole).toString());
153 void FreeSound::slotParseResults(KJob* job)
156 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
157 QJson::Parser parser;
159 QString html = QString("<style type=\"text/css\">tr.cellone {background-color: %1;}</style>").arg(qApp->palette().alternateBase().color().name());
161 QVariant data = parser.parse(storedQueryJob->data(), &ok);
162 if (data.canConvert(QVariant::Map)) {
163 QMap <QString, QVariant> infos = data.toMap();
164 //if (m_currentId != infos.value("id").toInt()) return;
166 html += "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">";
168 if (m_metaInfo.contains(i18n("Duration"))) {
170 html += "<td>" + i18n("Duration") + "</td><td>" + m_metaInfo.value(i18n("Duration")) + "</td></tr>";
171 m_metaInfo.remove(i18n("Duration"));
174 if (infos.contains("samplerate")) {
175 html += "<tr class=\"cellone\">";
176 html += "<td>" + i18n("Samplerate") + "</td><td>" + QString::number(infos.value("samplerate").toDouble()) + "</td></tr>";
178 if (infos.contains("channels")) {
180 html += "<td>" + i18n("Channels") + "</td><td>" + QString::number(infos.value("channels").toInt()) + "</td></tr>";
182 if (infos.contains("filesize")) {
183 html += "<tr class=\"cellone\">";
184 KIO::filesize_t fSize = infos.value("filesize").toDouble();
185 html += "<td>" + i18n("File size") + "</td><td>" + KIO::convertSize(fSize) + "</td></tr>";
187 if (infos.contains("license")) {
188 m_metaInfo.insert("license", infos.value("license").toString());
191 if (infos.contains("description")) {
192 m_metaInfo.insert("description", infos.value("description").toString());
195 emit gotMetaInfo(html);
196 emit gotMetaInfo(m_metaInfo);
201 bool FreeSound::startItemPreview(QListWidgetItem *item)
205 const QString url = item->data(previewRole).toString();
208 if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
209 m_previewProcess->close();
211 m_previewProcess->start(KdenliveSettings::ffplaypath(), QStringList() << url << "-nodisp");
216 void FreeSound::stopItemPreview(QListWidgetItem */*item*/)
218 if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
219 m_previewProcess->close();
223 QString FreeSound::getExtension(QListWidgetItem *item)
227 return QString("*.") + item->text().section('.', -1);
231 QString FreeSound::getDefaultDownloadName(QListWidgetItem *item)
238 #include "freesound.moc"