fix profile picture on login and logout

This commit is contained in:
Sam39 2022-08-18 11:23:11 +03:00
parent 1d5dc1e545
commit b9a2572b72
2 changed files with 40 additions and 16 deletions

View File

@ -28,7 +28,6 @@ std::string HTTP::Get(const std::string& IP) {
std::string Ret; std::string Ret;
if (res.error() == httplib::Error::Success) { if (res.error() == httplib::Error::Success) {
LOG(INFO) << res->status;
if (res->status == 200) { if (res->status == 200) {
Ret = res->body; Ret = res->body;
} else LOG(ERROR) << res->reason; } else LOG(ERROR) << res->reason;

View File

@ -63,6 +63,7 @@ class MyMainFrame : public wxFrame {
static void GameVersionLabel(); static void GameVersionLabel();
static inline MyAccountFrame* AccountFrame; static inline MyAccountFrame* AccountFrame;
static inline MyMainFrame* MainFrameInstance; static inline MyMainFrame* MainFrameInstance;
wxBitmapButton* BitAccount;
void OnClickAccount(wxCommandEvent& event); void OnClickAccount(wxCommandEvent& event);
private: private:
@ -359,6 +360,27 @@ std::string jsonRead () {
return ""; return "";
} }
/////////// Picture Type Function ///////////
// JPG 0 / PNG 1
std::string PictureType (const std::string& picture) {
for (int i = 0; i < 15; i++) {
if(picture[i] == 'J')
return ".jpg";
else if (picture[i] == 'P' && picture[i+1] == 'N')
return ".png";
}
return "";
}
/////////// Get Picture Name Function ///////////
std::string GetPictureName () {
for (const auto& entry : fs::recursive_directory_iterator("icons")) {
if (entry.path().filename().string().find(UIData::Username) != std::string::npos) {
return entry.path().string();
}
}
}
/////////// TestFrame Function /////////// /////////// TestFrame Function ///////////
/*MyTestFrame::MyTestFrame() : /*MyTestFrame::MyTestFrame() :
wxFrame(nullptr, wxID_ANY, "BeamMP Launcher V3", wxDefaultPosition,wxDefaultSize, wxFrame(nullptr, wxID_ANY, "BeamMP Launcher V3", wxDefaultPosition,wxDefaultSize,
@ -383,10 +405,6 @@ MyMainFrame::MyMainFrame() :
auto* HorizontalLine1 = new wxStaticLine(panel, wxID_ANY, wxPoint(10, 60), wxSize(950, 1)); auto* HorizontalLine1 = new wxStaticLine(panel, wxID_ANY, wxPoint(10, 60), wxSize(950, 1));
auto* HorizontalLine2 = new wxStaticLine(panel, wxID_ANY, wxPoint(10, 480), wxSize(950, 1)); auto* HorizontalLine2 = new wxStaticLine(panel, wxID_ANY, wxPoint(10, 480), wxSize(950, 1));
//PNG Handler:
auto *handler = new wxPNGHandler;
wxImage::AddHandler(handler);
//Hyperlinks: //Hyperlinks:
auto* HyperForum = new wxHyperlinkCtrl(panel, wxID_ANY, wxT("Forum"), wxT("https://forum.beammp.com"), wxPoint(10, 10)); auto* HyperForum = new wxHyperlinkCtrl(panel, wxID_ANY, wxT("Forum"), wxT("https://forum.beammp.com"), wxPoint(10, 10));
auto* txtSeparator1 = new wxStaticText(panel, wxID_ANY, wxT("|"), wxPoint(55, 10)); auto* txtSeparator1 = new wxStaticText(panel, wxID_ANY, wxT("|"), wxPoint(55, 10));
@ -432,13 +450,15 @@ MyMainFrame::MyMainFrame() :
auto* HorizontalLine3 = new wxStaticLine(panel, wxID_ANY, wxPoint(10, 550), wxSize(950, 1)); auto* HorizontalLine3 = new wxStaticLine(panel, wxID_ANY, wxPoint(10, 550), wxSize(950, 1));
//Account: wxInitAllImageHandlers();
auto* bitmap = new wxBitmapButton(panel, 1, wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH)), wxPoint(20, 560), wxSize(45,45));
if (UIData::LoginAuth && fs::exists( "icons/" + UIData::Username + ".png")) //Account:
bitmap->SetBitmap(wxBitmapBundle(wxImage( "icons/" + UIData::Username + ".png", wxBitmapType (wxBITMAP_TYPE_PNG | wxBITMAP_TYPE_JPEG)).Scale(45, 45, wxIMAGE_QUALITY_HIGH))); BitAccount = new wxBitmapButton(panel, 1, wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH)), wxPoint(20, 560), wxSize(45,45));
std::string PictureString = GetPictureName();
if (UIData::LoginAuth && fs::exists( PictureString))
BitAccount->SetBitmap(wxBitmapBundle(wxImage( PictureString).Scale(45, 45, wxIMAGE_QUALITY_HIGH)));
else else
bitmap->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH))); BitAccount->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45,45, wxIMAGE_QUALITY_HIGH)));
//Buttons: //Buttons:
auto btnSettings = new wxButton(panel, 2, wxT("Settings"), wxPoint(730,570), wxSize(110, 25)); auto btnSettings = new wxButton(panel, 2, wxT("Settings"), wxPoint(730,570), wxSize(110, 25));
@ -492,10 +512,10 @@ MyAccountFrame::MyAccountFrame() : wxFrame(nullptr, wxID_ANY, "Account Manager",
wxStaticBitmap *image; wxStaticBitmap *image;
image = new wxStaticBitmap( this, wxID_ANY, wxBitmapBundle(wxImage("icons/BeamMP_black.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)), wxPoint(180,20), wxSize(120, 120)); image = new wxStaticBitmap( this, wxID_ANY, wxBitmapBundle(wxImage("icons/BeamMP_black.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)), wxPoint(180,20), wxSize(120, 120));
auto* panel = new wxPanel(this, wxID_ANY, wxPoint(), wxSize(500,650)); auto* panel = new wxPanel(this, wxID_ANY, wxPoint(), wxSize(500,650));
std::string PictureString = GetPictureName();
if (UIData::LoginAuth) { if (UIData::LoginAuth) {
if (fs::exists( "icons/" + UIData::Username + ".png")) if (fs::exists( PictureString))
image->SetBitmap(wxBitmapBundle(wxImage( "icons/" + UIData::Username + ".png", wxBITMAP_TYPE_PNG).Scale(120, 120, wxIMAGE_QUALITY_HIGH))); image->SetBitmap(wxBitmapBundle(wxImage( PictureString).Scale(120, 120, wxIMAGE_QUALITY_HIGH)));
else else
image->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH))); image->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(120,120, wxIMAGE_QUALITY_HIGH)));
@ -680,23 +700,28 @@ void MyAccountFrame::OnClickRegister(wxCommandEvent& event WXUNUSED(event)) {
/////////// OnClick Login Event /////////// /////////// OnClick Login Event ///////////
void MyAccountFrame::OnClickLogin(wxCommandEvent& event WXUNUSED(event)) { void MyAccountFrame::OnClickLogin(wxCommandEvent& event WXUNUSED(event)) {
Json json; Json json;
json ["password"] = ctrlPassword->GetValue().utf8_string(); json ["password"] = ctrlPassword->GetValue().utf8_string();
json ["username"] = ctrlUsername->GetValue().utf8_string(); json ["username"] = ctrlUsername->GetValue().utf8_string();
if (Login(json.dump())) { if (Login(json.dump())) {
HTTP::Download("https://forum.beammp.com/user_avatar/forum.beammp.com/" + UIData::Username + "/240/4411_2.png", "icons/" + UIData::Username + ".png"); std::string picture = HTTP::Get("https://forum.beammp.com/user_avatar/forum.beammp.com/" + UIData::Username + "/240/4411_2.png");
std::ofstream File("icons/" + UIData::Username + PictureType(picture), std::ios::binary);
if (File.is_open()) {
File << picture;
File.close();
MyMainFrame::MainFrameInstance->BitAccount->SetBitmap(wxBitmapBundle(wxImage(GetPictureName()).Scale(45, 45, wxIMAGE_QUALITY_HIGH)));
}
MyMainFrame::AccountFrame->Destroy(); MyMainFrame::AccountFrame->Destroy();
MyMainFrame::MainFrameInstance->OnClickAccount(event); MyMainFrame::MainFrameInstance->OnClickAccount(event);
} }
} }
/////////// OnClick Logout Event /////////// /////////// OnClick Logout Event ///////////
void MyAccountFrame::OnClickLogout(wxCommandEvent& event WXUNUSED(event)) { void MyAccountFrame::OnClickLogout(wxCommandEvent& event WXUNUSED(event)) {
Login("LO"); Login("LO");
MyMainFrame::AccountFrame->Destroy(); MyMainFrame::AccountFrame->Destroy();
MyMainFrame::MainFrameInstance->BitAccount->SetBitmap(wxBitmapBundle(wxImage("icons/default.png", wxBITMAP_TYPE_PNG).Scale(45, 45, wxIMAGE_QUALITY_HIGH)));
MyMainFrame::MainFrameInstance->OnClickAccount(event); MyMainFrame::MainFrameInstance->OnClickAccount(event);
} }