]> git.sesse.net Git - kdenlive/blob - src/utils/archiveorg.cpp
Preliminary support for importing clips from archive.org (not working yet)
[kdenlive] / src / utils / archiveorg.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 "archiveorg.h"
23
24 #include <QPushButton>
25 #include <QSpinBox>
26 #include <QListWidget>
27 #include <QDomDocument>
28
29 #include <KDebug>
30 #include "kdenlivesettings.h"
31 #include <kio/job.h>
32 #include <KLocale>
33
34 #ifdef USE_QJSON
35 #include <qjson/parser.h>
36 #endif
37
38 ArchiveOrg::ArchiveOrg(QListWidget *listWidget, QObject *parent) :
39         AbstractService(listWidget, parent),
40         m_previewProcess(new QProcess)
41 {
42     serviceType = ARCHIVEORG;
43     hasPreview = true;
44     hasMetadata = true;
45     //connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
46 }
47
48 ArchiveOrg::~ArchiveOrg()
49 {
50     if (m_previewProcess) delete m_previewProcess;
51 }
52
53 void ArchiveOrg::slotStartSearch(const QString searchText, int page)
54 {
55     m_listWidget->clear();
56     QString uri = "http://www.archive.org/advancedsearch.php?q=";
57     uri.append(searchText);
58     uri.append("%20AND%20mediatype:MovingImage");
59     uri.append("&rows=30");
60     if (page > 1) uri.append("&page=" + QString::number(page));
61     uri.append("&output=json"); //&callback=callback&save=yes#raw");
62
63     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
64     connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotShowResults( KJob* ) ) );
65 }
66
67
68 void ArchiveOrg::slotShowResults(KJob* job)
69 {
70     if (job->error() != 0 ) return;
71     m_listWidget->blockSignals(true);
72     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
73 #ifdef USE_QJSON
74     QJson::Parser parser;
75     bool ok;
76     //kDebug()<<"// GOT RESULT: "<<m_result;
77     QVariant data = parser.parse(storedQueryJob->data(), &ok);
78     QVariant sounds;
79     if (data.canConvert(QVariant::Map)) {
80         QMap <QString, QVariant> map = data.toMap();
81         QMap<QString, QVariant>::const_iterator i = map.constBegin();
82         while (i != map.constEnd()) {
83             if (i.key() == "response") {
84                 sounds = i.value();
85                 if (sounds.canConvert(QVariant::Map)) {
86                     QMap <QString, QVariant> soundsList = sounds.toMap();
87                     if (soundsList.contains("numFound")) emit searchInfo(i18np("Found %1 result", "Found %1 results", soundsList.value("numFound").toInt()));
88                     QList <QVariant> resultsList;
89                     if (soundsList.contains("docs")) {
90                         resultsList = soundsList.value("docs").toList();
91                     }
92                     
93                     for (int j = 0; j < resultsList.count(); j++) {
94                         if (resultsList.at(j).canConvert(QVariant::Map)) {
95                             QMap <QString, QVariant> soundmap = resultsList.at(j).toMap();
96                             if (soundmap.contains("title")) {
97                                 QListWidgetItem *item = new   QListWidgetItem(soundmap.value("title").toString(), m_listWidget);
98                                 item->setData(descriptionRole, soundmap.value("description").toString());
99                                 item->setData(idRole, soundmap.value("identifier").toString());                        
100                                 item->setData(licenseRole, soundmap.value("licenseurl").toString());                        
101                             }
102                         }
103                     }
104                 }
105             }
106             ++i;
107         }
108     }
109 #endif  
110     m_listWidget->blockSignals(false);
111     m_listWidget->setCurrentRow(0);
112 }
113     
114
115 OnlineItemInfo ArchiveOrg::displayItemDetails(QListWidgetItem *item)
116 {
117     OnlineItemInfo info;
118     m_metaInfo.clear();
119     if (!item) {
120         return info;
121     }
122     info.itemPreview = item->data(previewRole).toString();
123     info.itemDownload = item->data(downloadRole).toString();
124     info.itemId = item->data(idRole).toInt();
125     info.itemName = item->text();
126     info.infoUrl = item->data(infoUrl).toString();
127     info.author = item->data(authorRole).toString();
128     info.authorUrl = item->data(authorUrl).toString();
129     m_metaInfo.insert(i18n("Duration"), item->data(durationRole).toString());
130     info.license = item->data(licenseRole).toString();
131     info.description = item->data(descriptionRole).toString();
132     
133     QString extraInfoUrl = item->data(infoData).toString();
134     if (!extraInfoUrl.isEmpty()) {
135         KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
136         connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotParseResults( KJob* ) ) );
137     }
138     info.imagePreview = item->data(imageRole).toString();
139     return info;
140 }
141
142
143 void ArchiveOrg::slotParseResults(KJob* job)
144 {
145 #ifdef USE_QJSON
146     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
147     QJson::Parser parser;
148     bool ok;
149     QVariant data = parser.parse(storedQueryJob->data(), &ok);
150     if (data.canConvert(QVariant::Map)) {
151         QMap <QString, QVariant> infos = data.toMap();
152         //if (m_currentId != infos.value("id").toInt()) return;
153         if (infos.contains("samplerate"))
154             m_metaInfo.insert(i18n("Samplerate"), QString::number(infos.value("samplerate").toDouble()));
155         if (infos.contains("channels"))
156             m_metaInfo.insert(i18n("Channels"), QString::number(infos.value("channels").toInt()));
157         if (infos.contains("filesize")) {
158             KIO::filesize_t fSize = infos.value("filesize").toDouble();
159             m_metaInfo.insert(i18n("File size"), KIO::convertSize(fSize));
160         }
161         if (infos.contains("description")) {
162             m_metaInfo.insert("description", infos.value("description").toString());
163         }
164         if (infos.contains("license")) {
165             m_metaInfo.insert("license", infos.value("license").toString());
166         }
167     }
168     emit gotMetaInfo(m_metaInfo);
169 #endif    
170 }
171
172
173 bool ArchiveOrg::startItemPreview(QListWidgetItem *item)
174 {    
175     if (!item) return false;
176     QString url = item->data(previewRole).toString();
177     if (url.isEmpty()) return false;
178     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
179         m_previewProcess->close();
180     }
181     m_previewProcess->start("ffplay", QStringList() << url << "-nodisp");
182     return true;
183 }
184
185
186 void ArchiveOrg::stopItemPreview(QListWidgetItem *item)
187 {    
188     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
189         m_previewProcess->close();
190     }
191 }
192
193 QString ArchiveOrg::getExtension(QListWidgetItem *item)
194 {
195     if (!item) return QString();
196     return QString("*.") + item->text().section('.', -1);
197 }
198
199
200 QString ArchiveOrg::getDefaultDownloadName(QListWidgetItem *item)
201 {
202     if (!item) return QString();
203     return item->text();
204 }