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

📄 uploadgroupjob.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@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.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;

import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.GroupModel;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.packets.out.UploadGroupFriendPacket;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.swt.models.ShutterModel;

/**
 * 上传分组任务
 * 
 * @author luma
 */
public class UploadGroupJob extends AbstractJob implements IQQListener {
	private boolean uploadNameFinished, uploadFriendFinished;
	private IProgressMonitor monitor;

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.ui.jobs.IJob#prepare(edu.tsinghua.lumaqq.ui.MainShell)
     */
    public void prepare(MainShell main) {
        super.prepare(main);
        uploadNameFinished = false;
        uploadFriendFinished = false;
        main.getClient().addQQListener(this);
    }

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

    /* (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.upload.group.1"));
        
        ShutterModel model = main.getModel();
        QQClient client = main.getClient();
		// 组装上传分组名称包,注意i从1开始,因为我的好友组是缺省的,不需要上传
		// 同时组装上传分组好友列表包
		List groups = new ArrayList();
		UploadGroupFriendPacket ugfPacket = new UploadGroupFriendPacket(main.getClient().getUser());
		int tabSize = model.getTabCount();
		for(int i = 0, g = 0; i < tabSize; i++) {
			GroupModel gModel = (GroupModel)model.getTab(i);
			if(gModel.needUpload()) {
				// 等于0表示是“我的好友”,这个是不需要上传名字的
				if(i != 0)
					groups.add(model.getTabName(i));
				
				// 添加组中好友
				int itemSize = model.getItemCount(i);
				for(int j = 0; j < itemSize; j++) {
					FriendModel fModel = (FriendModel)model.getItem(i, j);
					int qqNum = fModel.getQQ();
					ugfPacket.addFriend(g, qqNum);
				}
				g++;
			}
		}
		
		monitor.worked(10);
		
		// 发送
		client.uploadGroup(groups);
		client.sendPacket(ugfPacket);
		
		monitor.worked(20);
		
		synchronized(this) {
		    while(!uploadFriendFinished || !uploadNameFinished) {
                try {
                    this.wait(2000);
                } catch (InterruptedException e) {                    
                }	        
		    }
		}
		
		main.setGroupDirty(false);
		
		monitor.done();
    }
    
	/**
	 * 唤醒任务线程,使其返回
	 */
	private void wake() {
		synchronized(this) {
		    this.notify();
		}
	}

	/**
	 * 处理上传分组好友列表失败事件
	 * 
	 * @param e
	 * 			QQEvent
	 */
	private void processUploadFail(QQEvent e) {
	    uploadFriendFinished = uploadNameFinished = true;
	    errorMessage = LumaQQ.getString("job.upload.group.error");
	    wake();
	}

	/**
	 * 处理上传分组好友列表成功事件
	 * 
	 * @param e
	 * 		QQEvent
	 */
	private void processUploadGroupFriendSuccess(QQEvent e) {
		monitor.worked(20);
	    uploadFriendFinished = true;
	    wake();
	}

    /**
     * @param e
     */
    private void processUploadGroupNameSuccess(QQEvent e) {
		monitor.worked(20);
        uploadNameFinished = true;
        wake();
    }
    
    /* (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_UPLOAD_GROUP_FRIEND_FAIL:
				processUploadFail(e);
				break;
			case QQEvent.QQ_UPLOAD_GROUP_FRIEND_SUCCESS:
				processUploadGroupFriendSuccess(e);
				break;
			case QQEvent.QQ_UPLOAD_GROUP_NAME_SUCCESS:
			    processUploadGroupNameSuccess(e);
				break;
			case QQEvent.QQ_OPERATION_TIMEOUT:
			    switch(e.operation) {
			        case QQ.QQ_CMD_UPLOAD_GROUP_FRIEND:
			        case QQ.QQ_CMD_GROUP_DATA_OP:
						processUploadFail(e);
			        	break;
			    }
				break;
        }
    }

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

⌨️ 快捷键说明

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