⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsrosterbean.java

📁 一款即时通讯软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package edu.ou.kmi.buddyspace.core;

/*
 * BSRosterBean.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 17 July 2002, 11:03
 */

import java.util.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
import org.jabber.jabberbeans.Extension.*;

/**
 * <code>BSRosterBean</code> provides roster handling.
 * It relies on <code>BSInfoQueryBean</code>, which must be set after each
 * reconnection. It uses <code>RosterBean</code>.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class BSRosterBean implements RosterListener, ConnectionListener {
    private Hashtable curRoster;
    private RosterBean rosterBean = null;
    private final String name = "Roster";
    private Vector rosterListeners;
    
    /**
     * Constructor
     */
    BSRosterBean() {
        curRoster = new Hashtable();
        rosterListeners = new Vector();
    }
    
    /**
     * Constructor, which sets existing and connected <code>IQBean</code>.
     * Then <code>RosterBean</code> is created and this is registered
     * as listener for connection and roster event.
     */
    BSRosterBean(IQBean iqBean) {
        curRoster = new Hashtable();
        rosterListeners = new Vector();
        setIQBean(iqBean);
    }
    
    /**
     * Sets existing and connected <code>IQBean</code>. 
     * Then <code>RosterBean</code> is created and this is registered
     * as listener for connection and roster event.
     */
    protected void setIQBean(IQBean iqBean) {
        if (iqBean != null) {
            rosterBean = new RosterBean();
            rosterBean.setIQBean(iqBean);
            rosterBean.addRosterListener(this);
            ConnectionBean connection = iqBean.getConnection();
            if (connection != null) 
                connection.addConnectionListener(this);
        }
        curRoster.clear();
    }

    /**
     * Returns currently used <code>IQBean</code>.
     */
    protected IQBean getIQBean() {
        return (rosterBean != null)? rosterBean.getIQBean() : null;
    }
    
    /**
     * Returns currently used <code>RosterBean</code>.
     */
    protected RosterBean getRosterBean() {
        return rosterBean;
    }
    
    /**
     * Returns <code>Enumeration</code> of roster entries. The entries are of
     * <code>RosterItem</code> type.
     */
    public Enumeration entries() {
        return curRoster.elements();
    }
    
    /**
     * Frees all object bindings to allow object destroy
     */
    protected void prepareToDestroy() {
        removeAllRosterListeners();
        rosterBean = null;
    }
    
    /**
     * Sends request to server for roster refresh.
     * After the refreshed roster comes <code>replacedRoster</code>
     * is invoked.
     */
    public void refreshRoster() {
        if (rosterBean == null) {
            BSCore.logEvent(name, "error: not connected");
            return;
        }
        try {
            rosterBean.refreshRoster();
        } catch (InstantiationException e) {
            BSCore.logEvent(name, "error: roster refresh failed");
        }
    }
    
    /**
     * Clears the current roster.
     * This function is typically called after disconnecting.
     */
    public void clear() {
        curRoster.clear();
        try {
            RosterBuilder rb = new RosterBuilder();
            fireReplacedRoster(new Roster(rb));
        } catch(InstantiationException e) {}
    }
    
    /**
     * Adds contact into the roster. Returns if the request was successfuly
     * sent to server. But still the server can return error as a response.
     */
    public boolean addContact(JID jid, String nick, String group) {
        if (jid == null || rosterBean == null)
            return false;
        
        RosterItemBuilder rib = new RosterItemBuilder();
        
        // sets the group
        if (group != null && !group.equals(""))
            rib.addGroup(group);
        
        // checks if the jid is already in some other group
        String str = getRosterItemHashString(jid, "", false);
        RosterItem old = (RosterItem) curRoster.get(str);
        if (old != null) {
            Enumeration groupEnum = old.enumerateGroups();
            while (groupEnum.hasMoreElements()) {
                String gr = (String) groupEnum.nextElement();
                rib.addGroup(gr);
            }
        }
        
        // sets the nick
        if (nick != null && !nick.equals(""))
            rib.setFriendlyName(nick);
        else rib.setFriendlyName(jid.toString());
        
        // sets JID
        rib.setJID(jid);
        
        try {
            rosterBean.addRosterItem(rib.build());
            return true;
        } catch (InstantiationException e) {
            return false;
        }
    }
    
    /**
     * Changes contact in roster. Returns if the request was successfuly
     * sent to server. But still the server can return error as a response.
     * When the group or oldGroup is null, it means that no group is used
     * (the contact is not in any group).
     */
    public boolean changeContact(JID jid, String nick, String group, String oldGroup) {
        if (jid == null || rosterBean == null)
            return false;
        
        RosterItemBuilder rib = new RosterItemBuilder();
        
        // goes through groups and changes the right one
        boolean changed = false;
        String str = getRosterItemHashString(jid, "", false);
        RosterItem old = (RosterItem) curRoster.get(str);
        if (old != null) {
            Enumeration groupEnum = old.enumerateGroups();
            // if there was no group before
            if (!groupEnum.hasMoreElements() && oldGroup == null) {
                rib.addGroup(group);
                changed = true;
            }
            // if there were groups
            while (groupEnum.hasMoreElements()) {
                String gr = (String) groupEnum.nextElement();
                if (!changed && gr.equals(oldGroup)) {
                    // if there is a new group
                    if (group != null)
                        rib.addGroup(group);
                    changed = true;
                }
                else {
                    rib.addGroup(gr);
                }
            }
        }
        else {
            // sets the group
            if (group != null && !group.equals(""))
                rib.addGroup(group);
        }
        
        // sets the nick
        if (nick != null && !nick.equals(""))
            rib.setFriendlyName(nick);
        else rib.setFriendlyName(jid.toString());
        
        // sets JID
        rib.setJID(jid);
        
        try {
            rosterBean.addRosterItem(rib.build());
            return true;
        } catch (InstantiationException e) {
            return false;
        }
    }
    
    /**
     * Deletes contact with JID from given group in the roster.
     * Returns if the request was successfuly
     * sent to server. But still the server can return error as a response.
     */
    public boolean deleteJIDFromGroup(JID jid, String group) {
        if (jid == null || rosterBean == null || group == null)
            return false;
        
        RosterItemBuilder rib = new RosterItemBuilder();
        
        String str = getRosterItemHashString(jid, "", false);
        RosterItem oldItem = (RosterItem) curRoster.get(str);
        
        rib.copyItem(oldItem);
        Vector groupsVec = rib.getGroups();
        boolean changed = groupsVec.remove(group);
        if (groupsVec.size() == 0) {
            try {
                rosterBean.delRosterItem(rib.build());
                return true;
            } catch (InstantiationException e) {
                return false;
            }
        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -