00001 #include "Common/Ini.h"
00002 #include "Client/Profile.h"
00003
00004
00005 KProfile::KProfile()
00006 {
00007 m_pLogin = NULL;
00008 m_pPassword = NULL;
00009 m_pNickname = NULL;
00010 }
00011
00012
00013 KProfile::KProfile( char* pLogin, char* pPassword, char* pNickname )
00014 {
00015 m_pLogin = NULL;
00016 m_pPassword = NULL;
00017 m_pNickname = NULL;
00018
00019 SetpLogin( pLogin );
00020 SetpPassword( pPassword );
00021 SetpNickname( pNickname );
00022 }
00023
00024
00025 KProfile::~KProfile()
00026 {
00027 SetpLogin( NULL );
00028 SetpPassword( NULL );
00029 SetpNickname( NULL );
00030 }
00031
00032
00033 void KProfile::SetpLogin( char* pLogin )
00034 {
00035 if( m_pLogin )
00036 Freep( m_pLogin );
00037
00038 if( pLogin )
00039 m_pLogin = strdup( pLogin );
00040 }
00041
00042
00043 void KProfile::SetpPassword( char* pPassword )
00044 {
00045 if( m_pPassword )
00046 Freep( m_pPassword );
00047
00048 if( pPassword )
00049 m_pPassword = strdup( pPassword );
00050 }
00051
00052
00053 void KProfile::SetpNickname( char* pNickname )
00054 {
00055 if( m_pNickname )
00056 Freep( m_pNickname );
00057
00058 if( pNickname )
00059 m_pNickname = strdup( pNickname );
00060 }
00061
00062
00063
00064
00065 KProfileBank::KProfileBank()
00066 {
00067 m_pCurrent = NULL;
00068 m_pIni = new KIni( "Profiles.ini" );
00069 }
00070
00071
00072 KProfileBank::~KProfileBank()
00073 {
00074 Deletep( m_pIni );
00075 Flush();
00076 }
00077
00078
00079 void KProfileBank::Flush()
00080 {
00081 for( u32 i = 0; i < m_ProfileList.GetSize(); i ++ )
00082 {
00083 if( m_ProfileList[i] )
00084 Deletep( m_ProfileList[i] );
00085 }
00086 m_pCurrent = NULL;
00087 }
00088
00089
00090 bool KProfileBank::LoadProfiles()
00091 {
00092 u32 ProfileId = 0;
00093 char pSection[256];
00094 s32 CurrentId;
00095 const char* pLogin;
00096 const char* pPassword;
00097 const char* pNickname;
00098
00099 Flush();
00100
00101 CurrentId = m_pIni->ReadInt( "GENERAL", "CurrentProfile", -1 );
00102
00103 do
00104 {
00105 sprintf( pSection, "PROFILE%i", ProfileId );
00106 pLogin = m_pIni->ReadString( pSection, "Login", NULL );
00107 pPassword = m_pIni->ReadString( pSection, "Password", NULL );
00108 pNickname = m_pIni->ReadString( pSection, "Nickname", NULL );
00109
00110
00111 if( pLogin && pPassword && pNickname )
00112 {
00113 KProfile* pProfile = new KProfile( (char*)pLogin, (char*)pPassword, (char*)pNickname );
00114 AddpProfile( pProfile );
00115
00116 if( CurrentId == ProfileId )
00117 m_pCurrent = pProfile;
00118 }
00119 ProfileId++;
00120 }while( pLogin && pPassword && pNickname );
00121
00122
00123 if( !m_pCurrent && m_ProfileList.GetSize() )
00124 {
00125 m_pCurrent = m_ProfileList[0];
00126 }
00127
00128 return true;
00129 }
00130
00131
00132 bool KProfileBank::SaveProfiles()
00133 {
00134 KProfile* pProfile;
00135 char pSection[256];
00136
00137
00138 for( u32 i = 0; i < m_ProfileList.GetSize(); i ++ )
00139 {
00140 if( m_pCurrent == m_ProfileList[i] )
00141 {
00142 m_pIni->WriteInt( "GENERAL", "CurrentProfile", i );
00143 break;
00144 }
00145 }
00146
00147
00148 for( u32 i = 0; i < m_ProfileList.GetSize(); i ++ )
00149 {
00150 pProfile = m_ProfileList[i];
00151
00152 sprintf( pSection, "PROFILE%i", i );
00153 m_pIni->WriteString( pSection, "Login", pProfile->GetpLogin() );
00154 m_pIni->WriteString( pSection, "Password", pProfile->GetpPassword() );
00155 m_pIni->WriteString( pSection, "Nickname", pProfile->GetpNickname() );
00156 }
00157
00158 return true;
00159 }