]> git.sesse.net Git - kdenlive/blob - src/utils/resourcewidget.cpp
Cleanup
[kdenlive] / src / utils / resourcewidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21
22 #include "resourcewidget.h"
23 #include "freesound.h"
24 #include "openclipart.h"
25 #include "archiveorg.h"
26
27 #include <QPushButton>
28 #include <QSpinBox>
29 #include <QListWidget>
30 #include <QAction>
31 #include <QMenu>
32
33 #include <KDebug>
34 #include <kdeversion.h>
35 #include "kdenlivesettings.h"
36 #include <KGlobalSettings>
37 #include <KMessageBox>
38 #include <KFileDialog>
39 #include <kio/job.h>
40 #include <KIO/NetAccess>
41 #include <Solid/Networking>
42 #include <KRun>
43 #if KDE_IS_VERSION(4,4,0)
44 #include <KPixmapSequence>
45 #include <KPixmapSequenceOverlayPainter>
46 #endif
47 #include <KFileItem>
48
49 #ifdef USE_NEPOMUK
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>
57 #endif
58 #endif
59
60 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
61         QDialog(parent),
62         m_folder(folder),
63         m_currentService(NULL)
64 {
65     setFont(KGlobalSettings::toolBarFont());
66     setupUi(this);
67     setAttribute(Qt::WA_DeleteOnClose);
68 #ifdef USE_QJSON
69     service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
70     service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
71 #endif
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) {
82         slotOffline();
83     }
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 &)));
90     
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"));
97
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());
102 #endif
103     
104     sound_box->setEnabled(false);
105     search_text->setFocus();
106 #ifdef USE_NEPOMUK
107 #if KDE_IS_VERSION(4,6,0)    
108     Nepomuk::ResourceManager::instance()->init();
109 #endif
110 #endif
111     slotChangeService();
112 }
113
114 ResourceWidget::~ResourceWidget()
115 {
116     if (m_currentService) delete m_currentService;
117     KIO::NetAccess::removeTempFile(m_tmpThumbFile);
118 }
119
120 void ResourceWidget::slotStartSearch(int page)
121 {
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();
127 #endif
128     m_currentService->slotStartSearch(search_text->text(), page);
129 }
130
131 void ResourceWidget::slotUpdateCurrentSound()
132 {
133     KIO::NetAccess::removeTempFile(m_tmpThumbFile);
134     if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
135     item_license->clear();
136     m_title.clear();
137     m_image.clear();
138     m_desc.clear();
139     m_meta.clear();
140     QListWidgetItem *item = search_results->currentItem();
141     if (!item) {
142         sound_box->setEnabled(false);
143         return;
144     }
145     m_currentInfo = m_currentService->displayItemDetails(item);
146     
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>");
152     
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 />");
159     }
160     else if (!m_currentInfo.author.isEmpty())
161         title.append(m_currentInfo.author + "<br />");
162     else title.append("<br />");
163     
164     slotSetTitle(title);
165     if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
166     if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
167 }
168
169
170 void ResourceWidget::slotLoadThumb(const QString url)
171 {
172     KUrl img(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);
178             
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());
183             }
184             else {
185                 item_image->setScaledContents(true);
186                 item_image->setFixedHeight(newHeight);
187             }
188             item_image->setPixmap(pix);*/
189         }
190     }
191 }
192
193
194 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
195 {
196     if (metaInfo.contains("license")) {
197         parseLicense(metaInfo.value("license"));
198     }
199     if (metaInfo.contains("description")) {
200         slotSetDescription(metaInfo.value("description"));
201     }
202 }
203
204
205 void ResourceWidget::slotPlaySound()
206 {
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"));
211 }
212
213
214 void ResourceWidget::slotSaveItem(const QString originalUrl)
215 {
216     //if (m_currentUrl.isEmpty()) return;
217     QListWidgetItem *item = search_results->currentItem();
218     if (!item) return;
219     QString path = m_folder;
220     QString ext;
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;
226     }
227     else {
228         path.append(m_currentService->getDefaultDownloadName(item));
229         ext = m_currentService->getExtension(search_results->currentItem());
230     }
231     QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
232     KIO::UDSEntry entry;
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);
236     
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*)));
245     getJob->start();
246 }
247
248 void ResourceWidget::slotGotFile(KJob *job)
249 {
250     if (job->error() != 0 ) return;
251     KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
252     const KUrl filePath = copyJob->destUrl();
253 #ifdef USE_NEPOMUK
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(), 
262 #endif
263 #endif
264         emit addClip(filePath, stringMap());
265 }
266
267 void ResourceWidget::slotOpenUrl(const QString &url)
268 {
269     new KRun(KUrl(url), this);
270 }
271
272 void ResourceWidget::slotChangeService()
273 {
274     if (m_currentService) {
275         delete m_currentService;
276         m_currentService = NULL;
277     }
278     SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
279     if (service == FREESOUND) {
280         m_currentService = new FreeSound(search_results);
281     }
282     else if (service == OPENCLIPART) {
283         m_currentService = new OpenClipArt(search_results);
284     }
285     else if (service == ARCHIVEORG) {
286         m_currentService = new ArchiveOrg(search_results);
287     }
288
289     connect(m_currentService, SIGNAL(gotMetaInfo(const QString)), this, SLOT(slotSetMetadata(const 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(const QString)), this, SLOT(slotLoadThumb(const QString)));
294 #if KDE_IS_VERSION(4,4,0)
295     connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
296 #endif
297     
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();
302 }
303
304 void ResourceWidget::slotSetMaximum(int max)
305 {
306     page_number->setMaximum(max);
307 }
308
309 void ResourceWidget::slotOnline()
310 {
311     button_search->setEnabled(true);
312     search_info->setText(QString());
313 }
314
315 void ResourceWidget::slotOffline()
316 {
317     button_search->setEnabled(false);
318     search_info->setText(i18n("You need to be online\n for searching"));
319 }
320
321 void ResourceWidget::slotNextPage()
322 {
323     int ix = page_number->value();
324     if (search_results->count() > 0) page_number->setValue(ix + 1);
325 }
326
327 void ResourceWidget::slotPreviousPage()
328 {
329     int ix = page_number->value();
330     if (ix > 1) page_number->setValue(ix - 1);
331 }
332
333 void ResourceWidget::parseLicense(const QString &licenseUrl)
334 {
335     QString licenseName;
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);
357 }
358
359
360 void ResourceWidget::slotOpenLink(const QUrl &url)
361 {
362     QString path = url.toEncoded();
363     if (path.endsWith("_import")) {
364         path.chop(7);
365         // import file in Kdenlive
366         slotSaveItem(path);
367     }
368     else {
369         slotOpenUrl(path);
370     }
371 }
372
373 void ResourceWidget::slotSetDescription(const QString desc)
374 {
375     m_desc = desc;
376     updateLayout();
377 }
378
379 void ResourceWidget::slotSetMetadata(const QString desc)
380 {
381     m_meta = desc;
382     updateLayout();
383 }
384
385 void ResourceWidget::slotSetImage(const QString desc)
386 {
387     m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
388     updateLayout();
389 }
390
391 void ResourceWidget::slotSetTitle(const QString desc)
392 {
393     m_title = desc;
394     updateLayout();
395 }
396
397 void ResourceWidget::updateLayout()
398 {
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);
404 }
405