]> git.sesse.net Git - kdenlive/blob - src/utils/openclipart.cpp
a1202661af71513034be56ddeb1652a00db3cc86
[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) uri.append("&page=" + QString::number(page));
48         
49     KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
50     connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotShowResults( KJob* ) ) );
51 }
52
53
54 void OpenClipArt::slotShowResults(KJob* job)
55 {
56     if (job->error() != 0 ) return;
57     m_listWidget->blockSignals(true);    
58     KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
59     
60     QDomDocument doc;
61     doc.setContent(QString::fromAscii(storedQueryJob->data()));
62     QDomNodeList items = doc.documentElement().elementsByTagName("item");
63     for (int i = 0; i < items.count(); i++) {
64         QDomElement currentClip = items.at(i).toElement();
65         QDomElement title = currentClip.firstChildElement("title");
66         QListWidgetItem *item = new QListWidgetItem(title.firstChild().nodeValue(), m_listWidget);
67         QDomElement thumb = currentClip.firstChildElement("media:thumbnail");
68         item->setData(imageRole, thumb.attribute("url"));
69         QDomElement enclosure = currentClip.firstChildElement("enclosure");
70         item->setData(downloadRole, enclosure.attribute("url"));
71         QDomElement link = currentClip.firstChildElement("link");
72         item->setData(infoUrl, link.firstChild().nodeValue());
73         QDomElement license = currentClip.firstChildElement("cc:license");
74         item->setData(licenseRole, license.firstChild().nodeValue());
75         QDomElement desc = currentClip.firstChildElement("description");
76         item->setData(descriptionRole, desc.firstChild().nodeValue());
77         QDomElement author = currentClip.firstChildElement("dc:creator");
78         item->setData(authorRole, author.firstChild().nodeValue());
79         item->setData(authorUrl, QString("http://openclipart.org/user-detail/") + author.firstChild().nodeValue());
80     }        
81     m_listWidget->blockSignals(false);
82     m_listWidget->setCurrentRow(0);
83     emit searchDone();
84 }
85     
86
87 OnlineItemInfo OpenClipArt::displayItemDetails(QListWidgetItem *item)
88 {
89     OnlineItemInfo info;
90     if (!item) {
91         return info;
92     }
93     info.itemPreview = item->data(previewRole).toString();
94     info.itemDownload = item->data(downloadRole).toString();
95     info.itemId = item->data(idRole).toInt();
96     info.itemName = item->text();
97     info.infoUrl = item->data(infoUrl).toString();
98     info.author = item->data(authorRole).toString();
99     info.authorUrl = item->data(authorUrl).toString();
100     info.license = item->data(licenseRole).toString();
101     info.description = item->data(descriptionRole).toString();
102     emit gotThumb(item->data(imageRole).toString());
103     return info;
104 }
105
106 QString OpenClipArt::getExtension(QListWidgetItem *item)
107 {
108     if (!item) return QString();
109     QString url = item->data(downloadRole).toString();
110     return QString("*.") + url.section('.', -1);
111 }
112
113 QString OpenClipArt::getDefaultDownloadName(QListWidgetItem *item)
114 {
115     if (!item) return QString();
116     QString url = item->data(downloadRole).toString();
117     QString path = item->text();
118     path.append("." + url.section('.', -1));
119     return path;
120 }
121