]> git.sesse.net Git - kdenlive/blob - src/utils/resourcewidget.cpp
Cleanup online resource feature
[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
26 #include <QPushButton>
27 #include <QSpinBox>
28 #include <QListWidget>
29 #include <QDomDocument>
30
31 #include <KDebug>
32 #include "kdenlivesettings.h"
33 #include <KGlobalSettings>
34 #include <KMessageBox>
35 #include <KFileDialog>
36 #include <kio/job.h>
37 #include <KIO/NetAccess>
38 #include <Solid/Networking>
39 #include <KRun>
40
41 #ifdef USE_NEPOMUK
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>
48 #endif
49
50
51 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
52         QDialog(parent),
53         m_folder(folder),
54         m_currentService(NULL)
55 {
56     setFont(KGlobalSettings::toolBarFont());
57     setupUi(this);
58     setAttribute(Qt::WA_DeleteOnClose);
59 #ifdef USE_QJSON
60     service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
61 #endif
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) {
76         slotOffline();
77     }
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();
86     slotChangeService();
87 }
88
89 ResourceWidget::~ResourceWidget()
90 {
91     if (m_currentService) delete m_currentService;
92 }
93
94 void ResourceWidget::slotStartSearch(int page)
95 {
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);
102     /*QString uri;
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");
108     }
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));
113     }*/
114 }
115
116 void ResourceWidget::slotUpdateCurrentSound()
117 {
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();
123     if (!item) {
124         sound_box->setEnabled(false);
125         return;
126     }
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();*/
131     
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);
139     
140
141     KUrl img(m_currentInfo.imagePreview);
142     if (img.isEmpty()) return;
143     if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
144         QString tmpFile;
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());
151             }
152             else {
153                 sound_image->setScaledContents(true);
154                 sound_image->setFixedHeight(newHeight);
155             }
156             sound_image->setPixmap(pix);
157             KIO::NetAccess::removeTempFile(tmpFile);
158         }
159     }
160 }
161
162
163 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
164 {
165     if (metaInfo.contains("description")) {
166         item_description->setHtml(metaInfo.value("description"));
167         metaInfo.remove("description");
168     }
169     if (metaInfo.contains("license")) {
170         parseLicense(metaInfo.value("license"));
171         metaInfo.remove("license");
172     }
173     QMap<QString, QString>::const_iterator i = metaInfo.constBegin();
174     while (i != metaInfo.constEnd()) {
175         new QTreeWidgetItem(info_widget, QStringList() << i.key() << i.value());
176         ++i;
177     }
178     info_widget->resizeColumnToContents(0);
179     info_widget->resizeColumnToContents(1);
180 }
181
182
183 void ResourceWidget::slotPlaySound()
184 {
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"));
189 }
190
191
192 void ResourceWidget::slotForcePlaySound(bool play)
193 {
194     /*
195     if (m_service != FREESOUND) return;
196     m_previewProcess->close();
197     if (m_currentPreview.isEmpty()) return;
198     if (play)
199         m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
200     */
201 }
202
203 void ResourceWidget::slotPreviewStatusChanged(QProcess::ProcessState state)
204 {
205     /*if (state == QProcess::NotRunning)
206         button_preview->setText(i18n("Preview"));
207     else 
208         button_preview->setText(i18n("Stop"));*/
209 }
210
211 void ResourceWidget::slotSaveSound()
212 {
213     //if (m_currentUrl.isEmpty()) return;
214     QListWidgetItem *item = search_results->currentItem();
215     if (!item) return;
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);
224 #ifdef USE_NEPOMUK
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(), 
231 #endif
232         emit addClip(KUrl(saveUrl), QString());//, sound_name->url());
233     }
234 }
235
236 void ResourceWidget::slotOpenUrl(const QString &url)
237 {
238     new KRun(KUrl(url), this);
239 }
240
241 void ResourceWidget::slotChangeService()
242 {
243     if (m_currentService) {
244         delete m_currentService;
245         m_currentService = NULL;
246     }
247     SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
248     if (service == FREESOUND) {
249         m_currentService = new FreeSound(search_results);
250     }
251     else if (service == OPENCLIPART) {
252         m_currentService = new OpenClipArt(search_results);
253     }
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     
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();
263 }
264
265 void ResourceWidget::slotOnline()
266 {
267     button_search->setEnabled(true);
268     search_info->setText(QString());
269 }
270
271 void ResourceWidget::slotOffline()
272 {
273     button_search->setEnabled(false);
274     search_info->setText(i18n("You need to be online\n for searching"));
275 }
276
277 void ResourceWidget::slotNextPage()
278 {
279     int ix = page_number->value();
280     if (search_results->count() > 0) page_number->setValue(ix + 1);
281 }
282
283 void ResourceWidget::slotPreviousPage()
284 {
285     int ix = page_number->value();
286     if (ix > 1) page_number->setValue(ix - 1);
287 }
288
289 void ResourceWidget::parseLicense(const QString &licenseUrl)
290 {
291     QString licenseName;
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);
312 }
313