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(QString)), this, SLOT(slotOpenUrl(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(QUrl)), this, SLOT(slotOpenLink(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 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(const 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::NCO::creator(), (Nepomuk::Variant) job->property("author") );
260 //res.setDescription(item_description->toPlainText());
261 //res.setProperty( Soprano::Vocabulary::NAO::description(),
264 emit addClip(filePath, stringMap());
267 void ResourceWidget::slotOpenUrl(const QString &url)
269 new KRun(KUrl(url), this);
272 void ResourceWidget::slotChangeService()
274 if (m_currentService) {
275 delete m_currentService;
276 m_currentService = NULL;
278 SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
279 if (service == FREESOUND) {
280 m_currentService = new FreeSound(search_results);
282 else if (service == OPENCLIPART) {
283 m_currentService = new OpenClipArt(search_results);
285 else if (service == ARCHIVEORG) {
286 m_currentService = new ArchiveOrg(search_results);
289 connect(m_currentService, SIGNAL(gotMetaInfo(QString)), this, SLOT(slotSetMetadata(QString)));
290 connect(m_currentService, SIGNAL(gotMetaInfo(QMap<QString,QString>)), this, SLOT(slotDisplayMetaInfo(QMap<QString,QString>)));
291 connect(m_currentService, SIGNAL(maxPages(int)), this, SLOT(slotSetMaximum(int)));
292 connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
293 connect(m_currentService, SIGNAL(gotThumb(QString)), this, SLOT(slotLoadThumb(QString)));
294 #if KDE_IS_VERSION(4,4,0)
295 connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
298 button_preview->setVisible(m_currentService->hasPreview);
299 button_import->setVisible(!m_currentService->inlineDownload);
300 search_info->setText(QString());
301 if (!search_text->text().isEmpty()) slotStartSearch();
304 void ResourceWidget::slotSetMaximum(int max)
306 page_number->setMaximum(max);
309 void ResourceWidget::slotOnline()
311 button_search->setEnabled(true);
312 search_info->setText(QString());
315 void ResourceWidget::slotOffline()
317 button_search->setEnabled(false);
318 search_info->setText(i18n("You need to be online\n for searching"));
321 void ResourceWidget::slotNextPage()
323 int ix = page_number->value();
324 if (search_results->count() > 0) page_number->setValue(ix + 1);
327 void ResourceWidget::slotPreviousPage()
329 int ix = page_number->value();
330 if (ix > 1) page_number->setValue(ix - 1);
333 void ResourceWidget::parseLicense(const QString &licenseUrl)
336 if (licenseUrl.contains("/sampling+/"))
337 licenseName = "Sampling+";
338 else if (licenseUrl.contains("/by/"))
339 licenseName = "Attribution";
340 else if (licenseUrl.contains("/by-nd/"))
341 licenseName = "Attribution-NoDerivs";
342 else if (licenseUrl.contains("/by-nc-sa/"))
343 licenseName = "Attribution-NonCommercial-ShareAlike";
344 else if (licenseUrl.contains("/by-sa/"))
345 licenseName = "Attribution-ShareAlike";
346 else if (licenseUrl.contains("/by-nc/"))
347 licenseName = "Attribution-NonCommercial";
348 else if (licenseUrl.contains("/by-nc-nd/"))
349 licenseName = "Attribution-NonCommercial-NoDerivs";
350 else if (licenseUrl.contains("/publicdomain/zero/"))
351 licenseName = "Creative Commons 0";
352 else if (licenseUrl.endsWith("/publicdomain"))
353 licenseName = "Public Domain";
354 else licenseName = i18n("Unknown");
355 item_license->setText(licenseName);
356 item_license->setUrl(licenseUrl);
360 void ResourceWidget::slotOpenLink(const QUrl &url)
362 QString path = url.toEncoded();
363 if (path.endsWith("_import")) {
365 // import file in Kdenlive
373 void ResourceWidget::slotSetDescription(const QString &desc)
379 void ResourceWidget::slotSetMetadata(const QString &desc)
385 void ResourceWidget::slotSetImage(const QString &desc)
387 m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
391 void ResourceWidget::slotSetTitle(const QString &desc)
397 void ResourceWidget::updateLayout()
399 QString content = m_title;
400 if (!m_image.isEmpty()) content.append(m_image + "<br clear=\"all\" />");
401 if (!m_desc.isEmpty()) content.append(m_desc);
402 if (!m_meta.isEmpty()) content.append(m_meta);
403 info_browser->setHtml(content);
407 #include "resourcewidget.moc"