From 3ab1aeb9ef785c9bf5adea7a93b0a9aa997e3f59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 5 Dec 2013 17:49:37 +0100 Subject: [PATCH] Install zip file screen: Add working progress bar for install. --- Core/Util/GameManager.cpp | 22 ++++++++++++++++------ Core/Util/GameManager.h | 9 ++++++--- UI/InstallZipScreen.cpp | 34 +++++++++++++++++++++++++++------- UI/InstallZipScreen.h | 7 ++++++- 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Core/Util/GameManager.cpp b/Core/Util/GameManager.cpp index bf8840d334..4aefcd6217 100644 --- a/Core/Util/GameManager.cpp +++ b/Core/Util/GameManager.cpp @@ -16,7 +16,8 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include "file/file_util.h" -#include "native/ext/libzip/zip.h" +#include "ext/libzip/zip.h" +#include "thread/thread.h" #include "util/text/utf8.h" #include "Common/Log.h" @@ -109,8 +110,7 @@ void GameManager::Update() { } } -// Does NOT delete the zip file, that is done by the function calling this, if applicable. -bool GameManager::InstallGame(std::string zipfile) { +bool GameManager::InstallGame(std::string zipfile, bool deleteAfter) { if (installInProgress_) { ERROR_LOG(HLE, "Cannot have two installs in progress at the same time"); return false; @@ -253,15 +253,25 @@ bool GameManager::InstallGame(std::string zipfile) { zip_close(z); installProgress_ = 1.0f; installInProgress_ = false; + if (deleteAfter) { + deleteFile(zipfile.c_str()); + } + InstallDone(); return true; } -bool GameManager::InstallGameOnThread(std::string zipFile) { +bool GameManager::InstallGameOnThread(std::string zipFile, bool deleteAfter) { if (installInProgress_) { return false; } - // TODO: Actually launch a thread - InstallGame(zipFile); + installThread_.reset(new std::thread(std::bind(&GameManager::InstallGame, this, zipFile, deleteAfter))); + installThread_->detach(); return true; } + +void GameManager::InstallDone() { + if (installThread_.get() != 0) { + installThread_.reset(); + } +} diff --git a/Core/Util/GameManager.h b/Core/Util/GameManager.h index bf9cafdd66..0a115e0822 100644 --- a/Core/Util/GameManager.h +++ b/Core/Util/GameManager.h @@ -22,6 +22,7 @@ #pragma once +#include "thread/thread.h" #include "net/http_client.h" class GameManager { @@ -47,13 +48,15 @@ public: } // Only returns false if there's already an installation in progress. - bool InstallGameOnThread(std::string zipFile); + bool InstallGameOnThread(std::string zipFile, bool deleteAfter); private: - bool InstallGame(std::string zipfile); + bool InstallGame(std::string zipfile, bool deleteAfter = false); + void InstallDone(); + std::string GetTempFilename() const; std::shared_ptr curDownload_; - + std::shared_ptr installThread_; bool installInProgress_; float installProgress_; }; diff --git a/UI/InstallZipScreen.cpp b/UI/InstallZipScreen.cpp index 4589189348..47e96c044f 100644 --- a/UI/InstallZipScreen.cpp +++ b/UI/InstallZipScreen.cpp @@ -33,6 +33,7 @@ void InstallZipScreen::CreateViews() { bool success = getFileInfo(zipPath_.c_str(), &fileInfo); I18NCategory *di = GetI18NCategory("Dialog"); + I18NCategory *iz = GetI18NCategory("InstallZip"); Margins actionMenuMargins(0, 100, 15, 0); @@ -41,26 +42,45 @@ void InstallZipScreen::CreateViews() { ViewGroup *leftColumn = new AnchorLayout(new LinearLayoutParams(1.0f)); root_->Add(leftColumn); - leftColumn->Add(new TextView(di->T("Install game from ZIP file?"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE))); + leftColumn->Add(new TextView(iz->T("Install game from ZIP file?"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE))); leftColumn->Add(new TextView(zipPath_, ALIGN_LEFT, false, new AnchorLayoutParams(10, 60, NONE, NONE))); + progressBar_ = leftColumn->Add(new ProgressBar(new AnchorLayoutParams(10, 200, 200, NONE))); ViewGroup *rightColumnItems = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(300, FILL_PARENT, actionMenuMargins)); root_->Add(rightColumnItems); - installChoice_ = rightColumnItems->Add(new Choice(di->T("Install"))); + installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install"))); installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall); - rightColumnItems->Add(new Choice(di->T("Back")))->OnClick.Handle(this, &UIScreen::OnOK); // OK so that EmuScreen will handle it right - rightColumnItems->Add(new CheckBox(&deleteZipFile_, di->T("Delete ZIP file"))); + backChoice_ = rightColumnItems->Add(new Choice(di->T("Back"))); + backChoice_->OnClick.Handle(this, &UIScreen::OnOK); // OK so that EmuScreen will handle it right + + rightColumnItems->Add(new CheckBox(&deleteZipFile_, iz->T("Delete ZIP file"))); +} + +void InstallZipScreen::key(const KeyInput &key) { + // Ignore all key presses during installation to avoid user escape + if (!g_GameManager.IsInstallInProgress()) { + UIScreen::key(key); + } } UI::EventReturn InstallZipScreen::OnInstall(UI::EventParams ¶ms) { - installChoice_->SetEnabled(false); - if (g_GameManager.InstallGameOnThread(zipPath_)) { - screenManager()->finishDialog(this, DR_OK); + if (g_GameManager.InstallGameOnThread(zipPath_, deleteZipFile_)) { + installStarted_ = true; + installChoice_->SetEnabled(false); } return UI::EVENT_DONE; } void InstallZipScreen::update(InputState &input) { + using namespace UI; + if (g_GameManager.IsInstallInProgress()) { + progressBar_->SetVisibility(V_VISIBLE); + progressBar_->SetProgress(g_GameManager.GetCurrentInstallProgress()); + backChoice_->SetEnabled(false); + } else { + progressBar_->SetVisibility(V_GONE); + backChoice_->SetEnabled(true); + } UIScreen::update(input); } diff --git a/UI/InstallZipScreen.h b/UI/InstallZipScreen.h index 7f362bf1a4..e6ab6b86c3 100644 --- a/UI/InstallZipScreen.h +++ b/UI/InstallZipScreen.h @@ -25,8 +25,9 @@ class InstallZipScreen : public UIDialogScreenWithBackground { public: - InstallZipScreen(std::string zipPath) : installChoice_(0), zipPath_(zipPath), deleteZipFile_(false) {} + InstallZipScreen(std::string zipPath) : installChoice_(0), zipPath_(zipPath), installStarted_(false), deleteZipFile_(false) {} virtual void update(InputState &input); + virtual void key(const KeyInput &key); protected: virtual void CreateViews(); @@ -35,7 +36,11 @@ private: UI::EventReturn OnInstall(UI::EventParams ¶ms); UI::Choice *installChoice_; + UI::Choice *backChoice_; + UI::ProgressBar *progressBar_; + UI::TextView *doneView_; std::string zipPath_; + bool installStarted_; bool deleteZipFile_; };