]> git.sesse.net Git - kdenlive/blob - src/utils/resourcewidget.cpp
Fix indent, Use const'ref
[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 #ifdef USE_NEPOMUKCORE
61 #include <Nepomuk2/Variant>
62 #include <Nepomuk2/Resource>
63 #include <Nepomuk2/ResourceManager>
64 #include <Nepomuk2/Vocabulary/NIE>
65 #include <Nepomuk2/Vocabulary/NCO>
66 #include <Nepomuk2/Vocabulary/NDO>
67 #endif
68
69 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
70     QDialog(parent),
71     m_folder(folder),
72     m_currentService(NULL)
73 {
74     setFont(KGlobalSettings::toolBarFont());
75     setupUi(this);
76     setAttribute(Qt::WA_DeleteOnClose);
77 #ifdef USE_QJSON
78     service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
79     service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
80 #endif
81     service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
82     setWindowTitle(i18n("Search Online Resources"));
83     info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
84     connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
85     connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
86     connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
87     connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
88     connect(item_license, SIGNAL(leftClickedUrl(QString)), this, SLOT(slotOpenUrl(QString)));
89     connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
90     if (Solid::Networking::status() == Solid::Networking::Unconnected) {
91         slotOffline();
92     }
93     connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
94     connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
95     connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
96     connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
97     connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
98     connect(info_browser, SIGNAL(anchorClicked(QUrl)), this, SLOT(slotOpenLink(QUrl)));
99     
100     m_autoPlay = new QAction(i18n("Auto Play"), this);
101     m_autoPlay->setCheckable(true);
102     QMenu *resourceMenu = new QMenu;
103     resourceMenu->addAction(m_autoPlay);
104     config_button->setMenu(resourceMenu);
105     config_button->setIcon(KIcon("configure"));
106
107 #if KDE_IS_VERSION(4,4,0)
108     m_busyWidget = new KPixmapSequenceOverlayPainter(this);
109     m_busyWidget->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
110     m_busyWidget->setWidget(search_results->viewport());
111 #endif
112     
113     sound_box->setEnabled(false);
114     search_text->setFocus();
115 #ifdef USE_NEPOMUK
116 #if KDE_IS_VERSION(4,6,0)    
117     Nepomuk::ResourceManager::instance()->init();
118 #endif
119 #endif
120     slotChangeService();
121 }
122
123 ResourceWidget::~ResourceWidget()
124 {
125     delete m_currentService;
126     KIO::NetAccess::removeTempFile(m_tmpThumbFile);
127 }
128
129 void ResourceWidget::slotStartSearch(int page)
130 {
131     page_number->blockSignals(true);
132     page_number->setValue(page);
133     page_number->blockSignals(false);
134 #if KDE_IS_VERSION(4,4,0)
135     m_busyWidget->start();
136 #endif
137     m_currentService->slotStartSearch(search_text->text(), page);
138 }
139
140 void ResourceWidget::slotUpdateCurrentSound()
141 {
142     KIO::NetAccess::removeTempFile(m_tmpThumbFile);
143     if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
144     item_license->clear();
145     m_title.clear();
146     m_image.clear();
147     m_desc.clear();
148     m_meta.clear();
149     QListWidgetItem *item = search_results->currentItem();
150     if (!item) {
151         sound_box->setEnabled(false);
152         return;
153     }
154     m_currentInfo = m_currentService->displayItemDetails(item);
155     
156     if (m_autoPlay->isChecked() && m_currentService->hasPreview) m_currentService->startItemPreview(item);
157     sound_box->setEnabled(true);
158     QString title = "<h3>" + m_currentInfo.itemName;
159     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"));
160     title.append("</h3>");
161     
162     if (!m_currentInfo.authorUrl.isEmpty()) {
163         title += QString("<a href=\"%1\">").arg(m_currentInfo.authorUrl);
164         if (!m_currentInfo.author.isEmpty())
165             title.append(m_currentInfo.author);
166         else title.append(i18n("Author"));
167         title.append("</a><br />");
168     }
169     else if (!m_currentInfo.author.isEmpty())
170         title.append(m_currentInfo.author + "<br />");
171     else title.append("<br />");
172     
173     slotSetTitle(title);
174     if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
175     if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
176 }
177
178
179 void ResourceWidget::slotLoadThumb(const QString &url)
180 {
181     KUrl img(url);
182     if (img.isEmpty()) return;
183     if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
184         if (KIO::NetAccess::download(img, m_tmpThumbFile, this)) {
185             slotSetImage(m_tmpThumbFile);
186             /*QPixmap pix(tmpFile);
187
188             int newHeight = pix.height() * item_image->width() / pix.width();
189             if (newHeight > 200) {
190                 item_image->setScaledContents(false);
191                 //item_image->setFixedHeight(item_image->width());
192             }
193             else {
194                 item_image->setScaledContents(true);
195                 item_image->setFixedHeight(newHeight);
196             }
197             item_image->setPixmap(pix);*/
198         }
199     }
200 }
201
202
203 void ResourceWidget::slotDisplayMetaInfo(const QMap<QString, QString> &metaInfo)
204 {
205     if (metaInfo.contains("license")) {
206         parseLicense(metaInfo.value("license"));
207     }
208     if (metaInfo.contains("description")) {
209         slotSetDescription(metaInfo.value("description"));
210     }
211 }
212
213
214 void ResourceWidget::slotPlaySound()
215 {
216     if (!m_currentService) return;
217     bool started = m_currentService->startItemPreview(search_results->currentItem());
218     if (started) button_preview->setText(i18n("Preview"));
219     else button_preview->setText(i18n("Stop"));
220 }
221
222
223 void ResourceWidget::slotSaveItem(const QString &originalUrl)
224 {
225     //if (m_currentUrl.isEmpty()) return;
226     QListWidgetItem *item = search_results->currentItem();
227     if (!item) return;
228     QString path = m_folder;
229     QString ext;
230     if (!path.endsWith('/')) path.append('/');
231     if (!originalUrl.isEmpty()) {
232         path.append(KUrl(originalUrl).fileName());
233         ext = "*." + KUrl(originalUrl).fileName().section('.', -1);
234         m_currentInfo.itemDownload = originalUrl;
235     }
236     else {
237         path.append(m_currentService->getDefaultDownloadName(item));
238         ext = m_currentService->getExtension(search_results->currentItem());
239     }
240     QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
241     KIO::UDSEntry entry;
242     KUrl srcUrl(m_currentInfo.itemDownload);
243     if (saveUrl.isEmpty() || !KIO::NetAccess::stat(srcUrl, entry, this)) return;
244     KIO::FileCopyJob * getJob = KIO::file_copy(srcUrl, KUrl(saveUrl), -1, KIO::Overwrite);
245     
246     KFileItem info(entry, srcUrl);
247     getJob->setSourceSize(info.size());
248     getJob->setProperty("license", item_license->text());
249     getJob->setProperty("licenseurl", item_license->url());
250     getJob->setProperty("originurl", m_currentInfo.itemDownload);
251     if (!m_currentInfo.authorUrl.isEmpty()) getJob->setProperty("author", m_currentInfo.authorUrl);
252     else if (!m_currentInfo.author.isEmpty()) getJob->setProperty("author", m_currentInfo.author);
253     connect(getJob, SIGNAL(result(KJob*)), this, SLOT(slotGotFile(KJob*)));
254     getJob->start();
255 }
256
257 void ResourceWidget::slotGotFile(KJob *job)
258 {
259     if (job->error() != 0 ) return;
260     KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
261     const KUrl filePath = copyJob->destUrl();
262 #ifdef USE_NEPOMUK
263 #if KDE_IS_VERSION(4,6,0)
264     Nepomuk::Resource res( filePath );
265     res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) job->property("license") );
266     res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) job->property("licenseurl") );
267     res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), (Nepomuk::Variant) job->property("originurl") );
268     res.setProperty( Nepomuk::Vocabulary::NCO::creator(), (Nepomuk::Variant) job->property("author") );
269     //res.setDescription(item_description->toPlainText());
270     //res.setProperty( Soprano::Vocabulary::NAO::description(),
271 #endif
272 #endif
273
274 #ifdef USE_NEPOMUKCORE
275     Nepomuk2::Resource res( filePath );
276     res.setProperty( Nepomuk2::Vocabulary::NIE::license(), (Nepomuk2::Variant) job->property("license") );
277     res.setProperty( Nepomuk2::Vocabulary::NIE::licenseType(), (Nepomuk2::Variant) job->property("licenseurl") );
278     res.setProperty( Nepomuk2::Vocabulary::NDO::copiedFrom(), (Nepomuk2::Variant) job->property("originurl") );
279     res.setProperty( Nepomuk2::Vocabulary::NCO::creator(), (Nepomuk2::Variant) job->property("author") );
280     //res.setDescription(item_description->toPlainText());
281     //res.setProperty( Soprano::Vocabulary::NAO::description(),
282 #endif
283
284     emit addClip(filePath, stringMap());
285 }
286
287 void ResourceWidget::slotOpenUrl(const QString &url)
288 {
289     new KRun(KUrl(url), this);
290 }
291
292 void ResourceWidget::slotChangeService()
293 {
294     if (m_currentService) {
295         delete m_currentService;
296         m_currentService = NULL;
297     }
298     SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
299     if (service == FREESOUND) {
300         m_currentService = new FreeSound(search_results);
301     }
302     else if (service == OPENCLIPART) {
303         m_currentService = new OpenClipArt(search_results);
304     }
305     else if (service == ARCHIVEORG) {
306         m_currentService = new ArchiveOrg(search_results);
307     }
308
309     connect(m_currentService, SIGNAL(gotMetaInfo(QString)), this, SLOT(slotSetMetadata(QString)));
310     connect(m_currentService, SIGNAL(gotMetaInfo(QMap<QString,QString>)), this, SLOT(slotDisplayMetaInfo(QMap<QString,QString>)));
311     connect(m_currentService, SIGNAL(maxPages(int)), this, SLOT(slotSetMaximum(int)));
312     connect(m_currentService, SIGNAL(searchInfo(QString)), search_info, SLOT(setText(QString)));
313     connect(m_currentService, SIGNAL(gotThumb(QString)), this, SLOT(slotLoadThumb(QString)));
314 #if KDE_IS_VERSION(4,4,0)
315     connect(m_currentService, SIGNAL(searchDone()), m_busyWidget, SLOT(stop()));
316 #endif
317     
318     button_preview->setVisible(m_currentService->hasPreview);
319     button_import->setVisible(!m_currentService->inlineDownload);
320     search_info->setText(QString());
321     if (!search_text->text().isEmpty()) slotStartSearch();
322 }
323
324 void ResourceWidget::slotSetMaximum(int max)
325 {
326     page_number->setMaximum(max);
327 }
328
329 void ResourceWidget::slotOnline()
330 {
331     button_search->setEnabled(true);
332     search_info->setText(QString());
333 }
334
335 void ResourceWidget::slotOffline()
336 {
337     button_search->setEnabled(false);
338     search_info->setText(i18n("You need to be online\n for searching"));
339 }
340
341 void ResourceWidget::slotNextPage()
342 {
343     int ix = page_number->value();
344     if (search_results->count() > 0) page_number->setValue(ix + 1);
345 }
346
347 void ResourceWidget::slotPreviousPage()
348 {
349     int ix = page_number->value();
350     if (ix > 1) page_number->setValue(ix - 1);
351 }
352
353 void ResourceWidget::parseLicense(const QString &licenseUrl)
354 {
355     QString licenseName;
356     if (licenseUrl.contains("/sampling+/"))
357         licenseName = "Sampling+";
358     else if (licenseUrl.contains("/by/"))
359         licenseName = "Attribution";
360     else if (licenseUrl.contains("/by-nd/"))
361         licenseName = "Attribution-NoDerivs";
362     else if (licenseUrl.contains("/by-nc-sa/"))
363         licenseName = "Attribution-NonCommercial-ShareAlike";
364     else if (licenseUrl.contains("/by-sa/"))
365         licenseName = "Attribution-ShareAlike";
366     else if (licenseUrl.contains("/by-nc/"))
367         licenseName = "Attribution-NonCommercial";
368     else if (licenseUrl.contains("/by-nc-nd/"))
369         licenseName = "Attribution-NonCommercial-NoDerivs";
370     else if (licenseUrl.contains("/publicdomain/zero/"))
371         licenseName = "Creative Commons 0";
372     else if (licenseUrl.endsWith("/publicdomain"))
373         licenseName = "Public Domain";
374     else licenseName = i18n("Unknown");
375     item_license->setText(licenseName);
376     item_license->setUrl(licenseUrl);
377 }
378
379
380 void ResourceWidget::slotOpenLink(const QUrl &url)
381 {
382     QString path = url.toEncoded();
383     if (path.endsWith("_import")) {
384         path.chop(7);
385         // import file in Kdenlive
386         slotSaveItem(path);
387     }
388     else {
389         slotOpenUrl(path);
390     }
391 }
392
393 void ResourceWidget::slotSetDescription(const QString &desc)
394 {
395     m_desc = desc;
396     updateLayout();
397 }
398
399 void ResourceWidget::slotSetMetadata(const QString &desc)
400 {
401     m_meta = desc;
402     updateLayout();
403 }
404
405 void ResourceWidget::slotSetImage(const QString &desc)
406 {
407     m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
408     updateLayout();
409 }
410
411 void ResourceWidget::slotSetTitle(const QString &desc)
412 {
413     m_title = desc;
414     updateLayout();
415 }
416
417 void ResourceWidget::updateLayout()
418 {
419     QString content = m_title;
420     if (!m_image.isEmpty()) content.append(m_image + "<br clear=\"all\" />");
421     if (!m_desc.isEmpty()) content.append(m_desc);
422     if (!m_meta.isEmpty()) content.append(m_meta);
423     info_browser->setHtml(content);
424 }
425
426
427 #include "resourcewidget.moc"