PlotnikovPhilipp

Untitled

Apr 20th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #include <client.hpp>
  2.  
  3. using namespace Qt;
  4.  
  5. /**
  6. * Create the constructor of class Client
  7. */
  8.  
  9. Client::Client(ImageProvider* pImage, QObject* pointer) : QObject(pointer), sizeOfData(0) {
  10. pTcpSocket = new QTcpSocket(this);
  11. image = pImage;
  12. pTcpSocket->connectToHost("77.223.89.11", 2323);
  13. connect(pTcpSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
  14. }
  15.  
  16. /**
  17. * Create the slot the reception of data from server
  18. */
  19.  
  20. void Client::slotReadyRead () {
  21. QDataStream in(pTcpSocket);
  22. in.setVersion(QDataStream::Qt_5_3);
  23. in>>sizeOfData;
  24. while(pTcpSocket->bytesAvailable() < sizeOfData) {
  25. pTcpSocket->waitForReadyRead();
  26. }
  27. if (str[0] == "SigningIn") {
  28. str.clear();
  29. in>>number;
  30. in>>str;
  31. in>>name2;
  32. in>>imgs;
  33. if (!str.isEmpty() && str[0] == name) {
  34. accesSigningIn(name, number, imgs);
  35. }
  36. else {
  37. notAccesSigningIn(name);
  38. }
  39. }
  40. else if (str[0] == "SigningUp") {
  41. str.clear();
  42. in>>number;
  43. in>>str;
  44. in>>name2;
  45. in>>imgs;
  46. if (!str.isEmpty() && str[0] == name) {
  47. accesSigningUp(name, number, imgs);
  48. }
  49. else {
  50. notAccesSigningUp(name);
  51. }
  52. }
  53. name = "";
  54. number = 0;
  55. imgs.clear();
  56. name2.clear();
  57. str.clear();
  58. }
  59.  
  60. /**
  61. * Create the slot that send to server messages
  62. */
  63.  
  64. void Client::slotSendToServer(QString argName, QString argPassword) {
  65. name = argName.trimmed();
  66. password = argPassword.trimmed();
  67. if (name.isEmpty() || password.isEmpty()) {
  68. somethingEmpty(name);
  69. return;
  70. }
  71. QByteArray arrBlock;
  72. QDataStream out(&arrBlock, QIODevice::WriteOnly);
  73. out.setVersion(QDataStream::Qt_5_3);
  74. QString info;
  75. str << sender()->objectName();
  76. if (str[0] == "SigningIn") {
  77. info = QString("SELECT \"Name\" FROM \"Authorization\" WHERE \"Name\" = '%1' AND \"password\" = '%2';|SELECT * FROM \"Amount\";|SELECT * FROM \"museumofnature\";").arg(name).arg(password);
  78. }
  79. else {
  80. info = QString("INSERT INTO \"Authorization\"(\"Name\", \"password\") VALUES('%1', '%2');|SELECT \"Name\" FROM \"Authorization\" WHERE \"password\" = '%2';|SELECT * FROM \"Amount\";|SELECT * FROM \"museumofnature\";").arg(name).arg(password);
  81. }
  82. out<<int(0)<<info;
  83. out.device()->seek(0);
  84. out << int(arrBlock.size() - sizeof(int));
  85. pTcpSocket->write(arrBlock);
  86. }
  87.  
  88. /**
  89. * Create the slot to get content of application
  90. */
  91.  
  92. void Client::slotQueryServerContent(int index) {
  93. QByteArray arrBlock;
  94. QDataStream out(&arrBlock, QIODevice::WriteOnly);
  95. out.setVersion(QDataStream::Qt_5_3);
  96. out << int(0) << QString("SELECT * FROM \"museumofnature\" WHERE id = %1;").arg(index);
  97. out.device()->seek(0);
  98. out << int(arrBlock.size() - sizeof (int));
  99. pTcpSocket->write(arrBlock);
  100. }
  101.  
  102. /**
  103. * Create the deconstructor
  104. */
  105.  
  106. Client::~Client() {
  107. delete this;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment