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