/thirdparty/liblastfm2/src/core/UrlBuilder.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 83 lines · 60 code · 4 blank · 19 comment · 0 complexity · 776e5de4ae518ed5219527a2b1665d9a MD5 · raw file

  1. /*
  2. Copyright 2009 Last.fm Ltd.
  3. - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4. This file is part of liblastfm.
  5. liblastfm 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 3 of the License, or
  8. (at your option) any later version.
  9. liblastfm is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "UrlBuilder.h"
  17. #include <QRegExp>
  18. #include <QStringList>
  19. QUrl
  20. lastfm::UrlBuilder::url() const
  21. {
  22. QUrl url;
  23. url.setScheme( "http" );
  24. url.setHost( host() );
  25. url.setEncodedPath( path );
  26. return url;
  27. }
  28. QByteArray //static
  29. lastfm::UrlBuilder::encode( QString s )
  30. {
  31. foreach (QChar c, QList<QChar>() << '%' << '&' << '/' << ';' << '+' << '#' << '"')
  32. if (s.contains( c ))
  33. // the middle step may seem odd but this is what the site does
  34. // eg. search for the exact string "Radiohead 2 + 2 = 5"
  35. return QUrl::toPercentEncoding( s ).replace( "%20", "+" ).toPercentEncoding( "", "+" );;
  36. return QUrl::toPercentEncoding( s.replace( ' ', '+' ), "+" );
  37. }
  38. QString //static
  39. lastfm::UrlBuilder::host( const QLocale& locale )
  40. {
  41. switch (locale.language())
  42. {
  43. case QLocale::Portuguese: return "www.lastfm.com.br";
  44. case QLocale::Turkish: return "www.lastfm.com.tr";
  45. case QLocale::French: return "www.lastfm.fr";
  46. case QLocale::Italian: return "www.lastfm.it";
  47. case QLocale::German: return "www.lastfm.de";
  48. case QLocale::Spanish: return "www.lastfm.es";
  49. case QLocale::Polish: return "www.lastfm.pl";
  50. case QLocale::Russian: return "www.lastfm.ru";
  51. case QLocale::Japanese: return "www.lastfm.jp";
  52. case QLocale::Swedish: return "www.lastfm.se";
  53. case QLocale::Chinese: return "cn.last.fm";
  54. default: return "www.last.fm";
  55. }
  56. }
  57. QUrl //static
  58. lastfm::UrlBuilder::localize( QUrl url)
  59. {
  60. url.setHost( url.host().replace( QRegExp("^(www.)?last.fm"), host() ) );
  61. return url;
  62. }
  63. QUrl //static
  64. lastfm::UrlBuilder::mobilize( QUrl url )
  65. {
  66. url.setHost( url.host().replace( QRegExp("^(www.)?last"), "m.last" ) );
  67. return url;
  68. }