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

📄 mainshell.java

📁 lumaQQ的源文件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		if(display == null)
			initialize();
		// 后初始化
		configHelper.postInitSystemOptions();
		// 设置窗口标题
		setTitle(String.valueOf(myModel.qq));
		// 初始化自动停靠
		initAutoDock();
		// 初始化备注信息
		configHelper.initRemarks();
		// 初始化快捷回复
		configHelper.initReplies();
		// 初始化代理列表
		configHelper.initProxies();
		// 初始化自定义表情
		configHelper.initFaces();
		// 初始化model
        blindHelper.initModels();
        blindHelper.setTreeMode(optionHelper.isTreeMode());
		// 初始化聊天记录管理器
		initMessageManager(myModel.qq);
		// 初始化QQ秀管理器
		initQQShowManager(myModel.qq);
		
		// 设置声音精灵线程的使能性
		sounder.setEnable(optionHelper.isEnableSound());
		// 设置其他Helper参数
		menuHelper.setClient(client);
	}	

	/**
	 * 设置标题条
	 * 
	 * @param string
	 */
	private void setTitle(String string) {
		shell.setText(string);
		BorderStyler styler = (BorderStyler)shell.getData(BorderStyler.STYLER);
		styler.repaintTitleBar();
	}

	/**
	 * 为shell加上自动停靠
	 */
	private void initAutoDock() {
		if(shell.getData(AutoDockManager.DOCK_MANAGER) == null) {
			BorderStyler styler = (BorderStyler)shell.getData(BorderStyler.STYLER);
			new AutoDockManager(this).addDockSupport(shell, styler);
		}
	}

	/**
	 * 初始化聊天记录管理器
	 */
	private void initMessageManager(int qqNum) {
		rm = new RecordManager(LumaQQ.RECORD_DIRECTORY);
	}
	
    /**
	 * 初始化QQ秀管理器
	 * @param qq
	 */
	private void initQQShowManager(int qqNum) {
		if(sm != null)
			sm.removeQQShowListener(this);
		sm = QQShowManager.getInstance(LumaQQ.INSTALL_DIR + "/" + String.valueOf(qqNum));
		sm.addQQShowListener(this);
	}
	
	/**
	 * 打开shell,开始事件循环
	 */
	public void open()	{
		sounder.start();
		diskJobQueue.execute();
		imJobQueue.execute();
		// 初始化系统Tray Icon
		initTray();

		// 设置窗口位置和大小
		Rectangle bound = new Rectangle(optionHelper.getLocationX(), optionHelper.getLocationY(), 0, 0);
		bound.width = optionHelper.getWidth();
		bound.height = optionHelper.getHeight();
		if(bound.width == -1) bound.width = 200;
		if(bound.height == -1) bound.height = 500;
		shell.setSize(bound.width, bound.height);
		shell.setLocation(bound.x, bound.y);
		// 保存窗口位置,本来这个位置是在controlMoved事件里面保存的,但是有可能有的系统
		// 一开始没有这个事件,从而造成空指针,所以还是要在这里保存一下
		shellLocation = shell.getLocation();
		// 打开shell,开始事件循环
		shell.layout();
		shell.open();
		// 登陆
		checkLogin(false, false);
		// 开始事件循环
		while(!shell.isDisposed()) 	{
			if(!display.readAndDispatch()) 
				display.sleep();
		}
	}

	/**
	 * 检查登陆状态,如果正在登录或者已经登录,直接返回,如果尚未登录则执行登录操作
	 * 
	 * @param forceRandom
	 * 		true表示强制随机选择服务器
	 * @return
	 * 		如果当前正在登陆,则返回false,否则返回true
	 */
	public boolean checkLogin(boolean forceRandom, boolean forceTcp) {
		// 如果正在登陆,不执行动作
		if(client.isLogging()) return false;
		// 万一用户还没有登陆,需要先登陆
		if(!client.getUser().isLoggedIn()) {
			// 重设界面
			if(getCurrentPanel() != PANEL_WAITING) {
				switchPanel(PANEL_WAITING);
				setWaitingPanelHint(hint_login);
				resumeWaitingPanelAnimation();
			}
			// 如果设置了强制tcp方式,则不管设置什么都用tcp
			boolean useTcp = forceTcp ? true : optionHelper.isUseTcp();
			// 设置登陆的服务器和登陆方式
			if(optionHelper.isAutoSelect() || forceRandom) {
				String[] servers = useTcp ? LumaQQ.tcpServers : LumaQQ.udpServers;
				client.setLoginServer(servers[Util.random().nextInt(servers.length)]);
			} else {
			    if(forceTcp && optionHelper.isUseTcp() != forceTcp)
			        client.setLoginServer(LumaQQ.tcpServers[0]);
			    else 
			        client.setLoginServer(optionHelper.getServer());
			}
			// 如果是TCP方式登录,设置TCP服务器的端口和登录模式
			if(useTcp) {
			    client.getUser().setUdp(false);
			    client.setTcpLoginPort(optionHelper.getTcpPort());
			} else
			    client.getUser().setUdp(true);
			// 设置代理服务器的类型,地址和验证用户名密码
			try {
                client.setProxy(new InetSocketAddress(optionHelper.getProxyServer(), optionHelper.getProxyPort()));
				client.setProxyType(optionHelper.getProxyType().getName());
				client.setProxyUsername(optionHelper.getProxyUsername());
				client.setProxyPassword(optionHelper.getProxyPassword());
            } catch (IllegalArgumentException e) {
                client.setProxyType("None");
            }
            // 设置是否显示虚拟摄像头
            client.getUser().setShowFakeCam(optionHelper.isShowFakeCam());
			// 开始登陆
            uiHelper.startStatusAnimation();
			processor.clear();
			client.addQQListener(processor);
			client.addQQListener(faceReceiver);
			try {
			    client.login();
			} catch (Exception e) {
                client.getUser().setStatus(QQ.QQ_STATUS_OFFLINE);
			    logout();
			    MessageDialog.openError(shell, message_box_common_fail_title, e.getMessage());
			}
			return false;
		}
		return true;
	}

	/**
	 * 使自己处于下线状态
	 */
	public void logout() {
		// 注销
		client.logout();
		// 停止所有动画
		uiHelper.stopStatusAnimation();
		uiHelper.stopBlinkSystemMessageIcon();
		uiHelper.stopBlinkImage();
		blindHelper.stopAllEffectOnBlind();
		// 释放定时器
		timerHelper.dispose();
		// 重设图标状态和相关变量,把所有好友设为下线模式
		uiHelper.setTrayIconByStatus();
		uiHelper.setAllFriendOffline();
		mq.clear();
	}		
    
	/**
     * 添加程序图标到系统托盘中
     */
    private void initTray() {
    	// 创建Tray Icon
    	Tray tray = display.getSystemTray();
    	if(tray == null)
    		return;
    	item = new TrayItem(tray, SWT.NONE);
    	// 设置图标
    	item.setImage(res.getImage(Resources.icoOffline));
    	// 添加listener
    	// 鼠标单击事件
    	item.addListener (SWT.Selection, new Listener () {
    		public void handleEvent (Event event) {
    			if(mq.hasNext()) {
    				populateMessage();
    			} else {
    				menuHelper.setStatusMenuVisible(false);
    				if(shell.getMinimized()) {
    					shell.setVisible(true);														    
    					shell.setMinimized(false);
    				} else if(isAutoDockEnabled()) {
    					if(isDocking())
        					pend();
        				else if(!isPending()) {
        					tipHelper.closeFriendTipShell();
        				    shell.setMinimized(true);
        				    shell.setVisible(false);
        				}
    				} else {
    					shell.setMinimized(true);
    					if(optionHelper.isHideWhenMinimize())
    						shell.setVisible(false);
    				}
    			}
    		}
    	});
    	// 鼠标右键点击事件
    	item.addListener (SWT.MenuDetect, new Listener () {
    		public void handleEvent (Event event) {
    			if(menuHelper.isSystemMenuVisible())
    				menuHelper.setSystemMenuVisible(false);
    			else {
    				menuHelper.setSystemMenuData(1);
    				menuHelper.setSystemMenuLocation(display.getCursorLocation());
    				menuHelper.setSystemMenuVisible(true);    				
    			}
    		}
    	});
		item.setToolTipText("LumaQQ " + String.valueOf(client.getUser().getQQ()));
    }

	/**
	 * 解析下一个消息,弹出相应的界面
	 */
	protected void populateMessage() {
		switch(mq.nextMessageSource()) {
			case QQ.QQ_IM_FROM_SYS:
				InPacket in = mq.getSystemMessage();
				shellLauncher.openReceiveSystemMessageShell(in);
				if(in instanceof SystemNotificationPacket) {
					SystemNotificationPacket packet = (SystemNotificationPacket)in;
					if(packet.type == QQ.QQ_SYS_ADD_FRIEND_APPROVED ||
							packet.type == QQ.QQ_SYS_ADD_FRIEND_APPROVED_AND_ADD)
						blindHelper.addFriend(packet.from);    					
				}
				break;
			case QQ.QQ_IM_FROM_SMS:
				onSMS();
				break;
			case QQ.QQ_IM_FROM_TEMP_SESSION:
				shellLauncher.openTempSessionIMWindow(mq.nextSender());
				break;
			default:
				shellLauncher.openIMShell(mq.nextSender());	
				break;
		}
	}

	/**
	 * 使主界面处于悬挂状态
	 */
	protected void pend() {
		((AutoDockManager)getShell().getData(AutoDockManager.DOCK_MANAGER)).pend();
	}

	/**
	 * @return
	 * 		QQClient对象
	 */
	public QQClient getClient() {
		return client;
	}

	public MenuHelper getMenuHelper() {
		return menuHelper;
	}

	public Display getDisplay() {
		return display;
	}

	/**
	 * 创建字体
	 */
	public void createDefaultStyle() {
		String fontName = optionHelper.getFontName();
		if(fontName.equals(""))
		    fontName = default_font;
		int style = SWT.NORMAL;
		if(optionHelper.getBold()) style |= SWT.BOLD;
		if(optionHelper.getItalic()) style |= SWT.ITALIC;
		
		int c = optionHelper.getFontColor();
		defaultStyle = new LineStyle(new Color(display, (c >>> 16) & 0xFF, (c >>> 8) & 0xFF, c & 0xFF), null, fontName, style, optionHelper.getFontSize());		
	}
    
    /**
     * 设置组背景色
     * 
     * @param color
     * 		背景色
     */
    public void setGroupColor(Color color) {
        groupColor = color;
        blind.setContentBackground(groupColor);
    }

    /**
     * 初始化界面布局
     */
    private void initLayout() {
    	Composite body = initShell();
    	body.setLayout(new StackLayout());
		
    	panels = new Composite[2];
    	panels[PANEL_MAIN] = initMainPanel(body);
    	panels[PANEL_WAITING] = initWaitingPanel(body);
    	
    	switchView(VIEW_FRIEND);
    	switchPanel(PANEL_WAITING);
    }

	/**
	 * 初始化等待面板
	 * 
	 * @param body
	 */
	private Composite initWaitingPanel(Composite body) {
		Composite panel = new Composite(body, SWT.NONE | SWT.NO_BACKGROUND);
		panel.setLayout(new FormLayout());
		panel.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				Color blue = new Color(display, 0xD5, 0xEF, 0xFF);
				Composite panel = (Composite)e.getSource();
				Rectangle rect = panel.getClientArea();
				int height = rect.height >> 2;
				e.gc.setForeground(blue);
				e.gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
				e.gc.fillGradientRectangle(0, 0, rect.width, height, true);
				e.gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
				e.gc.setBackground(blue);
				e.gc.fillGradientRectangle(0, rect.height - height, rect.width, height, true);
				e.gc.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
				e.gc.fillRectangle(0, height, rect.width, rect.height - height - height);
				blue.dispose();
			}
		});
		// logo
		final Label lblLogo = new Label(panel, SWT.CENTER);
		lblLogo.setImage(res.getImage(Resources.bmpLumaQQ));
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 0);
		fd.top = new FormAttachment(0, 0);
		fd.height = 47;
		fd.width = 160;
		lblLogo.setLayoutData(fd);
		// 进度条动画
		waitingPanelAnimator = new Animator(panel);
		waitingPanelAnimator.setLoader(res.getImageLoader(Resources.bmpProgress));
		fd = new FormData();
		fd.left = new FormAttachment(0, 0);
		fd.top = new FormAttachment(0, 0);
		fd.height = 19;
		fd.width = 108;
		waitingPanelAnimator.setLayoutData(fd);
		// 提示
		waitingPanelHint = new Label(panel, SWT.CENTER);
		waitingPanelHint.setBackground(Colors.WHITE);
		waitingPanelHint.setText(hint_login);
		fd = new FormData();
		fd.left = new FormAttachment(0, 0);
		fd.top = new FormAttachment(0, 0);
		fd.width = 160;
		waitingPanelHint.setLayoutData(fd);
		// 取消登录
		final Label lblCancel = new Label(panel, SWT.CENTER);
		lblCancel.setImage(res.getImage(Resources.bmpCancelLoginNormal));
		fd = new FormData();
		fd.left = new FormAttachment(0, 0);
		fd.top = new FormAttachment(0, 0);
		fd.height = 26;
		fd.width = 90;
		lblCancel.setLayoutData(fd);
		lblCancel.addMouseTrackListener(new MouseTrackAdapter() {
			@Override
			public void mouseEnter(MouseEvent e) {
				lblCancel.setImage(res.getImage(Resources.bmpCancelLoginHover));
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				lblCancel.setImage(res.getImage(Resources.bmpCancelLoginNormal));
			}
		});
		lblCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				lblCancel.setImage(res.getImage(Resources.bmpCancelLoginDown));
			}
			
			@Override
			public void mouseUp(MouseEvent e) {
				lblCancel.setImage(res.getImage(Resources.bmpCancelLoginNormal));
				restartLogin(null);
			}
		});
		
		panel.addControlListener(new ControlAdapter() {

⌨️ 快捷键说明

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