1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "gui/downloaddialog.h"
24 #include "backends/cloud/cloudmanager.h"
25 #include "common/config-manager.h"
26 #include "common/translation.h"
27 #include "common/util.h"
28 #include "engines/metaengine.h"
29 #include "gui/browser.h"
30 #include "gui/chooser.h"
31 #include "gui/editgamedialog.h"
32 #include "gui/gui-manager.h"
33 #include "gui/launcher.h"
34 #include "gui/message.h"
35 #include "gui/remotebrowser.h"
36 #include "gui/widgets/edittext.h"
37 #include "gui/widgets/list.h"
38 
39 namespace GUI {
40 
41 enum {
42 	kDownloadDialogButtonCmd = 'Dldb'
43 };
44 
DownloadDialog(uint32 storageId,LauncherDialog * launcher)45 DownloadDialog::DownloadDialog(uint32 storageId, LauncherDialog *launcher) :
46 	Dialog("GlobalOptions_Cloud_DownloadDialog"), _launcher(launcher), _close(false) {
47 	_backgroundType = GUI::ThemeEngine::kDialogBackgroundPlain;
48 
49 	_browser = new BrowserDialog(_("Select directory where to download game data"), true);
50 	_remoteBrowser = new RemoteBrowserDialog(_("Select directory with game data"));
51 
52 	_remoteDirectoryLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.RemoteDirectory", _("From: "));
53 	_localDirectoryLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.LocalDirectory", _("To: "));
54 	uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
55 	_progressBar = new SliderWidget(this, "GlobalOptions_Cloud_DownloadDialog.ProgressBar");
56 	_progressBar->setMinValue(0);
57 	_progressBar->setMaxValue(100);
58 	_progressBar->setValue(progress);
59 	_progressBar->setEnabled(false);
60 	_percentLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.PercentText", Common::String::format("%u %%", progress));
61 	_downloadSizeLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.DownloadSize", "");
62 	_downloadSpeedLabel = new StaticTextWidget(this, "GlobalOptions_Cloud_DownloadDialog.DownloadSpeed", "");
63 	if (g_system->getOverlayWidth() > 320)
64 		_cancelButton = new ButtonWidget(this, "GlobalOptions_Cloud_DownloadDialog.MainButton", _("Cancel download"), 0, kDownloadDialogButtonCmd);
65 	else
66 		_cancelButton = new ButtonWidget(this, "GlobalOptions_Cloud_DownloadDialog.MainButton", _c("Cancel download", "lowres"), 0, kDownloadDialogButtonCmd);
67 
68 	_closeButton = new ButtonWidget(this, "GlobalOptions_Cloud_DownloadDialog.CloseButton", _("Hide"), 0, kCloseCmd);
69 	refreshWidgets();
70 
71 	CloudMan.setDownloadTarget(this);
72 }
73 
~DownloadDialog()74 DownloadDialog::~DownloadDialog() {
75 	CloudMan.setDownloadTarget(nullptr);
76 }
77 
open()78 void DownloadDialog::open() {
79 	Dialog::open();
80 	CloudMan.setDownloadTarget(this);
81 	if (!CloudMan.isDownloading())
82 		if (!selectDirectories())
83 			close();
84 	reflowLayout();
85 	g_gui.scheduleTopDialogRedraw();
86 }
87 
close()88 void DownloadDialog::close() {
89 	CloudMan.setDownloadTarget(nullptr);
90 	Dialog::close();
91 }
92 
handleCommand(CommandSender * sender,uint32 cmd,uint32 data)93 void DownloadDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
94 	switch (cmd) {
95 	case kDownloadDialogButtonCmd:
96 		{
97 			CloudMan.setDownloadTarget(nullptr);
98 			CloudMan.cancelDownload();
99 			close();
100 			break;
101 		}
102 	case kDownloadProgressCmd:
103 		if (!_close) {
104 			refreshWidgets();
105 			g_gui.scheduleTopDialogRedraw();
106 		}
107 		break;
108 	case kDownloadEndedCmd:
109 		_close = true;
110 		break;
111 	default:
112 		Dialog::handleCommand(sender, cmd, data);
113 	}
114 }
115 
selectDirectories()116 bool DownloadDialog::selectDirectories() {
117 	if (g_system->isConnectionLimited()) {
118 		MessageDialog alert(_("It looks like your connection is limited. "
119 			"Do you really want to download files with it?"), _("Yes"), _("No"));
120 		if (alert.runModal() != GUI::kMessageOK)
121 			return false;
122 	}
123 
124 	//first user should select remote directory to download
125 	if (_remoteBrowser->runModal() <= 0)
126 		return false;
127 
128 	Cloud::StorageFile remoteDirectory = _remoteBrowser->getResult();
129 
130 	//now user should select local directory to download into
131 	if (_browser->runModal() <= 0)
132 		return false;
133 
134 	Common::FSNode dir(_browser->getResult());
135 	Common::FSList files;
136 	if (!dir.getChildren(files, Common::FSNode::kListAll)) {
137 		MessageDialog alert(_("ScummVM couldn't open the specified directory!"));
138 		alert.runModal();
139 		return false;
140 	}
141 
142 	//check that there is no file with the remote directory's name in the local one
143 	for (Common::FSList::iterator i = files.begin(); i != files.end(); ++i) {
144 		if (i->getName().equalsIgnoreCase(remoteDirectory.name())) {
145 			//if there is, ask user whether it's OK
146 			if (!i->isDirectory()) {
147 				GUI::MessageDialog alert(_("Cannot create a directory to download - the specified directory has a file with the same name."), _("OK"));
148 				alert.runModal();
149 				return false;
150 			}
151 			GUI::MessageDialog alert(
152 				Common::String::format(_("The \"%s\" already exists in the specified directory.\nDo you really want to download files into that directory?"), remoteDirectory.name().c_str()),
153 				_("Yes"),
154 				_("No")
155 				);
156 			if (alert.runModal() != GUI::kMessageOK)
157 				return false;
158 			break;
159 		}
160 	}
161 
162 	//make a local path
163 	Common::String localPath = dir.getPath();
164 
165 	//simple heuristic to determine which path separator to use
166 	if (localPath.size() && localPath.lastChar() != '/' && localPath.lastChar() != '\\') {
167 		int backslashes = 0;
168 		for (uint32 i = 0; i < localPath.size(); ++i)
169 			if (localPath[i] == '/')
170 				--backslashes;
171 			else if (localPath[i] == '\\')
172 				++backslashes;
173 
174 		if (backslashes > 0)
175 			localPath += '\\' + remoteDirectory.name();
176 		else
177 			localPath += '/' + remoteDirectory.name();
178 	} else {
179 		localPath += remoteDirectory.name();
180 	}
181 
182 	CloudMan.startDownload(remoteDirectory.path(), localPath);
183 	CloudMan.setDownloadTarget(this);
184 	_localDirectory = localPath;
185 	return true;
186 }
187 
handleTickle()188 void DownloadDialog::handleTickle() {
189 	if (_close) {
190 		if (_launcher)
191 			_launcher->doGameDetection(_localDirectory);
192 		close();
193 		_close = false;
194 		return;
195 	}
196 
197 	int32 progress = (int32)(100 * CloudMan.getDownloadingProgress());
198 	if (_progressBar->getValue() != progress) {
199 		refreshWidgets();
200 		g_gui.scheduleTopDialogRedraw();
201 	}
202 
203 	Dialog::handleTickle();
204 }
205 
reflowLayout()206 void DownloadDialog::reflowLayout() {
207 	Dialog::reflowLayout();
208 	refreshWidgets();
209 }
210 
getSizeLabelText()211 Common::String DownloadDialog::getSizeLabelText() {
212 	Common::String downloaded, downloadedUnits, total, totalUnits;
213 	downloaded = getHumanReadableBytes(CloudMan.getDownloadBytesNumber(), downloadedUnits);
214 	total = getHumanReadableBytes(CloudMan.getDownloadTotalBytesNumber(), totalUnits);
215 	return Common::String::format(_("Downloaded %s %s / %s %s"), downloaded.c_str(), _(downloadedUnits.c_str()), total.c_str(), _(totalUnits.c_str()));
216 }
217 
getSpeedLabelText()218 Common::String DownloadDialog::getSpeedLabelText() {
219 	Common::String speed, speedUnits;
220 	speed = getHumanReadableBytes(CloudMan.getDownloadSpeed(), speedUnits);
221 	speedUnits += "/s";
222 	return Common::String::format(_("Download speed: %s %s"), speed.c_str(), _(speedUnits.c_str()));
223 }
224 
refreshWidgets()225 void DownloadDialog::refreshWidgets() {
226 	_localDirectory = CloudMan.getDownloadLocalDirectory();
227 	_remoteDirectoryLabel->setLabel(_("From: ") + CloudMan.getDownloadRemoteDirectory());
228 	_localDirectoryLabel->setLabel(_("To: ") + _localDirectory);
229 	uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
230 	_percentLabel->setLabel(Common::String::format("%u %%", progress));
231 	_downloadSizeLabel->setLabel(getSizeLabelText());
232 	_downloadSpeedLabel->setLabel(getSpeedLabelText());
233 	_progressBar->setValue(progress);
234 }
235 
236 } // End of namespace GUI
237