Browse Source

Reduced the amount of duplicate code.

main
Nichole Mattera 2 years ago
parent
commit
a2f81cdce8
10 changed files with 239 additions and 354 deletions
  1. +120
    -0
      src/View.cpp
  2. +45
    -0
      src/View.hpp
  3. +7
    -73
      src/views/AppVersionView.cpp
  4. +5
    -9
      src/views/AppVersionView.hpp
  5. +17
    -78
      src/views/AppView.cpp
  6. +5
    -9
      src/views/AppView.hpp
  7. +8
    -74
      src/views/PackageVersionView.cpp
  8. +5
    -11
      src/views/PackageVersionView.hpp
  9. +22
    -89
      src/views/PackageView.cpp
  10. +5
    -11
      src/views/PackageView.hpp

+ 120
- 0
src/View.cpp View File

@ -0,0 +1,120 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// 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 "View.hpp"
#include <iostream>
#include <string>
#include <vector>
#include "zipper/unzipper.h"
using namespace std;
using namespace zipper;
namespace KUDiag {
View::View(function<void()> backCallback) {
_backCallback = backCallback;
_hasDrawn = false;
_hasFinished = false;
_request = NULL;
}
View::~View() {
if (_request) {
delete _request;
}
}
void View::draw(u64 kDown) {
if (_hasFinished) {
if (kDown & KEY_A) {
_backCallback();
reset();
}
return;
}
if (_request) {
Mutex mutext = _request->getMutex();
mutexLock(&mutext);
if (_request->isComplete()) {
if (_request->hasError()) {
cout << "\x1b[3;0HError: " << _request->getErrorMessage() << "\n\n";
} else {
cout << "\x1b[3;14H100";
_requestCompletedSuccessfully();
}
cout << "\nPress A to continue.";
_hasFinished = true;
} else {
int progress = _request->getProgress();
if (progress < 10) {
cout << "\x1b[3;16H" << progress;
} else if (progress < 100) {
cout << "\x1b[3;15H" << progress;
} else {
cout << "\x1b[3;14H" << progress;
}
}
mutexUnlock(&mutext);
}
if (!_hasDrawn) {
consoleClear();
cout << "\x1b[0;0H" << _getTitle() << "\n\n";
cout << "Downloading: 0%";
_request = new WebRequest("GET", _getURL());
if (!_request->start()) {
cout << "\x1b[1;12H100";
cout << "\x1b[3;0HError: Problem starting thread.\n\n";
cout << "Press A to continue.";
_hasFinished = true;
}
_hasDrawn = true;
}
}
void View::reset() {
_hasDrawn = false;
_hasFinished = false;
if (_request != NULL) {
delete _request;
_request = NULL;
}
}
string View::_getTitle() {
return "";
}
string View::_getURL() {
return "";
}
void View::_requestCompletedSuccessfully() {}
}

+ 45
- 0
src/View.hpp View File

@ -0,0 +1,45 @@
// Kosmos Updater Diagnostic
// Copyright (C) 2019 Steven Mattera
//
// 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 <switch.h>
#include <functional>
#include "WebRequest.hpp"
#pragma once
namespace KUDiag {
class View {
public:
View(std::function<void()> backCallback);
virtual ~View();
void draw(u64 kDown);
void reset();
protected:
WebRequest * _request;
private:
virtual std::string _getTitle();
virtual std::string _getURL();
virtual void _requestCompletedSuccessfully();
std::function<void()> _backCallback;
bool _hasDrawn;
bool _hasFinished;
};
}

+ 7
- 73
src/views/AppVersionView.cpp View File

@ -22,83 +22,17 @@
using namespace std;
namespace KUDiag {
AppVersionView::AppVersionView(function<void()> backCallback) {
_backCallback = backCallback;
_hasDrawn = false;
_hasFinished = false;
_request = NULL;
}
AppVersionView::AppVersionView(std::function<void()> backCallback) : View(backCallback) {}
AppVersionView::~AppVersionView() {
if (_request) {
delete _request;
}
string AppVersionView::_getTitle() {
return string("Kosmos Updater Diagnostic ") + VERSION + " - Get latest app version";
}
void AppVersionView::draw(u64 kDown) {
if (_hasFinished) {
if (kDown & KEY_A) {
_backCallback();
reset();
}
return;
}
if (_request) {
if (_request->isComplete()) {
if (_request->hasError()) {
cout << "\x1b[3;0HError: " << _request->getErrorMessage() << "\n\n";
} else {
cout << "\x1b[3;14H100";
cout << "\x1b[5;0HLatest App Version: " << _request->getData() << "\n\n";
}
cout << "Press A to continue.";
_hasFinished = true;
} else {
Mutex mutext = _request->getMutex();
mutexLock(&mutext);
int progress = _request->getProgress();
mutexUnlock(&mutext);
if (progress < 10) {
cout << "\x1b[3;16H" << progress;
} else if (progress < 100) {
cout << "\x1b[3;15H" << progress;
} else {
cout << "\x1b[3;14H" << progress;
}
}
}
if (!_hasDrawn) {
consoleClear();
cout << "\x1b[0;0HKosmos Updater Diagnostic " << VERSION << " - Get latest app version\n\n";
cout << "Downloading: 0%";
_request = new WebRequest("GET", string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/app/version-number");
if (!_request->start()) {
cout << "\x1b[1;12H100";
cout << "\x1b[3;0HError: Problem starting thread.\n\n";
cout << "Press A to continue.";
_hasFinished = true;
}
_hasDrawn = true;
}
string AppVersionView::_getURL() {
return string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/app/version-number";
}
void AppVersionView::reset() {
_hasDrawn = false;
_hasFinished = false;
if (_request != NULL) {
delete _request;
_request = NULL;
}
void AppVersionView::_requestCompletedSuccessfully() {
cout << "\x1b[5;0HLatest App Version: " << _request->getData() << "\n\n";
}
}

+ 5
- 9
src/views/AppVersionView.hpp View File

@ -17,22 +17,18 @@
#include <switch.h>
#include <functional>
#include "../WebRequest.hpp"
#include "../View.hpp"
#pragma once
namespace KUDiag {
class AppVersionView {
class AppVersionView : public View {
public:
AppVersionView(std::function<void()> backCallback);
~AppVersionView();
void draw(u64 kDown);
void reset();
private:
std::function<void()> _backCallback;
bool _hasDrawn;
bool _hasFinished;
WebRequest * _request;
std::string _getTitle();
std::string _getURL();
void _requestCompletedSuccessfully();
};
}

+ 17
- 78
src/views/AppView.cpp View File

@ -23,88 +23,27 @@
using namespace std;
namespace KUDiag {
AppView::AppView(function<void()> backCallback) {
_backCallback = backCallback;
_hasDrawn = false;
_hasFinished = false;
_request = NULL;
}
void AppView::draw(u64 kDown) {
if (_hasFinished) {
if (kDown & KEY_A) {
_backCallback();
reset();
}
return;
}
if (_request) {
if (_request->isComplete()) {
if (_request->hasError()) {
cout << "\x1b[3;0HError: " << _request->getErrorMessage() << "\n\n";
} else {
cout << "\x1b[3;14H100";
NacpStruct nacp = _getNACP(_request->getRawData());
NacpLanguageEntry * langEntry = NULL;
Result rc = nacpGetLanguageEntry(&nacp, &langEntry);
if (R_SUCCEEDED(rc) && langEntry != NULL) {
cout << "\x1b[5;0HApp Name: " << langEntry->name << "\n";
cout << "App Author: " << langEntry->author << "\n";
cout << "App Version: " << nacp.version << "\n\n";
} else {
cout << "\x1b[5;0HApp Version: " << nacp.version << "\n\n";
}
}
cout << "Press A to continue.";
AppView::AppView(std::function<void()> backCallback) : View(backCallback) {}
_hasFinished = true;
} else {
Mutex mutext = _request->getMutex();
mutexLock(&mutext);
int progress = _request->getProgress();
mutexUnlock(&mutext);
if (progress < 10) {
cout << "\x1b[3;16H" << progress;
} else if (progress < 100) {
cout << "\x1b[3;15H" << progress;
} else {
cout << "\x1b[3;14H" << progress;
}
}
}
if (!_hasDrawn) {
consoleClear();
cout << "\x1b[0;0HKosmos Updater Diagnostic " << VERSION << " - Get latest app\n\n";
cout << "Downloading: 0%";
_request = new WebRequest("GET", string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/app");
if (!_request->start()) {
cout << "\x1b[1;12H100";
cout << "\x1b[3;0HError: Problem starting thread.\n\n";
cout << "Press A to continue.";
_hasFinished = true;
}
string AppView::_getTitle() {
return string("Kosmos Updater Diagnostic ") + VERSION + " - Get latest app";
}
_hasDrawn = true;
}
string AppView::_getURL() {
return string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/app";
}
void AppView::reset() {
_hasDrawn = false;
_hasFinished = false;
if (_request != NULL) {
delete _request;
_request = NULL;
void AppView::_requestCompletedSuccessfully() {
NacpStruct nacp = _getNACP(_request->getRawData());
NacpLanguageEntry * langEntry = NULL;
Result rc = nacpGetLanguageEntry(&nacp, &langEntry);
if (R_SUCCEEDED(rc) && langEntry != NULL) {
cout << "\x1b[5;0HApp Name: " << langEntry->name << "\n";
cout << "App Author: " << langEntry->author << "\n";
cout << "App Version: " << nacp.version << "\n\n";
} else {
cout << "\x1b[5;0HApp Version: " << nacp.version << "\n\n";
}
}


+ 5
- 9
src/views/AppView.hpp View File

@ -17,23 +17,19 @@
#include <switch.h>
#include <functional>
#include "../WebRequest.hpp"
#include "../View.hpp"
#pragma once
namespace KUDiag {
class AppView {
class AppView : public View {
public:
AppView(std::function<void()> backCallback);
void draw(u64 kDown);
void reset();
private:
NacpStruct _getNACP(char * data);
std::function<void()> _backCallback;
bool _hasDrawn;
bool _hasFinished;
WebRequest * _request;
std::string _getTitle();
std::string _getURL();
void _requestCompletedSuccessfully();
};
}

+ 8
- 74
src/views/PackageVersionView.cpp View File

@ -22,85 +22,19 @@
using namespace std;
namespace KUDiag {
PackageVersionView::PackageVersionView(function<void()> backCallback) {
_backCallback = backCallback;
_hasDrawn = false;
_hasFinished = false;
_request = NULL;
}
PackageVersionView::PackageVersionView(std::function<void()> backCallback) : View(backCallback) {}
PackageVersionView::~PackageVersionView() {
if (_request) {
delete _request;
}
string PackageVersionView::_getTitle() {
return string("Kosmos Updater Diagnostic ") + VERSION + " - Get latest package version";
}
void PackageVersionView::draw(u64 kDown) {
if (_hasFinished) {
if (kDown & KEY_A) {
_backCallback();
reset();
}
return;
}
if (_request) {
if (_request->isComplete()) {
if (_request->hasError()) {
cout << "\x1b[3;0HError: " << _request->getErrorMessage() << "\n\n";
} else {
cout << "\x1b[3;14H100";
cout << "\x1b[5;0HChannel: " << _getChannel() << "\n";
cout << "Latest Package Version: " << _request->getData() << "\n\n";
}
cout << "Press A to continue.";
_hasFinished = true;
} else {
Mutex mutext = _request->getMutex();
mutexLock(&mutext);
int progress = _request->getProgress();
mutexUnlock(&mutext);
if (progress < 10) {
cout << "\x1b[3;16H" << progress;
} else if (progress < 100) {
cout << "\x1b[3;15H" << progress;
} else {
cout << "\x1b[3;14H" << progress;
}
}
}
if (!_hasDrawn) {
consoleClear();
cout << "\x1b[0;0HKosmos Updater Diagnostic " << VERSION << " - Get latest package version\n\n";
cout << "Downloading: 0%";
_request = new WebRequest("GET", string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/package/version-number?channel=" + _getChannel());
if (!_request->start()) {
cout << "\x1b[1;12H100";
cout << "\x1b[3;0HError: Problem starting thread.\n\n";
cout << "Press A to continue.";
_hasFinished = true;
}
_hasDrawn = true;
}
string PackageVersionView::_getURL() {
return string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/package/version-number?channel=" + _getChannel();
}
void PackageVersionView::reset() {
_hasDrawn = false;
_hasFinished = false;
if (_request != NULL) {
delete _request;
_request = NULL;
}
void PackageVersionView::_requestCompletedSuccessfully() {
cout << "\x1b[5;0HChannel: " << _getChannel() << "\n";
cout << "Latest Package Version: " << _request->getData() << "\n\n";
}
string PackageVersionView::_getChannel() {


+ 5
- 11
src/views/PackageVersionView.hpp View File

@ -17,26 +17,20 @@
#include <switch.h>
#include <functional>
#include "../WebRequest.hpp"
#include "../View.hpp"
#pragma once
namespace KUDiag {
class PackageVersionView {
class PackageVersionView : public View {
public:
PackageVersionView(std::function<void()> backCallback);
~PackageVersionView();
void draw(u64 kDown);
void reset();
int channel;
private:
std::string _getChannel();
std::function<void()> _backCallback;
bool _hasDrawn;
bool _hasFinished;
WebRequest * _request;
std::string _getTitle();
std::string _getURL();
void _requestCompletedSuccessfully();
};
}

+ 22
- 89
src/views/PackageView.cpp View File

@ -25,105 +25,38 @@ using namespace std;
using namespace zipper;
namespace KUDiag {
PackageView::PackageView(function<void()> backCallback) {
_backCallback = backCallback;
_hasDrawn = false;
_hasFinished = false;
_request = NULL;
}
PackageView::PackageView(std::function<void()> backCallback) : View(backCallback) {}
PackageView::~PackageView() {
if (_request) {
delete _request;
}
string PackageView::_getTitle() {
return string("Kosmos Updater Diagnostic ") + VERSION + " - Get latest package";
}
void PackageView::draw(u64 kDown) {
if (_hasFinished) {
if (kDown & KEY_A) {
_backCallback();
reset();
}
return;
}
if (_request) {
if (_request->isComplete()) {
if (_request->hasError()) {
cout << "\x1b[3;0HError: " << _request->getErrorMessage() << "\n\n";
} else {
cout << "\x1b[3;14H100";
unsigned char * data = (unsigned char *) _request->getRawData();
vector<unsigned char> vectorData(data, data + _request->getSize());
Unzipper unzipper(vectorData);
vector<ZipEntry> entries = unzipper.entries();
cout << "\x1b[5;0HChannel: " << _getChannel() << "\n";
cout << "Bundle: " << _getBundle() << "\n";
cout << "Number of entries: " << entries.size() << "\n\n";
cout << "First Ten Entries\n";
int i = 0;
for (auto const& entry : entries) {
if (!entry.name.empty()) {
cout << "Entry #" << i << ": " << entry.name << "\n";
i++;
}
string PackageView::_getURL() {
return string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/package?channel=" + _getChannel() + "&bundle=" + _getBundle();
}
if (i == 10) {
break;
}
}
}
void PackageView::_requestCompletedSuccessfully() {
unsigned char * data = (unsigned char *) _request->getRawData();
cout << "\nPress A to continue.";
vector<unsigned char> vectorData(data, data + _request->getSize());
Unzipper unzipper(vectorData);
vector<ZipEntry> entries = unzipper.entries();
_hasFinished = true;
} else {
Mutex mutext = _request->getMutex();
mutexLock(&mutext);
int progress = _request->getProgress();
mutexUnlock(&mutext);
cout << "\x1b[5;0HChannel: " << _getChannel() << "\n";
cout << "Bundle: " << _getBundle() << "\n";
cout << "Number of entries: " << entries.size() << "\n\n";
cout << "First Ten Entries\n";
if (progress < 10) {
cout << "\x1b[3;16H" << progress;
} else if (progress < 100) {
cout << "\x1b[3;15H" << progress;
} else {
cout << "\x1b[3;14H" << progress;
}
int i = 0;
for (auto const& entry : entries) {
if (!entry.name.empty()) {
cout << "Entry #" << i << ": " << entry.name << "\n";
i++;
}
}
if (!_hasDrawn) {
consoleClear();
cout << "\x1b[0;0HKosmos Updater Diagnostic " << VERSION << " - Get latest package\n\n";
cout << "Downloading: 0%";
_request = new WebRequest("GET", string("http://kosmos-updater.teamatlasnx.com/") + API_VERSION + "/package?channel=" + _getChannel() + "&bundle=" + _getBundle());
if (!_request->start()) {
cout << "\x1b[1;12H100";
cout << "\x1b[3;0HError: Problem starting thread.\n\n";
cout << "Press A to continue.";
_hasFinished = true;
if (i == 10) {
break;
}
_hasDrawn = true;
}
}
void PackageView::reset() {
_hasDrawn = false;
_hasFinished = false;
if (_request != NULL) {
delete _request;
_request = NULL;
}
}


+ 5
- 11
src/views/PackageView.hpp View File

@ -17,28 +17,22 @@
#include <switch.h>
#include <functional>
#include "../WebRequest.hpp"
#include "../View.hpp"
#pragma once
namespace KUDiag {
class PackageView {
class PackageView : public View {
public:
PackageView(std::function<void()> backCallback);
~PackageView();
void draw(u64 kDown);
void reset();
int channel;
int bundle;
private:
std::string _getChannel();
std::string _getBundle();
std::function<void()> _backCallback;
bool _hasDrawn;
bool _hasFinished;
WebRequest * _request;
std::string _getTitle();
std::string _getURL();
void _requestCompletedSuccessfully();
};
}

Loading…
Cancel
Save