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

📄 hubframe.cpp

📁 p2p技术C源代码.rar
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		ctrlUsers.GetClientRect(&rc);
		if(uMsg == WM_CONTEXTMENU)
			ctrlUsers.ScreenToClient(&pt); 
		
		if (PtInRect(&rc, pt)) { 
			ctrlUsers.ClientToScreen(&pt);
			showMenu = true;
		}
	}

	if(showMenu) {
		if(client->getOp()) {
			// Alrite, now add the special menu items...
			int added = 0;
			int n = 0;
			UserCommand::List& ul = HubManager::getInstance()->getUserCommands();
			for(UserCommand::Iter ui = ul.begin(); ui != ul.end(); ++ui) {
				UserCommand& uc = *ui;
				if(uc.getHub().empty() || uc.getHub() == "op" || 
					Util::stricmp(uc.getHub(), server) == 0) {
					// We add!
					if(added == 0) {
						opMenu.AppendMenu(MF_SEPARATOR, 0, (LPCTSTR)0);
						added++;
					}
					opMenu.AppendMenu(MF_STRING, IDC_USER_COMMAND+n, uc.getName().c_str());
					added++;
				}
				n++;
			}
			commands = ul.size();
			opMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, m_hWnd);
			while(added > 0) {
				opMenu.DeleteMenu(opMenu.GetMenuItemCount()-1, MF_BYPOSITION);
				added--;
			}
		} else {
			int added = 0;
			int n = 0;
			UserCommand::List& ul = HubManager::getInstance()->getUserCommands();
			for(UserCommand::Iter ui = ul.begin(); ui != ul.end(); ++ui) {
				UserCommand& uc = *ui;
				if(uc.getHub().empty() || 
					Util::stricmp(uc.getHub(), server) == 0) {
					// We add!
					if(added == 0) {
						userMenu.AppendMenu(MF_SEPARATOR, 0, (LPCTSTR)0);
						added++;
					}
					userMenu.AppendMenu(MF_STRING, IDC_USER_COMMAND+n, uc.getName().c_str());
					added++;
				}
				n++;
			}
			commands = ul.size();
			userMenu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, m_hWnd);
			while(added > 0) {
				userMenu.DeleteMenu(userMenu.GetMenuItemCount()-1, MF_BYPOSITION);
				added--;
			}
		}
	}
	return FALSE;
}

LRESULT HubFrame::onUserCommand(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	dcassert(wID >= IDC_USER_COMMAND);
	size_t n = (size_t)wID - IDC_USER_COMMAND;

	UserCommand::List& ul = HubManager::getInstance()->getUserCommands();
	dcassert(n < ul.size());

	UserCommand& uc = ul[n];
	ucParams["mynick"] = client->getNick();

	if(!WinUtil::getUCParams(m_hWnd, uc, ucParams))
		return 0;

	int sel = -1;
	while((sel = ctrlUsers.GetNextItem(sel, LVNI_SELECTED)) != -1) {
		UserInfo* u = (UserInfo*) ctrlUsers.GetItemData(sel);
		ucParams["nick"] = u->user->getNick();
		if(uc.getNick().empty()) {
			client->sendMessage(Util::formatParams(uc.getCommand(), ucParams));
		} else {
			client->privateMessage(Util::formatParams(uc.getNick(), ucParams), Util::formatParams(uc.getCommand(), ucParams));
		}
	}
	return 0;
};

