CLOUD: Update DropboxStorage to work via scummvm.org & StorageWizardDialog correspondingly

This commit is contained in:
Alexander Tkachev 2019-07-14 04:42:40 +07:00 committed by Matan Bareket
parent 1bebaf96f7
commit ddcfcc18b2
7 changed files with 35 additions and 87 deletions

View File

@ -336,14 +336,6 @@ bool CloudManager::isWorking() const {
return false;
}
bool CloudManager::couldUseLocalServer() {
#ifdef USE_SDL_NET
return Networking::LocalWebserver::getPort() == Networking::LocalWebserver::DEFAULT_SERVER_PORT;
#else
return false;
#endif
}
///// SavesSyncRequest-related /////
bool CloudManager::isSyncing() const {

View File

@ -227,9 +227,6 @@ public:
/** Returns whether there are any requests running. */
bool isWorking() const;
/** Returns whether LocalWebserver is available to use for auth. */
static bool couldUseLocalServer();
///// SavesSyncRequest-related /////
/** Returns whether there is a SavesSyncRequest running. */

View File

@ -42,29 +42,9 @@
namespace Cloud {
namespace Dropbox {
#define DROPBOX_OAUTH2_TOKEN "https://api.dropboxapi.com/oauth2/token"
#define DROPBOX_OAUTH2_TOKEN "https://scummvm.org/admin/cloud/cloud/dropbox/token/"
#define DROPBOX_API_FILES_DOWNLOAD "https://content.dropboxapi.com/2/files/download"
char *DropboxStorage::KEY = nullptr; //can't use CloudConfig there yet, loading it on instance creation/auth
char *DropboxStorage::SECRET = nullptr;
void DropboxStorage::loadKeyAndSecret() {
#ifdef ENABLE_RELEASE
KEY = RELEASE_DROPBOX_KEY;
SECRET = RELEASE_DROPBOX_SECRET;
#else
Common::String k = ConfMan.get("DROPBOX_KEY", ConfMan.kCloudDomain);
KEY = new char[k.size() + 1];
memcpy(KEY, k.c_str(), k.size());
KEY[k.size()] = 0;
k = ConfMan.get("DROPBOX_SECRET", ConfMan.kCloudDomain);
SECRET = new char[k.size() + 1];
memcpy(SECRET, k.c_str(), k.size());
SECRET[k.size()] = 0;
#endif
}
DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) {}
DropboxStorage::DropboxStorage(Common::String code) {
@ -74,20 +54,12 @@ DropboxStorage::DropboxStorage(Common::String code) {
DropboxStorage::~DropboxStorage() {}
void DropboxStorage::getAccessToken(Common::String code) {
if (!KEY || !SECRET)
loadKeyAndSecret();
Networking::JsonCallback callback = new Common::Callback<DropboxStorage, Networking::JsonResponse>(this, &DropboxStorage::codeFlowComplete);
Networking::ErrorCallback errorCallback = new Common::Callback<DropboxStorage, Networking::ErrorResponse>(this, &DropboxStorage::codeFlowFailed);
Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, errorCallback, DROPBOX_OAUTH2_TOKEN);
request->addPostField("code=" + code);
request->addPostField("grant_type=authorization_code");
request->addPostField("client_id=" + Common::String(KEY));
request->addPostField("client_secret=" + Common::String(SECRET));
if (Cloud::CloudManager::couldUseLocalServer()) {
request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F");
} else {
request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code");
}
Common::String url = Common::String(DROPBOX_OAUTH2_TOKEN) + code;
Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(callback, errorCallback, url);
addRequest(request);
}
@ -177,8 +149,6 @@ Networking::Request *DropboxStorage::info(StorageInfoCallback callback, Networki
Common::String DropboxStorage::savesDirectoryPath() { return "/saves/"; }
DropboxStorage *DropboxStorage::loadFromConfig(Common::String keyPrefix) {
loadKeyAndSecret();
if (!ConfMan.hasKey(keyPrefix + "access_token", ConfMan.kCloudDomain)) {
warning("DropboxStorage: no access_token found");
return nullptr;

View File

@ -31,10 +31,6 @@ namespace Cloud {
namespace Dropbox {
class DropboxStorage: public Cloud::Storage {
static char *KEY, *SECRET;
static void loadKeyAndSecret();
Common::String _token, _uid;
/** This private constructor is called from loadFromConfig(). */

View File

@ -105,11 +105,7 @@ void GoogleDriveStorage::getAccessToken(BoolCallback callback, Networking::Error
}
request->addPostField("client_id=" + Common::String(KEY));
request->addPostField("client_secret=" + Common::String(SECRET));
if (Cloud::CloudManager::couldUseLocalServer()) {
request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345");
} else {
request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code");
}
request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code");
addRequest(request);
}

View File

@ -105,11 +105,7 @@ void OneDriveStorage::getAccessToken(BoolCallback callback, Networking::ErrorCal
}
request->addPostField("client_id=" + Common::String(KEY));
request->addPostField("client_secret=" + Common::String(SECRET));
if (Cloud::CloudManager::couldUseLocalServer()) {
request->addPostField("&redirect_uri=http%3A%2F%2Flocalhost%3A12345%2F");
} else {
request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code");
}
request->addPostField("&redirect_uri=https%3A%2F%2Fwww.scummvm.org/c/code");
addRequest(request);
}

View File

@ -73,12 +73,6 @@ StorageWizardDialog::StorageWizardDialog(uint32 storageId):
// Initialy the code is empty, so disable the connect button
_connectWidget->setEnabled(false);
if (Cloud::CloudManager::couldUseLocalServer()) {
// hide fields and even the button if local webserver is on
_returnLine1->setLabel(_("You will be directed to ScummVM's page where"));
_returnLine2->setLabel(_("you should allow it to access your storage."));
}
_picture = new GraphicsWidget(container, "GlobalOptions_Cloud_ConnectionWizard_Container.Picture");
#ifndef DISABLE_FANCY_THEMES
if (g_gui.theme()->supportsImages()) {
@ -130,24 +124,9 @@ void StorageWizardDialog::open() {
return;
}
}
#ifdef USE_SDL_NET
if (Cloud::CloudManager::couldUseLocalServer()) {
_stopServerOnClose = !LocalServer.isRunning();
LocalServer.start(true); // using "minimal mode" (no "/files", "/download", etc available)
LocalServer.indexPageHandler().setTarget(this);
}
#endif
}
void StorageWizardDialog::close() {
#ifdef USE_SDL_NET
if (Cloud::CloudManager::couldUseLocalServer()) {
if (_stopServerOnClose)
LocalServer.stopOnIdle();
LocalServer.indexPageHandler().setTarget(nullptr);
}
#endif
Dialog::close();
}
@ -155,6 +134,19 @@ void StorageWizardDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3
switch (cmd) {
case kCodeBoxCmd: {
Common::String code, message;
if (_storageId == Cloud::kStorageDropboxId) {
// new handling
code = _codeWidget[0]->getEditString();
bool ok = (code.size() > 0);
message = ""; // (ok ? _("All OK!") : "");
_connectWidget->setEnabled(ok);
_messageWidget->setLabel(message);
return;
}
uint32 correctFields = 0;
for (uint32 i = 0; i < CODE_FIELDS; ++i) {
Common::String subcode = _codeWidget[i]->getEditString();
@ -236,6 +228,18 @@ void StorageWizardDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3
break;
}
case kConnectCmd: {
if (_storageId == Cloud::kStorageDropboxId) {
// new handling
Common::String code = _codeWidget[0]->getEditString();
if (code.size() == 0)
return;
CloudMan.connectStorage(_storageId, code);
setResult(1);
close();
return;
}
Common::String code;
for (uint32 i = 0; i < CODE_FIELDS; ++i) {
Common::String subcode = _codeWidget[i]->getEditString();
@ -283,9 +287,9 @@ void StorageWizardDialog::containerWidgetsReflow() {
if (_returnLine1) _returnLine1->setVisible(true);
if (_returnLine2) _returnLine2->setVisible(true);
bool showFields = (!Cloud::CloudManager::couldUseLocalServer());
bool showFields = true; // TODO: remove this const
for (uint32 i = 0; i < CODE_FIELDS; ++i)
_codeWidget[i]->setVisible(showFields);
_codeWidget[i]->setVisible(showFields && (_storageId != Cloud::kStorageDropboxId || i < 1)); // show only one field for Dropbox
_messageWidget->setVisible(showFields);
// left column / first bottom row
@ -312,7 +316,7 @@ Common::String StorageWizardDialog::getUrl() const {
Common::String url = "https://www.scummvm.org/c/";
switch (_storageId) {
case Cloud::kStorageDropboxId:
url += "db";
url = "https://cloud.scummvm.org/";
break;
case Cloud::kStorageOneDriveId:
url += "od";
@ -325,9 +329,6 @@ Common::String StorageWizardDialog::getUrl() const {
break;
}
if (Cloud::CloudManager::couldUseLocalServer())
url += "s";
return url;
}