]> git.sesse.net Git - kdenlive/blobdiff - src/utils/resourcewidget.cpp
Fix Qt 4.6 compilation: http://kdenlive.org/mantis/view.php?id=2735
[kdenlive] / src / utils / resourcewidget.cpp
index e2b4fa2b8319ca40c03d6193e6cd13aff6cf0e43..16d54a3fb9dc0959f28a78cee9375e20c4cfa1bb 100644 (file)
 #include "resourcewidget.h"
 #include "freesound.h"
 #include "openclipart.h"
+#include "archiveorg.h"
 
 #include <QPushButton>
 #include <QSpinBox>
 #include <QListWidget>
-#include <QDomDocument>
+#include <QAction>
+#include <QMenu>
 
 #include <KDebug>
+#include <kdeversion.h>
 #include "kdenlivesettings.h"
 #include <KGlobalSettings>
 #include <KMessageBox>
 #include <KIO/NetAccess>
 #include <Solid/Networking>
 #include <KRun>
+#if KDE_IS_VERSION(4,4,0)
+#include <KPixmapSequence>
+#include <KPixmapSequenceOverlayPainter>
+#endif
+#include <KFileItem>
 
 #ifdef USE_NEPOMUK
+#if KDE_IS_VERSION(4,6,0)
 #include <Nepomuk/Variant>
 #include <Nepomuk/Resource>
 #include <Nepomuk/ResourceManager>
-#include <Soprano/Vocabulary/NAO>
 #include <Nepomuk/Vocabulary/NIE>
+#include <Nepomuk/Vocabulary/NCO>
 #include <Nepomuk/Vocabulary/NDO>
 #endif
-
+#endif
 
 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
         QDialog(parent),
@@ -58,20 +67,17 @@ ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
     setAttribute(Qt::WA_DeleteOnClose);
 #ifdef USE_QJSON
     service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
+    service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
 #endif
     service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
     setWindowTitle(i18n("Search Online Resources"));
