diff --git a/src/apps/haikudepot/server/ServerHelper.cpp b/src/apps/haikudepot/server/ServerHelper.cpp index e550d63817..c3834ed7e8 100644 --- a/src/apps/haikudepot/server/ServerHelper.cpp +++ b/src/apps/haikudepot/server/ServerHelper.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018, Andrew Lindesay . + * Copyright 2017-2020, Andrew Lindesay . * All rights reserved. Distributed under the terms of the MIT License. */ @@ -63,11 +63,15 @@ ServerHelper::AlertServerJsonRpcError(BMessage* message) " in the request was not found on the server."); break; case ERROR_CODE_CAPTCHABADRESPONSE: - alertText = B_TRANSLATE("The response to the captcha was incorrect."); + alertText = B_TRANSLATE("The response to the captcha was" + " incorrect."); break; case ERROR_CODE_AUTHORIZATIONFAILURE: case ERROR_CODE_AUTHORIZATIONRULECONFLICT: - alertText = B_TRANSLATE("Authorization or security issue"); + alertText = B_TRANSLATE("Authorization or security issue. Logout" + " and log back in again to check that your password is correct" + " and also check that you have agreed to the latest usage" + " conditions."); break; default: alertText.SetToFormat( diff --git a/src/apps/haikudepot/server/WebAppInterface.cpp b/src/apps/haikudepot/server/WebAppInterface.cpp index 4cf368d49d..c88ce6813a 100644 --- a/src/apps/haikudepot/server/WebAppInterface.cpp +++ b/src/apps/haikudepot/server/WebAppInterface.cpp @@ -1,6 +1,6 @@ /* * Copyright 2014, Stephan Aßmus . - * Copyright 2016-2019, Andrew Lindesay . + * Copyright 2016-2020, Andrew Lindesay . * All rights reserved. Distributed under the terms of the MIT License. */ @@ -437,7 +437,7 @@ WebAppInterface::RetreiveUserRatingForPackageAndVersionByUser( status_t WebAppInterface::RetrieveUserDetailForCredentials( - const UserCredentials& credentials, UserDetail& userDetail) + const UserCredentials& credentials, BMessage& message) { if (!credentials.IsValid()) { debugger("the credentials supplied are invalid so it is not possible " @@ -459,15 +459,11 @@ WebAppInterface::RetrieveUserDetailForCredentials( requestEnvelopeWriter.WriteArrayEnd(); requestEnvelopeWriter.WriteObjectEnd(); - BMessage responseEnvelopeMessage; status_t result = _SendJsonRequest("user", credentials, requestEnvelopeData, _LengthAndSeekToZero(requestEnvelopeData), NEEDS_AUTHORIZATION, - responseEnvelopeMessage); + message); // note that the credentials used here are passed in as args. - if (result == B_OK) - result = _UnpackUserDetails(responseEnvelopeMessage, userDetail); - return result; } @@ -477,9 +473,9 @@ WebAppInterface::RetrieveUserDetailForCredentials( */ status_t -WebAppInterface::RetrieveCurrentUserDetail(UserDetail& userDetail) +WebAppInterface::RetrieveCurrentUserDetail(BMessage& message) { - return RetrieveUserDetailForCredentials(fCredentials, userDetail); + return RetrieveUserDetailForCredentials(fCredentials, message); } @@ -489,7 +485,7 @@ WebAppInterface::RetrieveCurrentUserDetail(UserDetail& userDetail) */ /*static*/ status_t -WebAppInterface::_UnpackUserDetails(BMessage& responseEnvelopeMessage, +WebAppInterface::UnpackUserDetail(BMessage& responseEnvelopeMessage, UserDetail& userDetail) { BMessage resultMessage; @@ -874,12 +870,12 @@ WebAppInterface::AuthenticateUser(const BString& nickName, */ int32 -WebAppInterface::ErrorCodeFromResponse(BMessage& response) +WebAppInterface::ErrorCodeFromResponse(BMessage& responseEnvelopeMessage) { BMessage error; double code; - if (response.FindMessage("error", &error) == B_OK + if (responseEnvelopeMessage.FindMessage("error", &error) == B_OK && error.FindDouble("code", &code) == B_OK) { return (int32) code; } diff --git a/src/apps/haikudepot/server/WebAppInterface.h b/src/apps/haikudepot/server/WebAppInterface.h index aa2a99c85e..449bff921f 100644 --- a/src/apps/haikudepot/server/WebAppInterface.h +++ b/src/apps/haikudepot/server/WebAppInterface.h @@ -1,6 +1,6 @@ /* * Copyright 2014, Stephan Aßmus . - * Copyright 2016-2019, Andrew Lindesay . + * Copyright 2016-2020, Andrew Lindesay . * All rights reserved. Distributed under the terms of the MIT License. */ #ifndef WEB_APP_INTERFACE_H @@ -91,10 +91,10 @@ public: status_t RetrieveUserDetailForCredentials( const UserCredentials& credentials, - UserDetail& userDetail); + BMessage& message); status_t RetrieveCurrentUserDetail( - UserDetail& userDetail); + BMessage& message); status_t RetrieveUserUsageConditions( const BString& code, @@ -120,12 +120,14 @@ public: const BString& passwordClear, BMessage& message); - static int32 ErrorCodeFromResponse(BMessage& response); + static int32 ErrorCodeFromResponse( + BMessage& responseEnvelopeMessage); -private: - static status_t _UnpackUserDetails( + static status_t UnpackUserDetail( BMessage& responseEnvelopeMessage, UserDetail& userDetail); +private: + status_t _RetrieveUserUsageConditionsMeta( const BString& code, BMessage& message); diff --git a/src/apps/haikudepot/ui/UserUsageConditionsWindow.cpp b/src/apps/haikudepot/ui/UserUsageConditionsWindow.cpp index b567acfdbe..62bb88de02 100644 --- a/src/apps/haikudepot/ui/UserUsageConditionsWindow.cpp +++ b/src/apps/haikudepot/ui/UserUsageConditionsWindow.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019, Andrew Lindesay . + * Copyright 2019-2020, Andrew Lindesay . * All rights reserved. Distributed under the terms of the MIT License. */ @@ -21,6 +21,7 @@ #include "MarkupTextView.h" #include "Model.h" #include "UserUsageConditions.h" +#include "ServerHelper.h" #include "WebAppInterface.h" @@ -318,36 +319,11 @@ UserUsageConditionsWindow::_FetchUserUsageConditionsCodePerform( switch (fMode) { case LATEST: code.SetTo(""); - // no code for the latest + // no code in order to get the latest return B_OK; case USER: - { - WebAppInterface interface = fModel.GetWebAppInterface(); - - if (interface.Nickname().IsEmpty()) - debugger("attempt to get user details for the current user, but" - " there is no current user"); - - status_t result = interface.RetrieveCurrentUserDetail(userDetail); - - if (result == B_OK) { - BString userUsageConditionsCode = userDetail.Agreement().Code(); - if (Logger::IsDebugEnabled()) { - printf("the user [%s] has agreed to uuc [%s]\n", - interface.Nickname().String(), - userUsageConditionsCode.String()); - } - code.SetTo(userUsageConditionsCode); - } else { - if (Logger::IsDebugEnabled()) { - printf("unable to get details of the user [%s]\n", - interface.Nickname().String()); - } - } - - return result; - break; - } + return _FetchUserUsageConditionsCodeForUserPerform( + userDetail, code); default: debugger("unhanded mode"); return B_ERROR; @@ -355,6 +331,60 @@ UserUsageConditionsWindow::_FetchUserUsageConditionsCodePerform( } +status_t +UserUsageConditionsWindow::_FetchUserUsageConditionsCodeForUserPerform( + UserDetail& userDetail, BString& code) +{ + WebAppInterface interface = fModel.GetWebAppInterface(); + + if (interface.Nickname().IsEmpty()) + debugger("attempt to get user details for the current user, but" + " there is no current user"); + + BMessage responseEnvelopeMessage; + status_t result = interface.RetrieveCurrentUserDetail( + responseEnvelopeMessage); + + if (result == B_OK) { + // could be an error or could be a valid response envelope + // containing data. + switch (interface.ErrorCodeFromResponse(responseEnvelopeMessage)) { + case ERROR_CODE_NONE: + result = WebAppInterface::UnpackUserDetail( + responseEnvelopeMessage, userDetail); + break; + default: + ServerHelper::NotifyServerJsonRpcError(responseEnvelopeMessage); + result = B_ERROR; + // just any old error to stop + break; + } + } else { + fprintf(stderr, "an error has arisen communicating with the" + " server to obtain data for a user's user usage conditions" + " [%s]\n", strerror(result)); + ServerHelper::NotifyTransportError(result); + } + + if (result == B_OK) { + BString userUsageConditionsCode = userDetail.Agreement().Code(); + if (Logger::IsDebugEnabled()) { + printf("the user [%s] has agreed to uuc [%s]\n", + interface.Nickname().String(), + userUsageConditionsCode.String()); + } + code.SetTo(userUsageConditionsCode); + } else { + if (Logger::IsDebugEnabled()) { + printf("unable to get details of the user [%s]\n", + interface.Nickname().String()); + } + } + + return result; +} + + void UserUsageConditionsWindow::_NotifyFetchProblem() { diff --git a/src/apps/haikudepot/ui/UserUsageConditionsWindow.h b/src/apps/haikudepot/ui/UserUsageConditionsWindow.h index 823c50fb2e..17e22bd43c 100644 --- a/src/apps/haikudepot/ui/UserUsageConditionsWindow.h +++ b/src/apps/haikudepot/ui/UserUsageConditionsWindow.h @@ -1,5 +1,5 @@ /* - * Copyright 2019, Andrew Lindesay . + * Copyright 2019-2020, Andrew Lindesay . * All rights reserved. Distributed under the terms of the MIT License. */ #ifndef USER_USAGE_CONDITIONS_WINDOW_H @@ -54,6 +54,8 @@ private: void _FetchDataPerform(); status_t _FetchUserUsageConditionsCodePerform( UserDetail& userDetail, BString& code); + status_t _FetchUserUsageConditionsCodeForUserPerform( + UserDetail& userDetail, BString& code); void _NotifyFetchProblem(); private: