]> git.sesse.net Git - kdenlive/blob - src/utils/resourcewidget.cpp
progress on online resources, archive.org now basically works
[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 <QDomDocument>
31
32 #include <KDebug>
33 #include <kdeversion.h>
34 #include "kdenlivesettings.h"
35 #include <KGlobalSettings>
36 #include <KMessageBox>
37 #include <KFileDialog>
38 #include <kio/job.h>
39 #include <KIO/NetAccess>
40 #include <Solid/Networking>
41 #include <KRun>
42
43 #ifdef USE_NEPOMUK
44 #if KDE_IS_VERSION(4,6,0)
45 #include <Nepomuk/Variant>
46 #include <Nepomuk/Resource>
47 #include <Nepomuk/ResourceManager>
48 #include <Soprano/Vocabulary/NAO>
49 #include <Nepomuk/Vocabulary/NIE>
50 #include <Nepomuk/Vocabulary/NDO>
51 #endif
52 #endif
53
54 ResourceWidget::ResourceWidget(const QString & folder, QWidget * parent) :
55         QDialog(parent),
56         m_folder(folder),
57         m_currentService(NULL)
58 {
59     setFont(KGlobalSettings::toolBarFont());
60     setupUi(this);
61     setAttribute(Qt::WA_DeleteOnClose);
62 #ifdef USE_QJSON
63     service_list->addItem(i18n("Freesound Audio Library"), FREESOUND);
64     service_list->addItem(i18n("Archive.org Video Library"), ARCHIVEORG);
65 #endif
66     service_list->addItem(i18n("Open Clip Art Graphic Library"), OPENCLIPART);
67     setWindowTitle(i18n("Search Online Resources"));
68     info_browser->setStyleSheet(QString("QTextBrowser { background-color: transparent;}"));
69     connect(button_search, SIGNAL(clicked()), this, SLOT(slotStartSearch()));
70     connect(search_results, SIGNAL(currentRowChanged(int)), this, SLOT(slotUpdateCurrentSound()));
71     connect(button_preview, SIGNAL(clicked()), this, SLOT(slotPlaySound()));
72     connect(button_import, SIGNAL(clicked()), this, SLOT(slotSaveItem()));
73     connect(sound_author, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
74     connect(item_license, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
75     connect(sound_name, SIGNAL(leftClickedUrl(const QString &)), this, SLOT(slotOpenUrl(const QString &)));
76     connect(service_list, SIGNAL(currentIndexChanged(int)), this, SLOT(slotChangeService()));
77     item_image->setFixedWidth(180);
78     if (Solid::Networking::status() == Solid::Networking::Unconnected) {
79         slotOffline();
80     }
81     connect(Solid::Networking::notifier(), SIGNAL(shouldConnect()), this, SLOT(slotOnline()));
82     connect(Solid::Networking::notifier(), SIGNAL(shouldDisconnect()), this, SLOT(slotOffline()));
83     connect(page_next, SIGNAL(clicked()), this, SLOT(slotNextPage()));
84     connect(page_prev, SIGNAL(clicked()), this, SLOT(slotPreviousPage()));
85     connect(page_number, SIGNAL(valueChanged(int)), this, SLOT(slotStartSearch(int)));
86     connect(info_browser, SIGNAL(anchorClicked(const QUrl &)), this, SLOT(slotOpenLink(const QUrl &)));
87     
88     sound_box->setEnabled(false);
89     search_text->setFocus();
90 #ifdef USE_NEPOMUK
91 #if KDE_IS_VERSION(4,6,0)    
92     Nepomuk::ResourceManager::instance()->init();
93 #endif
94 #endif
95     slotChangeService();
96 }
97
98 ResourceWidget::~ResourceWidget()
99 {
100     if (m_currentService) delete m_currentService;
101 }
102
103 void ResourceWidget::slotStartSearch(int page)
104 {
105     page_number->blockSignals(true);
106     page_number->setValue(page);
107     page_number->blockSignals(false);
108     m_currentService->slotStartSearch(search_text->text(), page);
109 }
110
111 void ResourceWidget::slotUpdateCurrentSound()
112 {
113     item_image->setEnabled(false);
114     if (!sound_autoplay->isChecked()) m_currentService->stopItemPreview(NULL);
115     info_browser->clear();
116     item_license->clear();
117     QListWidgetItem *item = search_results->currentItem();
118     if (!item) {
119         sound_box->setEnabled(false);
120         return;
121     }
122     m_currentInfo = m_currentService->displayItemDetails(item);
123     
124     if (sound_autoplay->isChecked()) m_currentService->startItemPreview(item);
125     sound_box->setEnabled(true);
126     sound_name->setText(item->text());
127     sound_name->setUrl(m_currentInfo.infoUrl);
128     sound_author->setText(m_currentInfo.author);
129     sound_author->setUrl(m_currentInfo.authorUrl);
130     if (!m_currentInfo.description.isEmpty()) info_browser->setHtml("<em>" + m_currentInfo.description + "</em>");
131     if (!m_currentInfo.license.isEmpty()) parseLicense(m_currentInfo.license);
132 }
133
134
135 void ResourceWidget::slotLoadThumb(const QString url)
136 {
137     KUrl img(url);
138     if (img.isEmpty()) return;
139     if (KIO::NetAccess::exists(img, KIO::NetAccess::SourceSide, this)) {
140         QString tmpFile;
141         if (KIO::NetAccess::download(img, tmpFile, this)) {
142             QPixmap pix(tmpFile);
143             int newHeight = pix.height() * item_image->width() / pix.width();
144             if (newHeight > 200) {
145                 item_image->setScaledContents(false);
146                 //item_image->setFixedHeight(item_image->width());
147             }
148             else {
149                 item_image->setScaledContents(true);
150                 item_image->setFixedHeight(newHeight);
151             }
152             item_image->setPixmap(pix);
153             KIO::NetAccess::removeTempFile(tmpFile);
154         }
155     }
156     item_image->setEnabled(true);
157 }
158
159
160 void ResourceWidget::slotDisplayMetaInfo(QMap <QString, QString> metaInfo)
161 {
162     if (metaInfo.contains("license")) {
163         parseLicense(metaInfo.value("license"));
164     }
165 }
166
167
168 void ResourceWidget::slotPlaySound()
169 {
170     if (!m_currentService) return;
171     bool started = m_currentService->startItemPreview(search_results->currentItem());
172     if (started) button_preview->setText(i18n("Preview"));
173     else button_preview->setText(i18n("Stop"));
174 }
175
176
177 void ResourceWidget::slotForcePlaySound(bool play)
178 {
179     /*
180     if (m_service != FREESOUND) return;
181     m_previewProcess->close();
182     if (m_currentPreview.isEmpty()) return;
183     if (play)
184         m_previewProcess->start("ffplay", QStringList() << m_currentPreview << "-nodisp");
185     */
186 }
187
188 void ResourceWidget::slotPreviewStatusChanged(QProcess::ProcessState state)
189 {
190     /*if (state == QProcess::NotRunning)
191         button_preview->setText(i18n("Preview"));
192     else 
193         button_preview->setText(i18n("Stop"));*/
194 }
195
196 void ResourceWidget::slotSaveItem(const QString originalUrl)
197 {
198     //if (m_currentUrl.isEmpty()) return;
199     QListWidgetItem *item = search_results->currentItem();
200     if (!item) return;
201     QString path = m_folder;
202     QString ext;
203     if (!path.endsWith('/')) path.append('/');
204     if (!originalUrl.isEmpty()) {
205         path.append(KUrl(originalUrl).fileName());
206         ext = "*." + KUrl(originalUrl).fileName().section(".", -1);
207         m_currentInfo.itemDownload = originalUrl;
208     }
209     else {
210         path.append(m_currentService->getDefaultDownloadName(item));
211         ext = m_currentService->getExtension(search_results->currentItem());
212     }
213     QString saveUrl = KFileDialog::getSaveFileName(KUrl(path), ext);
214     if (saveUrl.isEmpty()) return;
215     if (KIO::NetAccess::download(KUrl(m_currentInfo.itemDownload), saveUrl, this)) {
216         const KUrl filePath = KUrl(saveUrl);
217 #ifdef USE_NEPOMUK
218 #if KDE_IS_VERSION(4,6,0)
219         Nepomuk::Resource res( filePath );
220         res.setProperty( Nepomuk::Vocabulary::NIE::license(), (Nepomuk::Variant) item_license->text() );
221         res.setProperty( Nepomuk::Vocabulary::NIE::licenseType(), (Nepomuk::Variant) item_license->url() );
222         res.setProperty( Nepomuk::Vocabulary::NDO::copiedFrom(), sound_name->url() );
223         //res.setDescription(item_description->toPlainText());
224         //res.setProperty( Soprano::Vocabulary::NAO::description(), 
225 #endif
226 #endif
227         emit addClip(KUrl(saveUrl), QString());//, sound_name->url());
228     }
229 }
230
231 void ResourceWidget::slotOpenUrl(const QString &url)
232 {
233     new KRun(KUrl(url), this);
234 }
235
236 void ResourceWidget::slotChangeService()
237 {
238     if (m_currentService) {
239         delete m_currentService;
240         m_currentService = NULL;
241     }
242     SERVICETYPE service = (SERVICETYPE) service_list->itemData(service_list->currentIndex()).toInt();
243     if (service == FREESOUND) {
244         m_currentService = new FreeSound(search_results);
245     }
246     else if (service == OPENCLIPART) {
247         m_currentService = new OpenClipArt(search_results);
248     }
249     else if (service == ARCHIVEORG) {
250         m_currentService = new ArchiveOrg(search_results);
251     }
252
253     connect(m_currentService, SIGNAL(gotMetaInfo(const QString)), this, SLOT(slotGotMetaInfo(const QString)));
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     connect(m_currentService, SIGNAL(gotThumb(const QString)), this, SLOT(slotLoadThumb(const QString)));
258     
259     button_preview->setVisible(m_currentService->hasPreview);
260     button_import->setVisible(!m_currentService->inlineDownload);
261     sound_autoplay->setVisible(m_currentService->hasPreview);
262     search_info->setText(QString());
263     info_browser->setVisible(m_currentService->hasMetadata);
264     if (!search_text->text().isEmpty()) slotStartSearch();
265 }
266
267 void ResourceWidget::slotOnline()
268 {
269     button_search->setEnabled(true);
270     search_info->setText(QString());
271 }
272
273 void ResourceWidget::slotOffline()
274 {
275     button_search->setEnabled(false);
276     search_info->setText(i18n("You need to be online\n for searching"));
277 }
278
279 void ResourceWidget::slotNextPage()
280 {
281     int ix = page_number->value();
282     if (search_results->count() > 0) page_number->setValue(ix + 1);
283 }
284
285 void ResourceWidget::slotPreviousPage()
286 {
287     int ix = page_number->value();
288     if (ix > 1) page_number->setValue(ix - 1);
289 }
290
291 void ResourceWidget::parseLicense(const QString &licenseUrl)
292 {
293     QString licenseName;
294     if (licenseUrl.contains("/sampling+/"))
295         licenseName = "Sampling+";
296     else if (licenseUrl.contains("/by/"))
297         licenseName = "Attribution";
298     else if (licenseUrl.contains("/by-nd/"))
299         licenseName = "Attribution-NoDerivs";
300     else if (licenseUrl.contains("/by-nc-sa/"))
301         licenseName = "Attribution-NonCommercial-ShareAlike";
302     else if (licenseUrl.contains("/by-sa/"))
303         licenseName = "Attribution-ShareAlike";
304     else if (licenseUrl.contains("/by-nc/"))
305         licenseName = "Attribution-NonCommercial";
306     else if (licenseUrl.contains("/by-nc-nd/"))
307         licenseName = "Attribution-NonCommercial-NoDerivs";
308     else if (licenseUrl.contains("/publicdomain/zero/"))
309         licenseName = "Creative Commons 0";
310     else if (licenseUrl.endsWith("/publicdomain"))
311         licenseName = "Public Domain";
312     else licenseName = i18n("Unknown");
313     item_license->setText(licenseName);
314     item_license->setUrl(licenseUrl);
315 }
316
317 void ResourceWidget::slotGotMetaInfo(const QString info)
318 {
319     info_browser->setHtml(info_browser->toHtml() + info);
320 }
321
322
323 void ResourceWidget::slotOpenLink(const QUrl &url)
324 {
325     QString path = url.toEncoded();
326     if (path.endsWith("_preview")) {
327         path.chop(8);
328         slotOpenUrl(path);
329     }
330     else {
331         // import file in Kdenlive
332         slotSaveItem(path);
333     }
334 }
335