Advertisement
eartle

Untitled

Oct 24th, 2011
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. namespace lastfm
  2. {
  3.     class LASTFM_DLLEXPORT XmlQuery
  4.     {
  5.         QDomDocument domdoc;
  6.         QDomElement e;
  7.  
  8.     public:
  9.         XmlQuery();
  10.         void parse( const QByteArray& data ) throw( lastfm::ws::ParseError );
  11.  
  12.         operator QDomElement() const { return e; }
  13.     };
  14. }
  15.  
  16. void
  17. XmlQuery::parse( const QByteArray& bytes ) throw( lastfm::ws::ParseError )
  18. {  
  19.     try
  20.     {
  21.         if ( !bytes.size() )
  22.             throw lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "No data" );
  23.  
  24.         if( !domdoc.setContent( bytes ) )
  25.             throw lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "Invalid XML" );
  26.  
  27.         e = domdoc.documentElement();
  28.  
  29.         if (e.isNull())
  30.             throw lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "Lfm is null" );
  31.  
  32.         QString const status = e.attribute( "status" );
  33.         QDomElement error = e.firstChildElement( "error" );
  34.         uint const n = e.childNodes().count();
  35.  
  36.         // no elements beyond the lfm is perfectably acceptable <-- wtf?
  37.         // if (n == 0) // nothing useful in the response
  38.         if (status == "failed" || (n == 1 && !error.isNull()) )
  39.             throw error.isNull()
  40.                     ? lastfm::ws::ParseError( lastfm::ws::MalformedResponse, "" )
  41.                     : lastfm::ws::ParseError( lastfm::ws::Error( error.attribute( "code" ).toUInt() ), error.text() );
  42.  
  43.     }
  44.     catch ( lastfm::ws::ParseError e )
  45.     {
  46.         switch ( e.enumValue() )
  47.         {
  48.             case lastfm::ws::OperationFailed:
  49.             case lastfm::ws::InvalidApiKey:
  50.             case lastfm::ws::InvalidSessionKey:
  51.                 // NOTE will never be received during the LoginDialog stage
  52.                 // since that happens before this slot is registered with
  53.                 // QMetaObject in App::App(). Neat :)
  54.                 QMetaObject::invokeMethod( qApp, "onWsError", Q_ARG( lastfm::ws::Error, e.enumValue() ) );
  55.             default:
  56.                 throw e;
  57.         }
  58.     }
  59. }
  60.  
  61. void
  62. AppClass::onNetworkReplyFinished()
  63. {
  64.     try
  65.     {
  66.         XmlQuery lfm;
  67.         lfm.parse( qobject_cast<QNetworkReply*>(sender())->readAll() );
  68.  
  69.     // do some parsing.
  70.  
  71.     }
  72.     catch (...)
  73.     {
  74.     }
  75. }
  76.  
  77.         class ParseError : public std::runtime_error
  78.         {
  79.             Error e;
  80.             QString m_message;
  81.         public:
  82.             explicit ParseError( Error e, QString message )
  83.                 :std::runtime_error("lastfm::ws::Error"), e(e), m_message(message)
  84.             {}
  85.             Error enumValue() const { return e; }
  86.             QString message() const { return m_message; }
  87.  
  88.             ParseError& operator=( const ParseError& that )
  89.             {
  90.                 this->e = that.e;
  91.                 this->m_message = that.m_message;
  92.                 return *this;
  93.             }
  94.  
  95.             ~ParseError() throw() {;}
  96.         };
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement