void lmcMessageLog::reloadMessageLog(void) { initMessageLog(themePath, false); for(int index = 0; index < messageLog.count(); index++) { SingleMessage msg = messageLog[index]; appendMessageLog(msg.type, &msg.userId, &msg.userName, &msg.message, true); } }
void lmcChatRoomWindow::removeUser(QString* lpszUserId) { QTreeWidgetItem* pItem = getUserItem(lpszUserId); if(!pItem) return; QTreeWidgetItem* pGroup = pItem->parent(); pGroup->removeChild(pItem); QString userId = peerIds.value(*lpszUserId); QString userName = peerNames.value(*lpszUserId); peerIds.remove(*lpszUserId); peerNames.remove(*lpszUserId); if(groupMode) { XmlMessage xmlMessage; xmlMessage.addData(XN_THREAD, threadId); xmlMessage.addData(XN_GROUPMSGOP, GroupMsgOpNames[GMO_Leave]); appendMessageLog(MT_Leave, &userId, &userName, &xmlMessage); setWindowTitle(getWindowTitle()); } // If the local user is removed for some reason, prevent sending any further messages if(userId.compare(localId) == 0) ui.txtMessage->setEnabled(false); }
void lmcChatRoomWindow::sendMessage(void) { if(ui.txtMessage->document()->isEmpty()) return; if(bConnected) { QString szHtmlMessage(ui.txtMessage->toHtml()); encodeMessage(&szHtmlMessage); QTextDocument docMessage; docMessage.setHtml(szHtmlMessage); QString szMessage = docMessage.toPlainText(); QFont font = ui.txtMessage->font(); font.setPointSize(ui.txtMessage->fontPointSize()); MessageType type = groupMode ? MT_GroupMessage : MT_PublicMessage; XmlMessage xmlMessage; xmlMessage.addHeader(XN_TIME, QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch())); xmlMessage.addData(XN_FONT, font.toString()); xmlMessage.addData(XN_COLOR, messageColor.name()); xmlMessage.addData(XN_MESSAGE, szMessage); if(groupMode) { xmlMessage.addData(XN_THREAD, threadId); xmlMessage.addData(XN_GROUPMSGOP, GroupMsgOpNames[GMO_Message]); } appendMessageLog(type, &localId, &localName, &xmlMessage); if(groupMode) { QHash<QString, QString>::const_iterator index = peerIds.constBegin(); while (index != peerIds.constEnd()) { QString userId = index.value(); emit messageSent(type, &userId, &xmlMessage); index++; } } else emit messageSent(type, NULL, &xmlMessage); } else appendMessageLog(MT_Error, NULL, NULL, NULL); ui.txtMessage->clear(); ui.txtMessage->setFocus(); }
void lmcChatRoomWindow::receiveMessage(MessageType type, QString* lpszUserId, XmlMessage* pMessage) { QString title; // if lpszUserId is NULL, the message was sent locally QString senderId = lpszUserId ? *lpszUserId : localId; QString senderName = lpszUserId ? peerNames.value(senderId) : localName; QString data; switch(type) { case MT_PublicMessage: appendMessageLog(type, lpszUserId, &senderName, pMessage); if(isVisible() && !isActiveWindow()) pSoundPlayer->play(SE_NewPubMessage); break; case MT_GroupMessage: appendMessageLog(type, lpszUserId, &senderName, pMessage); if(isHidden() || !isActiveWindow()) { pSoundPlayer->play(SE_NewMessage); title = tr("%1 says..."); setWindowTitle(title.arg(senderName)); } break; case MT_LocalAvatar: data = pMessage->data(XN_FILEPATH); // this message may come with or without user id. NULL user id means avatar change // by local user, while non NULL user id means avatar change by a peer. setUserAvatar(&senderId, &data); break; case MT_UserName: data = pMessage->data(XN_NAME); if(peerNames.contains(senderId)) peerNames.insert(senderId, data); pMessageLog->updateUserName(&senderId, &data); break; case MT_Failed: appendMessageLog(type, lpszUserId, &senderName, pMessage); break; default: break; } }
void lmcChatRoomWindow::addUser(User* pUser) { if(!pUser) return; // Do not add user if user's version is 1.2.10 or less. These versions do not // support Public Chat feature. if(Helper::compareVersions(pUser->version, "1.2.10") <= 0) return; // Do not add user if user is already in the list of participants if(peerIds.contains(pUser->id)) return; peerIds.insert(pUser->id, pUser->id); peerNames.insert(pUser->id, pUser->name); int index = Helper::statusIndexFromCode(pUser->status); lmcUserTreeWidgetUserItem *pItem = new lmcUserTreeWidgetUserItem(); pItem->setData(0, IdRole, pUser->id); pItem->setData(0, TypeRole, "User"); pItem->setData(0, StatusRole, index); pItem->setData(0, SubtextRole, pUser->note); pItem->setText(0, pUser->name); if(index != -1) pItem->setIcon(0, QIcon(QPixmap(statusPic[index], "PNG"))); lmcUserTreeWidgetGroupItem* pGroupItem = (lmcUserTreeWidgetGroupItem*)getGroupItem(&GroupId); pGroupItem->addChild(pItem); pGroupItem->sortChildren(0, Qt::AscendingOrder); // this should be called after item has been added to tree setUserAvatar(&pUser->id); if(groupMode) { XmlMessage xmlMessage; xmlMessage.addData(XN_THREAD, threadId); xmlMessage.addData(XN_GROUPMSGOP, GroupMsgOpNames[GMO_Join]); appendMessageLog(MT_Join, &pUser->id, &pUser->name, &xmlMessage); setWindowTitle(getWindowTitle()); emit messageSent(MT_GroupMessage, NULL, &xmlMessage); } // Local user cannot participate in public chat if status is offline if(!groupMode && pUser->id.compare(localId) == 0) { bool offline = (statusType[Helper::statusIndexFromCode(pUser->status)] == StatusTypeOffline); ui.txtMessage->setEnabled(!offline); ui.txtMessage->setFocus(); } }
void lmcMessageLog::appendBroadcast(QString* lpszUserId, QString* lpszUserName, QString* lpszMessage, QDateTime* pTime) { Q_UNUSED(lpszUserId); decodeMessage(lpszMessage); QString html = themeData.pubMsg; QString caption = tr("Broadcast message from %1:"); html.replace("%iconpath%", "qrc"IDR_BROADCASTMSG); html.replace("%sender%", caption.arg(*lpszUserName)); html.replace("%time%", getTimeString(pTime)); html.replace("%style%", ""); html.replace("%message%", *lpszMessage); appendMessageLog(&html); }
void lmcMessageLog::appendMessageLog( MessageType type, QString* lpszUserId, QString* lpszUserName, XmlMessage* pMessage, bool bReload) { if(!pMessage && type != MT_Error) return; QString message; QString html; QString caption; QDateTime time; QFont font; QColor color; QString fontStyle; QString id = QString::null; bool addToLog = true; removeMessageLog("_lmc_statediv"); switch(type) { case MT_Message: time.setMSecsSinceEpoch(pMessage->header(XN_TIME).toLongLong()); message = pMessage->data(XN_MESSAGE); font.fromString(pMessage->data(XN_FONT)); color.setNamedColor(pMessage->data(XN_COLOR)); appendMessage(lpszUserId, lpszUserName, &message, &time, &font, &color); lastId = *lpszUserId; break; case MT_PublicMessage: case MT_GroupMessage: time.setMSecsSinceEpoch(pMessage->header(XN_TIME).toLongLong()); message = pMessage->data(XN_MESSAGE); font.fromString(pMessage->data(XN_FONT)); color.setNamedColor(pMessage->data(XN_COLOR)); appendPublicMessage(lpszUserId, lpszUserName, &message, &time, &font, &color); lastId = *lpszUserId; break; case MT_Broadcast: time.setMSecsSinceEpoch(pMessage->header(XN_TIME).toLongLong()); message = pMessage->data(XN_BROADCAST); appendBroadcast(lpszUserId, lpszUserName, &message, &time); lastId = QString::null; break; case MT_ChatState: message = pMessage->data(XN_CHATSTATE); caption = getChatStateMessage((ChatState) Helper::indexOf(ChatStateNames, CS_Max, message)); if(!caption.isNull()) { html = themeData.stateMsg; html.replace("%iconpath%", "qrc"IDR_BLANK); html.replace("%sender%", caption.arg(*lpszUserName)); html.replace("%message%", ""); appendMessageLog(&html); } addToLog = false; break; case MT_Failed: message = pMessage->data(XN_MESSAGE); font.fromString(pMessage->data(XN_FONT)); color.setNamedColor(pMessage->data(XN_COLOR)); html = themeData.sysMsg; caption = tr("This message was not delivered to %1:"); fontStyle = getFontStyle(&font, &color, true); decodeMessage(&message); html.replace("%iconpath%", "qrc"IDR_CRITICALMSG); html.replace("%sender%", caption.arg(*lpszUserName)); html.replace("%style%", fontStyle); html.replace("%message%", message); appendMessageLog(&html); lastId = QString::null; break; case MT_Error: html = themeData.sysMsg; html.replace("%iconpath%", "qrc"IDR_CRITICALMSG); html.replace("%sender%", tr("Your message was not sent.")); html.replace("%message%", ""); appendMessageLog(&html); lastId = QString::null; addToLog = false; break; case MT_File: case MT_LocalFile: appendFileMessage(type, lpszUserName, pMessage, bReload); id = pMessage->data(XN_TEMPID); pMessage->removeData(XN_TEMPID); lastId = QString::null; break; case MT_Join: case MT_Leave: message = pMessage->data(XN_GROUPMSGOP); caption = getChatRoomMessage((GroupMsgOp) Helper::indexOf(GroupMsgOpNames, GMO_Max, message)); if(!caption.isNull()) { html = themeData.sysMsg; html.replace("%iconpath%", "qrc"IDR_BLANK); html.replace("%sender%", caption.arg(*lpszUserName)); html.replace("%message%", ""); appendMessageLog(&html); } lastId = QString::null; default: break; } if(!bReload && addToLog && pMessage) { XmlMessage xmlMessage = pMessage->clone(); QString userId = lpszUserId ? *lpszUserId : QString::null; QString userName = lpszUserName ? *lpszUserName : QString::null; messageLog.append(SingleMessage(type, userId, userName, xmlMessage, id)); } }
void wavrChatWindow::receiveMessage(MessageType type, QString* lpszUserId, wavrXmlMessage* pMessage) { QString title; int statusIndex; // if lpszUserId is NULL, the message was sent locally QString senderId = lpszUserId ? *lpszUserId : localId; QString senderName = lpszUserId ? peerNames.value(senderId) : localName; QString data; switch(type) { case MT_Message: appendMessageLog(type, lpszUserId, &senderName, pMessage); if(isHidden() || !isActiveWindow()) { // pSoundPlayer->play(SE_NewMessage); title = tr("%1 says..."); setWindowTitle(title.arg(senderName)); } break; case MT_Broadcast: appendMessageLog(type, lpszUserId, &senderName, pMessage); if(isHidden() || !isActiveWindow()) { //pSoundPlayer->play(SE_NewMessage); title = tr("Broadcast from %1"); setWindowTitle(title.arg(senderName)); } break; case MT_ChatState: qDebug() << "Chat state message received"; appendMessageLog(type, lpszUserId, &senderName, pMessage); break; case MT_Status: data = pMessage->data(XML_STATUS); statusIndex = wavrHelper::statusIndexFromCode(data); if(statusIndex != -1) { setWindowIcon(QIcon(statusPic[statusIndex])); statusType[statusIndex] == StatusTypeOffline ? showStatus(IT_Offline, true) : showStatus(IT_Offline, false); statusType[statusIndex] == StatusTypeBusy ? showStatus(IT_Busy, true) : showStatus(IT_Busy, false); statusType[statusIndex] == StatusTypeAway ? showStatus(IT_Away, true) : showStatus(IT_Away, false); peerStatuses.insert(senderId, data); } break; case MT_Avatar: data = pMessage->data(XML_FILEPATH); // this message may come with or without user id. NULL user id means avatar change // by local user, while non NULL user id means avatar change by a peer. pMessageLog->updateAvatar(&senderId, &data); break; case MT_UserName: data = pMessage->data(XML_NAME); if(peerNames.contains(senderId)) { peerNames.insert(senderId, data); pMessageLog->peerName = data; } pMessageLog->updateUserName(&senderId, &data); break; case MT_Failed: appendMessageLog(type, lpszUserId, &senderName, pMessage); break; case MT_File: case MT_Folder: if(pMessage->data(XML_FILEOP) == FileOpNames[FO_Request]) { // a file request has been received appendMessageLog(type, lpszUserId, &senderName, pMessage); if(pMessage->data(XML_MODE) == FileModeNames[FM_Receive] && (isHidden() || !isActiveWindow())) { //pSoundPlayer->play(SE_NewFile); if(type == MT_File) title = tr("%1 sends a file..."); else title = tr("%1 sends a folder..."); setWindowTitle(title.arg(senderName)); } } else { // a file message of op other than request has been received processFileOp(pMessage); } break; case MT_Depart: pMessageLog->abortPendingFileOperations(); break; default: break; } }