]> git.sesse.net Git - kdenlive/blobdiff - src/utils/freesound.cpp
Const'ref
[kdenlive] / src / utils / freesound.cpp
index c67ecb8c6b487f2c3a4fb55da741e2c96575c531..962984f52a218de1cace9a9dbfb9d8ec160b98bf 100644 (file)
@@ -1,6 +1,6 @@
 /***************************************************************************
- *   Copyright (C) 2008 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
- *   Copyright (C) 2011 by Marco Gittler (marco@gitma.de)                  *
+ *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
+ *                                                                         *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
 #include <QPushButton>
 #include <QSpinBox>
 #include <QListWidget>
+#include <QDomDocument>
+#include <QApplication>
 
 #include <KDebug>
 #include "kdenlivesettings.h"
-#include <KGlobalSettings>
-#include <KMessageBox>
-#include <KFileDialog>
 #include <kio/job.h>
-#include <KIO/NetAccess>
-#include <KRun>
+#include <KLocale>
 
+#ifdef USE_QJSON
 #include <qjson/parser.h>
+#endif
 
-const int imageRole = Qt::UserRole;
-const int urlRole = Qt::UserRole + 1;
-const int soundRole = Qt::UserRole + 2;
-const int durationRole = Qt::UserRole + 3;
-const int previewRole = Qt::UserRole + 4;
-const int authorRole = Qt::UserRole + 5;
-const int authorUrl = Qt::UserRole + 6;
-const int soundUrl = Qt::UserRole + 7;
-
-FreeSound::FreeSound(const QString & folder, QWidget * parent) :
-        QDialog(parent),
-        m_folder(folder)
+FreeSound::FreeSound(QListWidget *listWidget, QObject *parent) :
+        AbstractService(listWidget, parent),
+        m_previewProcess(new QProcess)
 {
-    setFont(KGlobalSettings::toolBarFont());
-    setupUi(this);
-    setAttribute(Qt::WA_DeleteOnClose);
-    setWindowTitle(i18n("Search FreeSound Audio Library"));
-    connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
-    connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
-    connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
-    connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveSound()));
-    connect(sound_author, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
-    connect(sound_name, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
-    m_previewProcess = new QProcess;
-    connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
+    serviceType = FREESOUND;
+    hasPreview = true;
+    hasMetadata = true;
 }
 
 FreeSound::~FreeSound()
 {
-    if (m_previewProcess) delete m_previewProcess;
+    delete m_previewProcess;
 }
 
-void FreeSound::slotStartSearch()
+void FreeSound::slotStartSearch(const QString &searchText, int page)
 {
-    m_result.clear();
-    m_currentPreview.clear();
-    m_currentUrl.clear();
-    page_number->blockSignals(true);
-    page_number->setValue(0);
-    page_number->blockSignals(false);
+    m_listWidget->clear();
     QString uri = "http://www.freesound.org/api/sounds/search/?q=";
-    uri.append(search_text->text());
+    uri.append(searchText);
+    if (page > 1)
+        uri.append("&p=" + QString::number(page));
     uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
-    KIO::TransferJob *job = KIO::get(KUrl(uri));
-    connect (job, SIGNAL(  data(KIO::Job *, const QByteArray & )), this, SLOT(slotDataIsHere(KIO::Job *,const QByteArray &)));
-    connect(job, SIGNAL(result(KJob*)), this, SLOT(slotShowResults()));
-}
 
-void FreeSound::slotDataIsHere(KIO::Job *,const QByteArray & data )
-{
-  //kDebug() << "data is here";
-  m_result.append(data);
+    KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
+    connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
 }
 
-void FreeSound::slotShowResults()
+
+void FreeSound::slotShowResults(KJob* job)
 {
+    if (job->error() != 0 ) return;
+    m_listWidget->blockSignals(true);
+    KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
+#ifdef USE_QJSON
     QJson::Parser parser;
     bool ok;
-    m_data = parser.parse(m_result, &ok);
+    //kDebug()<<"// GOT RESULT: "<<m_result;
+    QVariant data = parser.parse(storedQueryJob->data(), &ok);
     QVariant sounds;
-    search_results->blockSignals(true);
-    search_results->clear();
-    if (m_data.canConvert(QVariant::Map)) {
-        QMap <QString, QVariant> map = m_data.toMap();
+    if (data.canConvert(QVariant::Map)) {
+        QMap <QString, QVariant> map = data.toMap();
         QMap<QString, QVariant>::const_iterator i = map.constBegin();
         while (i != map.constEnd()) {
-            if (i.key() == "num_results") search_info->setText(i18np("Found %1 result", "Found %1 results", i.value().toInt()));
+            if (i.key() == "num_results") emit searchInfo(i18np("Found %1 result", "Found %1 results", i.value().toInt()));
             else if (i.key() == "num_pages") {
-                page_number->setMaximum(i.value().toInt());
+                emit maxPages(i.value().toInt());
             }
             else if (i.key() == "sounds") {
                 sounds = i.value();
@@ -114,12 +91,14 @@ void FreeSound::slotShowResults()
                         if (soundsList.at(j).canConvert(QVariant::Map)) {
                             QMap <QString, QVariant> soundmap = soundsList.at(j).toMap();
                             if (soundmap.contains("original_filename")) {
-                                QListWidgetItem *item = new QListWidgetItem(soundmap.value("original_filename").toString(), search_results);
+                                QListWidgetItem *item = new   QListWidgetItem(soundmap.value("original_filename").toString(), m_listWidget);
                                 item->setData(imageRole, soundmap.value("waveform_m").toString());
-                                item->setData(soundUrl, soundmap.value("url").toString());
+                                item->setData(infoUrl, soundmap.value("url").toString());
+                                item->setData(infoData, soundmap.value("ref").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
                                 item->setData(durationRole, soundmap.value("duration").toDouble());
+                                item->setData(idRole, soundmap.value("id").toInt());
                                 item->setData(previewRole, soundmap.value("preview-hq-mp3").toString());
-                                item->setData(soundRole, soundmap.value("serve").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
+                                item->setData(downloadRole, soundmap.value("serve").toString() + "?api_key=a1772c8236e945a4bee30a64058dabf8");
                                 QVariant authorInfo = soundmap.value("user");
                                 if (authorInfo.canConvert(QVariant::Map)) {
                                     QMap <QString, QVariant> authorMap = authorInfo.toMap();
@@ -136,74 +115,124 @@ void FreeSound::slotShowResults()
             ++i;
         }
     }
-    search_results->blockSignals(false);
-    search_results->setCurrentRow(0);
+#endif  
+    m_listWidget->blockSignals(false);
+    m_listWidget->setCurrentRow(0);
+    emit searchDone();
 }
+    
 
-void FreeSound::slotUpdateCurrentSound()
+OnlineItemInfo FreeSound::displayItemDetails(QListWidgetItem *item)
 {
-    m_currentPreview.clear();
-    m_currentUrl.clear();
-    QListWidgetItem *item = search_results->currentItem();
+    OnlineItemInfo info;
+    m_metaInfo.clear();
     if (!item) {
-        sound_box->setEnabled(false);
-        return;
+        return info;
     }
-    m_currentPreview = item->data(previewRole).toString();
-    m_currentUrl = item->data(soundRole).toString();
-    button_preview->setEnabled(!m_currentPreview.isEmpty());
-    sound_box->setEnabled(true);
-    sound_name->setText(item->text());
-    sound_name->setUrl(item->data(soundUrl).toString());
-    sound_author->setText(item->data(authorRole).toString());
-    sound_author->setUrl(item->data(authorUrl).toString());
-    sound_duration->setText(QString::number(item->data(durationRole).toDouble()));
-    KUrl img(item->data(imageRole).toString());
-    if (img.isEmpty()) return;
-    if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
-        QString tmpFile;
-        if (KIO::NetAccess::download(img, tmpFile, this)) {
-            QPixmap pix(tmpFile);
-            sound_image->setPixmap(pix);
-            KIO::NetAccess::removeTempFile(tmpFile);
-        }
+    info.itemPreview = item->data(previewRole).toString();
+    info.itemDownload = item->data(downloadRole).toString();
+    info.itemId = item->data(idRole).toInt();
+    info.itemName = item->text();
+    info.infoUrl = item->data(infoUrl).toString();
+    info.author = item->data(authorRole).toString();
+    info.authorUrl = item->data(authorUrl).toString();
+    m_metaInfo.insert(i18n("Duration"), item->data(durationRole).toString());
+    info.license = item->data(licenseRole).toString();
+    info.description = item->data(descriptionRole).toString();
+    
+    QString extraInfoUrl = item->data(infoData).toString();
+    if (!extraInfoUrl.isEmpty()) {
+        KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
+        connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotParseResults(KJob*)) );
     }
+    emit gotThumb(item->data(imageRole).toString());
+    return info;
 }
 
 
-void FreeSound::slotPlaySound()
+void FreeSound::slotParseResults(KJob* job)
 {
-    if (m_currentPreview.isEmpty()) return;
+#ifdef USE_QJSON
+    KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
+    QJson::Parser parser;
+    bool ok;
+    QString html = QString("<style type=\"text/css\">tr.cellone {background-color: %1;}</style>").arg(qApp->palette().alternateBase().color().name());
+    
+    QVariant data = parser.parse(storedQueryJob->data(), &ok);
+    if (data.canConvert(QVariant::Map)) {
+        QMap <QString, QVariant> infos = data.toMap();
+        //if (m_currentId != infos.value("id").toInt()) return;
+        
+        html += "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">";
+    
+        if (m_metaInfo.contains(i18n("Duration"))) {
+           html += "<tr>";
+            html += "<td>" + i18n("Duration") + "</td><td>" + m_metaInfo.value(i18n("Duration")) + "</td></tr>";
+            m_metaInfo.remove(i18n("Duration"));
+        }
+        
+        if (infos.contains("samplerate")) {
+            html += "<tr class=\"cellone\">";
+            html += "<td>" + i18n("Samplerate") + "</td><td>" + QString::number(infos.value("samplerate").toDouble()) + "</td></tr>";
+        }
+        if (infos.contains("channels")) {
+            html += "<tr>";
+            html += "<td>" + i18n("Channels") + "</td><td>" + QString::number(infos.value("channels").toInt()) + "</td></tr>";
+        }
+        if (infos.contains("filesize")) {
+            html += "<tr class=\"cellone\">";
+            KIO::filesize_t fSize = infos.value("filesize").toDouble();
+            html += "<td>" + i18n("File size") + "</td><td>" + KIO::convertSize(fSize) + "</td></tr>";
+        }
+        if (infos.contains("license")) {
+            m_metaInfo.insert("license", infos.value("license").toString());
+        }
+        html +="</table>";
+        if (infos.contains("description")) {
+            m_metaInfo.insert("description", infos.value("description").toString());
+        }
+    }
+    emit gotMetaInfo(html);
+    emit gotMetaInfo(m_metaInfo);
+#endif    
+}
+
+
+bool FreeSound::startItemPreview(QListWidgetItem *item)
+{    
+    if (!item)
+        return false;
+    const QString url = item->data(previewRole).toString();
+    if (url.isEmpty())
+        return false;
     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
         m_previewProcess->close();
-        return;
     }
-    m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
+    m_previewProcess->start(KdenliveSettings::ffplaypath(), QStringList() << url << "-nodisp");
+    return true;
 }
 
-void FreeSound::slotPreviewStatusChanged(QProcess::ProcessState state)
-{
-    if (state == QProcess::NotRunning)
-        button_preview->setText(i18n("Preview"));
-    else 
-        button_preview->setText(i18n("Stop"));
+
+void FreeSound::stopItemPreview(QListWidgetItem */*item*/)
+{    
+    if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
+        m_previewProcess->close();
+    }
 }
 
-void FreeSound::slotSaveSound()
+QString FreeSound::getExtension(QListWidgetItem *item)
 {
-    if (m_currentUrl.isEmpty()) return;
-    QString path = m_folder;
-    if (!path.endsWith('/')) path.append('/');
-    path.append(sound_name->text());
-    QString ext = "*." + sound_name->text().section('.', -1);
-    QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
-    if (saveUrl.isEmpty()) return;
-    if (KIO::NetAccess::download(KUrl(m_currentUrl), saveUrl, this)) {
-        emit addClip(KUrl(saveUrl));
-    }
+    if (!item)
+        return QString();
+    return QString("*.") + item->text().section('.', -1);
 }
 
-void FreeSound::slotOpenUrl(const QString &url)
+
+QString FreeSound::getDefaultDownloadName(QListWidgetItem *item)
 {
-    new KRun(KUrl(url), this);
+    if (!item)
+        return QString();
+    return item->text();
 }
+
+#include "freesound.moc"