CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/UI/InstallZipScreen.cpp
Views: 1401
// Copyright (c) 2013- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include "Common/UI/UI.h"18#include "Common/UI/View.h"19#include "Common/UI/ViewGroup.h"2021#include "Common/StringUtils.h"22#include "Common/File/FileUtil.h"23#include "Common/Data/Text/I18n.h"24#include "Common/Data/Text/Parsers.h"25#include "Core/Config.h"26#include "Core/System.h"27#include "Core/Util/GameManager.h"28#include "Core/Loaders.h"29#include "UI/InstallZipScreen.h"30#include "UI/MainScreen.h"31#include "UI/OnScreenDisplay.h"32#include "UI/SavedataScreen.h"3334InstallZipScreen::InstallZipScreen(const Path &zipPath) : zipPath_(zipPath) {35g_GameManager.ResetInstallError();36}3738void InstallZipScreen::CreateViews() {39using namespace UI;4041File::FileInfo fileInfo;42bool success = File::GetFileInfo(zipPath_, &fileInfo);4344auto di = GetI18NCategory(I18NCat::DIALOG);45auto iz = GetI18NCategory(I18NCat::INSTALLZIP);46auto er = GetI18NCategory(I18NCat::ERRORS);47auto ga = GetI18NCategory(I18NCat::GAME);4849Margins actionMenuMargins(0, 100, 15, 0);5051root_ = new LinearLayout(ORIENT_HORIZONTAL);5253ViewGroup *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f, Margins(12)));54ViewGroup *rightColumnItems = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(300, FILL_PARENT, actionMenuMargins));55root_->Add(leftColumn);56root_->Add(rightColumnItems);5758std::string shortFilename = zipPath_.GetFilename();5960// TODO: Do in the background?61struct zip *z = ZipOpenPath(zipPath_);6263bool showDeleteCheckbox = false;64returnToHomebrew_ = false;65installChoice_ = nullptr;66doneView_ = nullptr;67installChoice_ = nullptr;68existingSaveView_ = nullptr;69destFolders_.clear();7071std::vector<Path> destOptions;7273if (z) {74DetectZipFileContents(z, &zipFileInfo_); // Even if this fails, it sets zipInfo->contents.75if (zipFileInfo_.contents == ZipFileContents::ISO_FILE || zipFileInfo_.contents == ZipFileContents::PSP_GAME_DIR) {76std::string_view question = iz->T("Install game from ZIP file?");7778leftColumn->Add(new TextView(question));79leftColumn->Add(new TextView(shortFilename));80if (!zipFileInfo_.contentName.empty()) {81leftColumn->Add(new TextView(zipFileInfo_.contentName));82}8384doneView_ = leftColumn->Add(new TextView(""));8586if (zipFileInfo_.contents == ZipFileContents::ISO_FILE) {87const bool isInDownloads = File::IsProbablyInDownloadsFolder(zipPath_);88Path parent;89if (!isInDownloads && zipPath_.CanNavigateUp()) {90parent = zipPath_.NavigateUp();91destFolders_.push_back(parent);92}93if (g_Config.currentDirectory.IsLocalType() && File::Exists(g_Config.currentDirectory) && g_Config.currentDirectory != parent) {94destFolders_.push_back(g_Config.currentDirectory);95}96destFolders_.push_back(g_Config.memStickDirectory);97} else {98destFolders_.push_back(GetSysDirectory(DIRECTORY_GAME));99}100101installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));102installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);103returnToHomebrew_ = true;104showDeleteCheckbox = true;105} else if (zipFileInfo_.contents == ZipFileContents::TEXTURE_PACK) {106std::string_view question = iz->T("Install textures from ZIP file?");107leftColumn->Add(new TextView(question));108leftColumn->Add(new TextView(shortFilename));109110doneView_ = leftColumn->Add(new TextView(""));111112installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));113installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);114backChoice_ = rightColumnItems->Add(new Choice(di->T("Back")));115116showDeleteCheckbox = true;117} else if (zipFileInfo_.contents == ZipFileContents::SAVE_DATA) {118std::string_view question = iz->T("Import savedata from ZIP file");119leftColumn->Add(new TextView(question))->SetBig(true);120leftColumn->Add(new TextView(zipFileInfo_.gameTitle + ": " + zipFileInfo_.savedataDir));121122Path savedataDir = GetSysDirectory(DIRECTORY_SAVEDATA);123bool overwrite = !CanExtractWithoutOverwrite(z, savedataDir, 50);124125destFolders_.push_back(savedataDir);126127if (overwrite) {128leftColumn->Add(new NoticeView(NoticeLevel::WARN, di->T("Confirm Overwrite"), ""));129}130131int columnWidth = 300;132133LinearLayout *compareColumns = leftColumn->Add(new LinearLayout(UI::ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));134LinearLayout *leftCompare = new LinearLayout(UI::ORIENT_VERTICAL);135leftCompare->Add(new TextView(iz->T("Data to import")));136compareColumns->Add(leftCompare);137leftCompare->Add(new SavedataView(*screenManager()->getUIContext(), Path(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY,138zipFileInfo_.gameTitle, zipFileInfo_.savedataTitle, zipFileInfo_.savedataDetails, NiceSizeFormat(zipFileInfo_.totalFileSize), zipFileInfo_.mTime, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));139140// Check for potential overwrite at destination, and ask the user if it's OK to overwrite.141if (overwrite) {142savedataToOverwrite_ = savedataDir / zipFileInfo_.savedataDir;143std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);144145LinearLayout *rightCompare = new LinearLayout(UI::ORIENT_VERTICAL);146rightCompare->Add(new TextView(iz->T("Existing data")));147148compareColumns->Add(rightCompare);149existingSaveView_ = rightCompare->Add(new SavedataView(*screenManager()->getUIContext(), ginfo.get(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));150if (System_GetPropertyBool(SYSPROP_CAN_SHOW_FILE)) {151rightCompare->Add(new Button(ga->T("Show In Folder")))->OnClick.Add([=](UI::EventParams &) {152System_ShowFileInFolder(savedataToOverwrite_);153return UI::EVENT_DONE;154});155}156}157158installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));159installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);160161doneView_ = leftColumn->Add(new TextView(""));162showDeleteCheckbox = true;163} else {164leftColumn->Add(new TextView(iz->T("Zip file does not contain PSP software"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));165}166} else {167leftColumn->Add(new TextView(er->T("Error reading file"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));168}169170if (destFolders_.size() > 1) {171leftColumn->Add(new TextView(iz->T("Install into folder")));172for (int i = 0; i < (int)destFolders_.size(); i++) {173leftColumn->Add(new RadioButton(&destFolderChoice_, i, destFolders_[i].ToVisualString()));174}175} else if (destFolders_.size() == 1 && zipFileInfo_.contents != ZipFileContents::SAVE_DATA) {176leftColumn->Add(new TextView(StringFromFormat("%s %s", iz->T_cstr("Install into folder:"), destFolders_[0].ToVisualString().c_str())));177}178179// OK so that EmuScreen will handle it right.180backChoice_ = rightColumnItems->Add(new Choice(di->T("Back")));181backChoice_->OnClick.Handle<UIScreen>(this, &UIScreen::OnOK);182183if (showDeleteCheckbox) {184rightColumnItems->Add(new CheckBox(&deleteZipFile_, iz->T("Delete ZIP file")));185}186}187188bool InstallZipScreen::key(const KeyInput &key) {189// Ignore all key presses during download and installation to avoid user escape190if (g_GameManager.GetState() == GameManagerState::IDLE) {191return UIDialogScreen::key(key);192}193return false;194}195196UI::EventReturn InstallZipScreen::OnInstall(UI::EventParams ¶ms) {197ZipFileTask task;198task.url = zipPath_;199task.fileName = zipPath_;200task.deleteAfter = deleteZipFile_;201task.zipFileInfo = zipFileInfo_;202if (!destFolders_.empty() && destFolderChoice_ < destFolders_.size()) {203task.destination = destFolders_[destFolderChoice_];204}205if (g_GameManager.InstallZipOnThread(task)) {206installStarted_ = true;207if (installChoice_) {208installChoice_->SetEnabled(false);209}210}211return UI::EVENT_DONE;212}213214void InstallZipScreen::update() {215auto iz = GetI18NCategory(I18NCat::INSTALLZIP);216217using namespace UI;218if (g_GameManager.GetState() != GameManagerState::IDLE) {219if (backChoice_) {220backChoice_->SetEnabled(false);221}222} else {223if (backChoice_) {224backChoice_->SetEnabled(true);225}226std::string err = g_GameManager.GetInstallError();227if (!err.empty()) {228if (doneView_)229doneView_->SetText(iz->T(err.c_str()));230} else if (installStarted_) {231if (doneView_)232doneView_->SetText(iz->T("Installed!"));233MainScreen::showHomebrewTab = returnToHomebrew_;234}235}236237if (existingSaveView_) {238std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);239existingSaveView_->UpdateGame(ginfo.get());240}241UIDialogScreenWithBackground::update();242}243244245