void HubFrame::onTab() {
	if(BOOLSETTING(TAB_COMPLETION) && (GetFocus() == ctrlMessage.m_hWnd)) {
		int n = ctrlMessage.GetWindowTextLength();
		AutoArray<char> buf(n+1);
		ctrlMessage.GetWindowText(buf, n+1);
		string text(buf, n);
		string::size_type textStart = text.find_last_of(" \n\t");

		if(complete.empty()) {
			if(textStart != string::npos) {
				complete = text.substr(textStart + 1);
			} else {
				complete = text;
			}
			if(complete.empty()) {
				// Still empty, no text entered...
				return;
			}
			int y = ctrlUsers.GetItemCount();

			for(int x = 0; x < y; ++x)
				ctrlUsers.SetItemState(x, 0, LVNI_FOCUSED | LVNI_SELECTED);
		}

		if(textStart == string::npos)
			textStart = 0;
		else
			textStart++;

		int start = ctrlUsers.GetNextItem(-1, LVNI_FOCUSED) + 1;
		int i = start;
		int j = ctrlUsers.GetItemCount();

		bool firstPass = i < j;
		if(!firstPass)
			i = 0;
		while(firstPass || (!firstPass && i < start)) {
			UserInfo* ui = (UserInfo*)ctrlUsers.GetItemData(i);
			const string& nick = ui->user->getNick();
			bool found = (Util::strnicmp(nick, complete, complete.length()) == 0);
			if(!found) {
				// Check if there's a [ISP] tag to ignore...
				if(nick[0] == '[') {
					string::size_type x = nick.find(']');
					if(x != string::npos) {
						found = (Util::strnicmp(nick.c_str() + x + 1, complete.c_str(), complete.length()) == 0);
					}
				}
			}
			if(found) {
				if((start - 1) != -1) {
					ctrlUsers.SetItemState(start - 1, 0, LVNI_SELECTED | LVNI_FOCUSED);
				}
				ctrlUsers.SetItemState(i, LVNI_FOCUSED | LVNI_SELECTED, LVNI_FOCUSED | LVNI_SELECTED);
				ctrlUsers.EnsureVisible(i, FALSE);
				ctrlMessage.SetSel(textStart, ctrlMessage.GetWindowTextLength(), TRUE);
				ctrlMessage.ReplaceSel(ui->user->getNick().c_str());
				return;
			}
			i++;
			if(i == j) {
				firstPass = false;
				i = 0;
			}
		}
	} else {
		HWND focus = GetFocus();

		if(focus == ctrlClient.m_hWnd) {
			ctrlMessage.SetFocus();
		} else if(focus == ctrlMessage.m_hWnd) {
			ctrlUsers.SetFocus();
		} else if(focus == ctrlUsers.m_hWnd) {
			ctrlClient.SetFocus();
		} 
	}
}

LRESULT HubFrame::onChar(UINT uMsg, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled) {
	if(!complete.empty() && wParam != VK_TAB && uMsg == WM_KEYDOWN)
		complete.clear();

	switch(wParam) {
		case VK_TAB:
			if(uMsg == WM_KEYDOWN) {
				onTab();
			}
			break;
		case VK_RETURN:
			if( (GetKeyState(VK_CONTROL) & 0x8000) || 
				(GetKeyState(VK_MENU) & 0x8000) ) {
					bHandled = FALSE;
				} else {
					if(uMsg == WM_KEYDOWN) {
						onEnter();
					}
				}
				break;
		default:
			bHandled = FALSE;
	}
	return 0;
}

LRESULT HubFrame::onShowUsers(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled) {
	bHandled = FALSE;
	if((wParam == BST_CHECKED) && !client->getUserInfo()) {
		User::NickMap& lst = client->lockUserList();
		ctrlUsers.SetRedraw(FALSE);
		for(User::NickIter i = lst.begin(); i != lst.end(); ++i) {
			updateUser(i->second);
		}
		client->unlockUserList();
		ctrlUsers.SetRedraw(TRUE);
		ctrlUsers.resort();

		client->setUserInfo(true);
		client->refreshUserList(true);		
	} else {
		client->setUserInfo(false);
		clearUserList();
	}

	UpdateLayout(FALSE);
	return 0;
}

LRESULT HubFrame::onFollow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	
	if(!redirect.empty()) {
		string s, f;
		short p = 411;
		Util::decodeUrl(redirect, s, p, f);
		if(ClientManager::getInstance()->isConnected(s, p)) {
			addClientLine(STRING(REDIRECT_ALREADY_CONNECTED));
			return 0;
		}
		
		dcassert(frames.find(server) != frames.end());
		dcassert(frames[server] == this);
		frames.erase(server);
		server = redirect;
		frames[server] = this;
		client->addListener(this);
		client->connect(redirect);
	}
	return 0;
}

LRESULT HubFrame::onEnterUsers(int /*idCtrl*/, LPNMHDR /* pnmh */, BOOL& /*bHandled*/) {
	int item = ctrlUsers.GetNextItem(-1, LVNI_FOCUSED);
	if(client->isConnected() && (item != -1)) {
		try {
			QueueManager::getInstance()->addList(((UserInfo*)ctrlUsers.GetItemData(item))->user);
		} catch(const Exception& e) {
			addClientLine(e.getError());
		}
	}
	return 0;
}


