]> git.sesse.net Git - kdenlive/blob - src/utils/archiveorg.cpp
Use KLocalizedString (for i18n only, in kf5 it will necessary => use a script for...
[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 #include <QApplication>
29
30 #include <KDebug>
31 #include "kdenlivesettings.h"
32 #include <kio/job.h>
33 #include <KLocalizedString>
34
35 #ifdef USE_QJSON
36 #include <qjson/parser.h>
37 #endif
38
39 ArchiveOrg::ArchiveOrg(QListWidget *listWidget, QObject *parent) :
40         AbstractService(listWidget, parent),
41         m_previewProcess(new QProcess)
42 {
43     serviceType = ARCHIVEORG;
44     hasPreview = false;
45     hasMetadata = true;
46     inlineDownload = true;
47     //connect(m_previewProcess, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(slotPreviewStatusChanged(QProcess::ProcessState)));
48 }
49
50 ArchiveOrg::~ArchiveOrg()
51 {
52     delete m_previewProcess;
53 }
54
55 void ArchiveOrg::slotStartSearch(const QString &searchText, int page)
56 {
57     m_listWidget->clear();
58     QString uri = "http://www.archive.org/advancedsearch.php?q=";
59     uri.append(searchText);
60     uri.append("%20AND%20mediatype:movies");//MovingImage");
61     uri.append("&fl%5B%5D=creator&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=licenseurl&fl%5B%5D=title");
62     uri.append("&rows=30");
63     if (page > 1) uri.append("&page=" + QString::number(page));
64     uri.append("&output=json"); //&callback=callback&save=yes#raw");
65
66     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
67     connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
68 }
69
70
71 void ArchiveOrg::slotShowResults(KJob* job)
72 {
73     if (job->error() != 0 ) return;
74     m_listWidget->blockSignals(true);
75     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
76 #ifdef USE_QJSON
77     QJson::Parser parser;
78     bool ok;
79     //kDebug()<<"// GOT RESULT: "<<m_result;
80     QVariant data = parser.parse(storedQueryJob->data(), &ok);
81     QVariant sounds;
82     if (data.canConvert(QVariant::Map)) {
83         QMap <QString, QVariant> map = data.toMap();
84         QMap<QString, QVariant>::const_iterator i = map.constBegin();
85         while (i != map.constEnd()) {
86             if (i.key() == "response") {
87                 sounds = i.value();
88                 if (sounds.canConvert(QVariant::Map)) {
89                     QMap <QString, QVariant> soundsList = sounds.toMap();
90                     if (soundsList.contains("numFound")) emit searchInfo(i18np("Found %1 result", "Found %1 results", soundsList.value("numFound").toInt()));
91                     QList <QVariant> resultsList;
92                     if (soundsList.contains("docs")) {
93                         resultsList = soundsList.value("docs").toList();
94                     }
95                     
96                     for (int j = 0; j < resultsList.count(); j++) {
97                         if (resultsList.at(j).canConvert(QVariant::Map)) {
98                             QMap <QString, QVariant> soundmap = resultsList.at(j).toMap();
99                             if (soundmap.contains("title")) {
100                                 QListWidgetItem *item = new   QListWidgetItem(soundmap.value("title").toString(), m_listWidget);
101                                 item->setData(descriptionRole, soundmap.value("description").toString());
102                                 item->setData(idRole, soundmap.value("identifier").toString());
103                                 QString author = soundmap.value("creator").toString();
104                                 item->setData(authorRole, author);
105                                 if (author.startsWith("http")) item->setData(authorUrl, author);
106                                 item->setData(infoUrl, "http://archive.org/details/" + soundmap.value("identifier").toString());
107                                 item->setData(downloadRole, "http://archive.org/download/" + soundmap.value("identifier").toString());
108                                 item->setData(licenseRole, soundmap.value("licenseurl").toString());                        
109                             }
110                         }
111                     }
112                 }
113             }
114             ++i;
115         }
116     }
117 #endif  
118     m_listWidget->blockSignals(false);
119     m_listWidget->setCurrentRow(0);
120     emit searchDone();
121 }
122     
123
124 OnlineItemInfo ArchiveOrg::displayItemDetails(QListWidgetItem *item)
125 {
126     OnlineItemInfo info;
127     m_metaInfo.clear();
128     if (!item) {
129         return info;
130     }
131     info.itemPreview = item->data(previewRole).toString();
132     info.itemDownload = item->data(downloadRole).toString();
133     info.itemId = item->data(idRole).toInt();
134     info.itemName = item->text();
135     info.infoUrl = item->data(infoUrl).toString();
136     info.author = item->data(authorRole).toString();
137     info.authorUrl = item->data(authorUrl).toString();
138     info.license = item->data(licenseRole).toString();
139     info.description = item->data(descriptionRole).toString();
140     
141     m_metaInfo.insert("url", info.itemDownload);
142     m_metaInfo.insert("id", info.itemId);
143     
144     QString extraInfoUrl = item->data(downloadRole).toString();
145     if (!extraInfoUrl.isEmpty()) {
146         KJob* resolveJob = KIO::storedGet( KUrl(extraInfoUrl), KIO::NoReload, KIO::HideProgressInfo );
147         resolveJob->setProperty("id", info.itemId);
148         connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotParseResults(KJob*)) );
149     }
150     return info;
151 }
152
153
154 void ArchiveOrg::slotParseResults(KJob* job)
155 {
156     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
157     QDomDocument doc;
158     doc.setContent(QString::fromUtf8(storedQueryJob->data()));
159     QDomNodeList links = doc.elementsByTagName("a");
160     QString html = QString("<style type=\"text/css\">tr.cellone {background-color: %1;}").arg(qApp->palette().alternateBase().color().name());
161     html += "</style><table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\">";
162     QString link;
163     int ct = 0;
164     m_thumbsPath.clear();
165     for (int i = 0; i < links.count(); ++i) {
166         QString href = links.at(i).toElement().attribute("href");
167         if (href.endsWith(".thumbs/")) {
168             // sub folder contains image thumbs, display one.
169             m_thumbsPath = m_metaInfo.value("url") + '/' + href;
170             KJob* thumbJob = KIO::storedGet( KUrl(m_thumbsPath), KIO::NoReload, KIO::HideProgressInfo );
171             thumbJob->setProperty("id", m_metaInfo.value("id"));
172             connect( thumbJob, SIGNAL(result(KJob*)), this, SLOT(slotParseThumbs(KJob*)) );
173         }
174         else if (!href.contains('/') && !href.endsWith(".xml")) {
175             link = m_metaInfo.value("url") + '/' + href;
176             ct++;
177             if (ct %2 == 0) {
178                 html += "<tr class=\"cellone\">";
179             }
180             else html += "<tr>";
181             html += "<td>" + KUrl(link).fileName() + QString("</td><td><a href=\"%1\">%2</a></td><td><a href=\"%3\">%4</a></td></tr>").arg(link).arg(i18n("Preview")).arg(link + "_import").arg(i18n("Import"));
182         }
183     }
184     html += "</table>";
185     if (m_metaInfo.value("id") == job->property("id").toString()) emit gotMetaInfo(html);
186 }
187
188
189 bool ArchiveOrg::startItemPreview(QListWidgetItem *item)
190 {    
191     if (!item) return false;
192     QString url = item->data(previewRole).toString();
193     if (url.isEmpty()) return false;
194     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
195         m_previewProcess->close();
196     }
197     m_previewProcess->start(KdenliveSettings::ffplaypath(), QStringList() << url << "-nodisp");
198     return true;
199 }
200
201
202 void ArchiveOrg::stopItemPreview(QListWidgetItem */*item*/)
203 {    
204     if (m_previewProcess && m_previewProcess->state() != QProcess::NotRunning) {
205         m_previewProcess->close();
206     }
207 }
208
209 QString ArchiveOrg::getExtension(QListWidgetItem *item)
210 {
211     if (!item) return QString();
212     return QString("*.") + item->text().section('.', -1);
213 }
214
215
216 QString ArchiveOrg::getDefaultDownloadName(QListWidgetItem *item)
217 {
218     if (!item) return QString();
219     return item->text();
220 }
221
222 void ArchiveOrg::slotParseThumbs(KJob* job)
223 {
224     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
225     QDomDocument doc;
226     doc.setContent(QString::fromUtf8(storedQueryJob->data()));
227     QDomNodeList links = doc.elementsByTagName("a");
228     if (links.isEmpty()) return;
229     for (int i = 0; i < links.count(); ++i) {
230         QString href = links.at(i).toElement().attribute("href");
231         if (!href.contains('/') && i >= links.count() / 2) {
232             QString thumbUrl = m_thumbsPath + href;
233             if (m_metaInfo.value("id") == job->property("id").toString())
234                 emit gotThumb(thumbUrl);
235             break;
236         }
237     }
238 }
239
240 #include "archiveorg.moc"