Ignore:
Timestamp:
04/08/10 18:31:53 (14 years ago)
Author:
alexandrecorreia
Message:

Ticket #986 - Correção da lista de contatos para a leitura de um contato em varios grupos.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sandbox/jabberit_messenger/trophy_expresso/js/trophyim.js

    r2437 r2471  
    198198        var cObj = {}; 
    199199        var cookies = document.cookie.split(';'); 
    200         for (var c in cookies) { 
    201             while (cookies[c].charAt(0)==' ') {  
     200        for (var c in cookies) 
     201        { 
     202            while (cookies[c].charAt(0)==' ') 
     203            {  
    202204                cookies[c] = cookies[c].substring(1,cookies[c].length); 
    203205            } 
    204             if (cookies[c].substr(0, 8) == "trophyim") { 
     206            if (cookies[c].substr(0, 8) == "trophyim") 
     207            { 
    205208                var nvpair = cookies[c].split("=", 2); 
    206209                cObj[nvpair[0]] = nvpair[1]; 
     
    208211            } 
    209212        } 
     213         
    210214        return cObj; 
    211215    }, 
     
    242246     *  Store all our data in the JSONStore, if it is active 
    243247     */ 
     248      
    244249   storeData : function() 
    245250   { 
     
    251256        } 
    252257    }, 
     258     
    253259    /**  Function: showlogin 
    254260     * 
     
    257263     *   exists, or creating a new one of we are re-attaching. 
    258264     */ 
     265      
    259266    showLogin : function() 
    260267        { 
     
    483490     *  one exists 
    484491     */ 
    485     clearClient : function() { 
    486         if(TrophyIM.logging_div) { 
    487             var logging_div = TrophyIM.client_div.removeChild( 
    488             document.getElementById('trophyimlog')); 
    489         } else { 
     492      
     493    clearClient : function() 
     494    { 
     495        if(TrophyIM.logging_div) 
     496        { 
     497            var logging_div = TrophyIM.client_div.removeChild(document.getElementById('trophyimlog')); 
     498        } 
     499        else 
     500        { 
    490501            var logging_div = null; 
    491502        } 
    492         while(TrophyIM.client_div.childNodes.length > 0) { 
     503         
     504        while(TrophyIM.client_div.childNodes.length > 0) 
     505        { 
    493506            TrophyIM.client_div.removeChild(TrophyIM.client_div.firstChild); 
    494507        } 
     508         
    495509        return logging_div; 
    496510    }, 
     511     
    497512    /** Function: onVersion 
    498513     * 
    499514     *  jabber:iq:version query handler 
    500515     */ 
    501     onVersion : function(msg) { 
     516      
     517    onVersion : function(msg) 
     518    { 
    502519        Strophe.debug("Version handler"); 
    503         if (msg.getAttribute('type') == 'get') { 
     520        if (msg.getAttribute('type') == 'get') 
     521        { 
    504522            var from = msg.getAttribute('from'); 
    505523            var to = msg.getAttribute('to'); 
     
    512530        return true; 
    513531    }, 
     532     
    514533    /** Function: onRoster 
    515534     * 
    516535     *  Roster iq handler 
    517536     */ 
     537     
    518538    onRoster : function(msg) 
    519539        { 
    520                 //Strophe.debug("Roster handler"); 
    521                  
    522         var roster_items = msg.firstChild.getElementsByTagName('item'); 
     540        var roster_items = msg.firstChild.getElementsByTagName('item'); 
    523541                 
    524542                for (var i = 0; i < roster_items.length; i++) 
    525543                { 
     544                        with(roster_items[i]) 
     545                        { 
     546                                var groups              = getElementsByTagName('group');         
     547                                var group_array = []; 
     548                                 
     549                                for( var g = 0 ; g < groups.length; g++ ) 
     550                                { 
     551                                        if( groups[g].hasChildNodes() ) 
     552                                                group_array[group_array.length] = groups[g].firstChild.nodeValue; 
     553                                } 
     554 
     555                                TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('subscription'), getAttribute('name'), group_array); 
     556                        } 
     557                         
     558                        /* 
    526559                        var groups = roster_items[i].getElementsByTagName('group'); 
    527             var group_array = new Array(); 
     560             
     561                        var group_array = new Array(); 
    528562             
    529563                        for (var g = 0; g < groups.length; g++) 
    530564                        { 
    531                                 group_array[group_array.length] = groups[g].firstChild.nodeValue; 
    532             } 
     565                                if( groups[g].firstChild != null ) 
     566                                        group_array[group_array.length] = groups[g].firstChild.nodeValue; 
     567            } 
     568                         
    533569                        with ( roster_items[i] ) 
    534570                        { 
     571                                alert(getAttribute('jid') + "\n" + getAttribute('subscription') + "\n" + getAttribute('name')); 
    535572                                TrophyIM.rosterObj.addContact(getAttribute('jid'), getAttribute('subscription'), getAttribute('name'), group_array); 
    536573                        } 
    537         } 
    538          
     574                        */ 
     575        } 
     576 
    539577                if ( msg.getAttribute('type') == 'set' ) 
    540578                { 
    541                         TrophyIM.connection.send($iq({type: 'reply', id: 
    542             msg.getAttribute('id'), to: msg.getAttribute('from')}).tree()); 
     579                        var _iq = $iq({type: 'reply', id: msg.getAttribute('id'), to: msg.getAttribute('from')}); 
     580                        TrophyIM.connection.send( _iq.tree()); 
    543581        } 
    544582         
     
    552590        { 
    553591                Strophe.debug("Presence handler"); 
    554         var type = msg.getAttribute('type') ? msg.getAttribute('type') : 'available'; 
    555         var show = msg.getElementsByTagName('show').length ? Strophe.getText(msg.getElementsByTagName('show')[0]) : type; 
    556         var status = msg.getElementsByTagName('status').length ? Strophe.getText(msg.getElementsByTagName('status')[0]) : ''; 
    557         var priority = msg.getElementsByTagName('priority').length ? parseInt(Strophe.getText(msg.getElementsByTagName('priority')[0])) : 0; 
    558                 TrophyIM.rosterObj.setPresence(msg.getAttribute('from'), priority, show, status); 
     592        var type                = msg.getAttribute('type') ? msg.getAttribute('type') : 'available'; 
     593        var show                = msg.getElementsByTagName('show').length ? Strophe.getText(msg.getElementsByTagName('show')[0]) : type; 
     594        var status              = msg.getElementsByTagName('status').length ? Strophe.getText(msg.getElementsByTagName('status')[0]) : ''; 
     595        var priority    = msg.getElementsByTagName('priority').length ? parseInt(Strophe.getText(msg.getElementsByTagName('priority')[0])) : 0; 
     596 
     597        TrophyIM.rosterObj.setPresence(msg.getAttribute('from'), priority, show, status); 
    559598                 
    560599        return true; 
     
    566605    onMessage : function(msg) 
    567606    { 
    568         Strophe.debug("Message handler"); 
    569607     
    570608        var from = msg.getAttribute('from'); 
     
    576614            var barejid         = Strophe.getBareJidFromJid(from); 
    577615            var jid_lower       = barejid.toLowerCase(); 
    578             var contact = ""; 
     616            var contact         = ""; 
    579617                 
    580             if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) 
     618            if( TrophyIM.rosterObj.roster[barejid] ) 
    581619            { 
    582                 contact = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; 
     620                    if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) 
     621                    { 
     622                        contact = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; 
     623                    } 
    583624            } 
    584625            else 
     
    597638            TrophyIM.addMessage(message, jid_lower); 
    598639        } 
     640         
    599641        return true; 
    600642    }, 
     643     
    601644    /** Function: makeChat 
    602645     * 
     
    604647     *  given resource 
    605648     */ 
     649      
    606650    makeChat : function(fulljid) 
    607651    { 
    608          
    609         var barjid = Strophe.getBareJidFromJid(fulljid); 
     652        var barejid             = Strophe.getBareJidFromJid(fulljid); 
     653        var titleWindow = ""; 
    610654         
    611655        var paramsChatBox = 
    612656        { 
    613                         'idChatBox'     : barjid + "__chatBox", 
    614                         'jidTo'                 : barjid, 
     657                        'idChatBox'     : barejid + "__chatBox", 
     658                        'jidTo'                 : barejid, 
    615659                                'path_jabberit' : path_jabberit 
    616660        }; 
    617661         
     662        if( TrophyIM.rosterObj.roster[barejid] ) 
     663        { 
     664            if( TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name'] ) 
     665            { 
     666                titleWindow = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']['name']; 
     667            } 
     668        } 
     669        else 
     670        { 
     671                titleWindow = barejid.toLowerCase(); 
     672                titleWindow = contact.substring(0, contact.indexOf('@')); 
     673        }        
     674 
    618675        var winChatBox =  
    619676        { 
    620                          id_window              : "window_chat_area_" + barjid, 
     677                         id_window              : "window_chat_area_" + barejid, 
    621678                         width                  : 387, 
    622679                         height                 : 365, 
     
    627684                         resizable              : true, 
    628685                         zindex                 : loadIM.getZIndex(), 
    629                          title                  : barjid.substring(0, barjid.indexOf('@')), 
     686                         title                  : titleWindow, 
    630687                         closeAction    : "hidden", 
    631688                         content                : loadIM.parse("chat_box","chatBox.xsl", paramsChatBox)  
    632689        } 
    633690         
    634         _winBuild(winChatBox) 
     691        _winBuild(winChatBox); 
    635692                 
    636                 loadIM.configEvents( 
    637                         document.getElementById( barjid + '__sendBox'), 
     693                loadIM.configEvents( document.getElementById( barejid + '__sendBox'), 
    638694                        'onkeyup', function(e) 
    639695                        { 
    640696                                if( e.keyCode == 13 ) 
    641697                                { 
    642                                         TrophyIM.sendMessage( barjid ); 
    643                                         document.getElementById( barjid + '__sendBox').value = ''; 
     698                                        TrophyIM.sendMessage( barejid ); 
     699                                        document.getElementById( barejid + '__sendBox').value = ''; 
    644700                                        return false; 
    645701                                } 
     
    665721         
    666722        // Add Contact 
    667         var newContact = $iq({type: 'set', id: 'setAddContact'}); 
     723        var _id = TrophyIM.connection.getUniqueId();  
     724                var newContact = $iq({type: 'set', id: _id }); 
    668725                        newContact = newContact.c('query').attrs({xmlns : 'jabber:iq:roster'}); 
    669                         newContact = newContact.c('item').attrs({jid: jidTo, name:name, ask:'subscribe', subscription:'to'}); 
     726                        newContact = newContact.c('item').attrs({jid: jidTo, name:name }); 
    670727                        newContact = newContact.c('group').t(group).tree(); 
    671728 
     
    704761        if( ( divItenContact = document.getElementById('itenContact_' + jidTo))) 
    705762        {        
     763                loadIM.removeGroup( divItenContact.parentNode ); 
     764                 
    706765                divItenContact.parentNode.removeChild(divItenContact); 
    707766                 
    708                 // Delete Object groups 
    709                         for( var nameGroup in TrophyIM.rosterObj.groups ) 
    710                                 for(var Jid in TrophyIM.rosterObj.groups[nameGroup]) 
    711                                         if(TrophyIM.rosterObj.groups[nameGroup][Jid] == jidTo ) 
    712                                                 delete TrophyIM.rosterObj.groups[nameGroup][Jid]; 
    713                          
    714                         // Delete Object roster 
    715                         if( TrophyIM.rosterObj.roster[jidTo] ) 
    716                                 delete TrophyIM.rosterObj.roster[jidTo]; 
    717                  
    718                         TrophyIM.rosterObj.save(); 
    719                          
    720767                // Remove Contact 
    721                 var delContact  = $iq({type: 'set', id: Date.parse( new Date )}) 
     768                        var _id = TrophyIM.connection.getUniqueId();     
     769                var delContact  = $iq({type: 'set', id: _id}) 
    722770                        delContact      = delContact.c('query').attrs({xmlns : 'jabber:iq:roster'}); 
    723                         delContact      = delContact.c('item').attrs({jid: jidTo, subscription:'remove'});//.tree(); 
     771                        delContact      = delContact.c('item').attrs({jid: jidTo, subscription:'remove'}).tree(); 
    724772                 
    725773                TrophyIM.connection.send(delContact); 
     
    735783        renderRoster : function() 
    736784        { 
    737                 if( TrophyIM.rosterObj.changes.length > 0 ) 
    738                 { 
    739                         var roster_div = document.getElementById('JabberIMRoster'); 
     785                var roster_div = document.getElementById('JabberIMRoster'); 
     786                 
     787                if( roster_div ) 
     788                { 
     789                        var users = new Array(); 
    740790                         
    741                         if( roster_div ) 
    742                         { 
    743                                 var users = new Array(); 
     791                        for( var user in TrophyIM.rosterObj.roster ) 
     792                        { 
     793                                users[users.length] = TrophyIM.rosterObj.roster[user].contact.jid; 
     794                        } 
     795 
     796                        users.sort(); 
     797                         
     798                        var groups              = new Array(); 
     799                        var flagGeral   = false; 
     800                         
     801                        for (var group in TrophyIM.rosterObj.groups) 
     802                        { 
     803                                if( group ) 
     804                                        groups[groups.length] = group; 
    744805                                 
    745                                 for( var user in TrophyIM.rosterObj.roster ) 
    746                                 { 
    747                                         users[users.length] = TrophyIM.rosterObj.roster[user].contact.jid; 
    748                                 } 
     806                                if( group == "Geral" ) 
     807                                        flagGeral = true; 
     808            } 
     809             
     810                        if( !flagGeral ) 
     811                                groups[groups.length] = "Geral"; 
    749812                                 
    750                                 users.sort(); 
    751                                  
    752                                 var groups = new Array(); 
    753                                  
    754                                 for (var group in TrophyIM.rosterObj.groups) 
    755                                 { 
    756                                         if( group ) 
    757                                                 groups[groups.length] = group; 
    758                 } 
    759                  
    760                                 groups.sort(); 
    761                                  
    762                                 for ( var i = 0; i < groups.length; i++ ) 
    763                                 { 
    764                                         TrophyIM.renderGroups( groups[i] , roster_div.lastChild );       
    765                                 } 
    766                                  
    767                                 TrophyIM.renderItensGroup(users, roster_div); 
     813                        groups.sort(); 
     814                         
     815                        for ( var i = 0; i < groups.length; i++ ) 
     816                        { 
     817                                TrophyIM.renderGroups( groups[i] , roster_div );         
    768818                        } 
     819                         
     820                        TrophyIM.renderItensGroup( users, roster_div ); 
    769821                } 
    770822                         
     
    781833                var _addGroup = function() 
    782834                { 
     835                        var _nameGroup  = nameGroup; 
     836                        var _element    = element; 
     837 
    783838                        var paramsGroup =  
    784839                        { 
    785                                 'nameGroup'     : arguments[0], 
     840                                'nameGroup'     : _nameGroup, 
    786841                                'path_jabberit' : path_jabberit 
    787842                        } 
    788843                         
    789                         element.innerHTML += loadIM.parse("group","groups.xsl", paramsGroup); 
    790                          
     844                        _element.innerHTML += loadIM.parse("group","groups.xsl", paramsGroup); 
    791845                } 
    792846 
    793847                if( !element.hasChildNodes() ) 
    794848                { 
    795                         _addGroup(nameGroup);    
     849                        _addGroup(); 
    796850                } 
    797851                else 
    798852                { 
    799                         var elementChild = element; 
    800                         var flag = false; 
     853                        var _NodeChild  = element.firstChild; 
     854                        var flagAdd             = false; 
    801855                         
    802                         while ( elementChild ) 
    803                         { 
    804                                 if ( elementChild.childNodes[0].childNodes[0].firstChild.nodeValue === nameGroup ) 
     856                        while( _NodeChild ) 
     857                        { 
     858                                if( _NodeChild.childNodes[0].nodeName.toLowerCase() === "span" ) 
    805859                                { 
    806                                         flag = true; 
     860                                        if( _NodeChild.childNodes[0].childNodes[0].nodeValue === nameGroup ) 
     861                                        { 
     862                                                flagAdd = true; 
     863                                        } 
    807864                                } 
    808865                                 
    809                                 elementChild = elementChild.nextSibling; 
     866                                _NodeChild = _NodeChild.nextSibling; 
    810867                        } 
    811                          
    812                         if( !flag ) 
    813                                 _addGroup( nameGroup ); 
     868 
     869                        if( !flagAdd ) 
     870                        { 
     871                                _addGroup(); 
     872                        } 
    814873                } 
    815                  
    816874        }, 
    817875 
     
    823881        renderItensGroup : function( users, element ) 
    824882        { 
    825                 var addItemGroup = function() 
     883                var addItem = function() 
    826884                { 
    827885                        if( arguments.length > 0 ) 
    828886                        { 
    829887                                var objContact  = arguments[0]; 
    830                                 var element             = arguments[1]; 
    831                                 var itensJid    = document.getElementById( 'itenContact_' + objContact.contact.jid ); 
     888                                var group               = arguments[1]; 
     889                                var element             = arguments[2]; 
     890                                var index               = arguments[3]; 
     891                                 
     892                                var itensJid    = document.getElementById( 'itenContact_' + objContact.contact.jid + '_' + index ); 
    832893                                 
    833894                                if( itensJid == null ) 
     
    853914                                                for (var resource in objContact.presence) 
    854915                                                { 
    855                                                         presence = objContact.presence[resource].show; 
    856                                                          
     916                                                        if( objContact.presence[resource].show != 'invisible' ) 
     917                                                                presence = objContact.presence[resource].show; 
     918 
    857919                                                        if( objContact.presence[resource].status ) 
    858920                                                        { 
     
    867929                                                'nameContact'   : nameContact, 
    868930                                                'jid'                   : objContact.contact.jid,        
    869                                                 'id'                    : 'itenContact_' + objContact.contact.jid, 
     931                                                'id'                    : 'itenContact_' + objContact.contact.jid + '_' + index , 
     932                                                'index'                 : index, 
     933                                                'path_jabberit' : path_jabberit, 
    870934                                                'presence'              : presence, 
    871                                                 'path_jabberit' : path_jabberit, 
    872935                                                'status'                : status, 
    873936                                                'statusDisplay' : statusDisplay 
    874937                                        } 
    875938                                         
    876                                         if( element.id == "rosterIM_with_groups" ) 
     939                                        if( group != "") 
    877940                                        { 
    878                                                 var elementChild = element; 
    879                                  
    880                                                 while ( elementChild ) 
     941                                                var _NodeChild          = element.firstChild; 
     942                                                 
     943                                                while( _NodeChild ) 
    881944                                                { 
    882                                                         if( elementChild.childNodes[0].childNodes[0].firstChild.nodeValue == objContact.contact.groups[0] ) 
    883                                                                 elementChild.childNodes[0].innerHTML += loadIM.parse("itens_group", "itensGroup.xsl", paramsContact); 
    884  
    885                                                         elementChild = elementChild.nextSibling; 
     945                                                        if( _NodeChild.childNodes[0].nodeName.toLowerCase() === "span" ) 
     946                                                        { 
     947                                                                if( _NodeChild.childNodes[0].childNodes[0].nodeValue === group ) 
     948                                                                { 
     949                                                                        _NodeChild.innerHTML += loadIM.parse("itens_group", "itensGroup.xsl", paramsContact); 
     950                                                                } 
     951                                                        } 
     952 
     953                                                        _NodeChild = _NodeChild.nextSibling; 
    886954                                                } 
    887                                         } 
    888                                         else 
    889                                         { 
    890                                                 element.innerHTML += loadIM.parse("itens_group", "itensGroup.xsl", paramsContact);                                                       
    891                                         } 
     955                                        }        
    892956                                } 
    893957                                else 
    894958                                { 
    895                                          
    896959                                        // Presence e Status 
    897960                                        var presence            = "unavailable"; 
     
    901964                                        if (objContact.presence) 
    902965                                        { 
    903                                                 for (var resource in objContact.presence) 
     966                                                for ( var resource in objContact.presence ) 
    904967                                                { 
    905968                                                        presence = objContact.presence[resource].show; 
     
    907970                                                        if( objContact.presence[resource].status ) 
    908971                                                        { 
    909                                                                 status                  = objContact.presence[resource].status; 
     972                                                                status                  = " ( " + objContact.presence[resource].status + " ) "; 
    910973                                                                statusDisplay   = "block"; 
    911974                                                        } 
     975                                                }        
     976                                        } 
     977                                                 
     978                                        var is_open = itensJid.parentNode.childNodes[0].style.backgroundImage;   
     979                                                is_open = is_open.indexOf("arrow_down.gif"); 
     980                                         
     981                                        with ( document.getElementById('span_show_' + 'itenContact_' + objContact.contact.jid + '_' + index ) ) 
     982                                        { 
     983                                                if( is_open > 0 ) 
     984                                                { 
     985                                                        style.display   = statusDisplay; 
     986                                                        innerHTML               = status; 
    912987                                                } 
    913988                                        } 
    914  
    915                                         itensJid.style.background                       = "url('"+path_jabberit+"templates/default/images/" + presence + ".gif')no-repeat center left"; 
    916                                         itensJid.lastChild.style.display        = statusDisplay; 
    917                                         itensJid.lastChild.innerHTML            = " ( " + status + " ) "; 
     989                                         
     990                                        itensJid.style.background       = "url('"+path_jabberit+"templates/default/images/" + presence + ".gif')no-repeat center left"; 
    918991                                } 
     992                                 
    919993                        } 
    920994                } 
     
    923997                { 
    924998                        if( TrophyIM.rosterObj.roster[users[i]].contact.groups ) 
    925                                 if( TrophyIM.rosterObj.roster[users[i]].contact.groups[0] ) 
    926                                         addItemGroup(TrophyIM.rosterObj.roster[users[i]], element.lastChild ); 
     999                        { 
     1000                                var groups = TrophyIM.rosterObj.roster[users[i]].contact.groups; 
     1001                                 
     1002                                if( groups.length > 0 ) 
     1003                                { 
     1004                                        for( var j = 0; j < groups.length; j++ ) 
     1005                                                addItem( TrophyIM.rosterObj.roster[users[i]], groups[j], element, j ); 
     1006                                } 
    9271007                                else 
    928                                         addItemGroup(TrophyIM.rosterObj.roster[users[i]], element.firstChild ); 
     1008                                { 
     1009                                        addItem( TrophyIM.rosterObj.roster[users[i]], "Geral", element, 0 ); 
     1010                                } 
     1011                        } 
    9291012                        else 
    930                                 addItemGroup(TrophyIM.rosterObj.roster[users[i]], element.firstChild ); 
     1013                        { 
     1014                                addItem( TrophyIM.rosterObj.roster[users[i]], "Geral", element, 0 ); 
     1015                        } 
    9311016                }        
    9321017        }, 
     
    10291114        } 
    10301115    } 
     1116 
     1117        /** Function: addChange 
     1118         * 
     1119         *  Adds given jid to this.changes, keeping this.changes sorted and 
     1120         *  preventing duplicates. 
     1121         * 
     1122         *  Parameters 
     1123         *    (String) jid : jid to add to this.changes 
     1124         */ 
     1125          
     1126        this.addChange = function(jid) 
     1127        { 
     1128                for (var c = 0; c < this.changes.length; c++) 
     1129                { 
     1130                        if (this.changes[c] == jid) 
     1131                        { 
     1132                                return; 
     1133                        } 
     1134                } 
     1135                 
     1136                this.changes[this.changes.length] = jid; 
     1137                 
     1138                this.changes.sort(); 
     1139        } 
    10311140         
    10321141    /** Function: addContact 
     
    10381147     *    (String) subscription - subscription attribute for contact 
    10391148     *    (String) name - name attribute for contact 
    1040      *    (Array) groups - array of groups contact is member of 
    1041      */ 
    1042      
    1043         this.addContact = function(jid, subscription, name, groups) 
    1044         { 
    1045         var contact = { jid:jid, subscription:subscription, name:name, groups:groups } 
    1046         var jid_lower = jid.toLowerCase(); 
    1047  
    1048                 if ( this.roster[jid_lower] ) 
    1049                 { 
    1050             this.roster[jid_lower]['contact'] = contact; 
    1051         } 
    1052                 else 
    1053                 { 
    1054             this.roster[jid_lower] = {contact:contact}; 
    1055         } 
    1056  
    1057                 groups = groups ? groups : ['']; 
    1058          
    1059                 for ( var g = 0; g < groups.length; g++ ) 
    1060                 { 
    1061                         if ( !this.groups[groups[g]] ) 
    1062                         { 
    1063                 this.groups[groups[g]] = {}; 
    1064             } 
    1065              
    1066                         this.groups[groups[g]][jid_lower] = jid_lower; 
    1067         } 
     1149     *    (Array)  groups - array of groups contact is member of 
     1150     */ 
     1151     
     1152        this.addContact = function(jid, subscription, name, groups ) 
     1153        { 
     1154        if( jid != loadIM.getUserCurrent().jid ) 
     1155        {  
     1156                if( subscription !== "remove" ) 
     1157                { 
     1158                        var contact             = { jid:jid, subscription:subscription, name:name, groups:groups } 
     1159                        var jid_lower   = jid.toLowerCase(); 
     1160                 
     1161                                if ( this.roster[jid_lower] ) 
     1162                                { 
     1163                            this.roster[jid_lower]['contact'] = contact; 
     1164                        } 
     1165                                else 
     1166                                { 
     1167                            this.roster[jid_lower] = {contact:contact}; 
     1168                        } 
     1169                 
     1170                                groups = groups ? groups : ['']; 
     1171                         
     1172                                for ( var g = 0; g < groups.length; g++ ) 
     1173                                { 
     1174                                        if ( !this.groups[groups[g]] ) 
     1175                                        { 
     1176                                this.groups[groups[g]] = {}; 
     1177                            } 
     1178                             
     1179                                        this.groups[groups[g]][jid_lower] = jid_lower; 
     1180                        } 
     1181                } 
     1182                else 
     1183                { 
     1184                        this.removeContact(jid); 
     1185                } 
     1186        } 
    10681187    } 
     1188     
    10691189    /** Function: getContact 
    10701190     * 
     
    10731193     *  Parameter: (String) jid - jid to return 
    10741194     */ 
     1195      
    10751196    this.getContact = function(jid) 
    10761197        { 
     
    10801201        } 
    10811202    } 
     1203 
     1204   /** Function: getPresence 
     1205        * 
     1206        *  Returns best presence for given jid as Array(resource, priority, show, 
     1207        *  status) 
     1208        * 
     1209        *  Parameter: (String) fulljid - jid to return best presence for 
     1210        */ 
     1211          
     1212        this.getPresence = function(fulljid) 
     1213        { 
     1214                var jid = Strophe.getBareJidFromJid(fulljid); 
     1215                var current = null; 
     1216                     
     1217                if (this.roster[jid.toLowerCase()] && this.roster[jid.toLowerCase()]['presence']) 
     1218                { 
     1219                        for (var resource in this.roster[jid.toLowerCase()]['presence']) 
     1220                        { 
     1221                                var presence = this.roster[jid.toLowerCase()]['presence'][resource]; 
     1222                                if (current == null) 
     1223                                { 
     1224                                        current = presence 
     1225                                } 
     1226                                else 
     1227                                { 
     1228                                        if(presence['priority'] > current['priority'] && ((presence['show'] == "chat" 
     1229                                        || presence['show'] == "available") || (current['show'] != "chat" || 
     1230                                        current['show'] != "available"))) 
     1231                                        { 
     1232                                                current = presence 
     1233                                        } 
     1234                                } 
     1235                        } 
     1236                } 
     1237                return current; 
     1238        } 
     1239 
     1240        /** Function: groupHasChanges 
     1241         * 
     1242         *  Returns true if current group has members in this.changes 
     1243         * 
     1244         *  Parameters: 
     1245         *    (String) group - name of group to check 
     1246         */ 
     1247          
     1248        this.groupHasChanges = function(group) 
     1249        { 
     1250                for (var c = 0; c < this.changes.length; c++) 
     1251                { 
     1252                        if (this.groups[group][this.changes[c]]) 
     1253                        { 
     1254                                return true; 
     1255                        } 
     1256                } 
     1257                return false; 
     1258        } 
     1259         
     1260        /** Function removeContact 
     1261         * 
     1262         * Parameters 
     1263         *       (String) jid            
     1264         */ 
     1265          
     1266         this.removeContact = function(jid) 
     1267         { 
     1268                var groups = this.roster[ jid ].contact.groups; 
     1269                 
     1270                for ( var i = 0; i < groups.length; i++ ) 
     1271                { 
     1272                        delete this.groups[ groups[ i ] ][ jid ]; 
     1273                } 
     1274 
     1275                for ( var i = 0; i < groups.length; i++ ) 
     1276                { 
     1277                        var contacts = 0; 
     1278                        for ( var contact in this.groups[ groups[ i ] ] ) 
     1279                                contacts++; 
     1280 
     1281                        if ( ! contacts ) 
     1282                                delete this.groups[ groups[ i ] ]; 
     1283                } 
     1284 
     1285                // Delete Object roster 
     1286                if( this.roster[jid] ) 
     1287                        delete this.roster[jid];                 
     1288         } 
     1289          
    10821290    /** Function: setPresence 
    10831291     * 
     
    10971305        var jid_lower = barejid.toLowerCase(); 
    10981306         
    1099                 if( show != 'unavailable') 
    1100                 { 
    1101             if (!this.roster[jid_lower]) 
    1102                         { 
    1103                 this.addContact(barejid, 'not-in-roster'); 
    1104             } 
    1105             var presence = 
    1106                         { 
    1107                 resource:resource, priority:priority, show:show, status:status 
    1108             } 
    1109              
    1110                         if (!this.roster[jid_lower]['presence']) 
    1111                         { 
    1112                 this.roster[jid_lower]['presence'] = {} 
    1113             } 
    1114             this.roster[jid_lower]['presence'][resource] = presence 
    1115         } 
    1116                 else if (this.roster[jid_lower] && this.roster[jid_lower]['presence'] && this.roster[jid_lower]['presence'][resource]) 
    1117                 { 
    1118             delete this.roster[jid_lower]['presence'][resource]; 
    1119         } 
    1120          
    1121                 this.addChange(jid_lower); 
    1122          
    1123                 if (TrophyIM.activeChats['divs'][jid_lower]) 
    1124                 { 
    1125             TrophyIM.setTabPresence(jid_lower, TrophyIM.activeChats['divs'][jid_lower]['tab']); 
     1307        if( barejid != loadIM.getUserCurrent().jid ) 
     1308        { 
     1309                        if( show != 'unavailable') 
     1310                        { 
     1311                    if (!this.roster[jid_lower]) 
     1312                                { 
     1313                        this.addContact(barejid, 'not-in-roster'); 
     1314                    } 
     1315                    var presence = 
     1316                                { 
     1317                        resource:resource, priority:priority, show:show, status:status 
     1318                    } 
     1319                     
     1320                                if (!this.roster[jid_lower]['presence']) 
     1321                                { 
     1322                        this.roster[jid_lower]['presence'] = {} 
     1323                    } 
     1324                    this.roster[jid_lower]['presence'][resource] = presence 
     1325                } 
     1326                        else if (this.roster[jid_lower] && this.roster[jid_lower]['presence'] && this.roster[jid_lower]['presence'][resource]) 
     1327                        { 
     1328                    delete this.roster[jid_lower]['presence'][resource]; 
     1329                } 
     1330                 
     1331                        this.addChange(jid_lower); 
     1332                 
     1333                        if (TrophyIM.activeChats['divs'][jid_lower]) 
     1334                        { 
     1335                    TrophyIM.setTabPresence(jid_lower, TrophyIM.activeChats['divs'][jid_lower]['tab']); 
     1336                } 
    11261337        } 
    11271338    } 
    1128     /** Function: addChange 
    1129      * 
    1130      *  Adds given jid to this.changes, keeping this.changes sorted and 
    1131      *  preventing duplicates. 
    1132      * 
    1133      *  Parameters 
    1134      *    (String) jid : jid to add to this.changes 
    1135      */ 
    1136     this.addChange = function(jid) { 
    1137         for (var c = 0; c < this.changes.length; c++) { 
    1138             if (this.changes[c] == jid) { 
    1139                 return; 
    1140             } 
    1141         } 
    1142         this.changes[this.changes.length] = jid; 
    1143         this.changes.sort(); 
    1144     } 
    1145     /** Function: getPresence 
    1146      * 
    1147      *  Returns best presence for given jid as Array(resource, priority, show, 
    1148      *  status) 
    1149      * 
    1150      *  Parameter: (String) fulljid - jid to return best presence for 
    1151      */ 
    1152     this.getPresence = function(fulljid) { 
    1153         var jid = Strophe.getBareJidFromJid(fulljid); 
    1154         var current = null; 
    1155         if (this.roster[jid.toLowerCase()] && 
    1156         this.roster[jid.toLowerCase()]['presence']) { 
    1157             for (var resource in this.roster[jid.toLowerCase()]['presence']) { 
    1158                 var presence = this.roster[jid.toLowerCase()]['presence'][resource]; 
    1159                 if (current == null) { 
    1160                     current = presence 
    1161                 } else { 
    1162                     if(presence['priority'] > current['priority'] && ((presence['show'] == "chat" 
    1163                     || presence['show'] == "available") || (current['show'] != "chat" || 
    1164                     current['show'] != "available"))) { 
    1165                         current = presence 
    1166                     } 
    1167                 } 
    1168             } 
    1169         } 
    1170         return current; 
    1171     } 
    1172     /** Function: groupHasChanges 
    1173      * 
    1174      *  Returns true if current group has members in this.changes 
    1175      * 
    1176      *  Parameters: 
    1177      *    (String) group - name of group to check 
    1178      */ 
    1179     this.groupHasChanges = function(group) { 
    1180         for (var c = 0; c < this.changes.length; c++) { 
    1181             if (this.groups[group][this.changes[c]]) { 
    1182                 return true; 
    1183             } 
    1184         } 
    1185         return false; 
    1186     } 
    1187     /** Fuction: save 
    1188      * 
    1189      *  Saves roster data to JSON store 
    1190      */ 
    1191     this.save = function() { 
    1192         if (TrophyIM.JSONStore.store_working) { 
    1193             TrophyIM.JSONStore.setData({roster:this.roster, 
    1194             groups:this.groups, active_chat:TrophyIM.activeChats['current'], 
    1195             chat_history:TrophyIM.chatHistory}); 
    1196         } 
    1197     } 
     1339 
     1340        /** Fuction: save 
     1341         * 
     1342         *  Saves roster data to JSON store 
     1343         */ 
     1344         
     1345        this.save = function() 
     1346        { 
     1347                if (TrophyIM.JSONStore.store_working) 
     1348                { 
     1349                        TrophyIM.JSONStore.setData({roster:this.roster, 
     1350                        groups:this.groups, active_chat:TrophyIM.activeChats['current'], 
     1351                        chat_history:TrophyIM.chatHistory}); 
     1352                } 
     1353        } 
     1354 
    11981355} 
    11991356/** Class: TrophyIMJSONStore 
Note: See TracChangeset for help on using the changeset viewer.