networkapi.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "networkapi.h"
  2. NetworkApi::NetworkApi(QObject *parent) : QObject(parent)
  3. {
  4. // init net manager
  5. nam = new QNetworkAccessManager(parent);
  6. // new line
  7. QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleNetworkReply(QNetworkReply*)));
  8. }
  9. void NetworkApi::loginUser(const QString *nick)
  10. {
  11. QNetworkRequest request(QUrl("http://orison.biz/applications/chatwebservice.asmx/Login"));
  12. QByteArray data;
  13. QUrl params;
  14. params.addQueryItem("Nick", *nick);
  15. data.append(params.toEncoded());
  16. data.remove(0, 1);
  17. nam->post(request, data);
  18. }
  19. void NetworkApi::logoutUser(const QString *nick)
  20. {
  21. QNetworkRequest request(QUrl("http://orison.biz/applications/chatwebservice.asmx/Logout"));
  22. QByteArray data;
  23. QUrl params;
  24. params.addQueryItem("Nick", *nick);
  25. data.append(params.toEncoded());
  26. data.remove(0, 1);
  27. nam->post(request, data);
  28. }
  29. void NetworkApi::getMessages(const QString *lastTime)
  30. {
  31. QNetworkRequest request(QUrl("http://orison.biz/applications/chatwebservice.asmx/getMessages"));
  32. QByteArray data;
  33. QUrl params;
  34. params.addQueryItem("lastMesgTime", *lastTime);
  35. data.append(params.toEncoded());
  36. data.remove(0, 1);
  37. nam->post(request, data);
  38. }
  39. void NetworkApi::getUsers()
  40. {
  41. QNetworkRequest request(QUrl("http://orison.biz/applications/chatwebservice.asmx/getUsers"));
  42. QByteArray data;
  43. nam->post(request, data);
  44. }
  45. void NetworkApi::postMessage(QString *nick, QString *message)
  46. {
  47. QNetworkRequest request(QUrl("http://orison.biz/applications/chatwebservice.asmx/postMessage"));
  48. QByteArray data;
  49. QUrl params;
  50. params.addQueryItem("Nick", *nick);
  51. params.addQueryItem("Mesg", *message);
  52. data.append(params.toEncoded());
  53. data.remove(0, 1);
  54. nam->post(request, data);
  55. QString str;
  56. emit messagePosted(&str);
  57. }
  58. void NetworkApi::handleNetworkReply(QNetworkReply* reply)
  59. {
  60. //QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
  61. // no error received?
  62. if (reply->error() == QNetworkReply::NoError)
  63. {
  64. QString stringXML(reply->readAll()); // raw xml
  65. qDebug(QString("\n\n---------------\n").append(stringXML).toUtf8());
  66. // process data
  67. if(stringXML.contains("status_code=\"101\"", Qt::CaseInsensitive))
  68. {
  69. emit commError(&QString("Nick already taken"));
  70. }
  71. else if(stringXML.contains("<Users", Qt::CaseInsensitive))
  72. {
  73. emit usersReceived(&stringXML);
  74. }
  75. else if(stringXML.contains("<Messages", Qt::CaseInsensitive))
  76. {
  77. emit messagesReceived(&stringXML);
  78. }
  79. }
  80. else
  81. {
  82. // handle errors here
  83. emit commError(&QString("Network error: ").append(reply->errorString()));
  84. }
  85. reply->deleteLater();
  86. }