📄 refreshfriendjob.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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
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.models.GroupModel;
import edu.tsinghua.lumaqq.models.IQQNode;
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;
import edu.tsinghua.swt.models.ShutterModel;
/**
* 刷新好友列表任务
*
* @author luma
*/
public class RefreshFriendJob extends AbstractJob implements IQQListener {
/**
* @author luma
*/
private class AddFriendModelRunnable implements Runnable {
public FriendModel f;
public void run() {
main.getMVCHelper().addFriendModel(f);
}
}
/**
* @author luma
*/
private class RemoveFriendModelRunnable implements Runnable {
public FriendModel f;
public void run() {
main.getMVCHelper().removeFriend(f);
}
}
private boolean finished;
private IProgressMonitor monitor;
private Map localFriends;
private List remoteFriends;
/* (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;
localFriends = new HashMap();
remoteFriends = new ArrayList();
hashLocalFriends();
main.getClient().addQQListener(this);
}
/**
* 保存本地好友列表到哈希表中
*/
private void hashLocalFriends() {
ShutterModel model = main.getModel();
int tabCount = model.getTabCount();
for(int i = 0; i < tabCount; i++) {
GroupModel g = (GroupModel)model.getTab(i);
if(g.isCluster() || g.getName().equals(LumaQQ.getString("group.default.latest")))
continue;
int itemCount = model.getItemCount(i);
for(int j = 0; j < itemCount; j++) {
FriendModel f = (FriendModel)model.getItem(i, j);
localFriends.put(f.getProperty(IQQNode.QQ_NUMBER), f);
}
}
}
/* (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.refresh.friend.1"));
main.getClient().getFriendList();
synchronized(this) {
while(!finished) {
try {
this.wait(2000);
} catch (InterruptedException e) {
}
}
}
monitor.done();
}
/* (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
*/
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();
Integer qq = new Integer(friend.qqNum);
if(localFriends.remove(qq) == null)
remoteFriends.add(ModelUtils.createFriendModel(friend));
}
// 如果是得到好友列表结束事件,开始刷新
if(packet.position == QQ.QQ_FRIEND_LIST_POSITION_END)
refresh();
else
main.getClient().getFriendList(packet.position);
}
/**
* 刷新好友列表
*/
private void refresh() {
monitor.setCanceled(false);
monitor.subTask(LumaQQ.getString("job.refresh.friend.2"));
RemoveFriendModelRunnable rfmr = new RemoveFriendModelRunnable();
Iterator i = localFriends.values().iterator();
while(i.hasNext()) {
rfmr.f = (FriendModel)i.next();
main.getDisplay().syncExec(rfmr);
}
AddFriendModelRunnable afmr = new AddFriendModelRunnable();
i = remoteFriends.iterator();
while(i.hasNext()) {
afmr.f = (FriendModel)i.next();
main.getDisplay().syncExec(afmr);
}
wake();
}
/**
* @param e
*/
private void processGetFriendListTimeout(QQEvent e) {
errorMessage = LumaQQ.getString("job.refresh.friend.error");
wake();
}
/**
* 唤醒任务线程,使其返回
*/
private void wake() {
synchronized(this) {
finished = true;
this.notify();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -