// MBEmotions.cpp : implementation file // #include "stdafx.h" #include "MBEmotions.h" #include "MBAMUIBaseFun.h" // CMBEmotions CMBEmotions::CMBEmotions() { m_mapEmotionsShortcut.RemoveAll(); } CMBEmotions::~CMBEmotions() { CleanData(); } CMBEmotions* CMBEmotions::Instance() { static CMBEmotions stkEmo; return &stkEmo; } void CMBEmotions::CleanData() { CMBEmotionItem *pItem; for(int i = 0; i < m_arEmotions.GetCount(); i++) { pItem = m_arEmotions.GetAt(i);; if(pItem) { if(pItem->m_pBitmap) pItem->m_pBitmap->Release(); delete pItem; } } m_arEmotions.RemoveAll(); } CString CMBEmotions::GetEmotionsFolder() { if ( m_strFolder.IsEmpty() ) { CString strExePath = CBaseCommFun::GetModulePath( ); m_strFolder.Format( _T("%s\\emotions\\"), strExePath); } return m_strFolder; } BOOL CMBEmotions::LoadEmotions( LPCTSTR strFolder ) { if ( strFolder != NULL ) { m_strFolder = strFolder; m_strFolder.TrimRight(_T("\\")); m_strFolder +=_T("\\"); } CString strEmotionsFolder = GetEmotionsFolder(); CString strCfgFile =strEmotionsFolder + _T("emotions.xml"); m_nEmotionEndIndex = -1; m_mapEmotionsShortcut.RemoveAll(); CleanData(); COIXMLParser xmlParser; IXMLDOMElementPtr ptrBody; IXMLDOMNodePtr ptrNode,ptrNext; CString strNodeName,strNodeValue,strAttrValue; CString strMsgItem; int nIndex = 0; if( !xmlParser.ParseXMLFile(strCfgFile, ptrBody) ) { return FALSE; } ptrBody->get_firstChild( &ptrNode); while(ptrNode) { if( !COIXMLParser::GetNodeAttrValue(ptrNode, _T("File"),strAttrValue )) { ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; continue; } CMBEmotionItem *pEmo = new CMBEmotionItem; pEmo->m_strFileDir = strEmotionsFolder; pEmo->m_strFileName = strAttrValue; pEmo->m_nIndex = nIndex; if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("FixedFile"),strAttrValue )) { pEmo->m_strFixedImage = strAttrValue; } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("PinYin"),strAttrValue )) { strAttrValue.Trim(); pEmo->m_strPinYin = strAttrValue; if( !strAttrValue.IsEmpty()) m_mapEmotionsShortcut.SetAt(strAttrValue,nIndex); } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("ShortCut"),strAttrValue )) { strAttrValue.Trim(); pEmo->m_strShortCut = strAttrValue; if( !strAttrValue.IsEmpty()) m_mapEmotionsShortcut.SetAt(strAttrValue,nIndex); } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("Meaning"),strAttrValue )) { pEmo->m_strTip = strAttrValue; } SStringW strFile; strFile.Format( _T("file:%s%s"), pEmo->m_strFileDir, pEmo->m_strFixedImage); pEmo->m_pBitmap = LOADIMAGE2(strFile); if( pEmo->m_pBitmap) { m_arEmotions.Add( pEmo); nIndex++; } else { delete pEmo; pEmo = NULL; } ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; } m_nEmotionEndIndex = nIndex; return TRUE; } BOOL CMBEmotions::LoadExtrenEmotions( LPCTSTR strFolder ) { m_strExtrenFolder2 = _T(""); m_strExtrenFolder3 = _T(""); m_nExtrenEmotionEndIndex2 = -1; m_nExtrenEmotionEndIndex3 = -1; if ( strFolder == NULL ) { return FALSE; } CString strEmotionsFolder = strFolder; strEmotionsFolder.TrimRight(_T("\\")); strEmotionsFolder +=_T("\\"); COIXMLParser xmlParser; IXMLDOMElementPtr ptrBody; IXMLDOMNodePtr ptrNode,ptrNext; CString strNodeName,strNodeValue,strAttrValue; CString strMsgItem; int nIndex = m_nEmotionEndIndex; CString strCfgFile; CString strEmotionsExtren2 = _T(""); CString strEmotionsExtren3 = _T(""); CString strTmpPath; WIN32_FIND_DATA wfd; strTmpPath.Format(_T("%s*.*"), strEmotionsFolder); HANDLE hFind = FindFirstFile(strTmpPath,&wfd); if(hFind == INVALID_HANDLE_VALUE) { return TRUE; } do { if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if ( _tcscmp( wfd.cFileName, _T( ".." ) ) == 0 || _tcscmp( wfd.cFileName, _T( "." ) ) == 0 ) continue ; if(strEmotionsExtren2.IsEmpty()) { m_strExtrenFolder2 = wfd.cFileName; strEmotionsExtren2.Format(_T("%s%s\\"), strEmotionsFolder, wfd.cFileName); } else if(strEmotionsExtren3.IsEmpty()) { m_strExtrenFolder3 = wfd.cFileName; strEmotionsExtren3.Format(_T("%s%s\\"), strEmotionsFolder, wfd.cFileName); } else break; } } while(FindNextFile(hFind,&wfd)); FindClose(hFind); if( !strEmotionsExtren2.IsEmpty()) { strCfgFile =strEmotionsExtren2 + _T("emotions.xml"); if( xmlParser.ParseXMLFile(strCfgFile, ptrBody) ) { ptrBody->get_firstChild( &ptrNode); while(ptrNode) { if( !COIXMLParser::GetNodeAttrValue(ptrNode, _T("File"),strAttrValue )) { ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; continue; } CMBEmotionItem *pEmo = new CMBEmotionItem; pEmo->m_nIndex = nIndex; pEmo->m_strFileDir = strEmotionsExtren2; pEmo->m_strFileName = strAttrValue; if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("FixedFile"),strAttrValue )) { pEmo->m_strFixedImage = strAttrValue; } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("PinYin"),strAttrValue )) { strAttrValue.Trim(); pEmo->m_strPinYin = strAttrValue; if( !strAttrValue.IsEmpty()) m_mapEmotionsShortcut.SetAt(strAttrValue,nIndex); } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("ShortCut"),strAttrValue )) { strAttrValue.Trim(); pEmo->m_strShortCut = strAttrValue; if( !strAttrValue.IsEmpty()) m_mapEmotionsShortcut.SetAt(strAttrValue,nIndex); } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("Meaning"),strAttrValue )) { pEmo->m_strTip = strAttrValue; } m_arEmotions.Add( pEmo); nIndex++; ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; } } } m_nExtrenEmotionEndIndex2 = nIndex; if( !strEmotionsExtren3.IsEmpty()) { strCfgFile =strEmotionsExtren3 + _T("emotions.xml"); if( xmlParser.ParseXMLFile(strCfgFile, ptrBody) ) { ptrBody->get_firstChild( &ptrNode); while(ptrNode) { if( !COIXMLParser::GetNodeAttrValue(ptrNode, _T("File"),strAttrValue )) { ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; continue; } CMBEmotionItem *pEmo = new CMBEmotionItem; pEmo->m_nIndex = nIndex; pEmo->m_strFileDir = strEmotionsExtren3; pEmo->m_strFileName = strAttrValue; if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("FixedFile"),strAttrValue )) { pEmo->m_strFixedImage = strAttrValue; } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("PinYin"),strAttrValue )) { strAttrValue.Trim(); pEmo->m_strPinYin = strAttrValue; if( !strAttrValue.IsEmpty()) m_mapEmotionsShortcut.SetAt(strAttrValue,nIndex); } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("ShortCut"),strAttrValue )) { strAttrValue.Trim(); pEmo->m_strShortCut = strAttrValue; if( !strAttrValue.IsEmpty()) m_mapEmotionsShortcut.SetAt(strAttrValue,nIndex); } if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("Meaning"),strAttrValue )) { pEmo->m_strTip = strAttrValue; } m_arEmotions.Add( pEmo); nIndex++; ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; } } } m_nExtrenEmotionEndIndex3 = nIndex; return TRUE; } int CMBEmotions::GetCount() { return (int)m_arEmotions.GetSize(); } int CMBEmotions::GetDefEndIndex() { return m_nEmotionEndIndex; } int CMBEmotions::GetExt2EndIndex() { return m_nExtrenEmotionEndIndex2; } int CMBEmotions::GetExt3EndIndex() { return m_nExtrenEmotionEndIndex3; } void CMBEmotions::GetExtEmotionFolderName( CString &strExtrenFolder2 ,CString &strExtrenFolder3) { strExtrenFolder2 = m_strExtrenFolder2; strExtrenFolder3 = m_strExtrenFolder3; } CMBEmotionItem* CMBEmotions::GetEmotion( int nIndex) { if ( nIndex < 0 || nIndex >= (int)m_arEmotions.GetSize()) { return NULL; } return m_arEmotions.GetAt(nIndex); } int CMBEmotions::LookupEmotionIndex(CString strShortcut) { int nIndex; if(m_mapEmotionsShortcut.Lookup(strShortcut,nIndex)) { return nIndex; } return -1; }