]> git.sesse.net Git - kdenlive/blob - src/utils/freesound.cpp
962984f52a218de1cace9a9dbfb9d8ec160b98bf
[kdenlive] / src / utils / freesound.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 "freesound.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 FreeSound::FreeSound(QListWidget *listWidget, QObject *parent) :
40         AbstractService(listWidget, parent),
41         m_previewProcess(new QProcess)
42 {
43     serviceType = FREESOUND;
44     hasPreview = true;
45     hasMetadata = true;
46 }
47
48 FreeSound::~FreeSound()
49 {
50     delete m_previewProcess;
51 }
52
53 void FreeSound::slotStartSearch(const QString &searchText, int page)
54 {
55     m_listWidget->clear();
56     QString uri = "http://www.freesound.org/api/sounds/search/?q=";
57     uri.append(searchText);
58     if (page > 1)
59         uri.append("&p=" + QString::number(page));
60     uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
61
62     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
63     connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
64 }
65
66
67 void FreeSound::slotShowResults(KJob* job)
68 {
69     if (job->error() != 0 ) return;
70     m_listWidget->blockSignals(true);
71     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
72 #ifdef USE_QJSON
73     QJson::Parser parser;
74     bool ok;
75     //kDebug()<<"// GOT RESULT: "<<m_result;
76     QVariant data = parser.parse(storedQueryJob->data(), &ok);
77     QVariant sounds;
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());
85             }
86             else if (i.key() == "sounds") {
87                 sounds = i.value();
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());
108                                     }
109                                 }
110                             }
111                         }
112                     }
113                 }
114             }
115             ++i;
116         }
117     }
118 #endif  
119     m_listWidget->blockSignals(false);
120     m_listWidget->setCurrentRow(0);
121     emit searchDone();
122 }
123     
124
125 OnlineItemInfo FreeSound::displayItemDetails(QListWidgetItem *item)
126 {
127     OnlineItemInfo info;
128     m_metaInfo.clear();
129     if (!item) {
130         return info;
131     }
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();
142     
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*)) );
147     }
148     emit gotThumb(item->data(imageRole).toString());
149     return info;
150 }
151
152
153 void FreeSound::slotParseResults(KJob* job)
154 {
155 #ifdef USE_QJSON
156     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
157     QJson::Parser parser;
158     bool ok;
159     QString html = QString("<style type=\"text/css\">tr.cellone {background-color: %1;}</style>").arg(qApp->palette().alternateBase().color().name());
160     
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;
165         
166         html += "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">";
167     
168         if (m_metaInfo.contains(i18n("Duration"))) {
169             html += "<tr>";
170             html += "<td>" + i18n("Duration") + "</td><td>" + m_metaInfo.value(i18n("Duration")) + "</td></tr>";
171             m_metaInfo.remove(i18n("Duration"));
172         }
173         
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>";
177         }
178         if (infos.contains("channels")) {
179             html += "<tr>";
180             html += "<td>" + i18n("Channels") + "</td><td>" + QString::number(infos.value("channels").toInt()) + "</td></tr>";
181         }
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>";
186         }
187         if (infos.contains("license")) {
188             m_metaInfo.insert("license", infos.value("license").toString());
189         }
190         html +="</table>";
191         if (infos.contains("description")) {
192             m_metaInfo.insert("description", infos.value("description").toString());
193         }
194     }
195     emit gotMetaInfo(html);
196     emit gotMetaInfo(m_metaInfo);
197 #endif    
198 }
199
200
201 bool FreeSound::startItemPreview(QListWidgetItem *item)
202 {    
203     if (!item)
204         return false;
205     const QString url = item->data(previewRole).toString();
206     if (url.isEmpty())
207         return false;
208     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
209         m_previewProcess->close();
210     }
211     m_previewProcess->start(KdenliveSettings::ffplaypath(), QStringList() << url << "-nodisp");
212     return true;
213 }
214
215
216 void FreeSound::stopItemPreview(QListWidgetItem */*item*/)
217 {    
218     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
219         m_previewProcess->close();
220     }
221 }
222
223 QString FreeSound::getExtension(QListWidgetItem *item)
224 {
225     if (!item)
226         return QString();
227     return QString("*.") + item->text().section('.', -1);
228 }
229
230
231 QString FreeSound::getDefaultDownloadName(QListWidgetItem *item)
232 {
233     if (!item)
234         return QString();
235     return item->text();
236 }
237
238 #include "freesound.moc"