GUI: Initial code for Update Icons dialog

- LIST downloading and parsing is done
This commit is contained in:
Eugene Sandulenko 2021-11-16 23:36:21 +01:00
parent 768cac42f9
commit d8829fa4fd
No known key found for this signature in database
GPG Key ID: 014D387312D34F08
8 changed files with 373 additions and 1 deletions

209
gui/downloadiconsdialog.cpp Normal file
View File

@ -0,0 +1,209 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "backends/cloud/cloudmanager.h"
#include "gui/downloadiconsdialog.h"
#include "gui/downloaddialog.h"
#include "backends/networking/curl/session.h"
#include "common/config-manager.h"
#include "common/translation.h"
#include "common/util.h"
#include "engines/metaengine.h"
#include "gui/browser.h"
#include "gui/chooser.h"
#include "gui/editgamedialog.h"
#include "gui/gui-manager.h"
#include "gui/launcher.h"
#include "gui/message.h"
#include "gui/remotebrowser.h"
#include "gui/widgets/edittext.h"
#include "gui/widgets/list.h"
namespace GUI {
enum {
kDownloadIconsDialogButtonCmd = 'Dldb',
kListEndedCmd = 'DLLE'
};
static DownloadIconsDialog *g_dialog;
DownloadIconsDialog::DownloadIconsDialog() :
Dialog("GlobalOptions_DownloadIconsDialog"), CommandSender(this), _close(false) {
g_dialog = this;
_backgroundType = GUI::ThemeEngine::kDialogBackgroundPlain;
uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
_progressBar = new SliderWidget(this, "GlobalOptions_DownloadIconsDialog.ProgressBar");
_progressBar->setMinValue(0);
_progressBar->setMaxValue(100);
_progressBar->setValue(progress);
_progressBar->setEnabled(false);
_percentLabel = new StaticTextWidget(this, "GlobalOptions_DownloadIconsDialog.PercentText", Common::String::format("%u %%", progress));
_downloadSizeLabel = new StaticTextWidget(this, "GlobalOptions_DownloadIconsDialog.DownloadSize", Common::U32String());
_downloadSpeedLabel = new StaticTextWidget(this, "GlobalOptions_DownloadIconsDialog.DownloadSpeed", Common::U32String());
if (g_system->getOverlayWidth() > 320)
_cancelButton = new ButtonWidget(this, "GlobalOptions_DownloadIconsDialog.MainButton", _("Cancel download"), Common::U32String(), kDownloadIconsDialogButtonCmd);
else
_cancelButton = new ButtonWidget(this, "GlobalOptions_DownloadIconsDialog.MainButton", _c("Cancel download", "lowres"), Common::U32String(), kDownloadIconsDialogButtonCmd);
_closeButton = new ButtonWidget(this, "GlobalOptions_DownloadIconsDialog.CloseButton", _("Hide"), Common::U32String(), kCloseCmd);
refreshWidgets();
CloudMan.setDownloadTarget(this);
_session = new Networking::Session();
downloadList();
}
DownloadIconsDialog::~DownloadIconsDialog() {
CloudMan.setDownloadTarget(nullptr);
_session->close();
delete _session;
}
void DownloadIconsDialog::open() {
Dialog::open();
reflowLayout();
g_gui.scheduleTopDialogRedraw();
}
void DownloadIconsDialog::close() {
CloudMan.setDownloadTarget(nullptr);
Dialog::close();
}
void DownloadIconsDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
warning("CMD is: %s", tag2str(cmd));
switch (cmd) {
case kDownloadIconsDialogButtonCmd:
{
CloudMan.setDownloadTarget(nullptr);
CloudMan.cancelDownload();
close();
break;
}
case kDownloadProgressCmd:
if (!_close) {
refreshWidgets();
g_gui.scheduleTopDialogRedraw();
}
break;
case kDownloadEndedCmd:
_close = true;
break;
case kListEndedCmd:
warning("List download ended");
break;
default:
Dialog::handleCommand(sender, cmd, data);
}
}
void DownloadIconsDialog::handleTickle() {
if (_close) {
close();
_close = false;
return;
}
int32 progress = (int32)(100 * CloudMan.getDownloadingProgress());
if (_progressBar->getValue() != progress) {
refreshWidgets();
g_gui.scheduleTopDialogRedraw();
}
Dialog::handleTickle();
}
void DownloadIconsDialog::reflowLayout() {
Dialog::reflowLayout();
refreshWidgets();
}
Common::U32String DownloadIconsDialog::getSizeLabelText() {
Common::String downloaded, downloadedUnits, total, totalUnits;
downloaded = getHumanReadableBytes(CloudMan.getDownloadBytesNumber(), downloadedUnits);
total = getHumanReadableBytes(CloudMan.getDownloadTotalBytesNumber(), totalUnits);
return Common::U32String::format(_("Downloaded %s %S / %s %S"), downloaded.c_str(), _(downloadedUnits).c_str(), total.c_str(), _(totalUnits).c_str());
}
Common::U32String DownloadIconsDialog::getSpeedLabelText() {
Common::String speed, speedUnits;
speed = getHumanReadableBytes(CloudMan.getDownloadSpeed(), speedUnits);
speedUnits += "/s";
return Common::U32String::format(_("Download speed: %s %S"), speed.c_str(), _(speedUnits).c_str());
}
void DownloadIconsDialog::refreshWidgets() {
uint32 progress = (uint32)(100 * CloudMan.getDownloadingProgress());
_percentLabel->setLabel(Common::String::format("%u %%", progress));
_downloadSizeLabel->setLabel(getSizeLabelText());
_downloadSpeedLabel->setLabel(getSpeedLabelText());
_progressBar->setValue(progress);
}
void DownloadIconsDialog::downloadListCallback(Networking::DataResponse response) {
Networking::SessionRequest *req = dynamic_cast<Networking::SessionRequest *>(response.request);
Common::MemoryReadStream stream(req->getData(), req->getSize());
int nline = 0;
while (!stream.eos()) {
Common::String s = stream.readString('\n');
nline++;
if (s.empty())
continue;
size_t pos = s.findFirstOf(',');
if (pos == Common::String::npos) {
warning("DownloadIconsDialog: wrong string format at line %d: <%s>", nline, s.c_str());
continue;
}
g_dialog->_fileHash.setVal(s.substr(0, pos), atol(s.substr(pos + 1).c_str()));
}
sendCommand(kListEndedCmd, 0);
}
void DownloadIconsDialog::errorCallback(Networking::ErrorResponse error) {
warning("Error %ld: %s", error.httpResponseCode, error.response.c_str());
}
void DownloadIconsDialog::downloadList() {
Networking::SessionRequest *rq = _session->get("https://downloads.scummvm.org/frs/icons/LIST",
new Common::Callback<DownloadIconsDialog, Networking::DataResponse>(this, &DownloadIconsDialog::downloadListCallback),
new Common::Callback<DownloadIconsDialog, Networking::ErrorResponse>(this, &DownloadIconsDialog::errorCallback),
true);
rq->start();
}
} // End of namespace GUI

