1 /* 2 * Copyright 2016-2024, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 * 5 * Note that this file included code earlier from `Model.cpp` and 6 * copyrights have been latterly been carried across in 2024. 7 */ 8 9 10 #include "PopulatePkgChangelogFromServerProcess.h" 11 12 #include <Autolock.h> 13 #include <Catalog.h> 14 15 #include "Logger.h" 16 #include "ServerHelper.h" 17 18 19 #undef B_TRANSLATION_CONTEXT 20 #define B_TRANSLATION_CONTEXT "PopulatePkgChangelogFromServerProcess" 21 22 23 PopulatePkgChangelogFromServerProcess::PopulatePkgChangelogFromServerProcess( 24 PackageInfoRef packageInfo, Model* model) 25 : 26 fModel(model), 27 fPackageInfo(packageInfo) 28 { 29 } 30 31 32 PopulatePkgChangelogFromServerProcess::~PopulatePkgChangelogFromServerProcess() 33 { 34 } 35 36 37 const char* 38 PopulatePkgChangelogFromServerProcess::Name() const 39 { 40 return "PopulatePkgChangelogFromServerProcess"; 41 } 42 43 44 const char* 45 PopulatePkgChangelogFromServerProcess::Description() const 46 { 47 return B_TRANSLATE("Fetching changelog for package"); 48 } 49 50 51 status_t 52 PopulatePkgChangelogFromServerProcess::RunInternal() 53 { 54 // TODO; use API spec to code generation techniques instead of this manually written client. 55 56 BMessage responsePayload; 57 BString packageName = fPackageInfo->Name(); 58 status_t result; 59 60 result = fModel->GetWebAppInterface()->GetChangelog(packageName, responsePayload); 61 62 if (result == B_OK) { 63 BMessage resultMessage; 64 BString content; 65 66 result = responsePayload.FindMessage("result", &resultMessage); 67 68 if (result == B_OK) { 69 if (resultMessage.FindString("content", &content) == B_OK) { 70 content.Trim(); 71 72 PackageLocalizedTextRef localizedText = fPackageInfo->LocalizedText(); 73 74 if (localizedText.IsSet()) { 75 localizedText = PackageLocalizedTextRef( 76 new PackageLocalizedText(*(localizedText.Get())), true); 77 } else { 78 localizedText = PackageLocalizedTextRef(new PackageLocalizedText(), true); 79 } 80 81 localizedText->SetChangelog(content); 82 83 // TODO; later work to make the `PackageInfo` immutable to avoid this locking 84 BAutolock locker(fModel->Lock()); 85 fPackageInfo->SetLocalizedText(localizedText); 86 HDDEBUG("changelog populated for [%s]", packageName.String()); 87 } else { 88 HDDEBUG("no changelog present for [%s]", packageName.String()); 89 } 90 } else { 91 int32 errorCode = WebAppInterface::ErrorCodeFromResponse(responsePayload); 92 93 if (errorCode != ERROR_CODE_NONE) 94 ServerHelper::NotifyServerJsonRpcError(responsePayload); 95 } 96 } else { 97 ServerHelper::NotifyTransportError(result); 98 } 99 100 return result; 101 } 102