📄 chwinfo.cpp
字号:
if (strValue.GetLength())
{
m_strHost = strValue;
}
}
else if (0 == strKey.CompareNoCase( ADDR_KEY ))
{
// Address key found
if (strValue.GetLength() == ADDR_LEN)
{
m_strAddr = strValue;
}
}
else if (0 == strKey.CompareNoCase( PORT_KEY ))
{
// Port key found
m_sPort = atoi( strValue );
}
else if (0 == strKey.CompareNoCase( TYPE_KEY ))
{
// Type key found
m_type.Set( strValue );
}
else if (0 == strKey.CompareNoCase( LOGIN_KEY ))
{
if (strValue == LOGIN_KEY_CONNECT)
{
m_loginType = connectLogin;
}
}
else if (0 == strKey.CompareNoCase( NAME_KEY ))
{
// Name key found
m_strName = strValue;
RemoveIllegalChars( m_strName );
}
else if (0 == strKey.CompareNoCase( DESC_KEY ))
{
// Type key found
m_strDesc = strValue;
}
else if (0 == strKey.CompareNoCase( USERNAME_KEY ))
{
// Username key found
m_strUsername = strValue;
RemoveIllegalChars( m_strUsername );
}
else if (0 == strKey.CompareNoCase( PASSWORD_KEY ))
{
string strTemp( strValue );
// Password key found
ChUtil::EncryptString( strTemp, false, '/' );
m_strPassword = strTemp;
}
else if (0 == strKey.CompareNoCase( HOMEPAGE_KEY ))
{
// Username key found
m_strHomePage = strValue;
}
Validate();
}
void ChWorldInfo::Validate()
{
if (m_strHost.GetLength() && m_sPort != 0)
{
m_boolValid = true;
}
else
{
m_boolValid = false;
}
}
void ChWorldInfo::Escape( string& strValue )
{
string strTemp;
const char* pstrValue = strValue;
while (*pstrValue)
{
if ((*pstrValue == '{') || (*pstrValue == '}') ||
(*pstrValue == '\\') || (*pstrValue == '"'))
{
strTemp += '\\';
}
strTemp += *pstrValue;
pstrValue++;
}
strValue = strTemp;
}
void ChWorldInfo::RemoveIllegalChars( string& strValue )
{
string strText;
int iLoc;
while (-1 != (iLoc = strValue.Find( WORLD_NAME_SEPARATOR )))
{
strValue = strValue.Left( iLoc ) + strValue.Mid( iLoc + 1 );
}
}
#if defined( CH_MSW )
void ChWorldInfo::CreateWindowsShortcut( ChCore* pCore )
{
/* First save the information to
a file */
string strPath;
string strShortcutDir;
string strFilename;
bool boolDone = false;
ChShortcutWizard shortcutWizard;
int iResult;
bool boolSuccess;
LOADSTRING( IDS_SHORTCUT_DIR, strShortcutDir );
ChUtil::GetAppDirectory( strPath );
strPath += strShortcutDir;
shortcutWizard.SetPath( strPath );
shortcutWizard.SetName( GetName() );
shortcutWizard.SetAccount( GetUsername(), GetPassword(), GetLoginType() );
shortcutWizard.SetServer( GetHost(), GetPort(), GetType() );
if (!GetHost().IsEmpty() && (GetPort() != 0) &&
(GetType() != undefinedType))
{ /* We have all the server
information we need */
shortcutWizard.SetUseCurrWorld();
}
// Present the dialog
iResult = shortcutWizard.DoModal();
switch( iResult )
{
case IDFINISH:
{
boolSuccess = true;
break;
}
case IDCANCEL:
case IDABORT:
{
boolSuccess = false;
break;
}
}
if (boolSuccess)
{
string strPath;
string strFilePath;
string strGroupName;
string strName;
string strUsername;
string strPassword;
string strHost;
chint16 sPort;
ChWorldType type;
ChLoginType loginType;
shortcutWizard.GetData( strPath, strFilePath, strGroupName, strName );
shortcutWizard.GetAccount( strUsername, strPassword, loginType );
shortcutWizard.GetServer( strHost, sPort, type );
boolSuccess = WriteWindowsShortcutFile( pCore, strPath, strFilePath,
strGroupName, strName,
strUsername, strPassword,
strHost, "", sPort, type,
loginType );
}
}
bool ChWorldInfo::WriteWindowsShortcutFile( ChCore* pCore, const string& strPath,
const string& strFilePath,
const string& strGroupName,
const string& strName,
const string& strUsername,
const string& strPassword,
const string& strHost,
const string& strHomePage,
chint16 sPort,
const ChWorldType& type,
ChLoginType loginType )
{
bool boolSuccess = true;
ChUtil::CreateDirectoryTree( strPath );
try
{
string strTemp( "Pueblo user" );
bool boolWritten = false;
CStdioFile shortcut( strFilePath, CFile::modeCreate |
CFile::modeWrite |
CFile::shareExclusive );
// strTemp = pCore->GetPuebloUsername();
if (!strTemp.IsEmpty())
{
shortcut.WriteString( "username = " );
shortcut.WriteString( strTemp );
shortcut.WriteString( "\n" );
boolWritten = true;
}
if (boolWritten)
{
shortcut.WriteString( "\n" );
}
if (!GetName().IsEmpty())
{
shortcut.WriteString( "worldname = " );
shortcut.WriteString( GetName() );
shortcut.WriteString( "\n" );
}
if (!strHost.IsEmpty())
{
shortcut.WriteString( "worldserver = " );
shortcut.WriteString( strHost );
shortcut.WriteString( "\n" );
}
if (0 != sPort)
{
char strPort[10];
sprintf( strPort, "%d", (int)sPort );
shortcut.WriteString( "worldport = " );
shortcut.WriteString( strPort );
shortcut.WriteString( "\n" );
}
if (undefinedType != type)
{
strTemp = type.GetName();
shortcut.WriteString( "worldtype = " );
shortcut.WriteString( strTemp );
shortcut.WriteString( "\n" );
}
if (connectLogin == loginType)
{
shortcut.WriteString( "logintype = connect\n" );
}
if (!strUsername.IsEmpty())
{
shortcut.WriteString( "worldusername = " );
shortcut.WriteString( strUsername );
shortcut.WriteString( "\n" );
}
if (!strPassword.IsEmpty())
{
strTemp = strPassword;
ChUtil::EncryptString( strTemp, true );
shortcut.WriteString( "worldpassword = " );
shortcut.WriteString( strTemp );
shortcut.WriteString( "\n" );
}
if (!strHomePage.IsEmpty())
{
shortcut.WriteString( "worldhomepage = " );
shortcut.WriteString( strHomePage );
shortcut.WriteString( "\n" );
}
}
catch( CFileException* pFileExcept )
{
CWnd* pFrame = pCore->GetFrameWnd();
string strErr;
string strCaption;
LOADSTRING( IDS_FILEERR_CAPTION, strCaption );
switch( pFileExcept->m_cause )
{
case CFileException::sharingViolation:
{
LOADSTRING( IDS_FILEERR_SHARING, strErr );
break;
}
case CFileException::diskFull:
{
LOADSTRING( IDS_FILEERR_DISK_FULL, strErr );
break;
}
default:
{
string strFormat;
LOADSTRING( IDS_FILEERR_DISK, strFormat );
strErr.Format( strFormat, (int)pFileExcept->m_cause,
(const char*)strFilePath );
break;
}
}
pFrame->MessageBox( strErr, strCaption, MB_OK | MB_ICONSTOP );
// Delete the exception
pFileExcept->Delete();
boolSuccess = false;
}
if (boolSuccess)
{
CreateProgmanIcon( strFilePath, strGroupName, strName );
}
return boolSuccess;
}
void ChWorldInfo::CreateProgmanIcon( const string& strFilePath,
const string& strGroupName,
const string& strName )
{
DWORD idDDEInst = ChCore::GetDDEInstance();
HCONV hDDEConversation;
HSZ hszShell;
string strRequest;
bool boolSuccess;
DWORD dwResult;
char* pstrBuffer = new char[1000];
char* pstrProgram = new char[PROGRAM_PATH_LEN];
int iLen;
ASSERT( pstrBuffer );
ASSERT( pstrProgram );
GetModuleFileName( 0, pstrProgram, PROGRAM_PATH_LEN );
/* Get a list of program manager
groups (via DDE) and add them
to the combo box */
hszShell = DdeCreateStringHandle( idDDEInst, PROGMAN_SERVICE, CP_WINANSI );
// Initiate a conversation
hDDEConversation = DdeConnect( idDDEInst, hszShell, hszShell, 0 );
/* Send the request to the server
to create (and/or activate)
the group */
sprintf( pstrBuffer, "[CreateGroup(%s)]", (const char*)strGroupName );
iLen = strlen( pstrBuffer );
boolSuccess = !!DdeClientTransaction( (LPBYTE)pstrBuffer, iLen + 1,
hDDEConversation, 0, CF_TEXT,
XTYP_EXECUTE, 1000, &dwResult );
if (boolSuccess)
{ /* Send the request to the server
to add the item */
sprintf( pstrBuffer, "[AddItem(\"%s\",%s,%s,%d)]",
(const char*)strFilePath, (const char*)strName,
pstrProgram, SHORTCUT_ICON_IDX );
iLen = strlen( pstrBuffer );
boolSuccess = !!DdeClientTransaction( (LPBYTE)pstrBuffer, iLen + 1,
hDDEConversation, 0, CF_TEXT,
XTYP_EXECUTE, 1000, &dwResult );
}
// Clean up from DDE
DdeFreeStringHandle( idDDEInst, hszShell );
delete [] pstrBuffer;
delete [] pstrProgram;
}
/////////////////////////////////////////
/// Methodfs to read the shortcut file
bool ChWorldInfo::FindTag( ifstream& ifile, const char* pstrTag, string& strVal )
{
bool boolFound = false;
char buffer[256];
ifile.clear(ios::goodbit);
ifile.seekg( 0 ); // start from the top of the file
while (!ifile.eof() && !boolFound)
{
char* pTagStart;
// Read the next line...
ifile.getline( buffer, sizeof( buffer ) );
// Eliminate white space...
pTagStart = buffer;
while (*pTagStart && isspace( *pTagStart ))
{
pTagStart++;
}
if ((0 != *pTagStart) && (COMMENT_PREFIX != *pTagStart))
{
// Look for an delimiter...
char* pDelim;
if (pDelim = strchr( pTagStart, TAG_DELIMITER ))
{
char* pLastChar;
/* Separate the tag string from
the value */
*pDelim = 0;
pLastChar = pDelim - 1;
while ((pLastChar != pTagStart) && isspace( *pLastChar ))
{
*pLastChar = 0;
--pLastChar;
}
if (0 == stricmp( pstrTag, pTagStart ))
{
// Yay! We found it!
strVal = ++pDelim;
strVal.TrimLeft();
strVal.TrimRight();
/* Decrypt the value just in case
it was encrypted */
ChUtil::EncryptString( strVal, false );
boolFound = true;
}
}
}
}
return boolFound;
}
bool ChWorldInfo::FindTag( ifstream& ifile, const char* pstrTag, chint16& sVal )
{
bool boolFound;
string strVal;
if (boolFound = FindTag( ifile, pstrTag, strVal ))
{
sVal = atoi( strVal );
}
return boolFound;
}
bool ChWorldInfo::FindTag( ifstream& ifile, const char* pstrTag, chuint16& suVal )
{
bool boolFound;
chint16 sVal;
if (boolFound = FindTag( ifile, pstrTag, sVal ))
{
suVal = (chuint16)sVal;
}
return boolFound;
}
bool ChWorldInfo::FindTag( ifstream& ifile, const char* pstrTag, chint32& lVal )
{
bool boolFound;
string strVal;
if (boolFound = FindTag( ifile, pstrTag, strVal ))
{
lVal = atol( strVal );
}
return boolFound;
}
bool ChWorldInfo::FindTag( ifstream& ifile, const char* pstrTag, chuint32& luVal )
{
bool boolFound;
chint32 lVal;
if (boolFound = FindTag( ifile, pstrTag, lVal ))
{
luVal = (chuint32)lVal;
}
return boolFound;
}
#endif // defined( CH_MSW )
// Local Variables: ***
// tab-width:4 ***
// End: ***
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -