Scoreboard.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <QNetworkAccessManager>
  2. #include <QUrl>
  3. #include <QNetworkRequest>
  4. #include <QNetworkReply>
  5. #include <QAuthenticator>
  6. #include <QXmlReader>
  7. #include <QXmlInputSource>
  8. #include "Scoreboard.h"
  9. #include "ScoreboardHandler.h"
  10. Scoreboard::Scoreboard() {
  11. _script = QUrl("http://yourserver/yourserver.com");
  12. _manager = new QNetworkAccessManager(this);
  13. connect(_manager, SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)), this, SLOT(authenticate(QNetworkReply*, QAuthenticator*)));
  14. }
  15. void Scoreboard::authenticate(QNetworkReply*, QAuthenticator *auth) {
  16. auth->setUser("user");
  17. auth->setPassword("password");
  18. return;
  19. }
  20. void Scoreboard::postIsReady( QNetworkReply * reply) {
  21. if (reply->error()==0) {
  22. emit boardSubmitSuccess(_score);
  23. } else {
  24. emit boardSubmitError();
  25. }
  26. disconnect(_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(postIsReady(QNetworkReply*)));
  27. getList();
  28. }
  29. void Scoreboard::fileIsReady( QNetworkReply * reply) {
  30. if (reply->error()==0) {
  31. QXmlSimpleReader xmlReader;
  32. Handler *handler = new Handler;
  33. xmlReader.setContentHandler(handler);
  34. xmlReader.setErrorHandler(handler);
  35. QXmlInputSource *source = new QXmlInputSource();
  36. source->setData(reply->readAll());
  37. bool ok = xmlReader.parse(source);
  38. if (ok) {
  39. emit boardChanged(handler->getData());
  40. }
  41. } else {
  42. emit boardError();
  43. }
  44. disconnect(_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileIsReady(QNetworkReply*)));
  45. }
  46. void Scoreboard::getList() {
  47. connect(_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(fileIsReady(QNetworkReply*)));
  48. _manager->get(QNetworkRequest(_script));
  49. }
  50. void Scoreboard::submitScore(QString name, int score) {
  51. _score = score;
  52. connect(_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(postIsReady(QNetworkReply*)));
  53. QByteArray data;
  54. data.append("name="+name+"&score="+QString::number(score));
  55. _manager->post(QNetworkRequest(_script), data);
  56. }