Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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