1 /* This file is part of Clementine.
2    Copyright 2012-2013, David Sansome <me@davidsansome.com>
3    Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
4    Copyright 2014, John Maguire <john.maguire@gmail.com>
5 
6    Clementine is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Clementine is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "lastfmcompat.h"
21 #include "core/logging.h"
22 
23 namespace lastfm {
24 namespace compat {
25 
26 #ifdef HAVE_LIBLASTFM1
27 
EmptyXmlQuery()28 XmlQuery EmptyXmlQuery() { return XmlQuery(); }
29 
ParseQuery(const QByteArray & data,XmlQuery * query,bool * connection_problems)30 bool ParseQuery(const QByteArray& data, XmlQuery* query,
31                 bool* connection_problems) {
32   const bool ret = query->parse(data);
33 
34   if (connection_problems) {
35     *connection_problems =
36         !ret &&
37         query->parseError().enumValue() == lastfm::ws::MalformedResponse;
38   }
39 
40   return ret;
41 }
42 
ParseUserList(QNetworkReply * reply,QList<User> * users)43 bool ParseUserList(QNetworkReply* reply, QList<User>* users) {
44   lastfm::XmlQuery lfm;
45   if (!lfm.parse(reply->readAll())) {
46     return false;
47   }
48 
49   *users = lastfm::UserList(lfm).users();
50   return true;
51 }
52 
ScrobbleTimeMin()53 uint ScrobbleTimeMin() { return lastfm::ScrobblePoint::scrobbleTimeMin(); }
54 
55 #else  // HAVE_LIBLASTFM1
56 
57 XmlQuery EmptyXmlQuery() {
58   QByteArray dummy;
59   return XmlQuery(dummy);
60 }
61 
62 bool ParseQuery(const QByteArray& data, XmlQuery* query,
63                 bool* connection_problems) {
64   try {
65     *query = lastfm::XmlQuery(data);
66 #ifdef Q_OS_WIN32
67     if (lastfm::ws::last_parse_error != lastfm::ws::NoError) {
68       return false;
69     }
70 #endif  // Q_OS_WIN32
71   } catch (lastfm::ws::ParseError e) {
72     qLog(Error) << "Last.fm parse error: " << e.enumValue();
73     if (connection_problems) {
74       *connection_problems = e.enumValue() == lastfm::ws::MalformedResponse;
75     }
76     return false;
77   } catch (std::runtime_error& e) {
78     qLog(Error) << e.what();
79     return false;
80   }
81 
82   if (connection_problems) {
83     *connection_problems = false;
84   }
85 
86   // Check for app errors.
87   if (QDomElement(*query).attribute("status") == "failed") {
88     return false;
89   }
90 
91   return true;
92 }
93 
94 bool ParseUserList(QNetworkReply* reply, QList<User>* users) {
95   try {
96     *users = lastfm::User::list(reply);
97 #ifdef Q_OS_WIN32
98     if (lastfm::ws::last_parse_error != lastfm::ws::NoError) {
99       return false;
100     }
101 #endif  // Q_OS_WIN32
102   } catch (std::runtime_error& e) {
103     qLog(Error) << e.what();
104     return false;
105   }
106   return true;
107 }
108 
109 uint ScrobbleTimeMin() { return ScrobblePoint::kScrobbleMinLength; }
110 
111 #endif  // HAVE_LIBLASTFM1
112 }  // namespace compat
113 }  // namespace lastfm
114