-    info_widget->setStyleSheet(QString("QTreeWidget { background-color: transparent;}"));
-    item_description->setStyleSheet(QString("KTextBrowser { background-color: transparent;}"));
+    info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
     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(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
     connect(item_license, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
-    connect(sound_name, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
     connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
-    sound_image->setFixedWidth(180);
     if (Solid::Networking::status() == Solid::Networking::Unconnected) {
         slotOffline();
     }
@@ -80,81 +86,106 @@ ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
     connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
     connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
     connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
+    connect(info_browser, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(slotOpenLink(const QUrl &)));
+    
+    m_autoPlay = new QAction(i18n("Auto Play"), this);
+    m_autoPlay->setCheckable(true);
+    QMenu *resourceMenu = new QMenu;
+    resourceMenu->addAction(m_autoPlay);
+    config_button->setMenu(resourceMenu);
+    config_button->setIcon(KIcon("configure"));
+
+#if KDE_IS_VERSION(4,4,0)
+    m_busyWidget = new KPixmapSequenceOverlayPainter(this);
+    m_busyWidget->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
+    m_busyWidget->setWidget(search_results->viewport());
+#endif
+    
     sound_box->setEnabled(false);
     search_text->setFocus();
+#ifdef USE_NEPOMUK
+#if KDE_IS_VERSION(4,6,0)    
     Nepomuk::ResourceManager::instance()->init();
+#endif
+#endif
     slotChangeService();
 }
 
 ResourceWidget::~ResourceWidget()
 {
     if (m_currentService) delete m_currentService;
+    KIO::NetAccess::removeTempFile(m_tmpThumbFile);
 }
 
 void ResourceWidget::slotStartSearch(int page)
 {
-    /*m_currentPreview.clear();
-    m_currentUrl.clear();*/
     page_number->blockSignals(true);
     page_number->setValue(page);
     page_number->blockSignals(false);
+#if KDE_IS_VERSION(4,4,0)
+    m_busyWidget->start();
+#endif
     m_currentService->slotStartSearch(search_text->text(), page);
-    /*QString uri;
-    if (m_service == FREESOUND) {
-        uri = "http://www.freesound.org/api/sounds/search/?q=";
-        uri.append(search_text->text());
-        if (page > 1) uri.append("&p=" + QString::number(page));
-        uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
-    }
-    else if (m_service == OPENCLIPART) {
-        uri = "http://openclipart.org/api/search/?query=";
-        uri.append(search_text->text());
-        if (page > 1) uri.append("&page=" + QString::number(page));
-    }*/
 }
 
 void ResourceWidget::slotUpdateCurrentSound()
 {
-    if (!sound_autoplay->isChecked()) m_currentService->stopItemPreview(NULL);
-    info_widget->clear();
-    item_description->clear();
+    KIO::NetAccess::removeTempFile(m_tmpThumbFile);
+    if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
     item_license->clear();
+    m_title.clear();
+    m_image.clear();
+    m_desc.clear();
+    m_meta.clear();
     QListWidgetItem *item = search_results->currentItem();
     if (!item) {
         sound_box->setEnabled(false);
         return;
     }
     m_currentInfo = m_currentService->displayItemDetails(item);
-    /*m_currentPreview = item->data(previewRole).toString();
-    m_currentUrl = item->data(downloadRole).toString();
-    m_currentId = item->data(idRole).toInt();*/
     
-    if (sound_autoplay->isChecked()) m_currentService->startItemPreview(item);
+    if (m_autoPlay->isChecked() && m_currentService->hasPreview) m_currentService->startItemPreview(item);
     sound_box->setEnabled(true);
-    sound_name->setText(item->text());
-    sound_name->setUrl(m_currentInfo.infoUrl);
-    sound_author->setText(m_currentInfo.author);
-    sound_author->setUrl(m_currentInfo.authorUrl);
-    item_description->setHtml(m_currentInfo.description);
+    QString title = "<h3>" + m_currentInfo.itemName;
+    if (!m_currentInfo.infoUrl.isEmpty()) title += QString(" (<a href=\"%1\">%2</a>)").arg(m_currentInfo.infoUrl).arg(i18nc("the url link pointing to a web page", "link"));
+    title.append("</h3>");
     
+    if (!m_currentInfo.authorUrl.isEmpty()) {
+        title += QString("<a href=\"%1\">").arg(m_currentInfo.authorUrl);
+        if (!m_currentInfo.author.isEmpty())
+            title.append(m_currentInfo.author);
+        else title.append(i18n("Author"));
+        title.append("</a><br />");
+    }
+    else if (!m_currentInfo.author.isEmpty())
+        title.append(m_currentInfo.author + "<br />");
+    else title.append("<br />");
+    
+    slotSetTitle(title);
+    if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
+    if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
+}
 
-    KUrl img(m_currentInfo.imagePreview);
+
+void ResourceWidget::slotLoadThumb(const QString url)
+{
+    KUrl img(url);
     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);
-            int newHeight = pix.height() * sound_image->width() / pix.width();
+        if (KIO::NetAccess::download(img, m_tmpThumbFile, this)) {
+            slotSetImage(m_tmpThumbFile);
+            /*QPixmap pix(tmpFile);
+            
+            int newHeight = pix.height() * item_image->width() / pix.width();
             if (newHeight > 200) {
-                sound_image->setScaledContents(false);
-                //sound_image->setFixedHeight(sound_image->width());
+                item_image->setScaledContents(false);
+                //item_image->setFixedHeight(item_image->width());
             }
             else {
-                sound_image->setScaledContents(true);
-                sound_image->setFixedHeight(newHeight);
+                item_image->setScaledContents(true);
+                item_image->setFixedHeight(newHeight);
             }
-            sound_image->setPixmap(pix);
-            KIO::NetAccess::removeTempFile(tmpFile);
+            item_image->setPixmap(pix);*/
         }
     }
 }
