1 /***************************************************************************
2 * copyright : (C) 2005 Ian Monroe <ian@monroe.nu>
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"
27 #include "collectiondb.h"
29 #include "ContextStatusBar.h"
32 #include <kio/jobclasses.h>
33 #include <KIO/Scheduler>
36 #include <q3valuelist.h>
37 #include <QDomDocument>
42 #include <QStringList>
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();
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
);
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
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
* ) ) );
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
)
105 KIO::StoredTransferJob
* const storedJob
= static_cast<KIO::StoredTransferJob
*>( xmlJob
);
106 const QString xml
= QString::fromUtf8( storedJob
->data().data(), storedJob
->data().size() );
109 if ( !doc
.setContent( xml
) )
112 QStringList imageSizes
;
113 imageSizes
<< "LargeImage" << "MediumImage" << "SmallImage";
115 foreach( const QString
&size
, imageSizes
)
117 QDomNode imageNode
= doc
.documentElement()
118 .namedItem( "Items" )
121 if ( !imageNode
.isNull() )
123 imageUrl
= imageNode
.namedItem( "URL" ).firstChild().toText().data();
124 if( !imageUrl
.isEmpty() )
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());
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" )
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
)
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
)
171 QString
RefreshImages::localeToTLD(const QString
& locale
)
175 else if(locale
=="jp")
177 else if(locale
=="uk")
183 #include "refreshimages.moc"