]> git.sesse.net Git - kdenlive/blob - src/utils/openclipart.cpp
Integrate with the required MLT hooks for getting Movit to work.
[kdenlive] / src / utils / openclipart.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 "openclipart.h"
23
24 #include <QListWidget>
25 #include <QDomDocument>
26
27 #include <KDebug>
28 #include <kio/job.h>
29 #include <KIO/NetAccess>
30
31
32 OpenClipArt::OpenClipArt(QListWidget *listWidget, QObject *parent) :
33         AbstractService(listWidget, parent)
34 {
35     serviceType = OPENCLIPART;
36 }
37
38 OpenClipArt::~OpenClipArt()
39 {
40 }
41
42 void OpenClipArt::slotStartSearch(const QString &searchText, int page)
43 {
44     m_listWidget->clear();
45     QString uri = "http://openclipart.org/api/search/?query=";
46     uri.append(searchText);
47     if (page > 1)
48         uri.append("&page=" + QString::number(page));
49         
50     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
51     connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
52 }
53
54
55 void OpenClipArt::slotShowResults(KJob* job)
56 {
57     if (job->error() != 0 ) return;
58     m_listWidget->blockSignals(true);    
59     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
60     
61     QDomDocument doc;
62     doc.setContent(QString::fromAscii(storedQueryJob->data()));
63     QDomNodeList items = doc.documentElement().elementsByTagName("item");
64     for (int i = 0; i < items.count(); ++i) {
65         QDomElement currentClip = items.at(i).toElement();
66         QDomElement title = currentClip.firstChildElement("title");
67         QListWidgetItem *item = new QListWidgetItem(title.firstChild().nodeValue(), m_listWidget);
68         QDomElement thumb = currentClip.firstChildElement("media:thumbnail");
69         item->setData(imageRole, thumb.attribute("url"));
70         QDomElement enclosure = currentClip.firstChildElement("enclosure");
71         item->setData(downloadRole, enclosure.attribute("url"));
72         QDomElement link = currentClip.firstChildElement("link");
73         item->setData(infoUrl, link.firstChild().nodeValue());
74         QDomElement license = currentClip.firstChildElement("cc:license");
75         item->setData(licenseRole, license.firstChild().nodeValue());
76         QDomElement desc = currentClip.firstChildElement("description");
77         item->setData(descriptionRole, desc.firstChild().nodeValue());
78         QDomElement author = currentClip.firstChildElement("dc:creator");
79         item->setData(authorRole, author.firstChild().nodeValue());
80         item->setData(authorUrl, QString("http://openclipart.org/user-detail/") + author.firstChild().nodeValue());
81     }        
82     m_listWidget->blockSignals(false);
83     m_listWidget->setCurrentRow(0);
84     emit searchDone();
85 }
86     
87
88 OnlineItemInfo OpenClipArt::displayItemDetails(QListWidgetItem *item)
89 {
90     OnlineItemInfo info;
91     if (!item) {
92         return info;
93     }
94     info.itemPreview = item->data(previewRole).toString();
95     info.itemDownload = item->data(downloadRole).toString();
96     info.itemId = item->data(idRole).toInt();
97     info.itemName = item->text();
98     info.infoUrl = item->data(infoUrl).toString();
99     info.author = item->data(authorRole).toString();
100     info.authorUrl = item->data(authorUrl).toString();
101     info.license = item->data(licenseRole).toString();
102     info.description = item->data(descriptionRole).toString();
103     emit gotThumb(item->data(imageRole).toString());
104     return info;
105 }
106
107 QString OpenClipArt::getExtension(QListWidgetItem *item)
108 {
109     if (!item) return QString();
110     QString url = item->data(downloadRole).toString();
111     return QString("*.") + url.section('.', -1);
112 }
113
114 QString OpenClipArt::getDefaultDownloadName(QListWidgetItem *item)
115 {
116     if (!item) return QString();
117     QString url = item->data(downloadRole).toString();
118     QString path = item->text();
119     path.append('.' + url.section('.', -1));
120     return path;
121 }
122
123
124 #include "openclipart.moc"