PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/gadu_protocol/socket-notifiers/gadu-token-socket-notifiers.cpp

https://gitlab.com/mziab/kadu
C++ | 126 lines | 81 code | 25 blank | 20 comment | 7 complexity | 02871a06eb09df5d3a0e34dab66f962b MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, BSD-3-Clause, CC-BY-3.0, GPL-2.0
  1. /*
  2. * %kadu copyright begin%
  3. * Copyright 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
  4. * Copyright 2011, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
  5. * %kadu copyright end%
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <QtCore/QSocketNotifier>
  21. #include <libgadu.h>
  22. #include "misc/misc.h"
  23. #include "debug.h"
  24. #include "gadu-token-socket-notifiers.h"
  25. void GaduTokenSocketNotifiers::watchFor(struct gg_http *h)
  26. {
  27. H = h;
  28. GaduSocketNotifiers::watchFor(H ? H->fd : -1);
  29. }
  30. bool GaduTokenSocketNotifiers::checkRead()
  31. {
  32. return H && (H->check & GG_CHECK_READ);
  33. }
  34. bool GaduTokenSocketNotifiers::checkWrite()
  35. {
  36. return H && (H->check & GG_CHECK_WRITE);
  37. }
  38. void GaduTokenSocketNotifiers::finished(const QString& tokenId, const QPixmap& tokenPixmap)
  39. {
  40. emit done(tokenId, tokenPixmap);
  41. watchFor(0);
  42. deleteLater();
  43. }
  44. void GaduTokenSocketNotifiers::socketEvent()
  45. {
  46. kdebugf();
  47. if (gg_token_watch_fd(H) == -1)
  48. {
  49. kdebugmf(KDEBUG_NETWORK|KDEBUG_INFO, "getting token error\n");
  50. finished(QString(), QPixmap());
  51. return;
  52. }
  53. struct gg_pubdir *p = (struct gg_pubdir *)H->data;
  54. switch (H->state)
  55. {
  56. case GG_STATE_CONNECTING:
  57. kdebugmf(KDEBUG_NETWORK|KDEBUG_INFO, "changing QSocketNotifiers.\n");
  58. watchFor(H);
  59. break;
  60. case GG_STATE_ERROR:
  61. kdebugmf(KDEBUG_NETWORK|KDEBUG_INFO, "getting token error\n");
  62. finished(QString(), QPixmap());
  63. break;
  64. case GG_STATE_DONE:
  65. if (p->success)
  66. {
  67. kdebugmf(KDEBUG_NETWORK|KDEBUG_INFO, "success\n");
  68. struct gg_token *t = (struct gg_token *)H->data;
  69. QString tokenId = QString::fromUtf8(t->tokenid);
  70. //nie optymalizowac!!!
  71. QByteArray buf(H->body_size, '0');
  72. for (unsigned int i = 0; i < H->body_size; ++i)
  73. buf[i] = H->body[i];
  74. QPixmap tokenImage;
  75. tokenImage.loadFromData(buf);
  76. finished(tokenId, tokenImage);
  77. }
  78. else
  79. {
  80. kdebugmf(KDEBUG_NETWORK|KDEBUG_INFO, "getting token error\n");
  81. finished(QString(), QPixmap());
  82. }
  83. break;
  84. }
  85. kdebugf2();
  86. }
  87. int GaduTokenSocketNotifiers::timeout()
  88. {
  89. return H
  90. ? H->timeout * 1000
  91. : -1;
  92. }
  93. bool GaduTokenSocketNotifiers::handleSoftTimeout()
  94. {
  95. return false;
  96. }
  97. void GaduTokenSocketNotifiers::connectionTimeout()
  98. {
  99. finished(QString(), QPixmap());
  100. }
  101. #include "moc_gadu-token-socket-notifiers.cpp"