1 /***************************************************************************
2 * Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
3 * Copyright (C) 2011 by Marco Gittler (marco@gitma.de) *
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>
30 #include "kdenlivesettings.h"
31 #include <KGlobalSettings>
32 #include <KMessageBox>
33 #include <KFileDialog>
35 #include <KIO/NetAccess>
36 #include <Solid/Networking>
40 #include <qjson/parser.h>
43 const int imageRole = Qt::UserRole;
44 const int urlRole = Qt::UserRole + 1;
45 const int downloadRole = Qt::UserRole + 2;
46 const int durationRole = Qt::UserRole + 3;
47 const int previewRole = Qt::UserRole + 4;
48 const int authorRole = Qt::UserRole + 5;
49 const int authorUrl = Qt::UserRole + 6;
50 const int infoUrl = Qt::UserRole + 7;
53 FreeSound::FreeSound(const QString & folder, QWidget * parent) :
58 setFont(KGlobalSettings::toolBarFont());
60 setAttribute(Qt::WA_DeleteOnClose);
62 service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
64 service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
65 setWindowTitle(i18n("Search Online Resources"));
66 connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
67 connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
68 connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
69 connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveSound()));
70 connect(sound_author, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
71 connect(sound_name, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
72 m_previewProcess = new QProcess;
73 connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
74 connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
75 sound_image->setFixedWidth(180);
76 if (Solid::Networking::status() == Solid::Networking::Unconnected) {
79 connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
80 connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
83 FreeSound::~FreeSound()
85 if (m_previewProcess) delete m_previewProcess;
88 void FreeSound::slotStartSearch()
91 m_currentPreview.clear();
93 page_number->blockSignals(true);
94 page_number->setValue(0);
95 page_number->blockSignals(false);
97 if (m_service == FREESOUND) {
98 uri = "http://www.freesound.org/api/sounds/search/?q=";
99 uri.append(search_text->text());
100 uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
102 else if (m_service == OPENCLIPART) {
103 uri = "http://openclipart.org/api/search/?query=";
104 uri.append(search_text->text());
107 KIO::TransferJob *job = KIO::get(KUrl(uri));
108 connect (job, SIGNAL( data(KIO::Job *, const QByteArray & )), this, SLOT(slotDataIsHere(KIO::Job *,const QByteArray &)));
109 connect(job, SIGNAL(result(KJob*)), this, SLOT(slotShowResults()));
112 void FreeSound::slotDataIsHere(KIO::Job *,const QByteArray & data )
114 m_result.append(data);
117 void FreeSound::slotShowResults()
119 search_results->blockSignals(true);
120 search_results->clear();
121 if (m_service == FREESOUND) {
123 QJson::Parser parser;
125 //kDebug()<<"// GOT RESULT: "<<m_result;
126 m_data = parser.parse(m_result, &ok);
128 if (m_data.canConvert(QVariant::Map)) {
129 QMap <QString, QVariant> map = m_data.toMap();
130 QMap<QString, QVariant>::const_iterator i = map.constBegin();
131 while (i != map.constEnd()) {
132 if (i.key() == "num_results") search_info->setText(i18np("Found %1 result", "Found %1 results", i.value().toInt()));
133 else if (i.key() == "num_pages") {
134 page_number->setMaximum(i.value().toInt());
136 else if (i.key() == "sounds") {
138 if (sounds.canConvert(QVariant::List)) {
139 QList <QVariant> soundsList = sounds.toList();
140 for (int j = 0; j < soundsList.count(); j++) {
141 if (soundsList.at(j).canConvert(QVariant::Map)) {
142 QMap <QString, QVariant> soundmap = soundsList.at(j).toMap();
143 if (soundmap.contains("original_filename")) {
144 QListWidgetItem *item = new QListWidgetItem(soundmap.value("original_filename").toString(), search_results);
145 item->setData(imageRole, soundmap.value("waveform_m").toString());
146 item->setData(infoUrl, soundmap.value("url").toString());
147 item->setData(durationRole, soundmap.value("duration").toDouble());
148 item->setData(previewRole, soundmap.value("preview-hq-mp3").toString());
149 item->setData(downloadRole, soundmap.value("serve").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
150 QVariant authorInfo = soundmap.value("user");
151 if (authorInfo.canConvert(QVariant::Map)) {
152 QMap <QString, QVariant> authorMap = authorInfo.toMap();
153 if (authorMap.contains("username")) {
154 item->setData(authorRole, authorMap.value("username").toString());
155 item->setData(authorUrl, authorMap.value("url").toString());
168 else if (m_service == OPENCLIPART) {
170 doc.setContent(m_result);
171 QDomNodeList items = doc.documentElement().elementsByTagName("item");
172 for (int i = 0; i < items.count(); i++) {
173 QDomElement currentClip = items.at(i).toElement();
174 QDomElement title = currentClip.firstChildElement("title");
175 QListWidgetItem *item = new QListWidgetItem(title.firstChild().nodeValue(), search_results);
176 QDomElement thumb = currentClip.firstChildElement("media:thumbnail");
177 item->setData(imageRole, thumb.attribute("url"));
178 QDomElement enclosure = currentClip.firstChildElement("enclosure");
179 item->setData(downloadRole, enclosure.attribute("url"));
180 QDomElement link = currentClip.firstChildElement("link");
181 item->setData(infoUrl, link.firstChild().nodeValue());
182 QDomElement author = currentClip.firstChildElement("dc:creator");
183 item->setData(authorRole, author.firstChild().nodeValue());
184 item->setData(authorUrl, QString("http://openclipart.org/user-detail/") + author.firstChild().nodeValue());
187 search_results->blockSignals(false);
188 search_results->setCurrentRow(0);
191 void FreeSound::slotUpdateCurrentSound()
193 m_currentPreview.clear();
194 m_currentUrl.clear();
195 QListWidgetItem *item = search_results->currentItem();
197 sound_box->setEnabled(false);
200 m_currentPreview = item->data(previewRole).toString();
201 m_currentUrl = item->data(downloadRole).toString();
202 button_preview->setEnabled(!m_currentPreview.isEmpty());
203 sound_box->setEnabled(true);
204 sound_name->setText(item->text());
205 sound_name->setUrl(item->data(infoUrl).toString());
206 sound_author->setText(item->data(authorRole).toString());
207 sound_author->setUrl(item->data(authorUrl).toString());
208 if (!item->data(durationRole).isNull()) sound_duration->setText(QString::number(item->data(durationRole).toDouble()));
209 KUrl img(item->data(imageRole).toString());
210 if (img.isEmpty()) return;
211 if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
213 if (KIO::NetAccess::download(img, tmpFile, this)) {
214 QPixmap pix(tmpFile);
215 int newHeight = pix.height() * sound_image->width() / pix.width();
216 if (newHeight > 2 * sound_image->width()) {
217 sound_image->setScaledContents(false);
218 //sound_image->setFixedHeight(sound_image->width());
221 sound_image->setScaledContents(true);
222 sound_image->setFixedHeight(newHeight);
224 sound_image->setPixmap(pix);
225 KIO::NetAccess::removeTempFile(tmpFile);
231 void FreeSound::slotPlaySound()
233 if (m_currentPreview.isEmpty()) return;
234 if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
235 m_previewProcess->close();
238 m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
241 void FreeSound::slotPreviewStatusChanged(QProcess::ProcessState state)
243 if (state == QProcess::NotRunning)
244 button_preview->setText(i18n("Preview"));
246 button_preview->setText(i18n("Stop"));
249 void FreeSound::slotSaveSound()
251 if (m_currentUrl.isEmpty()) return;
252 QString path = m_folder;
253 if (!path.endsWith('/')) path.append('/');
254 path.append(sound_name->text());
256 if (m_service == FREESOUND) {
257 ext = "*." + sound_name->text().section('.', -1);
259 else if (m_service == OPENCLIPART) {
260 path.append("." + m_currentUrl.section('.', -1));
261 ext = "*." + m_currentUrl.section('.', -1);
263 QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
264 if (saveUrl.isEmpty()) return;
265 if (KIO::NetAccess::download(KUrl(m_currentUrl), saveUrl, this)) {
266 emit addClip(KUrl(saveUrl), sound_name->url());
270 void FreeSound::slotOpenUrl(const QString &url)
272 new KRun(KUrl(url), this);
275 void FreeSound::slotChangeService()
277 m_service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
278 if (m_service == FREESOUND) {
279 button_preview->setVisible(true);
280 duration_label->setVisible(true);
281 search_info->setVisible(true);
283 else if (m_service == OPENCLIPART) {
284 button_preview->setVisible(false);
285 duration_label->setVisible(false);
286 search_info->setVisible(false);
287 sound_duration->setText(QString());
289 if (!search_text->text().isEmpty()) slotStartSearch();
292 void FreeSound::slotOnline()
294 button_search->setEnabled(true);
295 search_info->setText(QString());
298 void FreeSound::slotOffline()
300 button_search->setEnabled(false);
301 search_info->setText(i18n("You need to be online\n for searching"));