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);
47 if (page > 1) uri.append("&page=" + QString::number(page));
49 KJob* resolveJob = KIO::storedGet( KUrl(uri), KIO::NoReload, KIO::HideProgressInfo );
50 connect( resolveJob, SIGNAL( result( KJob* ) ), this, SLOT( slotShowResults( KJob* ) ) );
54 void OpenClipArt::slotShowResults(KJob* job)
56 if (job->error() != 0 ) return;
57 m_listWidget->blockSignals(true);
58 KIO::StoredTransferJob* storedQueryJob = static_cast<KIO::StoredTransferJob*>( job );
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());
81 m_listWidget->blockSignals(false);
82 m_listWidget->setCurrentRow(0);
87 OnlineItemInfo OpenClipArt::displayItemDetails(QListWidgetItem *item)
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());
106 QString OpenClipArt::getExtension(QListWidgetItem *item)
108 if (!item) return QString();
109 QString url = item->data(downloadRole).toString();
110 return QString("*.") + url.section('.', -1);
113 QString OpenClipArt::getDefaultDownloadName(QListWidgetItem *item)
115 if (!item) return QString();
116 QString url = item->data(downloadRole).toString();
117 QString path = item->text();
118 path.append('.' + url.section('.', -1));