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

📄 userinfowindow.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.config.user;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

import starlight.util.Base64;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.IQQNode;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.ContactInfo;
import edu.tsinghua.lumaqq.qq.beans.FriendRemark;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.packets.in.FriendDataOpReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.out.FriendDataOpPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ModifyInfoPacket;
import edu.tsinghua.lumaqq.ui.MainShell;
import edu.tsinghua.lumaqq.ui.config.AbstractConfigurationWindow;
import edu.tsinghua.lumaqq.ui.config.AbstractPage;
import edu.tsinghua.lumaqq.ui.config.IPacketFiller;
import edu.tsinghua.lumaqq.ui.helper.ConfigHelper;
import edu.tsinghua.lumaqq.xml.logins.Login;
import edu.tsinghua.lumaqq.xml.logins.Logins;
import edu.tsinghua.lumaqq.xml.remarks.Remark;
import edu.tsinghua.lumaqq.xml.remarks.RemarkImpl;
import edu.tsinghua.swt.models.INode;

/**
 * 用户信息窗口
 * 
 * @author luma
 */
public class UserInfoWindow extends AbstractConfigurationWindow implements IQQListener {
    private MainShell main;
    private FriendModel model;
    private ModifyInfoPacket mip;
    
    // style
    /** 用户资料可以编辑,对应于修改个人设置窗口 */
    public static final int EDITABLE = 1;
    /** 用户资料只读,对应于查看好友资料窗口 */
    public static final int READ_ONLY = 2;
    
    // 页ID
    private static final int PERSONAL_INFO = 0;
    private static final int QQ_SHOW = 1;
    private static final int CONTACT = 2;
    private static final int SECURITY = 3;
    private static final int REMARK = 3;
    
