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"
26 #include <QPushButton>
28 #include <QListWidget>
29 #include <QDomDocument>
32 #include "kdenlivesettings.h"
33 #include <KGlobalSettings>
34 #include <KMessageBox>
35 #include <KFileDialog>
37 #include <KIO/NetAccess>
38 #include <Solid/Networking>
42 #include <Nepomuk/Variant>
43 #include <Nepomuk/Resource>
44 #include <Nepomuk/ResourceManager>
45 #include <Soprano/Vocabulary/NAO>
46 #include <Nepomuk/Vocabulary/NIE>
47 #include <Nepomuk/Vocabulary/NDO>
51 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
54 m_currentService(NULL)
56 setFont(KGlobalSettings::toolBarFont());
58 setAttribute(Qt::WA_DeleteOnClose);
60 service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
62 service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
63 setWindowTitle(i18n("Search Online Resources"));
64 info_widget->setStyleSheet(QString("QTreeWidget { background-color: transparent;}"));
65 item_description->setStyleSheet(QString("KTextBrowser { background-color: transparent;}"));
66 connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
67 connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
68 connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
69 connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveSound()));
70 connect(sound_author, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
71 connect(item_license, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
72 connect(sound_name, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
73 connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
74 sound_image->setFixedWidth(180);
75 if (Solid::Networking::status() == Solid::Networking::Unconnected) {
78 connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
79 connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
80 connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
81 connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
82 connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
83 sound_box->setEnabled(false);
84 search_text->setFocus();
85 Nepomuk::ResourceManager::instance()->init();
89 ResourceWidget::~ResourceWidget()
91 if (m_currentService) delete m_currentService;
94 void ResourceWidget::slotStartSearch(int page)
96 /*m_currentPreview.clear();
97 m_currentUrl.clear();*/
98 page_number->blockSignals(true);
99 page_number->setValue(page);
100 page_number->blockSignals(false);
101 m_currentService->slotStartSearch(search_text->text(), page);
103 if (m_service == FREESOUND) {
104 uri = "http://www.freesound.org/api/sounds/search/?q=";
105 uri.append(search_text->text());
106 if (page > 1) uri.append("&p=" + QString::number(page));
107 uri.append("&api_key=a1772c8236e945a4bee30a64058dabf8");
109 else if (m_service == OPENCLIPART) {
110 uri = "http://openclipart.org/api/search/?query=";
111 uri.append(search_text->text());
112 if (page > 1) uri.append("&page=" + QString::number(page));
116 void ResourceWidget::slotUpdateCurrentSound()
118 if (!sound_autoplay->isChecked()) m_currentService->stopItemPreview(NULL);
119 info_widget->clear();
120 item_description->clear();
121 item_license->clear();
122 QListWidgetItem *item = search_results->currentItem();
124 sound_box->setEnabled(false);
127 m_currentInfo = m_currentService->displayItemDetails(item);
128 /*m_currentPreview = item->data(previewRole).toString();
129 m_currentUrl = item->data(downloadRole).toString();
130 m_currentId = item->data(idRole).toInt();*/
132 if (sound_autoplay->isChecked()) m_currentService->startItemPreview(item);
133 sound_box->setEnabled(true);
134 sound_name->setText(item->text());
135 sound_name->setUrl(m_currentInfo.infoUrl);
136 sound_author->setText(m_currentInfo.author);
137 sound_author->setUrl(m_currentInfo.authorUrl);
138 item_description->setHtml(m_currentInfo.description);
141 KUrl img(m_currentInfo.imagePreview);
142 if (img.isEmpty()) return;
143 if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
145 if (KIO::NetAccess::download(img, tmpFile, this)) {
146 QPixmap pix(tmpFile);
147 int newHeight = pix.height() * sound_image->width() / pix.width();
148 if (newHeight > 200) {
149 sound_image->setScaledContents(false);
150 //sound_image->setFixedHeight(sound_image->width());
153 sound_image->setScaledContents(true);
154 sound_image->setFixedHeight(newHeight);
156 sound_image->setPixmap(pix);
157 KIO::NetAccess::removeTempFile(tmpFile);
163 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
165 if (metaInfo.contains("description")) {
166 item_description->setHtml(metaInfo.value("description"));
167 metaInfo.remove("description");
169 if (metaInfo.contains("license")) {
170 parseLicense(metaInfo.value("license"));
171 metaInfo.remove("license");
173 QMap<QString, QString>::const_iterator i = metaInfo.constBegin();
174 while (i != metaInfo.constEnd()) {
175 new QTreeWidgetItem(info_widget, QStringList() << i.key() << i.value());
178 info_widget->resizeColumnToContents(0);
179 info_widget->resizeColumnToContents(1);
183 void ResourceWidget::slotPlaySound()
185 if (!m_currentService) return;
186 bool started = m_currentService->startItemPreview(search_results->currentItem());
187 if (started) button_preview->setText(i18n("Preview"));
188 else button_preview->setText(i18n("Stop"));
192 void ResourceWidget::slotForcePlaySound(bool play)
195 if (m_service != FREESOUND) return;
196 m_previewProcess->close();
197 if (m_currentPreview.isEmpty()) return;
199 m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
203 void ResourceWidget::slotPreviewStatusChanged(QProcess::ProcessState state)
205 /*if (state == QProcess::NotRunning)
206 button_preview->setText(i18n("Preview"));
208 button_preview->setText(i18n("Stop"));*/
211 void ResourceWidget::slotSaveSound()
213 //if (m_currentUrl.isEmpty()) return;
214 QListWidgetItem *item = search_results->currentItem();
216 QString path = m_folder;
217 if (!path.endsWith('/')) path.append('/');
218 path.append(m_currentService->getDefaultDownloadName(item));
219 QString ext = m_currentService->getExtension(search_results->currentItem());
220 QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
221 if (saveUrl.isEmpty()) return;
222 if (KIO::NetAccess::download(KUrl(m_currentInfo.itemDownload), saveUrl, this)) {
223 const KUrl filePath = KUrl(saveUrl);
225 Nepomuk::Resource res( filePath );
226 res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) item_license->text() );
227 res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) item_license->url() );
228 res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), sound_name->url() );
229 //res.setDescription(item_description->toPlainText());
230 //res.setProperty( Soprano::Vocabulary::NAO::description(),
232 emit addClip(KUrl(saveUrl), QString());//, sound_name->url());
236 void ResourceWidget::slotOpenUrl(const QString &url)
238 new KRun(KUrl(url), this);
241 void ResourceWidget::slotChangeService()
243 if (m_currentService) {
244 delete m_currentService;
245 m_currentService = NULL;
247 SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
248 if (service == FREESOUND) {
249 m_currentService = new FreeSound(search_results);
251 else if (service == OPENCLIPART) {
252 m_currentService = new OpenClipArt(search_results);
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)));
258 button_preview->setVisible(m_currentService->hasPreview);
259 sound_autoplay->setVisible(m_currentService->hasPreview);
260 search_info->setText(QString());
261 info_widget->setVisible(m_currentService->hasMetadata);
262 if (!search_text->text().isEmpty()) slotStartSearch();
265 void ResourceWidget::slotOnline()
267 button_search->setEnabled(true);
268 search_info->setText(QString());
271 void ResourceWidget::slotOffline()
273 button_search->setEnabled(false);
274 search_info->setText(i18n("You need to be online\n for searching"));
277 void ResourceWidget::slotNextPage()
279 int ix = page_number->value();
280 if (search_results->count() > 0) page_number->setValue(ix + 1);
283 void ResourceWidget::slotPreviousPage()
285 int ix = page_number->value();
286 if (ix > 1) page_number->setValue(ix - 1);
289 void ResourceWidget::parseLicense(const QString &licenseUrl)
292 if (licenseUrl.contains("/sampling+/"))
293 licenseName = "Sampling+";
294 else if (licenseUrl.contains("/by/"))
295 licenseName = "Attribution";
296 else if (licenseUrl.contains("/by-nd/"))
297 licenseName = "Attribution-NoDerivs";
298 else if (licenseUrl.contains("/by-nc-sa/"))
299 licenseName = "Attribution-NonCommercial-ShareAlike";
300 else if (licenseUrl.contains("/by-sa/"))
301 licenseName = "Attribution-ShareAlike";
302 else if (licenseUrl.contains("/by-nc/"))
303 licenseName = "Attribution-NonCommercial";
304 else if (licenseUrl.contains("/by-nc-nd/"))
305 licenseName = "Attribution-NonCommercial-NoDerivs";
306 else if (licenseUrl.contains("/publicdomain/zero/"))
307 licenseName = "Creative Commons 0";
308 else if (licenseUrl.endsWith("/publicdomain"))
309 licenseName = "Public Domain";
310 item_license->setText(i18n("License: %1", licenseName));
311 item_license->setUrl(licenseUrl);