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>
30 #include <QDomDocument>
33 #include <kdeversion.h>
34 #include "kdenlivesettings.h"
35 #include <KGlobalSettings>
36 #include <KMessageBox>
37 #include <KFileDialog>
39 #include <KIO/NetAccess>
40 #include <Solid/Networking>
44 #if KDE_IS_VERSION(4,6,0)
45 #include <Nepomuk/Variant>
46 #include <Nepomuk/Resource>
47 #include <Nepomuk/ResourceManager>
48 #include <Soprano/Vocabulary/NAO>
49 #include <Nepomuk/Vocabulary/NIE>
50 #include <Nepomuk/Vocabulary/NDO>
54 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
57 m_currentService(NULL)
59 setFont(KGlobalSettings::toolBarFont());
61 setAttribute(Qt::WA_DeleteOnClose);
63 service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
64 service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
66 service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
67 setWindowTitle(i18n("Search Online Resources"));
68 info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
69 connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
70 connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
71 connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
72 connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
73 connect(sound_author, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
74 connect(item_license, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
75 connect(sound_name, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
76 connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
77 item_image->setFixedWidth(180);
78 if (Solid::Networking::status() == Solid::Networking::Unconnected) {
81 connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
82 connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
83 connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
84 connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
85 connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
86 connect(info_browser, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(slotOpenLink(const QUrl &)));
88 sound_box->setEnabled(false);
89 search_text->setFocus();
91 #if KDE_IS_VERSION(4,6,0)
92 Nepomuk::ResourceManager::instance()->init();
98 ResourceWidget::~ResourceWidget()
100 if (m_currentService) delete m_currentService;
103 void ResourceWidget::slotStartSearch(int page)
105 page_number->blockSignals(true);
106 page_number->setValue(page);
107 page_number->blockSignals(false);
108 m_currentService->slotStartSearch(search_text->text(), page);
111 void ResourceWidget::slotUpdateCurrentSound()
113 item_image->setEnabled(false);
114 if (!sound_autoplay->isChecked()) m_currentService->stopItemPreview(NULL);
115 info_browser->clear();
116 item_license->clear();
117 QListWidgetItem *item = search_results->currentItem();
119 sound_box->setEnabled(false);
122 m_currentInfo = m_currentService->displayItemDetails(item);
124 if (sound_autoplay->isChecked()) m_currentService->startItemPreview(item);
125 sound_box->setEnabled(true);
126 sound_name->setText(item->text());
127 sound_name->setUrl(m_currentInfo.infoUrl);
128 sound_author->setText(m_currentInfo.author);
129 sound_author->setUrl(m_currentInfo.authorUrl);
130 if (!m_currentInfo.description.isEmpty()) info_browser->setHtml("<em>" + m_currentInfo.description + "</em>");
131 if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
135 void ResourceWidget::slotLoadThumb(const QString url)
138 if (img.isEmpty()) return;
139 if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
141 if (KIO::NetAccess::download(img, tmpFile, this)) {
142 QPixmap pix(tmpFile);
143 int newHeight = pix.height() * item_image->width() / pix.width();
144 if (newHeight > 200) {
145 item_image->setScaledContents(false);
146 //item_image->setFixedHeight(item_image->width());
149 item_image->setScaledContents(true);
150 item_image->setFixedHeight(newHeight);
152 item_image->setPixmap(pix);
153 KIO::NetAccess::removeTempFile(tmpFile);
156 item_image->setEnabled(true);
160 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
162 if (metaInfo.contains("license")) {
163 parseLicense(metaInfo.value("license"));
168 void ResourceWidget::slotPlaySound()
170 if (!m_currentService) return;
171 bool started = m_currentService->startItemPreview(search_results->currentItem());
172 if (started) button_preview->setText(i18n("Preview"));
173 else button_preview->setText(i18n("Stop"));
177 void ResourceWidget::slotForcePlaySound(bool play)
180 if (m_service != FREESOUND) return;
181 m_previewProcess->close();
182 if (m_currentPreview.isEmpty()) return;
184 m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
188 void ResourceWidget::slotPreviewStatusChanged(QProcess::ProcessState state)
190 /*if (state == QProcess::NotRunning)
191 button_preview->setText(i18n("Preview"));
193 button_preview->setText(i18n("Stop"));*/
196 void ResourceWidget::slotSaveItem(const QString originalUrl)
198 //if (m_currentUrl.isEmpty()) return;
199 QListWidgetItem *item = search_results->currentItem();
201 QString path = m_folder;
203 if (!path.endsWith('/')) path.append('/');
204 if (!originalUrl.isEmpty()) {
205 path.append(KUrl(originalUrl).fileName());
206 ext = "*." + KUrl(originalUrl).fileName().section(".", -1);
207 m_currentInfo.itemDownload = originalUrl;
210 path.append(m_currentService->getDefaultDownloadName(item));
211 ext = m_currentService->getExtension(search_results->currentItem());
213 QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
214 if (saveUrl.isEmpty()) return;
215 if (KIO::NetAccess::download(KUrl(m_currentInfo.itemDownload), saveUrl, this)) {
216 const KUrl filePath = KUrl(saveUrl);
218 #if KDE_IS_VERSION(4,6,0)
219 Nepomuk::Resource res( filePath );
220 res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) item_license->text() );
221 res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) item_license->url() );
222 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), sound_name->url() );
223 //res.setDescription(item_description->toPlainText());
224 //res.setProperty( Soprano::Vocabulary::NAO::description(),
227 emit addClip(KUrl(saveUrl), QString());//, sound_name->url());
231 void ResourceWidget::slotOpenUrl(const QString &url)
233 new KRun(KUrl(url), this);
236 void ResourceWidget::slotChangeService()
238 if (m_currentService) {
239 delete m_currentService;
240 m_currentService = NULL;
242 SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
243 if (service == FREESOUND) {
244 m_currentService = new FreeSound(search_results);
246 else if (service == OPENCLIPART) {
247 m_currentService = new OpenClipArt(search_results);
249 else if (service == ARCHIVEORG) {
250 m_currentService = new ArchiveOrg(search_results);
253 connect(m_currentService, SIGNAL(gotMetaInfo(const QString)), this, SLOT(slotGotMetaInfo(const QString)));
254 connect(m_currentService, SIGNAL(gotMetaInfo(QMap <QString, QString>)), this, SLOT(slotDisplayMetaInfo(QMap <QString, QString>)));
255 connect(m_currentService, SIGNAL(maxPages(int)), page_number, SLOT(setMaximum(int)));
256 connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
257 connect(m_currentService, SIGNAL(gotThumb(const QString)), this, SLOT(slotLoadThumb(const QString)));
259 button_preview->setVisible(m_currentService->hasPreview);
260 button_import->setVisible(!m_currentService->inlineDownload);
261 sound_autoplay->setVisible(m_currentService->hasPreview);
262 search_info->setText(QString());
263 info_browser->setVisible(m_currentService->hasMetadata);
264 if (!search_text->text().isEmpty()) slotStartSearch();
267 void ResourceWidget::slotOnline()
269 button_search->setEnabled(true);
270 search_info->setText(QString());
273 void ResourceWidget::slotOffline()
275 button_search->setEnabled(false);
276 search_info->setText(i18n("You need to be online\n for searching"));
279 void ResourceWidget::slotNextPage()
281 int ix = page_number->value();
282 if (search_results->count() > 0) page_number->setValue(ix + 1);
285 void ResourceWidget::slotPreviousPage()
287 int ix = page_number->value();
288 if (ix > 1) page_number->setValue(ix - 1);
291 void ResourceWidget::parseLicense(const QString &licenseUrl)
294 if (licenseUrl.contains("/sampling+/"))
295 licenseName = "Sampling+";
296 else if (licenseUrl.contains("/by/"))
297 licenseName = "Attribution";
298 else if (licenseUrl.contains("/by-nd/"))
299 licenseName = "Attribution-NoDerivs";
300 else if (licenseUrl.contains("/by-nc-sa/"))
301 licenseName = "Attribution-NonCommercial-ShareAlike";
302 else if (licenseUrl.contains("/by-sa/"))
303 licenseName = "Attribution-ShareAlike";
304 else if (licenseUrl.contains("/by-nc/"))
305 licenseName = "Attribution-NonCommercial";
306 else if (licenseUrl.contains("/by-nc-nd/"))
307 licenseName = "Attribution-NonCommercial-NoDerivs";
308 else if (licenseUrl.contains("/publicdomain/zero/"))
309 licenseName = "Creative Commons 0";
310 else if (licenseUrl.endsWith("/publicdomain"))
311 licenseName = "Public Domain";
312 else licenseName = i18n("Unknown");
313 item_license->setText(licenseName);
314 item_license->setUrl(licenseUrl);
317 void ResourceWidget::slotGotMetaInfo(const QString info)
319 info_browser->setHtml(info_browser->toHtml() + info);
323 void ResourceWidget::slotOpenLink(const QUrl &url)
325 QString path = url.toEncoded();
326 if (path.endsWith("_preview")) {
331 // import file in Kdenlive