PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/ceb/src/transfers_manager.h

http://cebmtpchat.googlecode.com/
C Header | 128 lines | 87 code | 24 blank | 17 comment | 0 complexity | bd0ccbb840661556fdeacd6e9266a6ee MD5 | raw file
Possible License(s): GPL-2.0
  1. /* This file is part of CeB.
  2. * Copyright (C) 2005 Guillaume Denry
  3. *
  4. * CeB is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * any later version.
  8. *
  9. * CeB is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with CeB; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef TRANSFERS_MANAGER_H
  19. #define TRANSFERS_MANAGER_H
  20. #include "session.h"
  21. class Transfer
  22. {
  23. friend class TransfersManager;
  24. public:
  25. enum Direction
  26. {
  27. Direction_In,
  28. Direction_Out
  29. };
  30. enum State
  31. {
  32. Initiated,
  33. Canceled,
  34. Refused,
  35. Accepted
  36. };
  37. Transfer(Session *session, int id, int peerId,
  38. Direction dir, const QString &nickName, const QString &fileName);
  39. void send(const QString &data);
  40. Session *session() const { return _session; }
  41. int id() const { return _id; } // Is uniq in all client transfers
  42. int peerId() const { return _peerId; } // Is uniq for the session itself but not for all client transfers
  43. const QString &peerKey() const { return _peerKey; } // A random key used to authentificate and associate peer connections
  44. void setPeerKey(const QString &value) { _peerKey = value; }
  45. State state() const { return _state; }
  46. const QString &nickName() const { return _nickName; }
  47. const QString &fileName() const { return _fileName; }
  48. QString baseFileName() const { return QFileInfo(_fileName).fileName(); }
  49. Direction dir() const { return _dir; }
  50. QString dirString() const;
  51. const QString &destinationDir() const { return _dstDirectory; }
  52. void setDstDirectory(const QString &path) { _dstDirectory = path; }
  53. bool finished() const;
  54. private:
  55. Session *_session;
  56. int _id;
  57. int _peerId;
  58. QString _peerKey;
  59. State _state;
  60. QString _nickName;
  61. QString _fileName;
  62. Direction _dir;
  63. QString _dstDirectory;
  64. };
  65. class TransfersManager : public QObject
  66. {
  67. Q_OBJECT
  68. public:
  69. static TransfersManager &instance();
  70. static void free();
  71. static bool destroyed();
  72. Transfer *newTransfer(Session *session, const QString &nickName,
  73. const QString &fileName, Transfer::Direction dir,
  74. int peerId = -1);
  75. QList<Transfer*> &transfers() { return _transfers; }
  76. const QList<Transfer*> &tranfers() const { return _transfers; }
  77. QList<Transfer*> getTransfers(Session *session) const;
  78. Transfer *getTransferById(int id) const;
  79. Transfer *getTransferByPeerId(Session *session, int id) const;
  80. static bool isCommand(const QString &str);
  81. static bool isProposeCommand(const QString &str, QString &fileName);
  82. void propose(Session *session, const QString &nickName, const QString &fileName);
  83. void cancel(Transfer *transfer);
  84. void refuse(Transfer *transfer);
  85. void accept(Transfer *transfer);
  86. void sendTransferKeys(Transfer *transfer);
  87. void cleanUpFinishedTransfers(Session *session);
  88. signals:
  89. void newTransferAdded(Transfer *transfer);
  90. void transferStateChanged(Transfer *transfer);
  91. void transferRemoved(Transfer *transfer);
  92. private:
  93. TransfersManager();
  94. ~TransfersManager();
  95. static TransfersManager *_instance;
  96. QList<Transfer*> _transfers;
  97. QTcpServer _tcpServer;
  98. QStringList getCommandArguments(const QString &data);
  99. int getUniqId() const;
  100. private slots:
  101. void newSessionToken(Session *session, const Token &token);
  102. void newConnection();
  103. };
  104. #endif