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

📄 lumaqq.java

📁 用java实现的
💻 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;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;

import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.shells.*;
import edu.tsinghua.lumaqq.xml.servers.Servers;
import edu.tsinghua.lumaqq.xml.servers.ServersUnmarshaller;


/**
 * LumaQQ的GUI主程序,主要负责构造界面
 *
 * @author 马若劼
 */
public class LumaQQ {
    // 国际化资源
	public static ResourceBundle resourceBundle;
	// 安装目录
	public static String INSTALL_DIR;
	// 一些系统需要使用的文件路径
	public static String LOGIN_HISTORY;
	public static String SERVERS;
	public static String IP_FILE;
	// 用户文件路径
	public static String GROUPS;
	public static String REPLIES;
	public static String PROXIES;
	public static String SYSTEM_OPTIONS;
	public static String REMARKS; 
	// 声音文件路径
	public static String MSG_SOUND;
	public static String SYS_MSG_SOUND;
	
	// 登陆服务器数组,需要从xml文件中读取
    // UDP服务器的列表
    public static String[] udpServers;
    // TCP服务器列表
    public static String[] tcpServers;

	/**
	 * LumaQQ启动主程序
	 * @param args
	 */
	public static void main(String[] args) {
		// 初始化系统文件路径
		initSysFilePath(args);
		// 初始化资源文件
		resourceBundle = ResourceBundle.getBundle("locale.LumaQQ", Locale.CHINESE, ClassLoader.getSystemClassLoader());
		MainShell main = new MainShell();
		// 读取服务器列表文件,如果不成功则退出,服务器数量为0也退出
		if(!readServerFile()) {
			MessageBox box = new MessageBox(main.shell, SWT.ICON_ERROR | SWT.OK);
			box.setText(LumaQQ.getResourceString("message.box.common.fail.title"));
			box.setMessage(LumaQQ.getResourceString("message.box.server.file.error"));
			box.open();
			main.close();
			System.exit(1);
		}
		// 服务器列表读取成功,开始登陆
		LoginDialog login = new LoginDialog(main.getShell());
		if(login.open()) {
			// 创建QQ用户对象
			QQUser me = new QQUser(login.getQqNum(), login.getMd5pwd());
			me.setUdp(false);
			if(login.isLoginHidden()) {
				me.setLoginMode(QQ.QQ_LOGIN_MODE_HIDDEN);
				me.setStatus(QQ.QQ_FRIEND_STATUS_HIDDEN);
			} else {
				me.setLoginMode(QQ.QQ_LOGIN_MODE_NORMAL);
				me.setStatus(QQ.QQ_FRIEND_STATUS_ONLINE);
			}
			// 创建QQ客户端对象
			QQClient client = new QQClient();
			client.setUser(me);
			// 初始化用户文件路径
			initUserFilePath(me);
			// 传递给主界面
			main.setClient(client);
			// 打开主界面
		    main.open();
		} else
			main.close();
	} 

	/**
	 * 初始化用户文件路径,对于每个用户来说,这些值是变化的。在改变用户的时候将被调用
	 * @param me
	 */
	public static void initUserFilePath(QQUser me) {
		GROUPS = INSTALL_DIR + "/" + me.getQqNum() + "/groups.xml";
		REPLIES = INSTALL_DIR + "/" + me.getQqNum() + "/replies.xml";
		PROXIES = INSTALL_DIR + "/" + me.getQqNum() + "/proxies.xml";
		SYSTEM_OPTIONS = INSTALL_DIR + "/" + me.getQqNum() + "/sysopts.xml";
		REMARKS = INSTALL_DIR + "/" + me.getQqNum() + "/remarks.xml";
	}

	/**
	 * 初始化系统文件的路径,对于每个用户来说,这些值是不变的,所以这个方法只调用一次
	 * @param args
	 */
	private static void initSysFilePath(String[] args) {
		if(args.length == 0)
			INSTALL_DIR = ".";
		else
			INSTALL_DIR = args[0];
		LOGIN_HISTORY = INSTALL_DIR + "/xml/logins.xml";
		SERVERS = INSTALL_DIR + "/xml/servers.xml";
		IP_FILE = INSTALL_DIR + "/QQWry.dat";
		MSG_SOUND = INSTALL_DIR + "/sound/msg.au";
		SYS_MSG_SOUND = INSTALL_DIR + "/sound/system.au";
	}

	/**
	 * 读取服务器列表文件
	 * @return true表示服务器列表文件没有问题,false表示有问题,程序不会继续执行下去
	 */
	private static boolean readServerFile() {
		File file = new File(LumaQQ.SERVERS);
		if(file.exists()) {
			Servers servers = null;
			try {
				servers = ServersUnmarshaller.unmarshal(file);
			} catch (IOException e) {
				return false;
			}		
			// 得到UDP服务器列表
			int i = 0;
			List list = servers.getUDPServerList();
			if(list != null && list.size() > 0) {
				udpServers = new String[list.size()];
				Iterator iter = list.iterator();
				while(iter.hasNext()) {
					udpServers[i++] = ((String)iter.next()).trim();
				}				
			} else
				udpServers = new String[0];
			// 得到TCP服务器列表
			i = 0;
			list = servers.getTCPServerList();
			if(list != null && list.size() > 0) {
				tcpServers = new String[list.size()];
				Iterator iter = list.iterator();
				while(iter.hasNext()) {
					tcpServers[i++] = ((String)iter.next()).trim();
				}				
			} else
				tcpServers = new String[0];
			// 判断服务器数量是否大于1
			if(tcpServers.length + udpServers.length < 1)
				return false;
			else
				return true;
		} else
			return false;
	}

	/**
     * 从资源文件中返回字符串 我们不希望程序崩溃,所以如果没有找到Key,就直接返回Key
     */
    public static String getResourceString(String key) {
        try {
            return resourceBundle.getString(key);
        } catch (MissingResourceException e) {
            return key;
        } catch (NullPointerException e) {
            return "!" + key + "!";
        }
    }

    /**
     * 从资源文件中返回字符串 我们不希望程序崩溃,所以如果没有找到Key,就直接返回Key
     */
    public static String getResourceString(String key, Object[] args) {
        try {
            return MessageFormat.format(getResourceString(key), args);
        } catch (MissingResourceException e) {
            return key;
        } catch (NullPointerException e) {
            return "!" + key + "!";
        }
    }
}

⌨️ 快捷键说明

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