Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

netsoul.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #ifndef NETSOUL_H
  2. #define NETSOUL_H
  3. #include <QObject>
  4. #include <QTcpSocket>
  5. #include <QRegExp>
  6. #include <QCryptographicHash>
  7. #include <QByteArray>
  8. #include <QUrl>
  9. #include <QMetaEnum>
  10. #include <QTimer>
  11. #include <QDateTime>
  12. #include <QDebug>
  13. #include <QStringList>
  14. #include "epimafia_global.h"
  15. #include "epimafia.h"
  16. class NSListUsersEmitter;
  17. class EPIMAFIASHARED_EXPORT NetSoul : public QObject
  18. {
  19. Q_OBJECT
  20. public:
  21. enum State
  22. {
  23. Disconnected,
  24. HostLookUp,
  25. Connecting,
  26. Connected,
  27. LoggingIn,
  28. LoggedIn,
  29. Disconnecting,
  30. NetworkError,
  31. ProtocolError,
  32. BadLogin
  33. };
  34. Q_ENUMS(State)
  35. enum UserState
  36. {
  37. UserStateNotSet,
  38. UnknownState,
  39. Actif,
  40. Away,
  41. Lock,
  42. Logout,
  43. Login
  44. };
  45. Q_ENUMS(UserState)
  46. struct User
  47. {
  48. QString id;
  49. QString login;
  50. QString ip;
  51. QString location;
  52. QString promo;
  53. UserState state;
  54. QString stateString;
  55. QString data;
  56. };
  57. typedef QList<NetSoul::User> Users;
  58. struct Message
  59. {
  60. User from;
  61. QString message;
  62. };
  63. enum Command
  64. {
  65. NoCommand,
  66. Whois,
  67. ChangeStatus,
  68. AddToWatchList,
  69. Ping,
  70. ListUsers,
  71. SendMessage
  72. };
  73. Q_ENUMS(Command)
  74. explicit NetSoul(QObject *parent = 0);
  75. QAbstractSocket::SocketError getLastNetworkError();
  76. QString getId() const;
  77. EpiUser getEpiUser() const;
  78. User getUser() const;
  79. NetSoul::State getState() const;
  80. static UserState getUserStateFromString(QString state);
  81. static QString getStringFromUserState(UserState state);
  82. static QString getDecodedString(QString enc);
  83. static QString getEncodedString(QString dec);
  84. static QString getStringFromState(State state);
  85. signals:
  86. void stateChanged(NetSoul::State);
  87. void userDataChanged(NetSoul::User);
  88. void newMessage(NetSoul::Message);
  89. void unknownCommand(QString);
  90. public slots:
  91. void setLocation(QString l);
  92. void setLogin(EpiUser user);
  93. void login();
  94. void login(EpiUser user);
  95. void logout();
  96. void setUserState(NetSoul::UserState state);
  97. void whois(QString user);
  98. void whois(QStringList users);
  99. void addToWatchList(QString user);
  100. void addToWatchList(QStringList users);
  101. void listUsers(QObject *receiver, const char *member, QString user = QString());
  102. void sendMessage(QString user, QString location, QString message);
  103. void sendMessage(NetSoul::User user, QString message);
  104. private slots:
  105. void socketConnected();
  106. void socketDisconnected();
  107. void socketError(QAbstractSocket::SocketError e);
  108. void socketReadyRead();
  109. void socketHostFound();
  110. void setState(State s);
  111. void protError();
  112. void startTimeout();
  113. void startTimeout(int t);
  114. void stopTimeout();
  115. void write(QString c);
  116. void write(QString c, int t);
  117. void timedout();
  118. void nextCommand();
  119. void addCommand(QString cmd, Command command);
  120. void addCommand(QString cmd, Command command, int t);
  121. void commandFinished();
  122. private:
  123. struct NextCommand
  124. {
  125. QString commandString;
  126. Command commandName;
  127. int timeout;
  128. };
  129. static int m_defaultTimeout;
  130. QTcpSocket* m_sock;
  131. EpiUser m_epiUser;
  132. User m_user;
  133. QString m_location;
  134. State m_state;
  135. UserState m_userState;
  136. QAbstractSocket::SocketError m_lastNetworkError;
  137. QString m_buf;
  138. QTimer* m_timeout;
  139. QString m_id;
  140. Command m_currentCmd;
  141. QList<NextCommand> m_nextCommands;
  142. QList<NSListUsersEmitter*> m_listEmitters;
  143. QString m_data;
  144. };
  145. Q_DECLARE_METATYPE(NetSoul::User)
  146. class NSListUsersEmitter : public QObject
  147. {
  148. Q_OBJECT
  149. public:
  150. NSListUsersEmitter(QObject* p = 0) : QObject(p) {}
  151. signals:
  152. void listed(NetSoul::Users);
  153. public slots:
  154. void emitList(NetSoul::Users users);
  155. };
  156. QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::Command&);
  157. QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::State&);
  158. QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::UserState&);
  159. QDebug EPIMAFIASHARED_EXPORT operator<< (QDebug, NetSoul::User&);
  160. #endif // NETSOUL_H