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 #include <KPixmapSequence>
44 #include <KPixmapSequenceOverlayPainter>
48 #if KDE_IS_VERSION(4,6,0)
49 #include <Nepomuk/Variant>
50 #include <Nepomuk/Resource>
51 #include <Nepomuk/ResourceManager>
52 #include <Nepomuk/Vocabulary/NIE>
53 #include <Nepomuk/Vocabulary/NCO>
54 #include <Nepomuk/Vocabulary/NDO>
58 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
61 m_currentService(NULL)
63 setFont(KGlobalSettings::toolBarFont());
65 setAttribute(Qt::WA_DeleteOnClose);
67 service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
68 service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
70 service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
71 setWindowTitle(i18n("Search Online Resources"));
72 info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
73 connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
74 connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
75 connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
76 connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
77 connect(item_license, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
78 connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
79 if (Solid::Networking::status() == Solid::Networking::Unconnected) {
82 connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
83 connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
84 connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
85 connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
86 connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
87 connect(info_browser, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(slotOpenLink(const QUrl &)));
89 m_autoPlay = new QAction(i18n("Auto Play"), this);
90 m_autoPlay->setCheckable(true);
91 QMenu *resourceMenu = new QMenu;
92 resourceMenu->addAction(m_autoPlay);
93 config_button->setMenu(resourceMenu);
94 config_button->setIcon(KIcon("configure"));
96 m_busyWidget = new KPixmapSequenceOverlayPainter(this);
97 m_busyWidget->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
98 m_busyWidget->setWidget(search_results->viewport());
100 sound_box->setEnabled(false);
101 search_text->setFocus();
103 #if KDE_IS_VERSION(4,6,0)
104 Nepomuk::ResourceManager::instance()->init();
110 ResourceWidget::~ResourceWidget()
112 if (m_currentService) delete m_currentService;
113 KIO::NetAccess::removeTempFile(m_tmpThumbFile);
116 void ResourceWidget::slotStartSearch(int page)
118 page_number->blockSignals(true);
119 page_number->setValue(page);
120 page_number->blockSignals(false);
121 m_busyWidget->start();
122 m_currentService->slotStartSearch(search_text->text(), page);
125 void ResourceWidget::slotUpdateCurrentSound()
127 KIO::NetAccess::removeTempFile(m_tmpThumbFile);
128 if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
129 item_license->clear();
134 QListWidgetItem *item = search_results->currentItem();
136 sound_box->setEnabled(false);
139 m_currentInfo = m_currentService->displayItemDetails(item);
141 if (m_autoPlay->isChecked() && m_currentService->hasPreview) m_currentService->startItemPreview(item);
142 sound_box->setEnabled(true);
143 QString title = "<h3>" + m_currentInfo.itemName;
144 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"));
145 title.append("</h3>");
147 if (!m_currentInfo.authorUrl.isEmpty()) {
148 title += QString("<a href=\"%1\">").arg(m_currentInfo.authorUrl);
149 if (!m_currentInfo.author.isEmpty())
150 title.append(m_currentInfo.author);
151 else title.append(i18n("Author"));
152 title.append("</a><br />");
154 else if (!m_currentInfo.author.isEmpty())
155 title.append(m_currentInfo.author + "<br />");
156 else title.append("<br />");
159 if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
160 if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
164 void ResourceWidget::slotLoadThumb(const QString url)
167 if (img.isEmpty()) return;
168 if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
169 if (KIO::NetAccess::download(img, m_tmpThumbFile, this)) {
170 slotSetImage(m_tmpThumbFile);
171 /*QPixmap pix(tmpFile);
173 int newHeight = pix.height() * item_image->width() / pix.width();
174 if (newHeight > 200) {
175 item_image->setScaledContents(false);
176 //item_image->setFixedHeight(item_image->width());
179 item_image->setScaledContents(true);
180 item_image->setFixedHeight(newHeight);
182 item_image->setPixmap(pix);*/
188 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
190 if (metaInfo.contains("license")) {
191 parseLicense(metaInfo.value("license"));
193 if (metaInfo.contains("description")) {
194 slotSetDescription(metaInfo.value("description"));
199 void ResourceWidget::slotPlaySound()
201 if (!m_currentService) return;
202 bool started = m_currentService->startItemPreview(search_results->currentItem());
203 if (started) button_preview->setText(i18n("Preview"));
204 else button_preview->setText(i18n("Stop"));
208 void ResourceWidget::slotSaveItem(const QString originalUrl)
210 //if (m_currentUrl.isEmpty()) return;
211 QListWidgetItem *item = search_results->currentItem();
213 QString path = m_folder;
215 if (!path.endsWith('/')) path.append('/');
216 if (!originalUrl.isEmpty()) {
217 path.append(KUrl(originalUrl).fileName());
218 ext = "*." + KUrl(originalUrl).fileName().section(".", -1);
219 m_currentInfo.itemDownload = originalUrl;
222 path.append(m_currentService->getDefaultDownloadName(item));
223 ext = m_currentService->getExtension(search_results->currentItem());
225 QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
227 KUrl srcUrl(m_currentInfo.itemDownload);
228 if (saveUrl.isEmpty() || !KIO::NetAccess::stat(srcUrl, entry, this)) return;
229 KIO::FileCopyJob * getJob = KIO::file_copy(srcUrl, KUrl(saveUrl), -1, KIO::Overwrite);
231 KFileItem info(entry, srcUrl);
232 getJob->setSourceSize(info.size());
233 getJob->setProperty("license", item_license->text());
234 getJob->setProperty("licenseurl", item_license->url());
235 getJob->setProperty("originurl", m_currentInfo.itemDownload);
236 if (!m_currentInfo.authorUrl.isEmpty()) getJob->setProperty("author", m_currentInfo.authorUrl);
237 else if (!m_currentInfo.author.isEmpty()) getJob->setProperty("author", m_currentInfo.author);
238 connect(getJob, SIGNAL(result(KJob*)), this, SLOT(slotGotFile(KJob*)));
242 void ResourceWidget::slotGotFile(KJob *job)
244 if (job->error() != 0 ) return;
245 KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
246 const KUrl filePath = copyJob->destUrl();
248 #if KDE_IS_VERSION(4,6,0)
249 Nepomuk::Resource res( filePath );
250 res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) job->property("license") );
251 res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) job->property("licenseurl") );
252 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
253 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
254 res.setProperty( Nepomuk::Vocabulary::NCO::creator(), (Nepomuk::Variant) job->property("author") );
255 //res.setDescription(item_description->toPlainText());
256 //res.setProperty( Soprano::Vocabulary::NAO::description(),
259 emit addClip(filePath, QString());
262 void ResourceWidget::slotOpenUrl(const QString &url)
264 new KRun(KUrl(url), this);
267 void ResourceWidget::slotChangeService()
269 if (m_currentService) {
270 delete m_currentService;
271 m_currentService = NULL;
273 SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
274 if (service == FREESOUND) {
275 m_currentService = new FreeSound(search_results);
277 else if (service == OPENCLIPART) {
278 m_currentService = new OpenClipArt(search_results);
280 else if (service == ARCHIVEORG) {
281 m_currentService = new ArchiveOrg(search_results);
284 connect(m_currentService, SIGNAL(gotMetaInfo(const QString)), this, SLOT(slotSetMetadata(const QString)));
285 connect(m_currentService, SIGNAL(gotMetaInfo(QMap <QString, QString>)), this, SLOT(slotDisplayMetaInfo(QMap <QString, QString>)));
286 connect(m_currentService, SIGNAL(maxPages(int)), page_number, SLOT(setMaximum(int)));
287 connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
288 connect(m_currentService, SIGNAL(gotThumb(const QString)), this, SLOT(slotLoadThumb(const QString)));
289 connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
291 button_preview->setVisible(m_currentService->hasPreview);
292 button_import->setVisible(!m_currentService->inlineDownload);
293 search_info->setText(QString());
294 if (!search_text->text().isEmpty()) slotStartSearch();
297 void ResourceWidget::slotOnline()
299 button_search->setEnabled(true);
300 search_info->setText(QString());
303 void ResourceWidget::slotOffline()
305 button_search->setEnabled(false);
306 search_info->setText(i18n("You need to be online\n for searching"));
309 void ResourceWidget::slotNextPage()
311 int ix = page_number->value();
312 if (search_results->count() > 0) page_number->setValue(ix + 1);
315 void ResourceWidget::slotPreviousPage()
317 int ix = page_number->value();
318 if (ix > 1) page_number->setValue(ix - 1);
321 void ResourceWidget::parseLicense(const QString &licenseUrl)
324 if (licenseUrl.contains("/sampling+/"))
325 licenseName = "Sampling+";
326 else if (licenseUrl.contains("/by/"))
327 licenseName = "Attribution";
328 else if (licenseUrl.contains("/by-nd/"))
329 licenseName = "Attribution-NoDerivs";
330 else if (licenseUrl.contains("/by-nc-sa/"))
331 licenseName = "Attribution-NonCommercial-ShareAlike";
332 else if (licenseUrl.contains("/by-sa/"))
333 licenseName = "Attribution-ShareAlike";
334 else if (licenseUrl.contains("/by-nc/"))
335 licenseName = "Attribution-NonCommercial";
336 else if (licenseUrl.contains("/by-nc-nd/"))
337 licenseName = "Attribution-NonCommercial-NoDerivs";
338 else if (licenseUrl.contains("/publicdomain/zero/"))
339 licenseName = "Creative Commons 0";
340 else if (licenseUrl.endsWith("/publicdomain"))
341 licenseName = "Public Domain";
342 else licenseName = i18n("Unknown");
343 item_license->setText(licenseName);
344 item_license->setUrl(licenseUrl);
348 void ResourceWidget::slotOpenLink(const QUrl &url)
350 QString path = url.toEncoded();
351 if (path.endsWith("_import")) {
353 // import file in Kdenlive
361 void ResourceWidget::slotSetDescription(const QString desc)
367 void ResourceWidget::slotSetMetadata(const QString desc)
373 void ResourceWidget::slotSetImage(const QString desc)
375 m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
379 void ResourceWidget::slotSetTitle(const QString desc)
385 void ResourceWidget::updateLayout()
387 QString content = m_title;
388 if (!m_image.isEmpty()) content.append(m_image + "<br clear=\"all\" />");
389 if (!m_desc.isEmpty()) content.append(m_desc);
390 if (!m_meta.isEmpty()) content.append(m_meta);
391 info_browser->setHtml(content);