1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-29 13:42:11 +00:00
nxdumptool/include/views/data_transfer_task_frame.hpp

178 lines
6.9 KiB
C++
Raw Normal View History

Add DataTransferTaskFrame and GameCardImageDumpTaskFrame classes DataTransferTaskFrame is a template class that's derived from brls::AppletFrame, which automatically starts a background task using an internal object belonging to a class derived from DataTransferTask. A DataTransferProgressDisplay view is used to show progress updates. If the background task hits an error, the class takes care of switching to an ErrorFrame view with the corresponding error message. GameCardImageDumpTaskFrame is a derived class of DataTransferTaskFrame that uses a GameCardImageDumpTask object as its background task. In layman's terms, this provides a way to fully dump gamecard images using the new UI. DataTransferTaskFrame depends on the newly added is_base_template helper from goneskiing to check if the class for the provided task is derived from DataTransferTask. Other changes include: * DataTransferProgressDisplay: rename setProgress() method to SetProgress(). * DataTransferTask: move post-task-execution code into its own new private method, PostExecutionCallback(), and update both OnCancelled() and OnPostExecute() callbacks to invoke it. * DataTransferTask: update OnProgressUpdate() to allow sending a last progress update to all event subscribers even if the background task was cancelled. * DataTransferTask: update OnProgressUpdate() to allow sending a first progress update if no data has been transferred but the total transfer size is already known. * DataTransferTask: update OnProgressUpdate() to avoid calculating the ETA if the speed isn't greater than 0. * DumpOptionsFrame: remove UpdateOutputStorages() method. * DumpOptionsFrame: update class to use the cached output storage value from our RootView. * DumpOptionsFrame: add GenerateOutputStoragesVector() method, which is used to avoid setting dummy options while initializing the output storages SelectListItem. * DumpOptionsFrame: update UMS task callback to add the rest of the leftover logic from UpdateOutputStorages(). * DumpOptionsFrame: update RegisterButtonListener() to use a wrapper callback around the user-provided callback to check if the USB host was selected as the output storage but no USB host connection is available. * ErrorFrame: use const references for all input string arguments. * FileWriter: fix a localization stirng name typo. * FileWriter: fix an exception that was previously being thrown by a fmt::format() call because of a wrong format specifier. * FocusableItem: add a static assert to check if the provided ViewType is derived from brls::View. * gamecard: redefine global gamecard status variable as an atomic unsigned 8-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call gamecardGetStatus(). * GameCardImageDumpOptionsFrame: define GAMECARD_TOGGLE_ITEM macro, which is used to initialize all ToggleListItem elements from the view. * GameCardImageDumpOptionsFrame: update button callback to push a GameCardImageDumpTaskFrame view onto the borealis stack. * GameCardImageDumpTask: move class into its own header and module files. * GameCardImageDumpTask: update class to also take in the checksum lookup method (not yet implemented). * GameCardImageDumpTask: update class to send its first progress update as soon as the gamecard image size is known. * GameCardImageDumpTask: update class to avoid returning a string if the task was cancelled -- DataTransferTaskFrame offers logic to display the appropiate cancel message on its own. * GameCardTab: update PopulateList() method to display the new version information available in TitleGameCardApplicationMetadataEntry elements as part of the generated TitlesTabItem objects. * i18n: add new localization strings. * OptionsTab: update background task callback logic to handle task cancellation, reflecting the changes made to DataTransferTask. * OptionsTab: reflect changes made to DataTransferProgressDisplay. * RootView: cache the currently selected output storage value at all times, which is propagated throughout different parts of the UI. Getter and setter helpers have been added to operate with this value. * RootView: add GetUsbHostSpeed() helper, which can be used by child views to retrieve the USB host speed on demand. * RootView: update UMS task callback to automatically reset the cached output storage value back to the SD card if a UMS device was previously selected. * title: define TitleGameCardApplicationMetadataEntry struct, which also holds version-specific information retrieved from the gamecard titles. * title: refactor titleGetGameCardApplicationMetadataEntries() to return a dynamically allocated array of TitleGameCardApplicationMetadataEntry elements. * usb: redefine global endpoint max packet size variable as an atomic unsigned 16-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call usbIsReady(). * UsbHostTask: add GetUsbHostSpeed() method.
2024-04-29 14:26:12 +01:00
/*
* data_transfer_task_frame.hpp
*
* Copyright (c) 2020-2024, DarkMatterCore <pabloacurielz@gmail.com>.
*
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
*
* nxdumptool 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 3 of the License, or
* (at your option) any later version.
*
* nxdumptool 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, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef __DATA_TRANSFER_TASK_FRAME_HPP__
#define __DATA_TRANSFER_TASK_FRAME_HPP__
#include "../utils/is_base_of_template.hpp"
Add DataTransferTaskFrame and GameCardImageDumpTaskFrame classes DataTransferTaskFrame is a template class that's derived from brls::AppletFrame, which automatically starts a background task using an internal object belonging to a class derived from DataTransferTask. A DataTransferProgressDisplay view is used to show progress updates. If the background task hits an error, the class takes care of switching to an ErrorFrame view with the corresponding error message. GameCardImageDumpTaskFrame is a derived class of DataTransferTaskFrame that uses a GameCardImageDumpTask object as its background task. In layman's terms, this provides a way to fully dump gamecard images using the new UI. DataTransferTaskFrame depends on the newly added is_base_template helper from goneskiing to check if the class for the provided task is derived from DataTransferTask. Other changes include: * DataTransferProgressDisplay: rename setProgress() method to SetProgress(). * DataTransferTask: move post-task-execution code into its own new private method, PostExecutionCallback(), and update both OnCancelled() and OnPostExecute() callbacks to invoke it. * DataTransferTask: update OnProgressUpdate() to allow sending a last progress update to all event subscribers even if the background task was cancelled. * DataTransferTask: update OnProgressUpdate() to allow sending a first progress update if no data has been transferred but the total transfer size is already known. * DataTransferTask: update OnProgressUpdate() to avoid calculating the ETA if the speed isn't greater than 0. * DumpOptionsFrame: remove UpdateOutputStorages() method. * DumpOptionsFrame: update class to use the cached output storage value from our RootView. * DumpOptionsFrame: add GenerateOutputStoragesVector() method, which is used to avoid setting dummy options while initializing the output storages SelectListItem. * DumpOptionsFrame: update UMS task callback to add the rest of the leftover logic from UpdateOutputStorages(). * DumpOptionsFrame: update RegisterButtonListener() to use a wrapper callback around the user-provided callback to check if the USB host was selected as the output storage but no USB host connection is available. * ErrorFrame: use const references for all input string arguments. * FileWriter: fix a localization stirng name typo. * FileWriter: fix an exception that was previously being thrown by a fmt::format() call because of a wrong format specifier. * FocusableItem: add a static assert to check if the provided ViewType is derived from brls::View. * gamecard: redefine global gamecard status variable as an atomic unsigned 8-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call gamecardGetStatus(). * GameCardImageDumpOptionsFrame: define GAMECARD_TOGGLE_ITEM macro, which is used to initialize all ToggleListItem elements from the view. * GameCardImageDumpOptionsFrame: update button callback to push a GameCardImageDumpTaskFrame view onto the borealis stack. * GameCardImageDumpTask: move class into its own header and module files. * GameCardImageDumpTask: update class to also take in the checksum lookup method (not yet implemented). * GameCardImageDumpTask: update class to send its first progress update as soon as the gamecard image size is known. * GameCardImageDumpTask: update class to avoid returning a string if the task was cancelled -- DataTransferTaskFrame offers logic to display the appropiate cancel message on its own. * GameCardTab: update PopulateList() method to display the new version information available in TitleGameCardApplicationMetadataEntry elements as part of the generated TitlesTabItem objects. * i18n: add new localization strings. * OptionsTab: update background task callback logic to handle task cancellation, reflecting the changes made to DataTransferTask. * OptionsTab: reflect changes made to DataTransferProgressDisplay. * RootView: cache the currently selected output storage value at all times, which is propagated throughout different parts of the UI. Getter and setter helpers have been added to operate with this value. * RootView: add GetUsbHostSpeed() helper, which can be used by child views to retrieve the USB host speed on demand. * RootView: update UMS task callback to automatically reset the cached output storage value back to the SD card if a UMS device was previously selected. * title: define TitleGameCardApplicationMetadataEntry struct, which also holds version-specific information retrieved from the gamecard titles. * title: refactor titleGetGameCardApplicationMetadataEntries() to return a dynamically allocated array of TitleGameCardApplicationMetadataEntry elements. * usb: redefine global endpoint max packet size variable as an atomic unsigned 16-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call usbIsReady(). * UsbHostTask: add GetUsbHostSpeed() method.
2024-04-29 14:26:12 +01:00
#include "error_frame.hpp"
#include "data_transfer_progress_display.hpp"
namespace nxdt::views
{
template<typename Task>
class DataTransferTaskFrame: public brls::AppletFrame
{
static_assert(nxdt::utils::is_base_of_template_v<nxdt::tasks::DataTransferTask, Task>, "Task must inherit from DataTransferTask");
protected:
Task task;
private:
DataTransferProgressDisplay *task_progress = nullptr;
ErrorFrame *error_frame = nullptr;
bool progress_displayed = false;
std::string notification{};
void DisplayProgress(void)
{
this->setContentView(this->task_progress);
this->progress_displayed = true;
}
void DisplayError(const std::string& msg)
{
this->error_frame->SetMessage(msg);
this->setContentView(this->error_frame);
this->progress_displayed = false;
}
template<typename... Params>
void Initialize(const std::string& title, brls::Image *icon, const Params&... params)
{
/* Set UI properties. */
this->setTitle(title);
this->setIcon(icon);
/* Update B button label. */
this->updateActionHint(brls::Key::B, brls::i18n::getStr("generic/cancel"));
/* Initialize progress display. */
this->task_progress = new DataTransferProgressDisplay();
/* Initialize error frame. */
this->error_frame = new ErrorFrame();
/* Subscribe to the background task. */
this->task.RegisterListener([this](const nxdt::tasks::DataTransferProgress& progress) {
/* Store notification message and return immediately if the background task was cancelled. */
if (this->task.IsCancelled())
{
this->notification = brls::i18n::getStr("generic/process_cancelled");
return;
}
/* Update progress. */
this->task_progress->SetProgress(progress);
/* Check if the background task has finished. */
if (this->task.IsFinished())
{
/* Get background task result and error reason. */
std::string error_msg{};
bool ret = this->GetTaskResult(error_msg);
if (ret)
{
/* Store notification message. */
this->notification = brls::i18n::getStr("generic/process_complete");
/* Pop view. */
this->onCancel();
} else {
/* Update B button label. */
this->updateActionHint(brls::Key::B, brls::i18n::getStr("brls/hints/back"));
/* Display error frame. */
this->DisplayError(error_msg);
}
}
});
/* Start background task. */
this->task.Execute(params...);
/* Set content view. */
this->DisplayProgress();
}
protected:
/* Set class as non-copyable and non-moveable. */
NON_COPYABLE(DataTransferTaskFrame);
NON_MOVEABLE(DataTransferTaskFrame);
bool onCancel(void) override final
{
/* Cancel background task. This will have no effect if the background task already finished or if it was already cancelled. */
this->task.Cancel();
/* Pop view. This will invoke this class' destructor. */
brls::Application::popView(brls::ViewAnimation::SLIDE_RIGHT);
return true;
}
/* Must be implemented by derived classes to determine if the background task succeeded or not by calling GetResult() on their own. */
/* If the task failed, false shall be returned and `error_msg` shall be updated to reflect the error reason. */
virtual bool GetTaskResult(std::string& error_msg) = 0;
public:
template<typename... Params>
DataTransferTaskFrame(const std::string& title, const Params&... params) : brls::AppletFrame(true, true)
{
/* Generate icon using the default image. */
brls::Image *icon = new brls::Image();
icon->setImage(BOREALIS_ASSET("icon/" APP_TITLE ".jpg"));
icon->setScaleType(brls::ImageScaleType::SCALE);
/* Initialize the rest of the elements. */
this->Initialize(title, icon, params...);
}
template<typename... Params>
DataTransferTaskFrame(const std::string& title, brls::Image *icon, const Params&... params) : brls::AppletFrame(true, true)
{
/* Initialize the rest of the elements. */
this->Initialize(title, icon, params...);
}
~DataTransferTaskFrame()
{
/* Delete the view that's not currently being displayed. */
/* The other one will be taken care of by brls::AppletFrame's destructor. */
if (this->progress_displayed)
{
delete this->error_frame;
} else {
delete this->task_progress;
}
/* Show relevant notification, if needed. */
/* This is done here to avoid a slowdown issue while attempting to pop the current view and display a notification at the same time. */
if (!this->notification.empty()) brls::Application::notify(this->notification);
}
};
}
#endif /* __DATA_TRANSFER_TASK_FRAME_HPP__ */