1 Xmpp4Js.Lang.namespace( "Xmpp4Js.Chat" );
  2 
  3 /**
  4 * @class A chat is a series of messages sent between two users. 
  5 * Each chat has a unique thread ID, which is used to 
  6 * track which messages are part of a particular 
  7 * conversation. Some messages are sent without a 
  8 * thread ID, and some clients don't send thread IDs 
  9 * at all. Therefore, if a message without a thread 
 10 * ID arrives it is routed to the most recently 
 11 * created Chat with the message sender.
 12 *
 13 * @param {Xmpp4Js.Chat.ChatManager} chatMan
 14 * @param {Xmpp4Js.Jid} jid
 15 * @param {String} thread
 16 *
 17 * @constructor
 18 */
 19 Xmpp4Js.Chat.Chat = function(chatMan, jid, thread) {
 20     jid = new Xmpp4Js.Jid( jid );
 21     
 22     /**
 23      * @type Xmpp4Js.Chat.ChatManager
 24      * @private
 25      */
 26     this.chatMan = chatMan;
 27     /**
 28      * @type Xmpp4Js.Jid
 29      * @private
 30      */
 31     this.participant = jid;
 32     /**
 33      * @type String
 34      * @private
 35      */
 36     this.thread = thread;
 37     
 38     this.addEvents({
 39         /**
 40         * @event messageReceived
 41         * Fires when a message is received.
 42         * @param {Xmpp4Js.Chat.Chat} chat the chat window
 43         * @param {Xmpp4Js.Packet.Message} message The incoming message
 44         */
 45         "messageReceived" : true,
 46         
 47         /**
 48         * @event messageSent
 49         * Fires when a message is sent.
 50         * @param {Xmpp4Js.Chat.Chat} chat the chat window
 51         * @param {Xmpp4Js.Packet.Message} message The incoming message
 52         */
 53         "messageSent" : true,
 54 
 55         /**
 56         * Fires when a chat is closed. At this point it only happens locally, with
 57         * a call to close(). If a new chat from the same user or with the same threadId
 58         * comes, it will be considered a new conversation and the chatStarted event
 59         * will be fired.
 60         */
 61         "close" : true
 62     });
 63 }
 64 
 65 Xmpp4Js.Chat.Chat.prototype = {
 66     /**
 67      * Send a message to the party in this message. Always to Jid and thread.
 68      * @param {Xmpp4Js.Packet.Message} msg
 69      */
 70     sendMessage : function( msg ) {
 71         if( !(msg instanceof Xmpp4Js.Packet.Message) ) {
 72             msg = this.createMessage( msg );
 73         }
 74 
 75         var toJid = this.getParticipant().withoutResource();
 76 
 77         // always set to and thread, no matter what the object says.
 78         msg.setToJid( toJid );
 79         msg.setThread( this.getThread() );
 80 
 81         this.chatMan.getConnection().send( msg );
 82 
 83         //hack
 84         //	this.con.stream.pw.flush();
 85 
 86         var messageText = msg.getBody();
 87         this.fireEvent("messageSent", this, msg);
 88     },
 89 
 90     /**
 91      * Returns the Jid of the message sender--obtained from the connection.
 92      * @see Xmpp4Js.JabberConnection#getJid
 93      * @return {Xmpp4Js.Jid}
 94      */
 95     getOutgoingJid : function() {
 96         return this.chatMan.getConnection().getJid();
 97     },
 98 
 99     /**
100      * Creates a message associated with this chat session, including thread and to.
101      * @param {String} message
102      * @return Xmpp4Js.Packet.Message
103      */
104     createMessage : function( msg ) {
105         var packetHelper = this.chatMan.getConnection().getPacketHelper();
106         var msg = packetHelper.createMessage( this.participant, "chat", msg );
107         msg.setThread( this.getThread() );
108 
109         return msg;
110     },
111 
112     /**
113      * Returns the Jid of the user the chat is with.
114      * @return {Xmpp4Js.Jid} 
115      */
116     getParticipant : function() {
117         return this.participant;
118     },
119 
120     /**
121      * Returns the thread ID of the current chat. Lazilly sets a thread if none exists
122      * @return {String} The thread ID 
123      */
124     getThread : function() {
125         if( !this.thread ) {
126             this.thread = Math.random();
127         }
128         return this.thread;
129     },
130     
131     /**
132      * Closes a conversation. See ChatManager.closeChat for more info.
133      * @see Xmpp4Js.Chat.ChatMan#closeChat
134      */
135     close : function() {
136         this.chatMan.closeChat( this );
137     }
138 };
139 
140 Xmpp4Js.Lang.extend(Xmpp4Js.Chat.Chat, Xmpp4Js.Event.EventProvider, Xmpp4Js.Chat.Chat.prototype);
141