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

📄 getfriendlistjob.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
* 					 糊涂虫 <jtdeng518@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.ui.jobs;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;

import org.eclipse.core.runtime.IProgressMonitor;

import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.ModelUtils;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.beans.QQFriend;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.packets.in.GetFriendListReplyPacket;
import edu.tsinghua.lumaqq.ui.MainShell;

/**
 * 得到好友列表的任务
 * 
 * @author luma
 */
public class GetFriendListJob extends AbstractJob implements IQQListener {
    private class AddItemRunnable implements Runnable {
        public int index;
        public FriendModel model;
        
        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        public void run() {
		    main.getModel().addItem(index, model);
        }
    }
    
	private boolean finished;
	private IProgressMonitor monitor;
	private AddItemRunnable addItemRunnable;
	
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.ui.jobs.AbstractJob#prepare(edu.tsinghua.lumaqq.ui.MainShell)
     */
    public void prepare(MainShell main) {
        super.prepare(main);
        finished = false;
        addItemRunnable = new AddItemRunnable();
        main.getClient().addQQListener(this);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.ui.jobs.IJob#clear()
     */
    public void clear() {
        main.getClient().removeQQListener(this);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.ui.jobs.IJob#isSuccess()
     */
    public boolean isSuccess() {
        return errorMessage != null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
     */
    public void run(IProgressMonitor monitor) throws InvocationTargetException,
            InterruptedException {
        this.monitor = monitor;
        monitor.beginTask("", 100);
        monitor.subTask(LumaQQ.getString("job.get.friend.list"));
        
        main.getShutter().setBatchLayout(true);
        main.getClient().getFriendList();
        
		synchronized(this) {
		    while(!finished) {
                try {
                    this.wait(2000);
                } catch (InterruptedException e) {                    
                }	        
		    }
		}
		
		main.getShutter().setBatchLayout(false);
		main.getDisplay().syncExec(new Runnable() {
            public void run() {
        		main.getShutter().refreshView();
            }
		});
		monitor.done();
    }
	
	/**
	 * 唤醒任务线程,使其返回
	 */
	private void wake() {
		synchronized(this) {
		    finished = true;
		    this.notify();
		}
	}
    
	/**
	 * 处理得到好友列表成功事件,我们得到每一个好友,然后生成model对象,添加到总的model中
	 * 如果事件是get friend list end事件,还需要处理postponed message
	 * 
	 * @param e
	 * 		QQEvent
	 */
	private void processGetFriendListSuccess(QQEvent e) {	
	    monitor.worked(15);
	    
		GetFriendListReplyPacket packet = (GetFriendListReplyPacket)e.getSource();		
		Iterator iter = packet.friends.iterator();
		while(iter.hasNext()) {
			QQFriend friend = (QQFriend)iter.next();
			addItemRunnable.index = 0;
			addItemRunnable.model = ModelUtils.createFriendModel(friend);
			main.getDisplay().syncExec(addItemRunnable);
		}			
		
		// 如果是得到好友列表结束事件,返回
		if(packet.position == QQ.QQ_FRIEND_LIST_POSITION_END)
		    wake();
		else
		    main.getClient().getFriendList(packet.position);
	}

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.events.IQQListener#qqEvent(edu.tsinghua.lumaqq.qq.events.QQEvent)
     */
    public void qqEvent(QQEvent e) {
        switch(e.type) {
            case QQEvent.QQ_GET_FRIEND_LIST_SUCCESS:
                processGetFriendListSuccess(e);
			case QQEvent.QQ_OPERATION_TIMEOUT:
			    switch(e.operation) {
			        case QQ.QQ_CMD_GET_FRIEND_LIST:
						processGetFriendListTimeout(e);
			        	break;
			    }
				break;
        }
    }	

	/**
	 * 处理得到好友列表超时事件,我们简单得重发超时的包
	 * 
	 * @param e
	 * 		QQEvent
	 */
	private void processGetFriendListTimeout(QQEvent e) {
	    errorMessage = LumaQQ.getString("job.get.friend.list.error");
	    wake();
	}
}

⌨️ 快捷键说明

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