KFTPGrabber – a powerful FTP client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.8KB

  1. /*
  2. * This file is part of the KFTPGrabber project
  3. *
  4. * Copyright (C) 2003-2006 by the KFTPGrabber developers
  5. * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
  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 the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
  14. * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
  15. * NON-INFRINGEMENT. See the 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, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
  20. * MA 02110-1301, USA.
  21. *
  22. * In addition, as a special exception, the copyright holders give
  23. * permission to link the code of portions of this program with the
  24. * OpenSSL library under certain conditions as described in each
  25. * individual source file, and distribute linked combinations
  26. * including the two.
  27. *
  28. * You must obey the GNU General Public License in all respects
  29. * for all of the code used other than OpenSSL. If you modify
  30. * file(s) with this exception, you may extend this exception to your
  31. * version of the file(s), but you are not obligated to do so. If you
  32. * do not wish to do so, delete this exception statement from your
  33. * version. If you delete this exception statement from all source
  34. * files in the program, then also delete it here.
  35. */
  36. #include "connectionretry.h"
  37. #include "socket.h"
  38. #include "thread.h"
  39. #include "event.h"
  40. #include <tdelocale.h>
  41. namespace KFTPEngine {
  42. ConnectionRetry::ConnectionRetry(Socket *socket)
  43. : TQObject(),
  44. m_socket(socket),
  45. m_delay(socket->getConfigInt("retry_delay")),
  46. m_max(socket->getConfigInt("max_retries")),
  47. m_iteration(0)
  48. {
  49. m_timer = new TQTimer(this);
  50. connect(m_timer, SIGNAL(timeout()), this, SLOT(slotShouldRetry()));
  51. connect(m_socket->thread()->eventHandler(), SIGNAL(engineEvent(KFTPEngine::Event*)), this, SLOT(slotEngineEvent(KFTPEngine::Event*)));
  52. }
  53. void ConnectionRetry::startRetry()
  54. {
  55. if ((m_iteration++ >= m_max && m_max != 0) || m_delay < 1) {
  56. abortRetry();
  57. return;
  58. }
  59. m_socket->setCurrentCommand(Commands::CmdConnectRetry);
  60. m_socket->emitEvent(Event::EventMessage, i18n("Waiting %1 seconds before reconnect...").arg(m_delay));
  61. m_socket->emitEvent(Event::EventState, i18n("Waiting..."));
  62. m_timer->start(1000 * m_delay, true);
  63. }
  64. void ConnectionRetry::slotShouldRetry()
  65. {
  66. m_socket->setCurrentCommand(Commands::CmdNone);
  67. if (m_max > 0)
  68. m_socket->emitEvent(Event::EventMessage, i18n("Retrying connection (%1/%2)...").arg(m_iteration).arg(m_max));
  69. else
  70. m_socket->emitEvent(Event::EventMessage, i18n("Retrying connection...").arg(m_iteration).arg(m_max));
  71. // Reconnect
  72. Thread *thread = m_socket->thread();
  73. thread->connect(m_socket->getCurrentUrl());
  74. }
  75. void ConnectionRetry::abortRetry()
  76. {
  77. m_timer->stop();
  78. // Disable retry so we avoid infinite loops
  79. m_socket->setConfig("retry", 0);
  80. m_socket->setCurrentCommand(Commands::CmdNone);
  81. m_socket->emitEvent(Event::EventMessage, i18n("Retry aborted."));
  82. m_socket->emitEvent(Event::EventState, i18n("Idle."));
  83. m_socket->emitEvent(Event::EventReady);
  84. m_socket->emitError(ConnectFailed);
  85. // This object should be automagicly removed
  86. TQObject::deleteLater();
  87. }
  88. void ConnectionRetry::slotEngineEvent(KFTPEngine::Event *event)
  89. {
  90. if (event->type() == Event::EventConnect) {
  91. m_socket->emitEvent(Event::EventRetrySuccess);
  92. // This object should be automagicly removed
  93. TQObject::deleteLater();
  94. }
  95. }
  96. }