1 /***************************************************************************
2 * Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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 "resourcewidget.h"
23 #include "freesound.h"
24 #include "openclipart.h"
25 #include "archiveorg.h"
27 #include <QPushButton>
29 #include <QListWidget>
34 #include <kdeversion.h>
35 #include "kdenlivesettings.h"
36 #include <KGlobalSettings>
37 #include <KMessageBox>
38 #include <KFileDialog>
40 #include <KIO/NetAccess>
41 #include <Solid/Networking>
43 #if KDE_IS_VERSION(4,4,0)
44 #include <KPixmapSequence>
45 #include <KPixmapSequenceOverlayPainter>
50 #if KDE_IS_VERSION(4,6,0)
51 #include <Nepomuk/Variant>
52 #include <Nepomuk/Resource>
53 #include <Nepomuk/ResourceManager>
54 #include <Nepomuk/Vocabulary/NIE>
55 #include <Nepomuk/Vocabulary/NCO>
56 #include <Nepomuk/Vocabulary/NDO>
60 #ifdef USE_NEPOMUKCORE
61 #include <Nepomuk2/Variant>
62 #include <Nepomuk2/Resource>
63 #include <Nepomuk2/ResourceManager>
64 #include <Nepomuk2/Vocabulary/NIE>
65 #include <Nepomuk2/Vocabulary/NCO>
66 #include <Nepomuk2/Vocabulary/NDO>
69 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
72 m_currentService(NULL)
74 setFont(KGlobalSettings::toolBarFont());
76 setAttribute(Qt::WA_DeleteOnClose);
78 service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
79 service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
81 service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
82 setWindowTitle(i18n("Search Online Resources"));
83 info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
84 connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
85 connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
86 connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
87 connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
88 connect(item_license, SIGNAL(leftClickedUrl(QString)), this, SLOT(slotOpenUrl(QString)));
89 connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
90 if (Solid::Networking::status() == Solid::Networking::Unconnected) {
93 connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
94 connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
95 connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
96 connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
97 connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
98 connect(info_browser, SIGNAL(anchorClicked(QUrl)), this, SLOT(slotOpenLink(QUrl)));
100 m_autoPlay = new QAction(i18n("Auto Play"), this);
101 m_autoPlay->setCheckable(true);
102 QMenu *resourceMenu = new QMenu;
103 resourceMenu->addAction(m_autoPlay);
104 config_button->setMenu(resourceMenu);
105 config_button->setIcon(KIcon("configure"));
107 #if KDE_IS_VERSION(4,4,0)
108 m_busyWidget = new KPixmapSequenceOverlayPainter(this);
109 m_busyWidget->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
110 m_busyWidget->setWidget(search_results->viewport());
113 sound_box->setEnabled(false);
114 search_text->setFocus();
116 #if KDE_IS_VERSION(4,6,0)
117 Nepomuk::ResourceManager::instance()->init();
123 ResourceWidget::~ResourceWidget()
125 delete m_currentService;
126 KIO::NetAccess::removeTempFile(m_tmpThumbFile);
129 void ResourceWidget::slotStartSearch(int page)
131 page_number->blockSignals(true);
132 page_number->setValue(page);
133 page_number->blockSignals(false);
134 #if KDE_IS_VERSION(4,4,0)
135 m_busyWidget->start();
137 m_currentService->slotStartSearch(search_text->text(), page);
140 void ResourceWidget::slotUpdateCurrentSound()
142 KIO::NetAccess::removeTempFile(m_tmpThumbFile);
143 if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
144 item_license->clear();
149 QListWidgetItem *item = search_results->currentItem();
151 sound_box->setEnabled(false);
154 m_currentInfo = m_currentService->displayItemDetails(item);
156 if (m_autoPlay->isChecked() && m_currentService->hasPreview) m_currentService->startItemPreview(item);
157 sound_box->setEnabled(true);
158 QString title = "<h3>" + m_currentInfo.itemName;
159 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"));
160 title.append("</h3>");
162 if (!m_currentInfo.authorUrl.isEmpty()) {
163 title += QString("<a href=\"%1\">").arg(m_currentInfo.authorUrl);
164 if (!m_currentInfo.author.isEmpty())
165 title.append(m_currentInfo.author);
166 else title.append(i18n("Author"));
167 title.append("</a><br />");
169 else if (!m_currentInfo.author.isEmpty())
170 title.append(m_currentInfo.author + "<br />");
171 else title.append("<br />");
174 if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
175 if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
179 void ResourceWidget::slotLoadThumb(const QString url)
182 if (img.isEmpty()) return;
183 if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
184 if (KIO::NetAccess::download(img, m_tmpThumbFile, this)) {
185 slotSetImage(m_tmpThumbFile);
186 /*QPixmap pix(tmpFile);
188 int newHeight = pix.height() * item_image->width() / pix.width();
189 if (newHeight > 200) {
190 item_image->setScaledContents(false);
191 //item_image->setFixedHeight(item_image->width());
194 item_image->setScaledContents(true);
195 item_image->setFixedHeight(newHeight);
197 item_image->setPixmap(pix);*/
203 void ResourceWidget::slotDisplayMetaInfo(const QMap<QString, QString> &metaInfo)
205 if (metaInfo.contains("license")) {
206 parseLicense(metaInfo.value("license"));
208 if (metaInfo.contains("description")) {
209 slotSetDescription(metaInfo.value("description"));
214 void ResourceWidget::slotPlaySound()
216 if (!m_currentService) return;
217 bool started = m_currentService->startItemPreview(search_results->currentItem());
218 if (started) button_preview->setText(i18n("Preview"));
219 else button_preview->setText(i18n("Stop"));
223 void ResourceWidget::slotSaveItem(const QString &originalUrl)
225 //if (m_currentUrl.isEmpty()) return;
226 QListWidgetItem *item = search_results->currentItem();
228 QString path = m_folder;
230 if (!path.endsWith('/')) path.append('/');
231 if (!originalUrl.isEmpty()) {
232 path.append(KUrl(originalUrl).fileName());
233 ext = "*." + KUrl(originalUrl).fileName().section('.', -1);
234 m_currentInfo.itemDownload = originalUrl;
237 path.append(m_currentService->getDefaultDownloadName(item));
238 ext = m_currentService->getExtension(search_results->currentItem());
240 QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
242 KUrl srcUrl(m_currentInfo.itemDownload);
243 if (saveUrl.isEmpty() || !KIO::NetAccess::stat(srcUrl, entry, this)) return;
244 KIO::FileCopyJob * getJob = KIO::file_copy(srcUrl, KUrl(saveUrl), -1, KIO::Overwrite);
246 KFileItem info(entry, srcUrl);
247 getJob->setSourceSize(info.size());
248 getJob->setProperty("license", item_license->text());
249 getJob->setProperty("licenseurl", item_license->url());
250 getJob->setProperty("originurl", m_currentInfo.itemDownload);
251 if (!m_currentInfo.authorUrl.isEmpty()) getJob->setProperty("author", m_currentInfo.authorUrl);
252 else if (!m_currentInfo.author.isEmpty()) getJob->setProperty("author", m_currentInfo.author);
253 connect(getJob, SIGNAL(result(KJob*)), this, SLOT(slotGotFile(KJob*)));
257 void ResourceWidget::slotGotFile(KJob *job)
259 if (job->error() != 0 ) return;
260 KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
261 const KUrl filePath = copyJob->destUrl();
263 #if KDE_IS_VERSION(4,6,0)
264 Nepomuk::Resource res( filePath );
265 res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) job->property("license") );
266 res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) job->property("licenseurl") );
267 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
268 res.setProperty( Nepomuk::Vocabulary::NCO::creator(), (Nepomuk::Variant) job->property("author") );
269 //res.setDescription(item_description->toPlainText());
270 //res.setProperty( Soprano::Vocabulary::NAO::description(),
274 #ifdef USE_NEPOMUKCORE
275 Nepomuk2::Resource res( filePath );
276 res.setProperty( Nepomuk2::Vocabulary::NIE::license(), (Nepomuk2::Variant) job->property("license") );
277 res.setProperty( Nepomuk2::Vocabulary::NIE::licenseType(), (Nepomuk2::Variant) job->property("licenseurl") );
278 res.setProperty( Nepomuk2::Vocabulary::NDO::copiedFrom(), (Nepomuk2::Variant) job->property("originurl") );
279 res.setProperty( Nepomuk2::Vocabulary::NCO::creator(), (Nepomuk2::Variant) job->property("author") );
280 //res.setDescription(item_description->toPlainText());
281 //res.setProperty( Soprano::Vocabulary::NAO::description(),
284 emit addClip(filePath, stringMap());
287 void ResourceWidget::slotOpenUrl(const QString &url)
289 new KRun(KUrl(url), this);
292 void ResourceWidget::slotChangeService()
294 if (m_currentService) {
295 delete m_currentService;
296 m_currentService = NULL;
298 SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
299 if (service == FREESOUND) {
300 m_currentService = new FreeSound(search_results);
302 else if (service == OPENCLIPART) {
303 m_currentService = new OpenClipArt(search_results);
305 else if (service == ARCHIVEORG) {
306 m_currentService = new ArchiveOrg(search_results);
309 connect(m_currentService, SIGNAL(gotMetaInfo(QString)), this, SLOT(slotSetMetadata(QString)));
310 connect(m_currentService, SIGNAL(gotMetaInfo(QMap<QString,QString>)), this, SLOT(slotDisplayMetaInfo(QMap<QString,QString>)));
311 connect(m_currentService, SIGNAL(maxPages(int)), this, SLOT(slotSetMaximum(int)));
312 connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
313 connect(m_currentService, SIGNAL(gotThumb(QString)), this, SLOT(slotLoadThumb(QString)));
314 #if KDE_IS_VERSION(4,4,0)
315 connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
318 button_preview->setVisible(m_currentService->hasPreview);
319 button_import->setVisible(!m_currentService->inlineDownload);
320 search_info->setText(QString());
321 if (!search_text->text().isEmpty()) slotStartSearch();
324 void ResourceWidget::slotSetMaximum(int max)
326 page_number->setMaximum(max);
329 void ResourceWidget::slotOnline()
331 button_search->setEnabled(true);
332 search_info->setText(QString());
335 void ResourceWidget::slotOffline()
337 button_search->setEnabled(false);
338 search_info->setText(i18n("You need to be online\n for searching"));
341 void ResourceWidget::slotNextPage()
343 int ix = page_number->value();
344 if (search_results->count() > 0) page_number->setValue(ix + 1);
347 void ResourceWidget::slotPreviousPage()
349 int ix = page_number->value();
350 if (ix > 1) page_number->setValue(ix - 1);
353 void ResourceWidget::parseLicense(const QString &licenseUrl)
356 if (licenseUrl.contains("/sampling+/"))
357 licenseName = "Sampling+";
358 else if (licenseUrl.contains("/by/"))
359 licenseName = "Attribution";
360 else if (licenseUrl.contains("/by-nd/"))
361 licenseName = "Attribution-NoDerivs";
362 else if (licenseUrl.contains("/by-nc-sa/"))
363 licenseName = "Attribution-NonCommercial-ShareAlike";
364 else if (licenseUrl.contains("/by-sa/"))
365 licenseName = "Attribution-ShareAlike";
366 else if (licenseUrl.contains("/by-nc/"))
367 licenseName = "Attribution-NonCommercial";
368 else if (licenseUrl.contains("/by-nc-nd/"))
369 licenseName = "Attribution-NonCommercial-NoDerivs";
370 else if (licenseUrl.contains("/publicdomain/zero/"))
371 licenseName = "Creative Commons 0";
372 else if (licenseUrl.endsWith("/publicdomain"))
373 licenseName = "Public Domain";
374 else licenseName = i18n("Unknown");
375 item_license->setText(licenseName);
376 item_license->setUrl(licenseUrl);
380 void ResourceWidget::slotOpenLink(const QUrl &url)
382 QString path = url.toEncoded();
383 if (path.endsWith("_import")) {
385 // import file in Kdenlive
393 void ResourceWidget::slotSetDescription(const QString &desc)
399 void ResourceWidget::slotSetMetadata(const QString &desc)
405 void ResourceWidget::slotSetImage(const QString &desc)
407 m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
411 void ResourceWidget::slotSetTitle(const QString &desc)
417 void ResourceWidget::updateLayout()
419 QString content = m_title;
420 if (!m_image.isEmpty()) content.append(m_image + "<br clear=\"all\" />");
421 if (!m_desc.isEmpty()) content.append(m_desc);
422 if (!m_meta.isEmpty()) content.append(m_meta);
423 info_browser->setHtml(content);
427 #include "resourcewidget.moc"