]> git.sesse.net Git - kdenlive/blob - src/utils/freesound.cpp
cb4459344afb302ff49360df5f534c3244ce8856
[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
29 #include <KDebug>
30 #include "kdenlivesettings.h"
31 #include <KGlobalSettings>
32 #include <KMessageBox>
33 #include <KFileDialog>
34 #include <kio/job.h>
35 #include <KIO/NetAccess>
36 #include <Solid/Networking>
37 #include <KRun>
38
39 #ifdef USE_NEPOMUK
40 #include <Nepomuk/Variant>
41 #include <Nepomuk/Resource>
42 #include <Nepomuk/ResourceManager>
43 #include <Soprano/Vocabulary/NAO>
44 #include <Nepomuk/Vocabulary/NIE>
45 #include <Nepomuk/Vocabulary/NDO>
46 #endif
47
48 #ifdef USE_QJSON
49 #include <qjson/parser.h>
50 #endif
51
52 FreeSound::FreeSound(QListWidget *listWidget, QObject *parent) :
53         AbstractService(listWidget, parent),
54         m_previewProcess(new QProcess)
55 {
56     serviceType = FREESOUND;
57     hasPreview = true;
58     hasMetadata = true;
59     //connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
60 }
61
62 FreeSound::~FreeSound()
63 {
64     if (m_previewProcess) delete m_previewProcess;
65 }
66
67 void FreeSound::slotStartSearch(const QString searchText, int page)
68 {
69     m_listWidget->clear();
70     QString uri = "http://www.freesound.org/api/sounds/search/?q=";
71     uri.append(searchText);
72     if (page > 1) uri.append("&p=" + QString::number(page));
73     uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
74
75     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
76     connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotShowResults( KJob* ) ) );
77 }
78
79
80 void FreeSound::slotShowResults(KJob* job)
81 {
82     if (job->error() != 0 ) return;
83     m_listWidget->blockSignals(true);
84     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
85 #ifdef USE_QJSON
86     QJson::Parser parser;
87     bool ok;
88     //kDebug()<<"// GOT RESULT: "<<m_result;
89     QVariant data = parser.parse(storedQueryJob->data(), &ok);
90     QVariant sounds;
91     if (data.canConvert(QVariant::Map)) {
92         QMap <QString, QVariant> map = data.toMap();
93         QMap<QString, QVariant>::const_iterator i = map.constBegin();
94         while (i != map.constEnd()) {
95             if (i.key() == "num_results") emit searchInfo(i18np("Found %1 result", "Found %1 results", i.value().toInt()));
96             else if (i.key() == "num_pages") {
97                 emit maxPages(i.value().toInt());
98             }
99             else if (i.key() == "sounds") {
100                 sounds = i.value();
101                 if (sounds.canConvert(QVariant::List)) {
102                     QList <QVariant> soundsList = sounds.toList();
103                     for (int j = 0; j < soundsList.count(); j++) {
104                         if (soundsList.at(j).canConvert(QVariant::Map)) {
105                             QMap <QString, QVariant> soundmap = soundsList.at(j).toMap();
106                             if (soundmap.contains("original_filename")) {
107                                 QListWidgetItem *item = new   QListWidgetItem(soundmap.value("original_filename").toString(), m_listWidget);
108                                 item->setData(imageRole, soundmap.value("waveform_m").toString());
109                                 item->setData(infoUrl, soundmap.value("url").toString());
110                                 item->setData(infoData, soundmap.value("ref").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
111                                 item->setData(durationRole, soundmap.value("duration").toDouble());
112                                 item->setData(idRole, soundmap.value("id").toInt());
113                                 item->setData(previewRole, soundmap.value("preview-hq-mp3").toString());
114                                 item->setData(downloadRole, soundmap.value("serve").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
115                                 QVariant authorInfo = soundmap.value("user");
116                                 if (authorInfo.canConvert(QVariant::Map)) {
117                                     QMap <QString, QVariant> authorMap = authorInfo.toMap();
118                                     if (authorMap.contains("username")) {
119                                         item->setData(authorRole, authorMap.value("username").toString());
120                                         item->setData(authorUrl, authorMap.value("url").toString());
121                                     }
122                                 }
123                             }
124                         }
125                     }
126                 }
127             }
128             ++i;
129         }
130     }
131 #endif  
132     m_listWidget->blockSignals(false);
133     m_listWidget->setCurrentRow(0);
134 }
135     
136
137 OnlineItemInfo FreeSound::displayItemDetails(QListWidgetItem *item)
138 {
139     OnlineItemInfo info;
140     m_metaInfo.clear();
141     if (!item) {
142         return info;
143     }
144     info.itemPreview = item->data(previewRole).toString();
145     info.itemDownload = item->data(downloadRole).toString();
146     info.itemId = item->data(idRole).toInt();
147     info.itemName = item->text();
148     info.infoUrl = item->data(infoUrl).toString();
149     info.author = item->data(authorRole).toString();
150     info.authorUrl = item->data(authorUrl).toString();
151     m_metaInfo.insert(i18n("Duration"), item->data(durationRole).toString());
152     info.license = item->data(licenseRole).toString();
153     info.description = item->data(descriptionRole).toString();
154     
155     QString extraInfoUrl = item->data(infoData).toString();
156     if (!extraInfoUrl.isEmpty()) {
157         KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
158         connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotParseResults( KJob* ) ) );
159     }
160     info.imagePreview = item->data(imageRole).toString();
161     return info;
162 }
163
164
165 void FreeSound::slotParseResults(KJob* job)
166 {
167 #ifdef USE_QJSON
168     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
169     QJson::Parser parser;
170     bool ok;
171     QVariant data = parser.parse(storedQueryJob->data(), &ok);
172     if (data.canConvert(QVariant::Map)) {
173         QMap <QString, QVariant> infos = data.toMap();
174         //if (m_currentId != infos.value("id").toInt()) return;
175         if (infos.contains("samplerate"))
176             m_metaInfo.insert(i18n("Samplerate"), QString::number(infos.value("samplerate").toDouble()));
177         if (infos.contains("channels"))
178             m_metaInfo.insert(i18n("Channels"), QString::number(infos.value("channels").toInt()));
179         if (infos.contains("filesize")) {
180             KIO::filesize_t fSize = infos.value("filesize").toDouble();
181             m_metaInfo.insert(i18n("File size"), KIO::convertSize(fSize));
182         }
183         if (infos.contains("description")) {
184             m_metaInfo.insert("description", infos.value("description").toString());
185         }
186         if (infos.contains("license")) {
187             m_metaInfo.insert("license", infos.value("license").toString());
188         }
189     }
190     emit gotMetaInfo(m_metaInfo);
191 #endif    
192 }
193
194
195 bool FreeSound::startItemPreview(QListWidgetItem *item)
196 {    
197     if (!item) return false;
198     QString url = item->data(previewRole).toString();
199     if (url.isEmpty()) return false;
200     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
201         m_previewProcess->close();
202     }
203     m_previewProcess->start("ffplay", QStringList() << url << "-nodisp");
204     return true;
205 }
206
207
208 void FreeSound::stopItemPreview(QListWidgetItem *item)
209 {    
210     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
211         m_previewProcess->close();
212     }
213 }
214
215 QString FreeSound::getExtension(QListWidgetItem *item)
216 {
217     if (!item) return QString();
218     return QString("*.") + item->text().section('.', -1);
219 }
220
221
222 QString FreeSound::getDefaultDownloadName(QListWidgetItem *item)
223 {
224     if (!item) return QString();
225     return item->text();
226 }