CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/InstallZipScreen.cpp
Views: 1401
1
// Copyright (c) 2013- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/UI/UI.h"
19
#include "Common/UI/View.h"
20
#include "Common/UI/ViewGroup.h"
21
22
#include "Common/StringUtils.h"
23
#include "Common/File/FileUtil.h"
24
#include "Common/Data/Text/I18n.h"
25
#include "Common/Data/Text/Parsers.h"
26
#include "Core/Config.h"
27
#include "Core/System.h"
28
#include "Core/Util/GameManager.h"
29
#include "Core/Loaders.h"
30
#include "UI/InstallZipScreen.h"
31
#include "UI/MainScreen.h"
32
#include "UI/OnScreenDisplay.h"
33
#include "UI/SavedataScreen.h"
34
35
InstallZipScreen::InstallZipScreen(const Path &zipPath) : zipPath_(zipPath) {
36
g_GameManager.ResetInstallError();
37
}
38
39
void InstallZipScreen::CreateViews() {
40
using namespace UI;
41
42
File::FileInfo fileInfo;
43
bool success = File::GetFileInfo(zipPath_, &fileInfo);
44
45
auto di = GetI18NCategory(I18NCat::DIALOG);
46
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
47
auto er = GetI18NCategory(I18NCat::ERRORS);
48
auto ga = GetI18NCategory(I18NCat::GAME);
49
50
Margins actionMenuMargins(0, 100, 15, 0);
51
52
root_ = new LinearLayout(ORIENT_HORIZONTAL);
53
54
ViewGroup *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f, Margins(12)));
55
ViewGroup *rightColumnItems = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(300, FILL_PARENT, actionMenuMargins));
56
root_->Add(leftColumn);
57
root_->Add(rightColumnItems);
58
59
std::string shortFilename = zipPath_.GetFilename();
60
61
// TODO: Do in the background?
62
struct zip *z = ZipOpenPath(zipPath_);
63
64
bool showDeleteCheckbox = false;
65
returnToHomebrew_ = false;
66
installChoice_ = nullptr;
67
doneView_ = nullptr;
68
installChoice_ = nullptr;
69
existingSaveView_ = nullptr;
70
destFolders_.clear();
71
72
std::vector<Path> destOptions;
73
74
if (z) {
75
DetectZipFileContents(z, &zipFileInfo_); // Even if this fails, it sets zipInfo->contents.
76
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE || zipFileInfo_.contents == ZipFileContents::PSP_GAME_DIR) {
77
std::string_view question = iz->T("Install game from ZIP file?");
78
79
leftColumn->Add(new TextView(question));
80
leftColumn->Add(new TextView(shortFilename));
81
if (!zipFileInfo_.contentName.empty()) {
82
leftColumn->Add(new TextView(zipFileInfo_.contentName));
83
}
84
85
doneView_ = leftColumn->Add(new TextView(""));
86
87
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE) {
88
const bool isInDownloads = File::IsProbablyInDownloadsFolder(zipPath_);
89
Path parent;
90
if (!isInDownloads && zipPath_.CanNavigateUp()) {
91
parent = zipPath_.NavigateUp();
92
destFolders_.push_back(parent);
93
}
94
if (g_Config.currentDirectory.IsLocalType() && File::Exists(g_Config.currentDirectory) && g_Config.currentDirectory != parent) {
95
destFolders_.push_back(g_Config.currentDirectory);
96
}
97
destFolders_.push_back(g_Config.memStickDirectory);
98
} else {
99
destFolders_.push_back(GetSysDirectory(DIRECTORY_GAME));
100
}
101
102
installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));
103
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
104
returnToHomebrew_ = true;
105
showDeleteCheckbox = true;
106
} else if (zipFileInfo_.contents == ZipFileContents::TEXTURE_PACK) {
107
std::string_view question = iz->T("Install textures from ZIP file?");
108
leftColumn->Add(new TextView(question));
109
leftColumn->Add(new TextView(shortFilename));
110
111
doneView_ = leftColumn->Add(new TextView(""));
112
113
installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));
114
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
115
backChoice_ = rightColumnItems->Add(new Choice(di->T("Back")));
116
117
showDeleteCheckbox = true;
118
} else if (zipFileInfo_.contents == ZipFileContents::SAVE_DATA) {
119
std::string_view question = iz->T("Import savedata from ZIP file");
120
leftColumn->Add(new TextView(question))->SetBig(true);
121
leftColumn->Add(new TextView(zipFileInfo_.gameTitle + ": " + zipFileInfo_.savedataDir));
122
123
Path savedataDir = GetSysDirectory(DIRECTORY_SAVEDATA);
124
bool overwrite = !CanExtractWithoutOverwrite(z, savedataDir, 50);
125
126
destFolders_.push_back(savedataDir);
127
128
if (overwrite) {
129
leftColumn->Add(new NoticeView(NoticeLevel::WARN, di->T("Confirm Overwrite"), ""));
130
}
131
132
int columnWidth = 300;
133
134
LinearLayout *compareColumns = leftColumn->Add(new LinearLayout(UI::ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
135
LinearLayout *leftCompare = new LinearLayout(UI::ORIENT_VERTICAL);
136
leftCompare->Add(new TextView(iz->T("Data to import")));
137
compareColumns->Add(leftCompare);
138
leftCompare->Add(new SavedataView(*screenManager()->getUIContext(), Path(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY,
139
zipFileInfo_.gameTitle, zipFileInfo_.savedataTitle, zipFileInfo_.savedataDetails, NiceSizeFormat(zipFileInfo_.totalFileSize), zipFileInfo_.mTime, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));
140
141
// Check for potential overwrite at destination, and ask the user if it's OK to overwrite.
142
if (overwrite) {
143
savedataToOverwrite_ = savedataDir / zipFileInfo_.savedataDir;
144
std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);
145
146
LinearLayout *rightCompare = new LinearLayout(UI::ORIENT_VERTICAL);
147
rightCompare->Add(new TextView(iz->T("Existing data")));
148
149
compareColumns->Add(rightCompare);
150
existingSaveView_ = rightCompare->Add(new SavedataView(*screenManager()->getUIContext(), ginfo.get(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));
151
if (System_GetPropertyBool(SYSPROP_CAN_SHOW_FILE)) {
152
rightCompare->Add(new Button(ga->T("Show In Folder")))->OnClick.Add([=](UI::EventParams &) {
153
System_ShowFileInFolder(savedataToOverwrite_);
154
return UI::EVENT_DONE;
155
});
156
}
157
}
158
159
installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));
160
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
161
162
doneView_ = leftColumn->Add(new TextView(""));
163
showDeleteCheckbox = true;
164
} else {
165
leftColumn->Add(new TextView(iz->T("Zip file does not contain PSP software"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));
166
}
167
} else {
168
leftColumn->Add(new TextView(er->T("Error reading file"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));
169
}
170
171
if (destFolders_.size() > 1) {
172
leftColumn->Add(new TextView(iz->T("Install into folder")));
173
for (int i = 0; i < (int)destFolders_.size(); i++) {
174
leftColumn->Add(new RadioButton(&destFolderChoice_, i, destFolders_[i].ToVisualString()));
175
}
176
} else if (destFolders_.size() == 1 && zipFileInfo_.contents != ZipFileContents::SAVE_DATA) {
177
leftColumn->Add(new TextView(StringFromFormat("%s %s", iz->T_cstr("Install into folder:"), destFolders_[0].ToVisualString().c_str())));
178
}
179
180
// OK so that EmuScreen will handle it right.
181
backChoice_ = rightColumnItems->Add(new Choice(di->T("Back")));
182
backChoice_->OnClick.Handle<UIScreen>(this, &UIScreen::OnOK);
183
184
if (showDeleteCheckbox) {
185
rightColumnItems->Add(new CheckBox(&deleteZipFile_, iz->T("Delete ZIP file")));
186
}
187
}
188
189
bool InstallZipScreen::key(const KeyInput &key) {
190
// Ignore all key presses during download and installation to avoid user escape
191
if (g_GameManager.GetState() == GameManagerState::IDLE) {
192
return UIDialogScreen::key(key);
193
}
194
return false;
195
}
196
197
UI::EventReturn InstallZipScreen::OnInstall(UI::EventParams &params) {
198
ZipFileTask task;
199
task.url = zipPath_;
200
task.fileName = zipPath_;
201
task.deleteAfter = deleteZipFile_;
202
task.zipFileInfo = zipFileInfo_;
203
if (!destFolders_.empty() && destFolderChoice_ < destFolders_.size()) {
204
task.destination = destFolders_[destFolderChoice_];
205
}
206
if (g_GameManager.InstallZipOnThread(task)) {
207
installStarted_ = true;
208
if (installChoice_) {
209
installChoice_->SetEnabled(false);
210
}
211
}
212
return UI::EVENT_DONE;
213
}
214
215
void InstallZipScreen::update() {
216
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
217
218
using namespace UI;
219
if (g_GameManager.GetState() != GameManagerState::IDLE) {
220
if (backChoice_) {
221
backChoice_->SetEnabled(false);
222
}
223
} else {
224
if (backChoice_) {
225
backChoice_->SetEnabled(true);
226
}
227
std::string err = g_GameManager.GetInstallError();
228
if (!err.empty()) {
229
if (doneView_)
230
doneView_->SetText(iz->T(err.c_str()));
231
} else if (installStarted_) {
232
if (doneView_)
233
doneView_->SetText(iz->T("Installed!"));
234
MainScreen::showHomebrewTab = returnToHomebrew_;
235
}
236
}
237
238
if (existingSaveView_) {
239
std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);
240
existingSaveView_->UpdateGame(ginfo.get());
241
}
242
UIDialogScreenWithBackground::update();
243
}
244
245