@@ -162,21 +193,12 @@ void ResourceWidget::slotUpdateCurrentSound()
 
 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
 {
-    if (metaInfo.contains("description")) {
-        item_description->setHtml(metaInfo.value("description"));
-        metaInfo.remove("description");
-    }
     if (metaInfo.contains("license")) {
         parseLicense(metaInfo.value("license"));
-        metaInfo.remove("license");
     }
-    QMap<QString, QString>::const_iterator i = metaInfo.constBegin();
-    while (i != metaInfo.constEnd()) {
-        new QTreeWidgetItem(info_widget, QStringList() << i.key() << i.value());
-        ++i;
+    if (metaInfo.contains("description")) {
+        slotSetDescription(metaInfo.value("description"));
     }
-    info_widget->resizeColumnToContents(0);
-    info_widget->resizeColumnToContents(1);
 }
 
 
@@ -189,48 +211,58 @@ void ResourceWidget::slotPlaySound()
 }
 
 
-void ResourceWidget::slotForcePlaySound(bool play)
-{
-    /*
-    if (m_service != FREESOUND) return;
-    m_previewProcess->close();
-    if (m_currentPreview.isEmpty()) return;
-    if (play)
-        m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
-    */
-}
-
-void ResourceWidget::slotPreviewStatusChanged(QProcess::ProcessState state)
-{
-    /*if (state == QProcess::NotRunning)
-        button_preview->setText(i18n("Preview"));
-    else 
-        button_preview->setText(i18n("Stop"));*/
-}
-
-void ResourceWidget::slotSaveSound()
+void ResourceWidget::slotSaveItem(const QString originalUrl)
 {
     //if (m_currentUrl.isEmpty()) return;
     QListWidgetItem *item = search_results->currentItem();
     if (!item) return;
     QString path = m_folder;
+    QString ext;
     if (!path.endsWith('/')) path.append('/');
-    path.append(m_currentService->getDefaultDownloadName(item));
-    QString ext = m_currentService->getExtension(search_results->currentItem());
+    if (!originalUrl.isEmpty()) {
+        path.append(KUrl(originalUrl).fileName());
+        ext = "*." + KUrl(originalUrl).fileName().section('.', -1);
+        m_currentInfo.itemDownload = originalUrl;
+    }
+    else {
+        path.append(m_currentService->getDefaultDownloadName(item));
+        ext = m_currentService->getExtension(search_results->currentItem());
+    }
     QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
-    if (saveUrl.isEmpty()) return;
-    if (KIO::NetAccess::download(KUrl(m_currentInfo.itemDownload), saveUrl, this)) {
-        const KUrl filePath = KUrl(saveUrl);
+    KIO::UDSEntry entry;
+    KUrl srcUrl(m_currentInfo.itemDownload);
+    if (saveUrl.isEmpty() || !KIO::NetAccess::stat(srcUrl, entry, this)) return;
+    KIO::FileCopyJob * getJob = KIO::file_copy(srcUrl, KUrl(saveUrl), -1, KIO::Overwrite);
+    
+    KFileItem info(entry, srcUrl); 
+    getJob->setSourceSize(info.size());
+    getJob->setProperty("license", item_license->text());
+    getJob->setProperty("licenseurl", item_license->url());
+    getJob->setProperty("originurl", m_currentInfo.itemDownload);
+    if (!m_currentInfo.authorUrl.isEmpty()) getJob->setProperty("author", m_currentInfo.authorUrl);
+    else if (!m_currentInfo.author.isEmpty()) getJob->setProperty("author", m_currentInfo.author);
+    connect(getJob, SIGNAL(result(KJob*)), this, SLOT(slotGotFile(KJob*)));
+    getJob->start();
+}
+
+void ResourceWidget::slotGotFile(KJob *job)
+{
+    if (job->error() != 0 ) return;
+    KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
+    const KUrl filePath = copyJob->destUrl();
 #ifdef USE_NEPOMUK
+#if KDE_IS_VERSION(4,6,0)
         Nepomuk::Resource res( filePath );
-        res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) item_license->text() );
-        res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) item_license->url() );
-        res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), sound_name->url() );
+        res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) job->property("license") );
+        res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) job->property("licenseurl") );
+        res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
+        res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
+        res.setProperty( Nepomuk::Vocabulary::NCO::creator(), (Nepomuk::Variant) job->property("author") );
         //res.setDescription(item_description->toPlainText());
         //res.setProperty( Soprano::Vocabulary::NAO::description(), 
 #endif