    /**
     * @param parent
     */
    public UserInfoWindow(MainShell main, FriendModel model, int style) {
        super(main.getShell(), style);
        this.model = model;
        this.main = main;
        setOKButtonText(LumaQQ.getString("common.button.modify"));
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#initialPages()
     */
    protected void initialPages() {
        addPage(new PersonalPage(getPageContainer(), model, style));        
        addPage(new QQShowPage(getPageContainer(), main, model, style));
        addPage(new ContactPage(getPageContainer(), model, style));      
        if(style == EDITABLE)
            addPage(new SecurityPage(getPageContainer(), model));
        if(style == READ_ONLY)
            addPage(new RemarkPage(getPageContainer(), main, model));
        
        main.getClient().addQQListener(this);
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#onOK()
     */
    protected void onOK() {
        if(style == EDITABLE) {
	        setOKButtonEnabled(false);
	        
	        // 检查旧密码是否输入正确
	        if(!((SecurityPage)getPage(SECURITY)).checkOldPassword()) {
	            MessageDialog.openError(parentShell, LumaQQ.getString("message.box.change.password.title"), LumaQQ.getString("error.old.password.wrong"));
	            setOKButtonEnabled(true);
	            return;
	        }
	        
	        // 检查新密码是否输入正确
	        if(!((SecurityPage)getPage(SECURITY)).checkNewPassword()) {
	            MessageDialog.openError(parentShell, LumaQQ.getString("message.box.change.password.title"), LumaQQ.getString("error.two.password.differ"));
	            setOKButtonEnabled(true);
	            return;
	        }
	        
	        // 组装包
	        mip = new ModifyInfoPacket(main.getClient().getUser());
	        mip.setContactInfo(new ContactInfo());
	        Iterator i = getPageIterator();
	        while(i.hasNext()) {
	            AbstractPage page = (AbstractPage)i.next();
	            if(page instanceof IPacketFiller)
	                ((IPacketFiller)page).fill(mip);
	        }
	        
	        // 发送包
	        main.getClient().sendPacket(mip);            
        } else {
            if(getCurrentPageId() == REMARK) {
                RemarkPage page = (RemarkPage)getPage(REMARK);
                page.doSave();
            } else {
                main.getClient().getUserInfo(model.getQQ());
            }
        }
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#pageChanged()
     */
    protected void pageChanged() {
        if(style == READ_ONLY) {
	        if(getCurrentPageId() == REMARK)
	            setOKButtonText(LumaQQ.getString("common.button.modify"));
	        else
	            setOKButtonText(LumaQQ.getString("common.button.update"));            
        }
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#onShellClose()
     */
    protected void onShellClose() {
        main.getShellRegistry().removeUserInfoWindow(model);
        main.getClient().removeQQListener(this);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#getTitle()
     */
    protected String getTitle() {
        return (style == EDITABLE) ? LumaQQ.getString("user.info.title.modify") : LumaQQ.getString("user.info.title.view");
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#getImage()
     */
    protected Image getImage() {
        return IconHolder.getInstance().getImage(IconHolder.icoPersonInfo);
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractConfigurationWindow#getInitialSize()
     */
    protected Point getInitialSize() {
        return new Point(600, 500);
    }
    
    /**
     * 设置好友model
     * 
     * @param model
     * 		FriendModel对象
     */
    public void setFriendModel(FriendModel model) {
        this.model = model;
        refreshPageModels(model);
        refreshPageValues();
    }

    /**
     * 设置QQ秀
     * 
     * @param showImage
     */
    public void setQQShow(Image showImage) {
        ((QQShowPage)getPage(QQ_SHOW)).setQQShow(showImage);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.qq.events.QQListener#qqEvent(edu.tsinghua.lumaqq.qq.events.QQEvent)
     */
    public void qqEvent(QQEvent e) {
        switch(e.type) {
			case QQEvent.QQ_UPLOAD_FRIEND_REMARK_SUCCESS:
				processUploadFriendRemarkSuccess(e);
				break;
			case QQEvent.QQ_MODIFY_INFO_SUCCESS:
			    processModifyInfoSuccess();
				break;
			case QQEvent.QQ_DOWNLOAD_FRIEND_REMARK_SUCCESS:
				processDownloadFriendRemarkSuccess(e);
				break;
			case QQEvent.QQ_OPERATION_TIMEOUT:
				if(e.operation == QQ.QQ_CMD_FRIEND_DATA_OP)
					processFriendRemarkOpTimeout(e);
				break;
        }
    }    
    
	/**
	 * 处理上传好友备注信息成功事件
	 * 
	 * @param e
	 */
	private void processUploadFriendRemarkSuccess(QQEvent e) {
		FriendDataOpPacket packet = (FriendDataOpPacket)e.getSource();
		if(packet.getQQ() == model.getQQ()) {
			main.getDisplay().syncExec(
				new Runnable() {
					public void run() {
		                RemarkPage page = (RemarkPage)getPage(REMARK);
		                page.setUploadRemarkEnable(true);
					    MessageDialog.openInformation(shell, LumaQQ.getString("message.box.common.success.title"), LumaQQ.getString("message.box.upload.remark.success"));
					}				
				}
			);			
		}
	}
    
	/**
	 * 处理备注操作超时事件
	 * 
	 * @param e
	 */
	private void processFriendRemarkOpTimeout(QQEvent e) {
		final FriendDataOpPacket packet = (FriendDataOpPacket)e.getSource();
		if(packet.getQQ() == model.getQQ()) {
			main.getDisplay().asyncExec(
				new Runnable() {
					public void run() {
						if(packet.getSubCommand() == QQ.QQ_DOWNLOAD_FRIEND_REMARK)
						    ((RemarkPage)getPage(REMARK)).setDownloadRemarkEnable(true);
						MessageDialog.openError(shell, LumaQQ.getString("message.box.common.fail.title"), LumaQQ.getString("message.box.common.timeout"));
					}				
				}
			);	
		}
	}

	/**
	 * 处理更新用户信息成功事件,更新信息肯定是自己,因为只有自己能更新自己的
	 */
	private void processModifyInfoSuccess() {
	    if(mip == null || style == READ_ONLY)
	        return;
	    
		main.getDisplay().syncExec(
			new Runnable() {
				public void run() {				
				    // 刷新自己的信息
				    ContactInfo info = mip.getContactInfo();
				    main.getMyModel().addProperty(IQQNode.CONTACT, info);
				    main.getMyModel().addProperty(IQQNode.HEAD, new Integer(getFaceId()));
				    main.getMyModel().addProperty(INode.NAME, info.infos[info.nick]);
				    main.getMyModel().addProperty(IQQNode.NICK, info.infos[info.nick]);

				    // 显示修改成功对话框
				    MessageDialog.openInformation(parentShell, LumaQQ.getString("message.box.success.modify.info.title"), LumaQQ.getString("success.modify.info"));
					
					// 保存新密码,关闭资料窗口
					saveNewPassword();
					close();
				}				
			}
		);
	}
	
	/**
	 * 处理下载好友备注信息成功事件
	 * 
	 * @param e
	 */
	private void processDownloadFriendRemarkSuccess(QQEvent e) {
	    if(style == EDITABLE)
	        return;
	    
		FriendDataOpReplyPacket packet = (FriendDataOpReplyPacket)e.getSource();
		if(packet.hasRemark) {
			if(packet.qqNum == model.getQQ()) {
				// 把下载到的信息保存到xml
				final Remark remark = createRemarkElement(packet.remark);
				((RemarkPage)getPage(REMARK)).saveToXML(remark);
				// 设置下载按钮为enable
				main.getDisplay().syncExec(
					new Runnable() {
						public void run() {
						    ((RemarkPage)getPage(REMARK)).setDownloadRemarkEnable(true);
							// 设置各文本框内容
						    ((RemarkPage)getPage(REMARK)).setRemarkInfo(remark);
						}
					}
				);			
			}		    
		} else {
		    // 好友没有备注,把按钮使能
		    main.getDisplay().syncExec(new Runnable() {
                public void run() {
					((RemarkPage)getPage(REMARK)).setDownloadRemarkEnable(true);
                }
		    });
		}
	}
	
	/**
	 * 从FriendRemark中创建Remark对象
	 * @param fr
	 * @return
	 */
	private Remark createRemarkElement(FriendRemark fr) {
		Remark remark = new RemarkImpl();
		remark.setName(fr.name.trim());
		remark.setZipcode(fr.zipcode);
		remark.setTelephone(fr.telephone);
		remark.setMobile(fr.mobile);
		remark.setEmail(fr.email);
		remark.setAddress(fr.address);
		remark.setNote(fr.note);
		remark.setQqNum(String.valueOf(model.getQQ()));
		return remark;
	}
	
	/**
	 * @return
	 * 		头像ID
	 */
	private int getFaceId() {
	    return ((PersonalPage)getPage(PERSONAL_INFO)).getHeadId();
	}
	
	/**
	 * 如果用户修改了密码,且用户选择了记住密码,则把新密码保存到logins.xml中
	 */
	public void saveNewPassword() {
	    if(mip.getOldPassword() == null)
	        return;
	    
        // 得到登陆历史信息文件对象,如果不存在,创建一个
        File loginHistory = new File(LumaQQ.LOGIN_HISTORY);
        if(!ConfigHelper.checkLoginHistoryFile(loginHistory)) return;
        // 读入登陆信息文件
    	Logins logins = ConfigHelper.loadLoginHistory(loginHistory);
    	if(logins == null)
    	    return;
    	// 得到自己的Login对象
    	Login login = ConfigHelper.findLogin(logins, String.valueOf(main.getMyModel().getQQ()));
    	if(login == null)
    	    return;
    	// 检查是否设置了记住密码
    	if(!"true".equalsIgnoreCase(login.getRememberPassword()))
    	    return;
    	
    	// 保存新密码
    	login.setPassword(new String(Base64.encode(Util.doMD5(Util.doMD5(mip.getNewPassword().getBytes())))));
        // 写入文件
        try {
            logins.marshal(loginHistory);
        } catch (IOException e) {
            log.error("新密码写入失败");
        }
	}
}

⌨️ 快捷键说明

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