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

📄 detecthiddenshell.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;

import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import edu.tsinghua.lumaqq.IPSeeker;
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.events.QQEvent;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.qq.packets.out.SendIMPacket;

/**
 * <pre>
 * 探测好友是否隐身的窗口,如来神掌,万无一失
 * </pre>
 * 
 * @author 马若劼
 */
public class DetectHiddenShell extends ShellAdapter implements IQQListener {
    /**
     * <pre>
     * 如果1分钟之内还没响应则取消探测
     * </pre>
     * 
     * @author 马若劼
     */
    private class TimeoutTask extends TimerTask {
        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        public void run() {
            setHint(LumaQQ.getString("rlsz.label.timeout", new Object[] { f.getName() }), false);
        }
    }
    
	private IconHolder icons = IconHolder.getInstance();
	private Label lblHint, lblInternetIp, lblLocalIp;
	private Shell shell;
	private MainShell main;
	private Display display;
	private Timer timer;
	
	// 要探测的好友
	private FriendModel f;
	
	public DetectHiddenShell(MainShell main, FriendModel f) {		
		this.main = main;
		this.display = main.getDisplay();
	    // create shell
		shell = new Shell(display, SWT.SHELL_TRIM);
		shell.setText(LumaQQ.getString("rlsz.title", new Object[] { String.valueOf(f.getQQ()) }));
		shell.setSize(500, 140);
		// set title and image
		shell.setImage(icons.getImage(IconHolder.icoPalm));
		shell.addShellListener(this);
		
		this.f = f;
		timer = new Timer();
		// init child controls
		initLayout();
	}
	
	/**
	 * 打开对话框
	 */
	public void open() {
		// layout
		shell.layout();
		// set dialog to center of screen
		Rectangle dialogRect = shell.getBounds();
		Rectangle displayRect = display.getBounds();
		shell.setLocation((displayRect.width-dialogRect.width)/2, (displayRect.height-dialogRect.height)/2);
		// open
		shell.open();
		// 发招
		main.getClient().addQQListener(this);
		main.getClient().detectHidden(f.getQQ());
		// 设置超时
		timer.schedule(new TimeoutTask(), 60000);
	}

	/**
	 * @param dialog
	 */
	private void initLayout() {
	    GridLayout layout = new GridLayout();
	    layout.marginHeight = layout.marginWidth = 15;
	    layout.verticalSpacing = 5;
		shell.setLayout(layout);
		// 提示标签
		lblHint = new Label(shell, SWT.NONE);
		lblHint.setText(LumaQQ.getString("rlsz.label.detecting", new Object[] { f.getName() }));
		lblHint.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 外部ip
		lblInternetIp = new Label(shell, SWT.NONE);
		lblInternetIp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 内部IP
		lblLocalIp = new Label(shell, SWT.NONE);
		lblLocalIp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// 关闭按钮
		Button btnClose = new Button(shell, SWT.PUSH);
		btnClose.setText(LumaQQ.getString("rlsz.button.close"));
		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gd.widthHint = 100;
		gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END;
		btnClose.setLayoutData(gd);
		btnClose.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                close();
            }
		});
	}
	
	/**
	 * 设置提示消息
	 * @param hint
	 */
	public void setHint(final String hint, final boolean hidden) {
	    display.syncExec(new Runnable() {
            public void run() {
			    lblHint.setText(hint);
			    if(hidden) {
			        if(f.isOffline())
			            main.setFriendProperty(f, IQQNode.STATUS, IQQNode.VALUE_HIDDEN);
			    } else {
			        if(f.isHidden())
			            main.setFriendProperty(f, IQQNode.STATUS, IQQNode.VALUE_OFFLINE);
			    }
            }
	    });
	}
	
	/**
	 * 设置好友的ip信息
	 * 
	 * @param internetIp
	 * 		外部ip
	 * @param localIp
	 * 		真实ip
	 */
	public void setIp(final byte[] internetIp, final byte[] localIp, final int internetPort) {
	    if(internetIp != null && !Util.isIpZero(internetIp)) {
	        String country = IPSeeker.getInstance().getCountry(internetIp);
	        String area = IPSeeker.getInstance().getArea(internetIp);
	        if(area.endsWith("CZ88.NET"))
	            area = "";
	        final String location = country + area;
	        
	        display.syncExec(new Runnable() {
                public void run() {
			        lblInternetIp.setText(LumaQQ.getString("rlsz.label.internet.ip", new Object[] { Util.convertIpToString(internetIp), location}));
			        main.setFriendProperty(f, IQQNode.IP, internetIp);
			        main.setFriendProperty(f, IQQNode.PORT, String.valueOf(internetPort));
                }
	        });
	    }
	    
	    if(localIp != null && !Util.isIpZero(localIp)) {
	        display.syncExec(new Runnable() {
                public void run() {
			        lblInternetIp.setText(LumaQQ.getString("rlsz.label.local.ip", new Object[] { Util.convertIpToString(localIp)}));
                }
	        });
	    }
	}
	
	/**
	 * 关闭窗口
	 */
	public void close() {
		shell.close();
	}
	
	/* (non-Javadoc)
     * @see org.eclipse.swt.events.ShellAdapter#shellClosed(org.eclipse.swt.events.ShellEvent)
     */
    public void shellClosed(ShellEvent arg0) {
        timer.cancel();
        main.getClient().removeQQListener(this);
    }

	/**
	 * @return true 如果对话框已经被释放
	 */
	public boolean isDisposed() {
		return (shell == null || shell.isDisposed());
	}

    /* (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_ACCEPT_SEND_FILE:
                processAcceptSendFile(e);
            	break;
            case QQEvent.QQ_OPERATION_TIMEOUT:
                if(e.operation == QQ.QQ_CMD_SEND_IM)
                    processSendIMTimeout(e);
                break;
        }
    }

    /**
     * @param e
     */
    private void processAcceptSendFile(QQEvent e) {
        ReceiveIMPacket packet = (ReceiveIMPacket)e.getSource();
        if(packet.fileArgs.transferType == QQ.QQ_TRANSFER_FACE && packet.normalHeader.sender == f.getQQ()) {            
	        timer.cancel();
	        setHint(LumaQQ.getString("rlsz.label.hitted", new Object[] { f.getName() }), true);
	        setIp(packet.fileArgs.internetIp, packet.fileArgs.localIp, packet.fileArgs.internetPort);
        }
    }

    /**
     * @param e
     */
    private void processSendIMTimeout(QQEvent e) {
        SendIMPacket packet = (SendIMPacket)e.getSource();
        if(packet.getTransferType() == QQ.QQ_TRANSFER_FACE && packet.getReceiver() == f.getQQ()) {
            timer.cancel();
            setHint(LumaQQ.getString("rlsz.label.timeout", new Object[] { f.getName() }), false);
        }
    }
}

⌨️ 快捷键说明

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