1 /***************************************************************************
2 * Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
22 #include "openclipart.h"
24 #include <QListWidget>
25 #include <QDomDocument>
29 #include <KIO/NetAccess>
32 OpenClipArt::OpenClipArt(QListWidget *listWidget, QObject *parent) :
33 AbstractService(listWidget, parent)
35 serviceType = OPENCLIPART;
38 OpenClipArt::~OpenClipArt()
42 void OpenClipArt::slotStartSearch(const QString &searchText, int page)
44 m_listWidget->clear();
45 QString uri = "http://openclipart.org/api/search/?query=";
46 uri.append(searchText);
48 uri.append("&page=" + QString::number(page));
50 KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
51 connect( resolveJob, SIGNAL(result(KJob*)), this, SLOT(slotShowResults(KJob*)) );
55 void OpenClipArt::slotShowResults(KJob* job)
57 if (job->error() != 0 ) return;
58 m_listWidget->blockSignals(true);
59 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
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());
82 m_listWidget->blockSignals(false);
83 m_listWidget->setCurrentRow(0);
88 OnlineItemInfo OpenClipArt::displayItemDetails(QListWidgetItem *item)
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());
107 QString OpenClipArt::getExtension(QListWidgetItem *item)
109 if (!item) return QString();
110 QString url = item->data(downloadRole).toString();
111 return QString("*.") + url.section('.', -1);
114 QString OpenClipArt::getDefaultDownloadName(QListWidgetItem *item)
116 if (!item) return QString();
117 QString url = item->data(downloadRole).toString();
118 QString path = item->text();
119 path.append('.' + url.section('.', -1));
124 #include "openclipart.moc"