/thirdparty/liblastfm2/src/radio/RadioStation.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 337 lines · 233 code · 83 blank · 21 comment · 41 complexity · 6493cdfe261ed09a044a186bdf72bcda MD5 · raw file

  1. /*
  2. Copyright 2009 Last.fm Ltd.
  3. This file is part of liblastfm.
  4. liblastfm is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. liblastfm is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with liblastfm. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <QRegExp>
  16. #include <QStringList>
  17. #include "RadioStation.h"
  18. #include "../core/XmlQuery.h"
  19. const float k_defaultRep(0.5);
  20. const float k_defaultMainstr(0.5);
  21. const bool k_defaultDisco(false);
  22. lastfm::RadioStation
  23. lastfm::RadioStation::library( const lastfm::User& user )
  24. {
  25. QList<lastfm::User> users;
  26. users << user;
  27. return library( users );
  28. }
  29. lastfm::RadioStation
  30. lastfm::RadioStation::library( QList<lastfm::User>& users )
  31. {
  32. RadioStation s( libraryStr( users ) );
  33. if( users.count() == 1 )
  34. s.setTitle( QObject::tr( "%1%2s Library Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
  35. else {
  36. QString title;
  37. for( QList<lastfm::User>::const_iterator i = users.begin(); i != users.end(); i++ ) {
  38. if( i == users.end() - 1 )
  39. title += " and " + *i;
  40. else
  41. title += ", " + *i;
  42. }
  43. s.setTitle( title );
  44. }
  45. return s;
  46. }
  47. lastfm::RadioStation
  48. lastfm::RadioStation::recommendations( const lastfm::User& user )
  49. {
  50. RadioStation s( recommendationsStr( user ) );
  51. s.setTitle( QObject::tr( "%1%2s Recommended Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
  52. return s;
  53. }
  54. lastfm::RadioStation
  55. lastfm::RadioStation::friends( const lastfm::User& user )
  56. {
  57. RadioStation s( friendsStr( user ) );
  58. s.setTitle( QObject::tr( "%1%2s Friends Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
  59. return s;
  60. }
  61. lastfm::RadioStation
  62. lastfm::RadioStation::neighbourhood( const lastfm::User& user )
  63. {
  64. RadioStation s( neighbourhoodStr( user ) );
  65. s.setTitle( QObject::tr( "%1%2s Neighbours%2 Radio").arg( lastfm::ws::Username, QChar(0x2019) ));
  66. return s;
  67. }
  68. lastfm::RadioStation
  69. lastfm::RadioStation::tag( const lastfm::Tag& tag )
  70. {
  71. QList<lastfm::Tag> tags;
  72. tags << tag;
  73. return lastfm::RadioStation::tag( tags );
  74. }
  75. lastfm::RadioStation
  76. lastfm::RadioStation::tag( QList<lastfm::Tag>& tag )
  77. {
  78. return RadioStation( tagStr( tag ) );
  79. }
  80. lastfm::RadioStation
  81. lastfm::RadioStation::similar( const lastfm::Artist& artist )
  82. {
  83. QList<lastfm::Artist> artists;
  84. artists << artist;
  85. return similar( artists );
  86. }
  87. lastfm::RadioStation
  88. lastfm::RadioStation::similar( QList<lastfm::Artist>& artists )
  89. {
  90. return RadioStation( similarStr( artists ) );
  91. }
  92. lastfm::RadioStation
  93. lastfm::RadioStation::mix( const lastfm::User& user )
  94. {
  95. RadioStation s( mixStr( user ) );
  96. s.setTitle( QObject::tr( "%1%2s Mix Radio").arg( lastfm::ws::Username, QChar(0x2019) ) );
  97. return s;
  98. }
  99. QString
  100. lastfm::RadioStation::url() const
  101. {
  102. return m_url.toString() + (m_tagFilter.isEmpty() ? "" : "/tag/" + m_tagFilter);
  103. }
  104. void
  105. lastfm::RadioStation::setTitle( const QString& s )
  106. {
  107. // Stop the radio station getting renamed when the web services don't know what it's called
  108. if ( !m_title.isEmpty() && s.compare( "a radio station", Qt::CaseInsensitive ) == 0 )
  109. return;
  110. QString title = s.trimmed();
  111. if ( title.compare( QObject::tr("%1%2s Library Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
  112. title = QObject::tr("My Library Radio");
  113. else if ( title.compare( QObject::tr("%1%2s Mix Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
  114. title = QObject::tr("My Mix Radio");
  115. else if ( title.compare( QObject::tr("%1%2s Recommended Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
  116. title = QObject::tr("My Recommended Radio");
  117. else if ( title.compare( QObject::tr("%1%2s Friends%2 Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
  118. title = QObject::tr("My Friends%1 Radio").arg( QChar( 0x2019 ) );
  119. else if ( title.compare( QObject::tr("%1%2s Friends Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
  120. title = QObject::tr("My Friends%1 Radio").arg( QChar( 0x2019 ) );
  121. else if ( title.compare( QObject::tr("%1%2s Neighbours%2 Radio").arg( lastfm::ws::Username, QChar(0x2019) ), Qt::CaseInsensitive ) == 0 )
  122. title = QObject::tr("My Neighbours%1 Radio").arg( QChar( 0x2019 ) );
  123. else if ( title.compare( QObject::tr("%1%2s Neighbours Radio").arg( lastfm::ws::Username ), Qt::CaseInsensitive ) == 0 )
  124. title = QObject::tr("My Neighbours%1 Radio").arg( QChar( 0x2019 ) );
  125. m_title = title;
  126. }
  127. QString
  128. lastfm::RadioStation::title() const
  129. {
  130. return m_title; // + (m_tagFilter.isEmpty() ? "" : ": " + m_tagFilter);
  131. }
  132. void
  133. lastfm::RadioStation::setTagFilter( const QString& tag )
  134. {
  135. m_tagFilter = tag;
  136. }
  137. QNetworkReply*
  138. lastfm::RadioStation::getSampleArtists( int limit ) const
  139. {
  140. QMap<QString, QString> map;
  141. map["method"] = "radio.getSampleArtists";
  142. map["station"] = m_url.toString();
  143. map["limit"] = QString::number( limit );
  144. return ws::get( map );
  145. }
  146. QNetworkReply*
  147. lastfm::RadioStation::getTagSuggestions( int limit ) const
  148. {
  149. QMap<QString, QString> map;
  150. map["method"] = "radio.getTagSuggestions";
  151. map["station"] = m_url.toString();
  152. map["limit"] = QString::number( limit );
  153. return ws::get( map );
  154. }
  155. //static
  156. QList<lastfm::RadioStation>
  157. lastfm::RadioStation::list( QNetworkReply* r )
  158. {
  159. QList<lastfm::RadioStation> result;
  160. try {
  161. foreach (XmlQuery xq, XmlQuery( r->readAll() ).children("station")) {
  162. lastfm::RadioStation rs( QUrl::fromPercentEncoding( xq["url"].text().toUtf8() ) );
  163. rs.setTitle(xq["name"].text());
  164. result.append(rs);
  165. }
  166. }
  167. catch (ws::ParseError& e)
  168. {
  169. qWarning() << e.what();
  170. }
  171. return result;
  172. }
  173. bool
  174. lastfm::RadioStation::operator==( const RadioStation& that ) const
  175. {
  176. return this->m_url == that.m_url && this->m_tagFilter == that.m_tagFilter;
  177. }
  178. void
  179. lastfm::RadioStation::setString( const QString& string )
  180. {
  181. // If it's a tag filtered station then extract that part
  182. QString tempString = string;
  183. if ( !tempString.startsWith("lastfm://tag/") )
  184. {
  185. int index = tempString.indexOf("/tag/");
  186. if ( index != -1 )
  187. {
  188. m_tagFilter = tempString.mid( index + 5, tempString.count() - (index + 5) );
  189. tempString = tempString.mid( 0, index );
  190. }
  191. }
  192. m_url = tempString;
  193. }
  194. void
  195. lastfm::RadioStation::setRep(float rep)
  196. {
  197. m_rep = rep;
  198. }
  199. void
  200. lastfm::RadioStation::setMainstr(float mainstr)
  201. {
  202. m_mainstr = mainstr;
  203. }
  204. void
  205. lastfm::RadioStation::setDisco(bool disco)
  206. {
  207. m_disco = disco;
  208. }
  209. float lastfm::RadioStation::rep() const
  210. {
  211. return m_rep;
  212. }
  213. float lastfm::RadioStation::mainstr() const
  214. {
  215. return m_mainstr;
  216. }
  217. bool lastfm::RadioStation::disco() const
  218. {
  219. return m_disco;
  220. }
  221. QString lastfm::RadioStation::libraryStr( QList<lastfm::User>& users )
  222. {
  223. qSort(users.begin(), users.end());
  224. QString url = (users.count() > 1) ? "lastfm://users/" : "lastfm://user/";
  225. url.append( users[0].name() );
  226. for ( int i = 1 ; i < users.count() ; ++i )
  227. url.append( "," + users[i].name() );
  228. url.append("/personal");
  229. return url;
  230. }
  231. QString lastfm::RadioStation::tagStr( QList<lastfm::Tag>& tags )
  232. {
  233. qSort(tags.begin(), tags.end());
  234. QString url = (tags.count() > 1) ? "lastfm://tag/" : "lastfm://globaltags/";
  235. url.append( tags[0].name() );
  236. for ( int i = 1 ; i < tags.count() ; ++i )
  237. url.append( "*" + tags[i].name() );
  238. return url;
  239. }
  240. QString lastfm::RadioStation::similarStr( QList<lastfm::Artist>& artists )
  241. {
  242. qSort(artists.begin(), artists.end());
  243. QString url = (artists.count() > 1) ? "lastfm://artistnames/" : "lastfm://artist/";
  244. url.append( artists[0].name() );
  245. for ( int i = 1 ; i < artists.count() ; ++i )
  246. url.append( "," + artists[i].name() );
  247. if (artists.count() == 1)
  248. url.append( "/similarartists" );
  249. return url;
  250. }