Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace lastfm
- {
- class LASTFM_DLLEXPORT XmlQuery
- {
- QDomDocument domdoc;
- QDomElement e;
- public:
- XmlQuery();
- void parse( const QByteArray& data ) throw( lastfm::ws::ParseError );
- operator QDomElement() const { return e; }
- };
- }
- void
- XmlQuery::parse( const QByteArray& bytes ) throw( lastfm::ws::ParseError )
- {
- try
- {
- if ( !bytes.size() )
- throw lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "No data" );
- if( !domdoc.setContent( bytes ) )
- throw lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "Invalid XML" );
- e = domdoc.documentElement();
- if (e.isNull())
- throw lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "Lfm is null" );
- QString const status = e.attribute( "status" );
- QDomElement error = e.firstChildElement( "error" );
- uint const n = e.childNodes().count();
- // no elements beyond the lfm is perfectably acceptable <-- wtf?
- // if (n == 0) // nothing useful in the response
- if (status == "failed" || (n == 1 && !error.isNull()) )
- throw error.isNull()
- ? lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "" )
- : lastfm::ws::ParseError( lastfm::ws::Error( error.attribute( "code" ).toUInt() ), error.text() );
- }
- catch ( lastfm::ws::ParseError e )
- {
- switch ( e.enumValue() )
- {
- case lastfm::ws::OperationFailed:
- case lastfm::ws::InvalidApiKey:
- case lastfm::ws::InvalidSessionKey:
- // NOTE will never be received during the LoginDialog stage
- // since that happens before this slot is registered with
- // QMetaObject in App::App(). Neat :)
- QMetaObject::invokeMethod( qApp, "onWsError", Q_ARG( lastfm::ws::Error, e.enumValue() ) );
- default:
- throw e;
- }
- }
- }
- void
- AppClass::onNetworkReplyFinished()
- {
- try
- {
- XmlQuery lfm;
- lfm.parse( qobject_cast<QNetworkReply*>(sender())->readAll() );
- // do some parsing.
- }
- catch (...)
- {
- }
- }
- class ParseError : public std::runtime_error
- {
- Error e;
- QString m_message;
- public:
- explicit ParseError( Error e, QString message )
- :std::runtime_error("lastfm::ws::Error"), e(e), m_message(message)
- {}
- Error enumValue() const { return e; }
- QString message() const { return m_message; }
- ParseError& operator=( const ParseError& that )
- {
- this->e = that.e;
- this->m_message = that.m_message;
- return *this;
- }
- ~ParseError() throw() {;}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement