See desc for more.

- Delete file, if download failed. This avoids having a useless file on the SD Card, when the download is not finished.

- Add a spritesheet download icon on Store Selection.
This commit is contained in:
StackZ
2020-11-02 01:14:10 +01:00
parent 06706e00c7
commit 67f1c6f32c
5 changed files with 88 additions and 18 deletions
+1
View File
@@ -10,6 +10,7 @@ sprites/noIcon.png
sprites/qr_code.png
sprites/search.png
sprites/settings.png
sprites/sheet.png
sprites/sort.png
sprites/sort_checked.png
sprites/sort_unchecked.png
Binary file not shown.

After

Width:  |  Height:  |  Size: 360 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 29 KiB

+78 -15
View File
@@ -46,10 +46,11 @@ static const std::vector<Structs::ButtonPos> mainButtons = {
{ 10, 184, 300, 22 },
/* Add, Delete, Info.. */
{ 92, 215, 16, 16 },
{ 136, 215, 16, 16 },
{ 180, 215, 16, 16 },
{ 224, 215, 16, 16 }
{ 70, 215, 16, 16 },
{ 114, 215, 16, 16 },
{ 158, 215, 16, 16 },
{ 202, 215, 16, 16 },
{ 246, 215, 16, 16 }
};
/*
@@ -66,8 +67,6 @@ static void DeleteStore(const std::string &file) {
if (storeJson["storeInfo"].contains("sheet") && storeJson["storeInfo"]["sheet"].is_array()) {
const std::vector<std::string> sht = storeJson["storeInfo"]["sheet"].get<std::vector<std::string>>();
/* Cause it's an array, delete all Spritesheets which exist. */
for (int i = 0; i < (int)sht.size(); i++) {
if ((std::string(_STORE_PATH) + sht[i]) != "") {
@@ -153,6 +152,56 @@ static bool DownloadStore(bool Cam = true) {
return doSheet;
}
/*
Re-Download SpriteSheets optional.
const std::string &file: Const Reference to the UniStore filename.
*/
static bool DownloadSheet(const std::string &file) {
FILE *temp = fopen((std::string(_STORE_PATH) + file).c_str(), "rt");
nlohmann::json storeJson = nlohmann::json::parse(temp, nullptr, false);
fclose(temp);
if (storeJson["storeInfo"].contains("sheetURL") && storeJson["storeInfo"]["sheetURL"].is_array()) {
if (storeJson["storeInfo"].contains("sheet") && storeJson["storeInfo"]["sheet"].is_array()) {
const std::vector<std::string> locs = storeJson["storeInfo"]["sheetURL"].get<std::vector<std::string>>();
const std::vector<std::string> sht = storeJson["storeInfo"]["sheet"].get<std::vector<std::string>>();
if (locs.size() == sht.size()) {
for (int i = 0; i < (int)sht.size(); i++) {
if (!(sht[i].find("/") != std::string::npos)) {
char msg[150];
snprintf(msg, sizeof(msg), Lang::get("DOWNLOADING_SPRITE_SHEET2").c_str(), i + 1, sht.size());
Msg::DisplayMsg(msg);
DownloadSpriteSheet(locs[i], sht[i]);
} else {
Msg::waitMsg(Lang::get("SHEET_SLASH"));
return false;
}
}
}
}
} else if (storeJson["storeInfo"].contains("sheetURL") && storeJson["storeInfo"]["sheetURL"].is_string()) {
if (storeJson["storeInfo"].contains("sheet") && storeJson["storeInfo"]["sheet"].is_string()) {
const std::string fl = storeJson["storeInfo"]["sheetURL"];
const std::string fl2 = storeJson["storeInfo"]["sheet"];
if (!(fl2.find("/") != std::string::npos)) {
Msg::DisplayMsg(Lang::get("DOWNLOADING_SPRITE_SHEET"));
DownloadSpriteSheet(fl, fl2);
} else {
Msg::waitMsg(Lang::get("SHEET_SLASH"));
return false;
}
}
}
return true;
}
/*
This is the UniStore Manage Handle.
Here you can..
@@ -226,8 +275,9 @@ void Overlays::SelectStore(std::unique_ptr<Store> &store, std::vector<std::uniqu
GFX::DrawSprite(sprites_delete_idx, mainButtons[7].x, mainButtons[7].y);
GFX::DrawSprite(sprites_update_idx, mainButtons[8].x, mainButtons[8].y);
GFX::DrawSprite(sprites_add_idx, mainButtons[9].x, mainButtons[9].y);
GFX::DrawSprite(sprites_qr_code_idx, mainButtons[10].x, mainButtons[10].y);
GFX::DrawSprite(sprites_sheet_idx, mainButtons[9].x, mainButtons[9].y);
GFX::DrawSprite(sprites_add_idx, mainButtons[10].x, mainButtons[10].y);
GFX::DrawSprite(sprites_qr_code_idx, mainButtons[11].x, mainButtons[11].y);
C3D_FrameEnd(0);
hidScanInput();
@@ -300,6 +350,16 @@ void Overlays::SelectStore(std::unique_ptr<Store> &store, std::vector<std::uniqu
}
}
/* Delete UniStore. */
if (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[7])) {
if (info[selection].FileName != "") {
DeleteStore(info[selection].FileName);
selection = 0;
info = GetUniStoreInfo(_STORE_PATH);
}
}
/* Update checks. */
if (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[8])) {
if (checkWifiStatus()) {
if (info[selection].Revision > 0) {
@@ -309,11 +369,12 @@ void Overlays::SelectStore(std::unique_ptr<Store> &store, std::vector<std::uniqu
}
}
if (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[7])) {
if (info[selection].FileName != "") {
DeleteStore(info[selection].FileName);
selection = 0;
info = GetUniStoreInfo(_STORE_PATH);
/* SpriteSheet Download. */
if (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[9])) {
if (checkWifiStatus()) {
if (info[selection].FileName != "") {
DownloadSheet(info[selection].FileName);
}
}
}
@@ -321,7 +382,8 @@ void Overlays::SelectStore(std::unique_ptr<Store> &store, std::vector<std::uniqu
else if (selection > sPos + 7 - 1) sPos = selection - 7 + 1;
}
if ((hidKeysDown() & KEY_Y) || (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[9]))) {
/* UniStore URL Download. */
if ((hidKeysDown() & KEY_Y) || (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[10]))) {
if (checkWifiStatus()) {
if (DownloadStore(false)) {
selection = 0;
@@ -330,7 +392,8 @@ void Overlays::SelectStore(std::unique_ptr<Store> &store, std::vector<std::uniqu
}
}
if (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[10])) {
/* UniStore QR Code Download. */
if (hidKeysDown() & KEY_TOUCH && touching(touch, mainButtons[11])) {
if (checkWifiStatus()) {
if (DownloadStore()) {
selection = 0;
+9 -3
View File
@@ -38,6 +38,7 @@
#include <malloc.h>
#include <regex>
#include <string>
#include <unistd.h>
#include <vector>
#define USER_AGENT APP_TITLE "-" VERSION_STRING
@@ -45,8 +46,6 @@
static char *result_buf = nullptr;
static size_t result_sz = 0;
static size_t result_written = 0;
std::vector<std::string> _topText;
std::string jsonName;
#define TIME_IN_US 1
#define TIMETYPE curl_off_t
@@ -138,13 +137,13 @@ static size_t file_handle_data(char *ptr, size_t size, size_t nmemb, void *userd
}
Result downloadToFile(const std::string &url, const std::string &path) {
bool needToDelete = false;
downloadTotal = 1;
downloadNow = 0;
CURLcode curlResult;
CURL *hnd;
Result retcode = 0;
downloadTotal = 1;
int res;
printf("Downloading from:\n%s\nto:\n%s\n", url.c_str(), path.c_str());
@@ -191,6 +190,7 @@ Result downloadToFile(const std::string &url, const std::string &path) {
if (curlResult != CURLE_OK) {
retcode = -curlResult;
needToDelete = true;
goto exit;
}
@@ -203,6 +203,7 @@ Result downloadToFile(const std::string &url, const std::string &path) {
if (!filecommit()) {
retcode = -3;
needToDelete = true;
goto exit;
}
@@ -240,6 +241,11 @@ exit:
file_buffer_pos = 0;
file_toCommit_size = 0;
writeError = false;
if (needToDelete) {
if (access(path.c_str(), F_OK) == 0) deleteFile(path.c_str()); // Delete file, cause not fully downloaded.
}
return retcode;
}