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

📄 securitypage.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 java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;

import starlight.util.Base64;
import edu.tsinghua.lumaqq.Colors;
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.packets.BasicOutPacket;
import edu.tsinghua.lumaqq.qq.packets.out.ModifyInfoPacket;
import edu.tsinghua.lumaqq.ui.config.AbstractPage;
import edu.tsinghua.lumaqq.ui.config.IPacketFiller;
import edu.tsinghua.lumaqq.ui.tool.CenterBorderPaintListener;
import edu.tsinghua.lumaqq.ui.tool.UITool;
import edu.tsinghua.lumaqq.xml.logins.Login;
import edu.tsinghua.lumaqq.xml.logins.Logins;
import edu.tsinghua.lumaqq.xml.logins.LoginsUnmarshaller;

/**
 * 网络安全配置页
 * 
 * @author luma
 */
public class SecurityPage extends AbstractPage implements IPacketFiller {    
    private FriendModel model;
    private PaintListener paintListener;
    
	private Button radioAnyOne, radioAuth, radioNever, chkChangePassword;
	private Text textOldPassword, textNewPassword, textConfirmPassword;
    
    /**
     * @param parent
     */
    public SecurityPage(Composite parent, FriendModel model) {
        super(parent);
        this.model = model;
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractPage#setModel(java.lang.Object)
     */
    public void setModel(Object model) {
        if(model instanceof FriendModel)
            this.model = (FriendModel)model;
    }
    
    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractPage#initialVariable()
     */
    protected void initialVariable() {
        paintListener = new CenterBorderPaintListener(new Class[] { Text.class }, 20, Colors.PAGE_CONTROL_BORDER);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.ui.config.AbstractPage#createContent(org.eclipse.swt.widgets.Composite)
     */
    protected Control createContent(Composite parent) {
        Composite content = new Composite(parent, SWT.NONE);
        content.setBackground(Colors.PAGE_BACKGROUND);
        content.setLayout(new FormLayout());
        
        Composite container = new Composite(content, SWT.NONE);
        FormData fd = new FormData();
        fd.top = fd.left = new FormAttachment(0, 0);
        fd.bottom = new FormAttachment(100, -30);
        fd.right = new FormAttachment(100, -100);
        container.setLayoutData(fd);
        GridLayout layout = new GridLayout(3, false);
        layout.marginHeight = layout.horizontalSpacing = 8;
        layout.verticalSpacing = 14;
        layout.marginWidth = 15;
        container.setLayout(layout);
        container.setBackground(Colors.PAGE_BACKGROUND);
        container.addPaintListener(paintListener);
        
        // 设置使用缺省背景色
        UITool.setDefaultBackground(null);
        
        // 修改密码
        GridData gd = new GridData();
        gd.horizontalSpan = 3;
        chkChangePassword = UITool.createCheckbox(container, LumaQQ.getString("user.info.security.changepassword"), gd);
        chkChangePassword.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                refreshControlStatus();                
            }
        });
        // 旧密码
        gd = new GridData();
        gd.horizontalIndent = 20;
        UITool.createLabel(container, LumaQQ.getString("user.info.security.oldpassword"), gd);
        textOldPassword = UITool.createSingleText(container, new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER), SWT.PASSWORD | SWT.SINGLE);
        textOldPassword.setTextLimit(16);
        UITool.createLabel(container, LumaQQ.getString("user.info.security.changepassword.hint"));
        // 新密码
        gd = new GridData();
        gd.horizontalIndent = 20;
        UITool.createLabel(container, LumaQQ.getString("user.info.security.newpassword"), gd);
        textNewPassword = UITool.createSingleText(container, new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER), SWT.PASSWORD | SWT.SINGLE);
        textNewPassword.setTextLimit(16);
        UITool.createLabel(container, LumaQQ.getString("user.info.security.changepassword.hint"));
        // 新密码确认
        gd = new GridData();
        gd.horizontalIndent = 20;
        UITool.createLabel(container, LumaQQ.getString("user.info.security.confirmpassword"), gd);
        textConfirmPassword = UITool.createSingleText(container, new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER), SWT.PASSWORD | SWT.SINGLE);
        textConfirmPassword.setTextLimit(16);
        UITool.createLabel(container, "");
        
        // 身份验证组
        layout = new GridLayout();
        layout.marginHeight = layout.marginWidth = 8;
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.horizontalSpan = 3;
        Group authGroup = UITool.createGroup(container, LumaQQ.getString("user.info.group.auth"), gd, layout);
        
        // 允许任何人加
        radioAnyOne = UITool.createRadio(authGroup, LumaQQ.getString("user.info.security.openhp.anyone"));
        radioAuth = UITool.createRadio(authGroup, LumaQQ.getString("user.info.security.openhp.auth"));
        radioNever = UITool.createRadio(authGroup, LumaQQ.getString("user.info.security.openhp.never"));
        
        return content;
    }
    
    /**
     * 刷新密码框状态
     */
    private void refreshControlStatus() {
        boolean enable = chkChangePassword.getSelection();
        textOldPassword.setEnabled(enable);
        textNewPassword.setEnabled(enable);
        textConfirmPassword.setEnabled(enable);
        if(enable) {
            textOldPassword.setBackground(Colors.PAGE_BACKGROUND);
            textNewPassword.setBackground(Colors.PAGE_BACKGROUND);
            textConfirmPassword.setBackground(Colors.PAGE_BACKGROUND);
        } else {
            textOldPassword.setBackground(Colors.PAGE_READONLY_CONTROL_BACKGROUND);
            textNewPassword.setBackground(Colors.PAGE_READONLY_CONTROL_BACKGROUND);
            textConfirmPassword.setBackground(Colors.PAGE_READONLY_CONTROL_BACKGROUND);            
        }
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractPage#saveDirtyProperty(int)
     */
    protected void saveDirtyProperty(int propertyId) {
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractPage#initializeValues()
     */
    protected void initializeValues() {
		ContactInfo info = (ContactInfo)model.getProperty(IQQNode.CONTACT);
		if(info == null)
		    return;
		// 好友认证设置
		int openHp = Util.getInt(info.infos[info.authType], 0);
		if(openHp == QQ.QQ_AUTH_NO_AUTH) {
			radioAnyOne.setSelection(true);
			radioAuth.setSelection(false);
			radioNever.setSelection(false);			
		} else if(openHp == QQ.QQ_AUTH_NEED_AUTH) {
			radioAnyOne.setSelection(false);			
			radioAuth.setSelection(true);
			radioNever.setSelection(false);			
		} else {
			radioAnyOne.setSelection(false);			
			radioAuth.setSelection(false);			
			radioNever.setSelection(true);
		}
		chkChangePassword.setSelection(false);
		refreshControlStatus();
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractPage#getImage()
     */
    protected Image getImage() {
        return IconHolder.getInstance().getImage(IconHolder.icoModifyPersonInfo24);
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.AbstractPage#getTitle()
     */
    protected String getTitle(int page) {
        return LumaQQ.getString("user.info.page.security");
    }

    /* (non-Javadoc)
     * @see edu.tsinghua.lumaqq.shells.IPacketFiller#fill(edu.tsinghua.lumaqq.qq.packets.OutPacket)
     */
    public void fill(BasicOutPacket packet) {
        if(packet instanceof ModifyInfoPacket) {
            ModifyInfoPacket mip = (ModifyInfoPacket)packet;
            ContactInfo info = mip.getContactInfo();
            if(chkChangePassword.getSelection()) {
                mip.setOldPassword(textOldPassword.getText());
                mip.setNewPassword(textNewPassword.getText());
            } else {
                mip.setOldPassword(null);
                mip.setNewPassword(null);
            }
    		if(radioAnyOne.getSelection())
    			info.infos[info.authType] = "0";
    		else if(radioAuth.getSelection())
    			info.infos[info.authType] = "1";
    		else
    			info.infos[info.authType] = "2";
        }
    }

	/**
	 * 检查新密码
	 * 
	 * @return
	 * 		新密码输入无误返回true,否则返回false
	 */
	protected boolean checkNewPassword() {
		if(chkChangePassword.getSelection())
			return textNewPassword.getText().equals(textConfirmPassword.getText());
		else
			return true;
	}
	
	/**
	 * 检查旧密码
	 * 
	 * @return
	 * 		输入正确返回true,错误返回false
	 */
	protected boolean checkOldPassword() {
		// 加密消息
		if(chkChangePassword.getSelection()) {
			Logins logins = null;
			try {
				logins = LoginsUnmarshaller.unmarshal(new File(LumaQQ.LOGIN_HISTORY));
			} catch (IOException e) {
				log.error("无法读取登陆历史信息文件");
				return false;
			}
			List list = logins.getLoginList();
			Iterator iter = list.iterator();
			while(iter.hasNext()) {
				Login login = (Login)iter.next();
				if(login.getQq().equals(((Integer)model.getProperty(IQQNode.QQ_NUMBER)).toString())) {
					// 哈,这里就是要人看着晕~
					String s = "";
					if(login.getRememberPassword().equalsIgnoreCase("true")) 
						s = new String(Base64.encode(Util.doMD5(Util.doMD5(textOldPassword.getText().getBytes()))));
					else
						s = new String(Base64.encode(Util.doMD5(new String(Base64.encode(Util.doMD5(Util.doMD5(textOldPassword.getText().getBytes())))).getBytes())));
					if(login.getPassword().equals(s))
						return true;
					else
						return false;
				}
			}
			return false;
		} else
			return true;
	}
}

⌨️ 快捷键说明

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