-        emit addClip(KUrl(saveUrl), QString());//, sound_name->url());
-    }
+#endif
+        emit addClip(filePath, QMap <QString, QString>());
 }
 
 void ResourceWidget::slotOpenUrl(const QString &url)
@@ -251,14 +283,22 @@ void ResourceWidget::slotChangeService()
     else if (service == OPENCLIPART) {
         m_currentService = new OpenClipArt(search_results);
     }
+    else if (service == ARCHIVEORG) {
+        m_currentService = new ArchiveOrg(search_results);
+    }
+
+    connect(m_currentService, SIGNAL(gotMetaInfo(const QString)), this, SLOT(slotSetMetadata(const QString)));
     connect(m_currentService, SIGNAL(gotMetaInfo(QMap <QString, QString>)), this, SLOT(slotDisplayMetaInfo(QMap <QString, QString>)));
     connect(m_currentService, SIGNAL(maxPages(int)), page_number, SLOT(setMaximum(int)));
     connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
+    connect(m_currentService, SIGNAL(gotThumb(const QString)), this, SLOT(slotLoadThumb(const QString)));
+#if KDE_IS_VERSION(4,4,0)
+    connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
+#endif
     
     button_preview->setVisible(m_currentService->hasPreview);
-    sound_autoplay->setVisible(m_currentService->hasPreview);
+    button_import->setVisible(!m_currentService->inlineDownload);
     search_info->setText(QString());
-    info_widget->setVisible(m_currentService->hasMetadata);
     if (!search_text->text().isEmpty()) slotStartSearch();
 }
 
@@ -307,7 +347,55 @@ void ResourceWidget::parseLicense(const QString &licenseUrl)
         licenseName = "Creative Commons 0";
     else if (licenseUrl.endsWith("/publicdomain"))
         licenseName = "Public Domain";
-    item_license->setText(i18n("License: %1", licenseName));
+    else licenseName = i18n("Unknown");
+    item_license->setText(licenseName);
     item_license->setUrl(licenseUrl);
 }
 
+
+void ResourceWidget::slotOpenLink(const QUrl &url)
+{
+    QString path = url.toEncoded();
+    if (path.endsWith("_import")) {
+        path.chop(7);
+        // import file in Kdenlive
+        slotSaveItem(path);
+    }
+    else {
+        slotOpenUrl(path);
+    }
+}
+
+void ResourceWidget::slotSetDescription(const QString desc)
+{
+    m_desc = desc;
+    updateLayout();
+}
+
+void ResourceWidget::slotSetMetadata(const QString desc)
+{
+    m_meta = desc;
+    updateLayout();
+}
+
+void ResourceWidget::slotSetImage(const QString desc)
+{
+    m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
+    updateLayout();
+}
+
+void ResourceWidget::slotSetTitle(const QString desc)
+{
+    m_title = desc;
+    updateLayout();
+}
+
+void ResourceWidget::updateLayout()
+{
+    QString content = m_title;
+    if (!m_image.isEmpty()) content.append(m_image + "<br clear=\"all\" />");
+    if (!m_desc.isEmpty()) content.append(m_desc);
+    if (!m_meta.isEmpty()) content.append(m_meta);
+    info_browser->setHtml(content);
+}
+