]> git.sesse.net Git - kdenlive/blob - src/utils/resourcewidget.cpp
Fix compilation without nepomuk
[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 #include <KPixmapSequence>
44 #include <KPixmapSequenceOverlayPainter>
45 #include <KFileItem>
46
47 #ifdef USE_NEPOMUK
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>
55 #endif
56 #endif
57
58 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
59         QDialog(parent),
60         m_folder(folder),
61         m_currentService(NULL)
62 {
63     setFont(KGlobalSettings::toolBarFont());
64     setupUi(this);
65     setAttribute(Qt::WA_DeleteOnClose);
66 #ifdef USE_QJSON
67     service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
68     service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
69 #endif
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) {
80         slotOffline();
81     }
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 &)));
88     
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"));
95
96     m_busyWidget = new KPixmapSequenceOverlayPainter(this);
97     m_busyWidget->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
98     m_busyWidget->setWidget(search_results->viewport());
99     
100     sound_box->setEnabled(false);
101     search_text->setFocus();
102 #ifdef USE_NEPOMUK
103 #if KDE_IS_VERSION(4,6,0)    
104     Nepomuk::ResourceManager::instance()->init();
105 #endif
106 #endif
107     slotChangeService();
108 }
109
110 ResourceWidget::~ResourceWidget()
111 {
112     if (m_currentService) delete m_currentService;
113     KIO::NetAccess::removeTempFile(m_tmpThumbFile);
114 }
115
116 void ResourceWidget::slotStartSearch(int page)
117 {
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);
123 }
124
125 void ResourceWidget::slotUpdateCurrentSound()
126 {
127     KIO::NetAccess::removeTempFile(m_tmpThumbFile);
128     if (!m_autoPlay->isChecked()) m_currentService->stopItemPreview(NULL);
129     item_license->clear();
130     m_title.clear();
131     m_image.clear();
132     m_desc.clear();
133     m_meta.clear();
134     QListWidgetItem *item = search_results->currentItem();
135     if (!item) {
136         sound_box->setEnabled(false);
137         return;
138     }
139     m_currentInfo = m_currentService->displayItemDetails(item);
140     
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>");
146     
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 />");
153     }
154     else if (!m_currentInfo.author.isEmpty())
155         title.append(m_currentInfo.author + "<br />");
156     else title.append("<br />");
157     
158     slotSetTitle(title);
159     if (!m_currentInfo.description.isEmpty()) slotSetDescription(m_currentInfo.description);
160     if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
161 }
162
163
164 void ResourceWidget::slotLoadThumb(const QString url)
165 {
166     KUrl img(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);
172             
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());
177             }
178             else {
179                 item_image->setScaledContents(true);
180                 item_image->setFixedHeight(newHeight);
181             }
182             item_image->setPixmap(pix);*/
183         }
184     }
185 }
186
187
188 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
189 {
190     if (metaInfo.contains("license")) {
191         parseLicense(metaInfo.value("license"));
192     }
193     if (metaInfo.contains("description")) {
194         slotSetDescription(metaInfo.value("description"));
195     }
196 }
197
198
199 void ResourceWidget::slotPlaySound()
200 {
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"));
205 }
206
207
208 void ResourceWidget::slotSaveItem(const QString originalUrl)
209 {
210     //if (m_currentUrl.isEmpty()) return;
211     QListWidgetItem *item = search_results->currentItem();
212     if (!item) return;
213     QString path = m_folder;
214     QString ext;
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;
220     }
221     else {
222         path.append(m_currentService->getDefaultDownloadName(item));
223         ext = m_currentService->getExtension(search_results->currentItem());
224     }
225     QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
226     KIO::UDSEntry entry;
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);
230     
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*)));
239     getJob->start();
240 }
241
242 void ResourceWidget::slotGotFile(KJob *job)
243 {
244     if (job->error() != 0 ) return;
245     KIO::FileCopyJob* copyJob = static_cast<KIO::FileCopyJob*>( job );
246     const KUrl filePath = copyJob->destUrl();
247 #ifdef USE_NEPOMUK
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(), 
257 #endif
258 #endif
259         emit addClip(filePath, QString());
260 }
261
262 void ResourceWidget::slotOpenUrl(const QString &url)
263 {
264     new KRun(KUrl(url), this);
265 }
266
267 void ResourceWidget::slotChangeService()
268 {
269     if (m_currentService) {
270         delete m_currentService;
271         m_currentService = NULL;
272     }
273     SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
274     if (service == FREESOUND) {
275         m_currentService = new FreeSound(search_results);
276     }
277     else if (service == OPENCLIPART) {
278         m_currentService = new OpenClipArt(search_results);
279     }
280     else if (service == ARCHIVEORG) {
281         m_currentService = new ArchiveOrg(search_results);
282     }
283
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()));
290     
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();
295 }
296
297 void ResourceWidget::slotOnline()
298 {
299     button_search->setEnabled(true);
300     search_info->setText(QString());
301 }
302
303 void ResourceWidget::slotOffline()
304 {
305     button_search->setEnabled(false);
306     search_info->setText(i18n("You need to be online\n for searching"));
307 }
308
309 void ResourceWidget::slotNextPage()
310 {
311     int ix = page_number->value();
312     if (search_results->count() > 0) page_number->setValue(ix + 1);
313 }
314
315 void ResourceWidget::slotPreviousPage()
316 {
317     int ix = page_number->value();
318     if (ix > 1) page_number->setValue(ix - 1);
319 }
320
321 void ResourceWidget::parseLicense(const QString &licenseUrl)
322 {
323     QString licenseName;
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);
345 }
346
347
348 void ResourceWidget::slotOpenLink(const QUrl &url)
349 {
350     QString path = url.toEncoded();
351     if (path.endsWith("_import")) {
352         path.chop(7);
353         // import file in Kdenlive
354         slotSaveItem(path);
355     }
356     else {
357         slotOpenUrl(path);
358     }
359 }
360
361 void ResourceWidget::slotSetDescription(const QString desc)
362 {
363     m_desc = desc;
364     updateLayout();
365 }
366
367 void ResourceWidget::slotSetMetadata(const QString desc)
368 {
369     m_meta = desc;
370     updateLayout();
371 }
372
373 void ResourceWidget::slotSetImage(const QString desc)
374 {
375     m_image = QString("<img src=\"%1\" style=\"max-height:150px\" max-width=\"%2px\" />").arg(desc).arg((int) (info_browser->width() * 0.9));
376     updateLayout();
377 }
378
379 void ResourceWidget::slotSetTitle(const QString desc)
380 {
381     m_title = desc;
382     updateLayout();
383 }
384
385 void ResourceWidget::updateLayout()
386 {
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);
392 }