Revert previous commit, was incorrect
[amarok.git] / src / refreshimages.cpp
blob87190749b9f41c696f0271326d1ce40e22c06ac1
1 /***************************************************************************
2 * copyright : (C) 2005 Ian Monroe <ian@monroe.nu>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **************************************************************************/
22 #define DEBUG_PREFIX "RefreshImages"
24 #include "refreshimages.h"
26 #include "amarok.h"
27 #include "collectiondb.h"
28 #include "debug.h"
29 #include "ContextStatusBar.h"
31 #include <KIO/Job>
32 #include <kio/jobclasses.h>
33 #include <KIO/Scheduler>
34 #include <KLocale>
36 #include <q3valuelist.h>
37 #include <QDomDocument>
38 #include <QDomNode>
39 #include <QImage>
40 #include <QMap>
41 #include <QObject>
42 #include <QStringList>
43 #include <QVariant>
47 RefreshImages::RefreshImages()
49 //"SELECT asin, locale, filename FROM amazon WHERE refetchdate > %1 ;"
50 const QStringList staleImages = CollectionDB::instance()->staleImages();
51 QStringList::ConstIterator it = staleImages.begin();
52 QStringList::ConstIterator end = staleImages.end();
54 while( it != end )
56 QString asin=*it;
57 it++;
58 QString locale = *it;
59 it++;
60 QString md5sum = *it;
61 if ( asin.isEmpty() || locale.isEmpty() || md5sum.isEmpty() )
63 //somehow we have entries without ASIN
64 if ( !md5sum.isEmpty() ) //I've never seen this, just to be sure
65 CollectionDB::instance()->removeInvalidAmazonInfo(md5sum);
66 it++;
67 if ( it==end )
68 deleteLater();
70 continue;
73 QString url =
74 QString("http://webservices.amazon.%1/onca/xml?Service=AWSECommerceService&SubscriptionId=%2&Operation=ItemLookup&ItemId=%3&ResponseGroup=Small,Images")
75 .arg(localeToTLD(locale))
76 .arg("0RQSQ0B8CRY7VX2VF3G2") //Ian Monroe
77 .arg(asin);
79 debug() << url;
81 KIO::TransferJob* job = KIO::storedGet( url, KIO::NoReload, KIO::HideProgressInfo );
82 KIO::Scheduler::scheduleJob( job );
84 //Amarok::ContextStatusBar::instance()->newProgressOperation( job );
85 job->setObjectName( md5sum );
86 it++; //iterate to the next set
88 m_jobInfo[md5sum] = JobInfo( asin, locale, it == end );
89 connect( job, SIGNAL( result( KIO::Job* ) ), SLOT( finishedXmlFetch( KIO::Job* ) ) );
93 void
94 RefreshImages::finishedXmlFetch( KIO::Job* xmlJob ) //SLOT
96 if ( xmlJob->error() )
98 Amarok::ContextStatusBar::instance()->shortMessage( i18n( "There was an error communicating with Amazon." ) );
99 if ( m_jobInfo[ xmlJob->objectName() ].m_last )
100 deleteLater();
102 return;
105 KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( xmlJob );
106 const QString xml = QString::fromUtf8( storedJob->data().data(), storedJob->data().size() );
108 QDomDocument doc;
109 if ( !doc.setContent( xml ) )
110 return;
112 QStringList imageSizes;
113 imageSizes << "LargeImage" << "MediumImage" << "SmallImage";
114 QString imageUrl;
115 foreach( const QString &size, imageSizes )
117 QDomNode imageNode = doc.documentElement()
118 .namedItem( "Items" )
119 .namedItem( "Item" )
120 .namedItem( size );
121 if ( !imageNode.isNull() )
123 imageUrl = imageNode.namedItem( "URL" ).firstChild().toText().data();
124 if( !imageUrl.isEmpty() )
125 break;
128 debug() << imageUrl;
129 KUrl testUrl( imageUrl );
130 if( !testUrl.isValid() ) //KIO crashs on empty strings!!!
132 //Amazon sometimes takes down covers
133 CollectionDB::instance()->removeInvalidAmazonInfo(xmlJob->objectName());
134 return;
137 KIO::TransferJob* imageJob = KIO::storedGet( imageUrl, KIO::NoReload, KIO::HideProgressInfo );
138 KIO::Scheduler::scheduleJob(imageJob);
139 //Amarok::ContextStatusBar::instance()->newProgressOperation( imageJob );
140 imageJob->setObjectName(xmlJob->objectName());
141 //get the URL of the detail page
142 m_jobInfo[xmlJob->objectName()].m_detailUrl = doc.documentElement()
143 .namedItem( "Items" )
144 .namedItem( "Item" )
145 .namedItem( "DetailPageURL" ).firstChild().toText().data();
146 connect( imageJob, SIGNAL( result(KIO::Job*) ), SLOT( finishedImageFetch(KIO::Job*) ) );
149 void RefreshImages::finishedImageFetch(KIO::Job* imageJob)
151 if( imageJob->error() ) {
152 Amarok::ContextStatusBar::instance()->shortMessage(i18n("There was an error communicating with Amazon."));
153 if(m_jobInfo[imageJob->objectName()].m_last)
154 deleteLater();
156 return;
158 QImage img;
159 img.loadFromData(static_cast<KIO::StoredTransferJob*>(imageJob)->data());
160 img.setText( "amazon-url", 0, m_jobInfo[imageJob->objectName()].m_detailUrl);
161 img.save( Amarok::saveLocation("albumcovers/large/") + imageJob->objectName(), "PNG");
163 CollectionDB::instance()->newAmazonReloadDate( m_jobInfo[imageJob->objectName()].m_asin
164 , m_jobInfo[imageJob->objectName()].m_locale
165 , imageJob->objectName());
167 if(m_jobInfo[imageJob->objectName()].m_last)
168 deleteLater();
171 QString RefreshImages::localeToTLD(const QString& locale)
173 if(locale=="us")
174 return "com";
175 else if(locale=="jp")
176 return "co.jp";
177 else if(locale=="uk")
178 return "co.uk";
179 else
180 return locale;
183 #include "refreshimages.moc"