80
gui/downloadiconsdialog.h Normal file
View File

@ -0,0 +1,80 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef GUI_DOWNLOADICONSDIALOG_H
#define GUI_DOWNLOADICONSDIALOG_H
#include "gui/dialog.h"
#include "common/str.h"
#include "common/ustr.h"
namespace Networking {
class Session;
};
namespace GUI {
class CommandSender;
class StaticTextWidget;
class ButtonWidget;
class SliderWidget;
class DownloadIconsDialog : public Dialog, public CommandSender {
StaticTextWidget *_percentLabel;
StaticTextWidget *_downloadSizeLabel;
StaticTextWidget *_downloadSpeedLabel;
SliderWidget *_progressBar;
ButtonWidget *_cancelButton;
ButtonWidget *_closeButton;
Common::String _localDirectory;
bool _close;
Common::U32String getSizeLabelText();
Common::U32String getSpeedLabelText();
void refreshWidgets();
Networking::Session *_session;
public:
Common::HashMap<Common::String, uint32> _fileHash;
public:
DownloadIconsDialog();
~DownloadIconsDialog() override;
void open() override;
void close() override;
void handleCommand(CommandSender *sender, uint32 cmd, uint32 data) override;
void handleTickle() override;
void reflowLayout() override;
void downloadListCallback(Networking::DataResponse response);
void errorCallback(Networking::ErrorResponse error);
private:
void downloadList();
};
} // End of namespace GUI
#endif

View File

@ -46,6 +46,7 @@ ifdef USE_CLOUD
ifdef USE_LIBCURL
MODULE_OBJS += \
downloaddialog.o \
downloadiconsdialog.o \
remotebrowser.o
endif
endif

View File

