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 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
63 m_currentService(NULL)
65 setFont(KGlobalSettings::toolBarFont());
67 setAttribute(Qt::WA_DeleteOnClose);
69 service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
70 service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
72 service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
73 setWindowTitle(i18n("Search Online Resources"));
74 info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
75 connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
76 connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
77 connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
78 connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
79 connect(item_license, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
80 connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
81 if (Solid::Networking::status() == Solid::Networking::Unconnected) {
84 connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
85 connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
86 connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
87 connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
88 connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
89 connect(info_browser, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(slotOpenLink(const QUrl &)));
91 m_autoPlay = new QAction(i18n("Auto Play"), this);
92 m_autoPlay->setCheckable(true);
93 QMenu *resourceMenu = new QMenu;
94 resourceMenu->addAction(m_autoPlay);
95 config_button->setMenu(resourceMenu);
96 config_button->setIcon(KIcon("configure"));
98 #if KDE_IS_VERSION(4,4,0)
99 m_busyWidget = new KPixmapSequenceOverlayPainter(this);
100 m_busyWidget->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
101 m_busyWidget->setWidget(search_results->viewport());
104 sound_box->setEnabled(false);
105 search_text->setFocus();
107 #if KDE_IS_VERSION(4,6,0)
108 Nepomuk::ResourceManager::instance()->init();
114 ResourceWidget::~ResourceWidget()
116 if (m_currentService) delete m_currentService;
117 KIO::NetAccess::removeTempFile(m_tmpThumbFile);
120 void ResourceWidget::slotStartSearch(int page)
122 page_number->blockSignals(true);
123 page_number->setValue(page);
124 page_number->blockSignals(false);
125 #if KDE_IS_VERSION(4,4,0)
126 m_busyWidget->start();
128 m_currentService->slotStartSearch(search_text->text(), page);
131 void ResourceWidget::slotUpdateCurrentSound()
133 KIO::NetAccess::removeTempFile(m_tmpThumbFile);
134 if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
135 item_license->clear();
140 QListWidgetItem *item = search_results->currentItem();
142 sound_box->setEnabled(false);
145 m_currentInfo = m_currentService->displayItemDetails(item);
147 if (m_autoPlay->isChecked() && m_currentService->hasPreview) m_currentService->startItemPreview(item);
148 sound_box->setEnabled(true);
149 QString title = "<h3>" + m_currentInfo.itemName;
150 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"));
151 title.append("</h3>");
153 if (!m_currentInfo.authorUrl.isEmpty()) {
154 title += QString("<a href=\"%1\">").arg(m_currentInfo.authorUrl);
155 if (!m_currentInfo.author.isEmpty())
156 title.append(m_currentInfo.author);
157 else title.append(i18n("Author"));
158 title.append("</a><br />");
160 else if (!m_currentInfo.author.isEmpty())
161 title.append(m_currentInfo.author + "<br />");
162 else title.append("<br />");
165 if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
166 if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
170 void ResourceWidget::slotLoadThumb(const QString url)
173 if (img.isEmpty()) return;
174 if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
175 if (KIO::NetAccess::download(img, m_tmpThumbFile, this)) {
176 slotSetImage(m_tmpThumbFile);
177 /*QPixmap pix(tmpFile);
179 int newHeight = pix.height() * item_image->width() / pix.width();
180 if (newHeight > 200) {
181 item_image->setScaledContents(false);
182 //item_image->setFixedHeight(item_image->width());
185 item_image->setScaledContents(true);
186 item_image->setFixedHeight(newHeight);
188 item_image->setPixmap(pix);*/
194 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
196 if (metaInfo.contains("license")) {
197 parseLicense(metaInfo.value("license"));
199 if (metaInfo.contains("description")) {
200 slotSetDescription(metaInfo.value("description"));
205 void ResourceWidget::slotPlaySound()
207 if (!m_currentService) return;
208 bool started = m_currentService->startItemPreview(search_results->currentItem());
209 if (started) button_preview->setText(i18n("Preview"));
210 else button_preview->setText(i18n("Stop"));
214 void ResourceWidget::slotSaveItem(const QString originalUrl)
216 //if (m_currentUrl.isEmpty()) return;
217 QListWidgetItem *item = search_results->currentItem();
219 QString path = m_folder;
221 if (!path.endsWith('/')) path.append('/');
222 if (!originalUrl.isEmpty()) {
223 path.append(KUrl(originalUrl).fileName());
224 ext = "*." + KUrl(originalUrl).fileName().section('.', -1);
225 m_currentInfo.itemDownload = originalUrl;
228 path.append(m_currentService->getDefaultDownloadName(item));
229 ext = m_currentService->getExtension(search_results->currentItem());
231 QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
233 KUrl srcUrl(m_currentInfo.itemDownload);
234 if (saveUrl.isEmpty() || !KIO::NetAccess::stat(srcUrl, entry, this)) return;
235 KIO::FileCopyJob * getJob = KIO::file_copy(srcUrl, KUrl(saveUrl), -1, KIO::Overwrite);
237 KFileItem info(entry, srcUrl);
238 getJob->setSourceSize(info.size());
239 getJob->setProperty("license", item_license->text());
240 getJob->setProperty("licenseurl", item_license->url());
241 getJob->setProperty("originurl", m_currentInfo.itemDownload);
242 if (!m_currentInfo.authorUrl.isEmpty()) getJob->setProperty("author", m_currentInfo.authorUrl);
243 else if (!m_currentInfo.author.isEmpty()) getJob->setProperty("author", m_currentInfo.author);
244 connect(getJob, SIGNAL(result(KJob*)), this, SLOT(slotGotFile(KJob*)));
248 void ResourceWidget::slotGotFile(KJob *job)
250 if (job->error() != 0 ) return;
251 KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
252 const KUrl filePath = copyJob->destUrl();
254 #if KDE_IS_VERSION(4,6,0)
255 Nepomuk::Resource res( filePath );
256 res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) job->property("license") );
257 res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) job->property("licenseurl") );
258 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
259 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
260 res.setProperty( Nepomuk::Vocabulary::NCO::creator(), (Nepomuk::Variant) job->property("author") );
261 //res.setDescription(item_description->toPlainText());
262 //res.setProperty( Soprano::Vocabulary::NAO::description(),
265 emit addClip(filePath, QString());
268 void ResourceWidget::slotOpenUrl(const QString &url)
270 new KRun(KUrl(url), this);
273 void ResourceWidget::slotChangeService()
275 if (m_currentService) {
276 delete m_currentService;
277 m_currentService = NULL;
279 SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
280 if (service == FREESOUND) {
281 m_currentService = new FreeSound(search_results);
283 else if (service == OPENCLIPART) {
284 m_currentService = new OpenClipArt(search_results);
286 else if (service == ARCHIVEORG) {
287 m_currentService = new ArchiveOrg(search_results);
290 connect(m_currentService, SIGNAL(gotMetaInfo(const QString)), this, SLOT(slotSetMetadata(const QString)));
291 connect(m_currentService, SIGNAL(gotMetaInfo(QMap <QString, QString>)), this, SLOT(slotDisplayMetaInfo(QMap <QString, QString>)));
292 connect(m_currentService, SIGNAL(maxPages(int)), page_number, SLOT(setMaximum(int)));
293 connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
294 connect(m_currentService, SIGNAL(gotThumb(const QString)), this, SLOT(slotLoadThumb(const QString)));
295 #if KDE_IS_VERSION(4,4,0)
296 connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
299 button_preview->setVisible(m_currentService->hasPreview);
300 button_import->setVisible(!m_currentService->inlineDownload);
301 search_info->setText(QString());
302 if (!search_text->text().isEmpty()) slotStartSearch();
305 void ResourceWidget::slotOnline()
307 button_search->setEnabled(true);
308 search_info->setText(QString());
311 void ResourceWidget::slotOffline()
313 button_search->setEnabled(false);
314 search_info->setText(i18n("You need to be online\n for searching"));
317 void ResourceWidget::slotNextPage()
319 int ix = page_number->value();
320 if (search_results->count() > 0) page_number->setValue(ix + 1);
323 void ResourceWidget::slotPreviousPage()
325 int ix = page_number->value();
326 if (ix > 1) page_number->setValue(ix - 1);
329 void ResourceWidget::parseLicense(const QString &licenseUrl)
332 if (licenseUrl.contains("/sampling+/"))
333 licenseName = "Sampling+";
334 else if (licenseUrl.contains("/by/"))
335 licenseName = "Attribution";
336 else if (licenseUrl.contains("/by-nd/"))
337 licenseName = "Attribution-NoDerivs";
338 else if (licenseUrl.contains("/by-nc-sa/"))
339 licenseName = "Attribution-NonCommercial-ShareAlike";
340 else if (licenseUrl.contains("/by-sa/"))
341 licenseName = "Attribution-ShareAlike";
342 else if (licenseUrl.contains("/by-nc/"))
343 licenseName = "Attribution-NonCommercial";
344 else if (licenseUrl.contains("/by-nc-nd/"))
345 licenseName = "Attribution-NonCommercial-NoDerivs";
346 else if (licenseUrl.contains("/publicdomain/zero/"))
347 licenseName = "Creative Commons 0";
348 else if (licenseUrl.endsWith("/publicdomain"))
349 licenseName = "Public Domain";
350 else licenseName = i18n("Unknown");
351 item_license->setText(licenseName);
352 item_license->setUrl(licenseUrl);
356 void ResourceWidget::slotOpenLink(const QUrl &url)
358 QString path = url.toEncoded();
359 if (path.endsWith("_import")) {
361 // import file in Kdenlive
369 void ResourceWidget::slotSetDescription(const QString desc)
375 void ResourceWidget::slotSetMetadata(const QString desc)
381 void ResourceWidget::slotSetImage(const QString desc)
383 m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
387 void ResourceWidget::slotSetTitle(const QString desc)
393 void ResourceWidget::updateLayout()
395 QString content = m_title;
396 if (!m_image.isEmpty()) content.append(m_image + "<br clear=\"all\" />");
397 if (!m_desc.isEmpty()) content.append(m_desc);
398 if (!m_meta.isEmpty()) content.append(m_meta);
399 info_browser->setHtml(content);