void HubFrame::addClientLine(const string& aLine, bool inChat /* = true */) {
	string line = "[" + Util::getShortTimeString() + "] " + aLine;

	ctrlStatus.SetText(0, (line + "\r\n").c_str());
	setDirty();
	
	if(BOOLSETTING(STATUS_IN_CHAT) && inChat) {
		addLine("*** " + aLine);
	}
}

void HubFrame::closeDisconnected() {
	for(FrameIter i=frames.begin(); i!= frames.end(); ++i) {
		if (!(i->second->client->isConnected())) {
			i->second->PostMessage(WM_CLOSE);
		}
	}
};

void HubFrame::onAction(TimerManagerListener::Types type, DWORD /*aTick*/) throw() {
	switch(type) {
		case TimerManagerListener::SECOND:
			updateStatusBar(); break;
	}
}

// ClientListener
void HubFrame::onAction(ClientListener::Types type, Client* client) throw() {
	switch(type) {
		case ClientListener::CONNECTING:
			speak(ADD_STATUS_LINE, STRING(CONNECTING_TO) + client->getServer() + "...");
			speak(SET_WINDOW_TITLE, client->getServer());
			break;
		case ClientListener::CONNECTED: speak(CONNECTED); break;
		case ClientListener::BAD_PASSWORD: client->setPassword(Util::emptyString); break;
		case ClientListener::GET_PASSWORD: speak(GET_PASSWORD); break;
		case ClientListener::HUB_NAME: speak(SET_WINDOW_TITLE, client->getName() + " (" + client->getServer() + ")"); break;
		case ClientListener::VALIDATE_DENIED:
			client->removeListener(this);
			client->disconnect();
			speak(ADD_STATUS_LINE, STRING(NICK_TAKEN));
			break;
	}
}

void HubFrame::onAction(ClientListener::Types type, Client* /*client*/, const string& line) throw() {
	switch(type) {
		case ClientListener::SEARCH_FLOOD: speak(ADD_STATUS_LINE, STRING(SEARCH_SPAM_FROM) + line); break;
		case ClientListener::FAILED: speak(ADD_STATUS_LINE, line); speak(DISCONNECTED); break;
		case ClientListener::MESSAGE: 
			if(SETTING(FILTER_MESSAGES)) {
				if((line.find("Hub-Security") != string::npos) && (line.find("was kicked by") != string::npos)) {
					// Do nothing...
				} else if((line.find("is kicking") != string::npos) && (line.find("because:") != string::npos)) {
					speak(ADD_SILENT_STATUS_LINE, line);
				} else {
					speak(ADD_CHAT_LINE, line);
				}
			} else {
				speak(ADD_CHAT_LINE, line);
			}
			break;

		case ClientListener::FORCE_MOVE:
			{
				string s, f;
				short p = 411;
				Util::decodeUrl(line, s, p, f);
				if(ClientManager::getInstance()->isConnected(s, p)) {
					speak(ADD_STATUS_LINE, STRING(REDIRECT_ALREADY_CONNECTED));
					return;
				}
			}
			redirect = line;
			if(BOOLSETTING(AUTO_FOLLOW)) {
				PostMessage(WM_COMMAND, IDC_FOLLOW, 0);
			} else {
				speak(ADD_STATUS_LINE, STRING(PRESS_FOLLOW) + line);
			}
			break;
	}
}

void HubFrame::onAction(ClientListener::Types type, Client* /*client*/, const User::Ptr& user) throw() {
	switch(type) {
		case ClientListener::MY_INFO: if(client->getUserInfo()) speak(UPDATE_USER, user); break;
		case ClientListener::QUIT: if(client->getUserInfo()) speak(REMOVE_USER, user); break;
		case ClientListener::HELLO: if(client->getUserInfo()) speak(UPDATE_USER, user); break;
	}
}

void HubFrame::onAction(ClientListener::Types type, Client* /*client*/, const User::List& aList) throw() {
	switch(type) {
		case ClientListener::OP_LIST: // Fall through
		case ClientListener::NICK_LIST: 
			if(client->getUserInfo()) speak(UPDATE_USERS, aList); break;
	}
}

void HubFrame::onAction(ClientListener::Types type, Client* /*client*/, const User::Ptr& user, const string&  line) throw() {
	switch(type) {
		case ClientListener::PRIVATE_MESSAGE: speak(PRIVATE_MESSAGE, user, line); break;
	}
}

/**
 * @file
 * $Id: HubFrame.cpp,v 1.27 2003/07/15 14:53:12 arnetheduck Exp $
 */

⌨️ 快捷键说明

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