@ -57,6 +57,7 @@
#ifdef USE_LIBCURL
#include "backends/cloud/cloudmanager.h"
#include "gui/downloaddialog.h"
#include "gui/downloadiconsdialog.h"
#endif
#ifdef USE_SDL_NET
@ -91,6 +92,7 @@ enum {
kChoosePluginsDirCmd = 'chpl',
kPluginsPathClearCmd = 'clpl',
kChooseThemeCmd = 'chtf',
kUpdateIconsCmd = 'upic',
kUpdatesCheckCmd = 'updc',
kKbdMouseSpeedChanged = 'kmsc',
kJoystickDeadzoneChanged= 'jodc',
@ -2414,6 +2416,12 @@ void GlobalOptionsDialog::addMiscControls(GuiObject *boss, const Common::String
new ButtonWidget(boss, prefix + "UpdatesCheckManuallyButton", _("Check now"), Common::U32String(), kUpdatesCheckCmd);
}
#endif // USE_UPDATES
#ifdef USE_CLOUD
#ifdef USE_LIBCURL
new ButtonWidget(boss, prefix + "UpdateIconsButton", _("Update Icons"), Common::U32String(), kUpdateIconsCmd);
#endif
#endif
}
#ifdef USE_CLOUD
@ -2691,7 +2699,7 @@ void GlobalOptionsDialog::apply() {
#ifdef USE_UPDATES
if (g_system->getUpdateManager()) {
ConfMan.setInt("updates_check", _updatesPopUp->getSelectedTag());
if (_updatesPopUp->getSelectedTag() == Common::UpdateManager::kUpdateIntervalNotSupported) {
g_system->getUpdateManager()->setAutomaticallyChecksForUpdates(Common::UpdateManager::kUpdateStateDisabled);
} else {
@ -2932,6 +2940,16 @@ void GlobalOptionsDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3
}
break;
}
case kUpdateIconsCmd: {
DownloadIconsDialog dia;
if (dia.runModal() > 0) {
warning("Success");
}
break;
}
#endif
#endif
case kThemePathClearCmd:

Binary file not shown.

Binary file not shown.

View File

@ -816,6 +816,9 @@
type = 'Checkbox'
/>
</layout>
<widget name = 'UpdateIconsButton'
type = 'Button'
/>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' align = 'center'>
<widget name = 'DiscordRpc'
type = 'Checkbox'
@ -1020,6 +1023,35 @@
</layout>
</dialog>
<dialog name = 'GlobalOptions_DownloadIconsDialog' overlays = 'Dialog.GlobalOptions'>
<layout type = 'vertical' padding = '16, 16, 16, 8' spacing = '8'>
<widget name = 'ProgressBar'
height = 'Globals.Button.Height'
/>
<space size = '1'/>
<widget name = 'PercentText'
height = 'Globals.Line.Height'
textalign = 'center'
/>
<widget name = 'DownloadSize'
height = 'Globals.Line.Height'
/>
<widget name = 'DownloadSpeed'
height = 'Globals.Line.Height'
/>
<space/>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10'>
<widget name = 'MainButton'
type = 'Button'
/>
<space/>
<widget name = 'CloseButton'
type = 'Button'
/>
</layout>
</layout>
</dialog>
<dialog name = 'GlobalOptions_Cloud_DownloadDialog' overlays = 'Dialog.GlobalOptions'>
<layout type = 'vertical' padding = '16, 16, 16, 8' spacing = '8'>
<widget name = 'RemoteDirectory'

View File

@ -789,6 +789,9 @@
type = 'Checkbox'
/>
</layout>
<widget name = 'UpdateIconsButton'
type = 'Button'
/>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '10' align = 'center'>
<widget name = 'DiscordRpc'
type = 'Checkbox'
@ -998,6 +1001,35 @@
</layout>
</dialog>
<dialog name = 'GlobalOptions_DownloadIconsDialog' overlays = 'Dialog.GlobalOptions'>
<layout type = 'vertical' padding = '8, 8, 8, 4' spacing = '8'>
<widget name = 'ProgressBar'
height = 'Globals.Button.Height'
/>
<space size = '1'/>
<widget name = 'PercentText'
height = 'Globals.Line.Height'
textalign = 'center'
/>
<widget name = 'DownloadSize'
height = 'Globals.Line.Height'
/>
<widget name = 'DownloadSpeed'
height = 'Globals.Line.Height'
/>
<space/>
<layout type = 'horizontal' padding = '0, 0, 0, 0' spacing = '6'>
<widget name = 'MainButton'
type = 'Button'
/>
<space/>
<widget name = 'CloseButton'
type = 'Button'
/>
</layout>
</layout>
</dialog>
<dialog name = 'GlobalOptions_Cloud_DownloadDialog' overlays = 'Dialog.GlobalOptions'>
<layout type = 'vertical' padding = '8, 8, 8, 4' spacing = '8'>
<widget name = 'RemoteDirectory'