MyXmppClient.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #ifndef MYXMPPCLIENT_H
  2. #define MYXMPPCLIENT_H
  3. #include "QXmppClient.h"
  4. #include "QXmppVCardIq.h"
  5. #include "QXmppVCardManager.h"
  6. #include "QXmppMessageReceiptManager.h"
  7. #include "QXmppMucManager.h"
  8. #include "QXmppDiscoveryManager.h"
  9. //#include "QXmppConfiguration.h"
  10. #include <QObject>
  11. #include <QtDeclarative>
  12. #include <QDeclarativeView>
  13. #include <QMap>
  14. #include <QList>
  15. #include <QVariant>
  16. #include "rosteritemmodel.h"
  17. #include "rosterlistmodel.h"
  18. #include "chatsitemmodel.h"
  19. #include "chatslistmodel.h"
  20. #include "mycache.h"
  21. #include "messagewrapper.h"
  22. #include "meegimsettings.h"
  23. #include "qmlvcard.h"
  24. #include "rosterxml.h"
  25. /****************/
  26. /* http://www.developer.nokia.com/Community/Wiki/Workaround_to_hide_VKB_in_QML_apps_%28Known_Issue%29 */
  27. class EventFilter : public QObject
  28. {
  29. protected:
  30. bool eventFilter(QObject *obj, QEvent *event) {
  31. QInputContext *ic = qApp->inputContext();
  32. if (ic)
  33. {
  34. if ( (ic->focusWidget() == 0) && prevFocusWidget)
  35. {
  36. QEvent closeSIPEvent( QEvent::CloseSoftwareInputPanel );
  37. ic->filterEvent(&closeSIPEvent);
  38. }
  39. else if ( (prevFocusWidget == 0) && (ic->focusWidget()) )
  40. {
  41. QEvent openSIPEvent( QEvent::RequestSoftwareInputPanel );
  42. ic->filterEvent(&openSIPEvent);
  43. }
  44. prevFocusWidget = ic->focusWidget();
  45. }
  46. return QObject::eventFilter(obj,event);
  47. }
  48. private:
  49. QWidget *prevFocusWidget;
  50. };
  51. /****************/
  52. typedef QMap<QString, QVariant> Map;
  53. class MyXmppClient : public QObject
  54. {
  55. Q_OBJECT
  56. Q_DISABLE_COPY( MyXmppClient )
  57. Q_PROPERTY( QString version READ getVersion NOTIFY versionChanged )
  58. Q_PROPERTY( QString bareJidLastMsg READ getJidLastMsg NOTIFY messageReceived )
  59. Q_PROPERTY( QString resourceLastMsg READ getResourceLastMsg NOTIFY messageReceived )
  60. Q_PROPERTY( StateConnect stateConnect READ getStateConnect NOTIFY connectingChanged )
  61. Q_PROPERTY( StatusXmpp status READ getStatus WRITE setStatus NOTIFY statusChanged )
  62. Q_PROPERTY( QString statusText READ getStatusText WRITE setStatusText NOTIFY statusTextChanged )
  63. Q_PROPERTY( bool isTyping READ getTyping NOTIFY typingChanged )
  64. Q_PROPERTY( RosterListModel* roster READ getRoster NOTIFY rosterChanged )
  65. Q_PROPERTY( QString myBareJid READ getMyJid WRITE setMyJid NOTIFY myJidChanged )
  66. Q_PROPERTY( QString myPassword READ getPassword() WRITE setPassword NOTIFY myPasswordChanged )
  67. Q_PROPERTY( QString host READ getHost WRITE setHost NOTIFY hostChanged )
  68. Q_PROPERTY( int port READ getPort WRITE setPort NOTIFY portChanged )
  69. Q_PROPERTY( QString resource READ getResource WRITE setResource NOTIFY resourceChanged )
  70. Q_PROPERTY( ChatsListModel* openChats READ getOpenChats NOTIFY openChatsChanged )
  71. Q_PROPERTY( MsgListModel* messages READ getMessages NOTIFY messagesChanged )
  72. Q_PROPERTY( QString chatJid READ getChatJid WRITE setChatJid NOTIFY chatJidChanged )
  73. Q_PROPERTY( QMLVCard* vcard READ getVCard NOTIFY vCardChanged )
  74. Q_PROPERTY( bool showOffline READ getShowOffline WRITE setShowOffline NOTIFY showOfflineChanged )
  75. //Q_PROPERTY( SmilesListModel* smilesModel READ getSmilesModel WRITE setSmilesModel NOTIFY smilesModelChanged )
  76. Q_PROPERTY( int keepAlive READ getKeepAlive WRITE setKeepAlive NOTIFY keepAliveChanged )
  77. Q_PROPERTY( bool reconnectOnError READ getReconnectOnError WRITE setReconnectOnError NOTIFY reconnectOnErrorChanged )
  78. Q_PROPERTY( QXmppMucManager* mucManager READ getMucManager NOTIFY mucManagerChanged )
  79. MyCache *cacheIM;
  80. MessageWrapper *msgWrapper;
  81. RosterXml *rosterXml;
  82. ChatsListModel *listModelChats;
  83. QXmppClient *xmppClient;
  84. QXmppRosterManager *rosterManager;
  85. QXmppVCardManager *vCardManager;
  86. QXmppMessageReceiptManager * xmppMessageReceiptManager;
  87. QXmppMucManager * xmppMucManager;
  88. QXmppDiscoveryManager *xmppDiscoveryManager;
  89. QMap< QString, RosterItemModel*> listRoster;
  90. RosterListModel *listModelRoster;
  91. bool flShowOffline;
  92. void setQMLListRoster();
  93. MeegIMSettings *mimOpt;
  94. QMLVCard * qmlVCard;
  95. QString flVCardRequest;
  96. //QXmppConfiguration xmppConfig;
  97. public :
  98. static QString getBareJidByJid( const QString &jid );
  99. enum StateConnect {
  100. Disconnect = 0,
  101. Connected = 1,
  102. Connecting = 2
  103. };
  104. enum StatusXmpp {
  105. Offline = 0,
  106. Online = 1,
  107. Chat = 2,
  108. Away = 3,
  109. XA = 4,
  110. DND = 5
  111. };
  112. Q_ENUMS( StateConnect StatusXmpp )
  113. explicit MyXmppClient();
  114. ~MyXmppClient();
  115. void initXmppClient();
  116. /* --- presence --- */
  117. Q_INVOKABLE void setMyPresence( StatusXmpp status, QString textStatus );
  118. /*--- typing ---*/
  119. Q_INVOKABLE void typingStart( QString bareJid, QString resource );
  120. Q_INVOKABLE void typingStop( QString bareJid, QString resource );
  121. /*--- unread msg ---*/
  122. /*Q_INVOKABLE */void incUnreadMessage( QString bareJid );
  123. Q_INVOKABLE void resetUnreadMessages( QString bareJid );
  124. /*--- vCard ---*/
  125. Q_INVOKABLE void requestVCard( QString bareJid );
  126. //Q_INVOKABLE void setMyVCard( QMLVCard* vCard );
  127. /*--- connect/disconnect ---*/
  128. Q_INVOKABLE void connectToXmppServer();
  129. Q_INVOKABLE void disconnectFromXmppServer();
  130. /*--- send msg ---*/
  131. Q_INVOKABLE bool sendMyMessage( QString bareJid, QString resource, QString msgBody );
  132. /*--- info by jid ---*/
  133. Q_INVOKABLE QString getPicPresenceByJid( QString bareJid );
  134. Q_INVOKABLE QString getStatusTextByJid( QString bareJid );
  135. Q_INVOKABLE QString getAvatarByJid( QString bareJid );
  136. Q_INVOKABLE QStringList getResourcesByJid( QString bareJid );
  137. /*--- add/remove contact ---*/
  138. Q_INVOKABLE void addContact(QString bareJid, QString nick, QString group, bool sendSubscribe );
  139. Q_INVOKABLE void removeContact( QString bareJid );
  140. Q_INVOKABLE void renameContact( QString bareJid, QString name );
  141. /*--- subscribe ---*/
  142. Q_INVOKABLE bool subscribe( const QString bareJid );
  143. Q_INVOKABLE bool unsubscribe( const QString bareJid );
  144. Q_INVOKABLE bool acceptSubscribtion( const QString bareJid );
  145. Q_INVOKABLE bool rejectSubscribtion( const QString bareJid );
  146. /*--- version ---*/
  147. static QString myVersion;
  148. QString getVersion() const { return MyXmppClient::myVersion; }
  149. /*--- muc ---*/
  150. Q_INVOKABLE void addMucRoster( QString mucJid, QString mucSubject = "", bool addToRoster = false );
  151. Q_INVOKABLE void setMucSubject( QString mucJid, QString mucSubject );
  152. /*--- chat options ---*/
  153. Q_INVOKABLE void clearChat( const QString bareJid ) { msgWrapper->clearChat( bareJid); }
  154. Q_INVOKABLE void attentionSend( QString bareJid, QString resource = "" );
  155. Q_INVOKABLE void dd() //DELETE ME
  156. {
  157. QString s = xmppDiscoveryManager->requestInfo("jabbercity.ru");
  158. qDebug() << "<!>:" << s;
  159. }
  160. /*----------------------------------*/
  161. /*--- getter/setter ---*/
  162. QString getJidLastMsg() const { return m_bareJidLastMessage; }
  163. QString getResourceLastMsg() const { return m_resourceLastMessage; }
  164. StateConnect getStateConnect() const { return m_stateConnect; }
  165. QString getStatusText() const { return m_statusText; }
  166. void setStatusText( const QString& );
  167. StatusXmpp getStatus() const { return m_status; }
  168. void setStatus( StatusXmpp __status );
  169. bool getTyping() const { return m_flTyping; }
  170. void setTyping( QString &jid, const bool isTyping ) { m_flTyping = isTyping; emit typingChanged(jid, isTyping); }
  171. RosterListModel* getRoster() const { return listModelRoster; }
  172. QString getMyJid() const { return m_myjid; }
  173. void setMyJid( const QString& myjid ) { if(myjid!=m_myjid) {m_myjid=myjid; emit myJidChanged(); } }
  174. QString getPassword() const { return m_password; }
  175. void setPassword( const QString& value ) { if(value!=m_password) {m_password=value; emit myPasswordChanged(); } }
  176. QString getHost() const { return m_host; }
  177. void setHost( const QString & value ) { if(value!=m_host) {m_host=value; emit hostChanged(); } }
  178. int getPort() const { return m_port; }
  179. void setPort( const int& value ) { if(value!=m_port) {m_port=value; emit portChanged(); } }
  180. QString getResource() const { return m_resource; }
  181. void setResource( const QString & value ) { if(value!=m_resource) {m_resource=value; emit resourceChanged(); } }
  182. ChatsListModel* getOpenChats() const { return listModelChats; }
  183. MsgListModel* getMessages() const { return msgWrapper->getMessages(); }
  184. QString getChatJid() const { return m_chatJid; }
  185. void setChatJid( const QString & value )
  186. {
  187. if(value!=m_chatJid) {
  188. m_chatJid=value;
  189. msgWrapper->setChatBareJid( m_chatJid );
  190. emit chatJidChanged();
  191. }
  192. }
  193. bool getShowOffline() const { return flShowOffline; }
  194. void setShowOffline( const bool value );
  195. //SmilesListModel* getSmilesModel() const { return m_smilesmodel; }
  196. //void setSmilesModel( SmilesListModel *value ) { if(value!=m_smilesmodel) {m_smilesmodel=value; emit smilesModelChanged(); } }
  197. QMLVCard* getVCard() const { return qmlVCard; }
  198. int getKeepAlive() const { return m_keepAlive; }
  199. void setKeepAlive(int arg)
  200. {
  201. if (m_keepAlive != arg) {
  202. m_keepAlive = arg;
  203. emit keepAliveChanged();
  204. }
  205. }
  206. bool getReconnectOnError() const { return m_reconnectOnError; }
  207. void setReconnectOnError(bool arg)
  208. {
  209. if (m_reconnectOnError != arg) {
  210. m_reconnectOnError = arg;
  211. emit reconnectOnErrorChanged();
  212. }
  213. }
  214. QXmppMucManager* getMucManager() const { return xmppMucManager; }
  215. QXmppDiscoveryManager* getDiscoveryManager() const { return xmppDiscoveryManager; }
  216. signals:
  217. void versionChanged();
  218. void messageReceived( QString fromBareJid, QString toBareJid );
  219. void connectingChanged();
  220. void statusTextChanged();
  221. void statusChanged();
  222. void typingChanged( QString bareJid, bool isTyping );
  223. void rosterChanged();
  224. void myJidChanged();
  225. void myPasswordChanged();
  226. void hostChanged();
  227. void portChanged();
  228. void resourceChanged();
  229. void openChatsChanged( QString bareJid, bool isMuc );
  230. void chatOpened( QString bareJid, bool isMuc );
  231. void chatClosed( QString bareJid, bool isMuc );
  232. void messagesChanged();
  233. void chatJidChanged();
  234. void vCardChanged();
  235. void showOfflineChanged();
  236. void presenceJidChanged( const QString &presenceBareJid, const QString &presenceTextStatus, const QString &presencePicStatus );
  237. void errorHappened( const QString &errorString );
  238. void smilesModelChanged();
  239. void subscriptionReceived( const QString bareJid );
  240. void keepAliveChanged();
  241. void reconnectOnErrorChanged();
  242. void mucManagerChanged(QXmppMucManager* arg);
  243. public slots:
  244. void clientStateChanged( QXmppClient::State state );
  245. Q_INVOKABLE void openChat( QString jid );
  246. Q_INVOKABLE void closeChat( QString jid );
  247. Q_INVOKABLE void hideChat() { this->setChatJid(""); msgWrapper->hideChat(); }
  248. private slots:
  249. void initRoster();
  250. void initPresence(const QString& bareJid, const QString& resource);
  251. void initVCard(const QXmppVCardIq &vCard);
  252. void subscriptionReceivedSlot( const QString &);
  253. void itemAdded( const QString &);
  254. void itemRemoved( const QString &);
  255. void itemChanged( const QString &);
  256. void messageReceivedSlot( const QXmppMessage &msg );
  257. void presenceReceived( const QXmppPresence & presence );
  258. void error(QXmppClient::Error);
  259. private:
  260. QString m_bareJidLastMessage;
  261. QString m_resourceLastMessage;
  262. StateConnect m_stateConnect;
  263. StatusXmpp m_status;
  264. QString m_statusText;
  265. bool m_flTyping;
  266. QString m_myjid;
  267. QString m_password;
  268. QString m_host;
  269. int m_port;
  270. QString m_resource;
  271. QString m_chatJid;
  272. //bool m_showOffline;
  273. SmilesListModel* m_smilesmodel;
  274. QString getPicPresence( const QXmppPresence &presence ) const;
  275. QString getTextStatus(const QString &textStatus, const QXmppPresence &presence ) const;
  276. int m_keepAlive;
  277. bool m_reconnectOnError;
  278. bool flSetPresenceWithoutAck;
  279. };
  280. QML_DECLARE_TYPE( MyXmppClient )
  281. #endif