File indexing completed on 2024-05-19 04:50:18

0001 /****************************************************************************************
0002  * Copyright (c) 2006,2007 Nikolaj Hald Nielsen <nhn@kde.org>                           *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "MagnatuneDownloadInfo.h"
0018 
0019 #include "core/support/Debug.h"
0020 
0021 #include <KLocalizedString>
0022 
0023 #include <QFile>
0024 #include <QTextStream>
0025 
0026 
0027 MagnatuneDownloadInfo::MagnatuneDownloadInfo()
0028 {
0029     m_selectedDownloadFormat.clear();
0030 }
0031 
0032 
0033 MagnatuneDownloadInfo::~MagnatuneDownloadInfo()
0034 {}
0035 
0036 bool
0037 MagnatuneDownloadInfo::initFromString( const QString &downloadInfoString, bool membershipDownload  )
0038 {
0039 
0040     m_membershipDownload = membershipDownload;
0041     //complete overkill to do a full SAX2 parser for this at the moment... I think....
0042 
0043     // lets make sure that this is actually a valid result
0044 
0045     int testIndex = downloadInfoString.indexOf( "<RESULT>" );
0046     if ( testIndex == -1 )
0047     {
0048         return false;
0049     }
0050 
0051     int startIndex;
0052     int endIndex;
0053 
0054     if ( membershipDownload == false ) {
0055         startIndex = downloadInfoString.indexOf( "<DL_USERNAME>", 0, Qt::CaseInsensitive );
0056         if ( startIndex != -1 )
0057         {
0058             endIndex = downloadInfoString.indexOf( "</DL_USERNAME>", 0, Qt::CaseInsensitive );
0059             if ( endIndex != -1 )
0060             {
0061                 startIndex += 13;
0062 
0063                 debug() << "found username: " << downloadInfoString.mid( startIndex, endIndex - startIndex );
0064                 m_userName = downloadInfoString.mid( startIndex, endIndex - startIndex );
0065             }
0066             else
0067             {
0068                 return false;
0069             }
0070         }
0071         else
0072         {
0073             return false;
0074         }
0075 
0076 
0077         startIndex = downloadInfoString.indexOf( "<DL_PASSWORD>", 0, Qt::CaseInsensitive );
0078         if ( startIndex != -1 )
0079         {
0080             endIndex = downloadInfoString.indexOf( "</DL_PASSWORD>", 0, Qt::CaseInsensitive );
0081             if ( endIndex != -1 )
0082             {
0083                 startIndex += 13;
0084                 debug() << "found password: " << downloadInfoString.mid( startIndex, endIndex - startIndex );
0085                 m_password = downloadInfoString.mid( startIndex, endIndex - startIndex );
0086             }
0087             else
0088             {
0089                 return false;
0090             }
0091         }
0092         else
0093         {
0094             return false;
0095         }
0096 
0097     } else {
0098         m_userName.clear();
0099         m_password.clear();
0100     }
0101 
0102 
0103     startIndex = downloadInfoString.indexOf( "<URL_WAVZIP>", 0, Qt::CaseInsensitive );
0104     if ( startIndex != -1 )
0105     {
0106         endIndex = downloadInfoString.indexOf( "</URL_WAVZIP>", 0, Qt::CaseInsensitive );
0107         if ( endIndex != -1 )
0108         {
0109             startIndex += 12;
0110             debug() << "found wav";
0111             m_downloadFormats[ "Wav" ] = downloadInfoString.mid( startIndex, endIndex - startIndex ).replace( "&amp;", "&" );
0112 
0113         }
0114     }
0115 
0116     startIndex = downloadInfoString.indexOf( "<URL_128KMP3ZIP>", 0, Qt::CaseInsensitive );
0117     if ( startIndex != -1 )
0118     {
0119         endIndex = downloadInfoString.indexOf( "</URL_128KMP3ZIP>", 0, Qt::CaseInsensitive );
0120         if ( endIndex != -1 )
0121         {
0122             startIndex += 16;
0123             debug() << "found 128k mp3";
0124             m_downloadFormats[ "128 kbit/s MP3" ] = downloadInfoString.mid( startIndex, endIndex - startIndex ).replace( "&amp;", "&" );
0125 
0126         }
0127     }
0128 
0129     startIndex = downloadInfoString.indexOf( "<URL_OGGZIP>", 0, Qt::CaseInsensitive );
0130     if ( startIndex != -1 )
0131     {
0132         endIndex = downloadInfoString.indexOf( "</URL_OGGZIP>", 0, Qt::CaseInsensitive );
0133         if ( endIndex != -1 )
0134         {
0135             startIndex += 12;
0136             debug() << "found ogg-vorbis";
0137             m_downloadFormats[ "Ogg-Vorbis" ] = downloadInfoString.mid( startIndex, endIndex - startIndex ).replace( "&amp;", "&" );
0138 
0139         }
0140     }
0141 
0142     startIndex = downloadInfoString.indexOf( "<URL_VBRZIP>", 0, Qt::CaseInsensitive );
0143     if ( startIndex != -1 )
0144     {
0145         endIndex = downloadInfoString.indexOf( "</URL_VBRZIP>", 0, Qt::CaseInsensitive );
0146         if ( endIndex != -1 )
0147         {
0148             startIndex += 12;
0149             debug() << "found vbr mp3";
0150             m_downloadFormats[ "VBR MP3" ] = downloadInfoString.mid( startIndex, endIndex - startIndex ).replace( "&amp;", "&" );
0151 
0152         }
0153     }
0154 
0155     startIndex = downloadInfoString.indexOf( "<URL_FLACZIP>", 0, Qt::CaseInsensitive );
0156     if ( startIndex != -1 )
0157     {
0158         endIndex = downloadInfoString.indexOf( "</URL_FLACZIP>", 0, Qt::CaseInsensitive );
0159         if ( endIndex != -1 )
0160         {
0161             startIndex += 13;
0162             debug() << "found flac";
0163             m_downloadFormats[ "FLAC" ] = downloadInfoString.mid( startIndex, endIndex - startIndex ).replace( "&amp;", "&" );
0164 
0165         }
0166     }
0167 
0168     startIndex = downloadInfoString.indexOf( "<DL_MSG>", 0, Qt::CaseInsensitive );
0169     if ( startIndex != -1 )
0170     {
0171         endIndex = downloadInfoString.indexOf( "</DL_MSG>", 0, Qt::CaseInsensitive );
0172         if ( endIndex != -1 )
0173         {
0174             startIndex += 9;
0175             debug() << "found dl-message";
0176             m_downloadMessage = downloadInfoString.mid( startIndex, endIndex - startIndex ).replace( "&amp;", "&" );
0177         }
0178     }
0179 
0180     return true;
0181 }
0182 
0183 bool
0184 MagnatuneDownloadInfo::initFromRedownloadXml( const QDomElement &element )
0185 {
0186 
0187 
0188     m_artistName = element.firstChildElement( "artist" ).text();
0189     m_albumName = element.firstChildElement( "album" ).text();
0190     m_userName = element.firstChildElement( "username" ).text();
0191     m_password = element.firstChildElement( "password" ).text();
0192     m_albumCode = element.firstChildElement( "sku" ).text();
0193     m_coverUrl = element.firstChildElement( "cover" ).text();
0194 
0195     //get formats
0196     QDomNode formats = element.firstChildElement( "formats" );
0197 
0198     QString wav_url = formats.firstChildElement( "wav_zip" ).text();
0199     m_downloadFormats[ "Wav" ] = wav_url;
0200     QString mp3_url = formats.firstChildElement( "mp3_zip" ).text();
0201     m_downloadFormats[ "128 kbit/s MP3" ] = mp3_url;
0202     QString vbr_url = formats.firstChildElement( "vbr_zip" ).text();
0203     m_downloadFormats[ "VBR MP3" ] = vbr_url;
0204     QString ogg_url = formats.firstChildElement( "ogg_zip" ).text();
0205     m_downloadFormats[ "Ogg-Vorbis" ] = ogg_url;
0206     QString flac_url = formats.firstChildElement( "flac_zip" ).text();
0207     m_downloadFormats[ "FLAC" ] = flac_url;
0208     
0209 
0210     m_downloadMessage = i18n( "Redownload of a previously purchased album \"%1\" by \"%2\" from Magnatune.com.\n\nUsername: %3\nPassword: %4\n", m_albumName, m_artistName, m_userName, m_password );
0211 
0212     return true;
0213 
0214 }
0215 
0216 QString
0217 MagnatuneDownloadInfo::userName( ) const
0218 {
0219     return m_userName;
0220 }
0221 
0222 QString
0223 MagnatuneDownloadInfo::password( ) const
0224 {
0225     return m_password;
0226 }
0227 
0228 QString
0229 MagnatuneDownloadInfo::downloadMessage( ) const
0230 {
0231     return m_downloadMessage;
0232 }
0233 
0234 DownloadFormatMap
0235 MagnatuneDownloadInfo::formatMap() const
0236 {
0237     return m_downloadFormats;
0238 }
0239 
0240 void
0241 MagnatuneDownloadInfo::setFormatSelection( const QString &selectedFormat )
0242 {
0243     m_selectedDownloadFormat = selectedFormat;
0244 }
0245 
0246 bool
0247 MagnatuneDownloadInfo::isReadyForDownload( )
0248 {
0249     return !m_selectedDownloadFormat.isEmpty();
0250 }
0251 
0252 QUrl
0253 MagnatuneDownloadInfo::completeDownloadUrl( )
0254 {
0255    QString url =  m_downloadFormats[ m_selectedDownloadFormat ];
0256    QUrl downloadUrl(url);
0257    downloadUrl.setUserName(m_userName);
0258    downloadUrl.setPassword(m_password);
0259 
0260    return downloadUrl;
0261 }
0262 
0263 void
0264 MagnatuneDownloadInfo::setUnpackUrl( const QString &unpackUrl )
0265 {
0266     m_unpackUrl = unpackUrl;
0267 }
0268 
0269 QString
0270 MagnatuneDownloadInfo::unpackLocation( ) const
0271 {
0272     return m_unpackUrl;
0273 }
0274 
0275 
0276 
0277 QString
0278 MagnatuneDownloadInfo::albumCode() const
0279 {
0280     return m_albumCode;
0281 }
0282 
0283 
0284 
0285 void
0286 MagnatuneDownloadInfo::setAlbumCode( const QString &albumCode )
0287 {
0288     m_albumCode = albumCode;
0289 }
0290 
0291 bool
0292 MagnatuneDownloadInfo::isMembershipDownload() const
0293 {
0294     return m_membershipDownload;
0295 }
0296 
0297 void
0298 MagnatuneDownloadInfo::setMembershipInfo( const QString &username, const QString &password )
0299 {
0300     m_userName = username;
0301     m_password = password;
0302 }
0303 
0304 
0305 const QString
0306 MagnatuneDownloadInfo::albumName() const
0307 {
0308     return m_albumName;
0309 }
0310 
0311 const QString
0312 MagnatuneDownloadInfo::artistName() const
0313 {
0314     return m_artistName;
0315 }
0316 
0317 const QString
0318 MagnatuneDownloadInfo::coverUrl() const
0319 {
0320     return m_coverUrl;
0321 }
0322 
0323 void
0324 MagnatuneDownloadInfo::setAlbumName( const QString  &albumName )
0325 {
0326     m_albumName = albumName;
0327 }
0328 
0329 void
0330 MagnatuneDownloadInfo::setArtistName( const QString  &artistName )
0331 {
0332     m_artistName = artistName;
0333 }
0334 
0335 void
0336 MagnatuneDownloadInfo::setCoverUrl( const QString &coverUrl )
0337 {
0338     m_coverUrl = coverUrl;
0339 }
0340