!467 capi添加对打印能力里不包含cupsOption时的处理

Merge pull request !467 from 张子明/master
This commit is contained in:
openharmony_ci 2024-11-02 07:32:14 +00:00 committed by Gitee
commit 8f7844f1a4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 20 additions and 29 deletions

View File

@ -180,30 +180,23 @@ void ReleasePrinterInfo(Print_PrinterInfo &printerInfo)
ReleaseDefaultValue(printerInfo.defaultValue);
}
void PageSizeArrayConvert(const nlohmann::json &pageSizeJsonArray, Print_PrinterInfo &nativePrinterInfo)
void PageSizeArrayConvert(const OHOS::Print::PrinterCapability &cap, Print_PrinterInfo &nativePrinterInfo)
{
ReleaseCapabilityPageSizes(nativePrinterInfo.capability);
PRINT_HILOGI("pageSizeJsonArray size = %{public}zu", pageSizeJsonArray.size());
std::vector<Print_PageSize> pageSizeVector;
for (auto &item : pageSizeJsonArray) {
Print_PageSize pageSize = {0};
if (!item.contains("id") || !item.contains("name") || !item.contains("width") || !item.contains("height")) {
PRINT_HILOGW("field missing");
continue;
}
if (!item["id"].is_string() || !item["name"].is_string() || !item["width"].is_number() ||
!item["height"].is_number()) {
PRINT_HILOGW("incorrect type");
continue;
}
pageSize.id = CopyString(item["id"].get<std::string>());
pageSize.name = CopyString(item["name"].get<std::string>());
pageSize.width = item["width"];
pageSize.height = item["height"];
pageSizeVector.push_back(pageSize);
std::vector<PrintPageSize> pageSizeVector;
std::vector<Print_PageSize> nativePageSizeVector;
cap.GetSupportedPageSize(pageSizeVector);
for (const auto &pageSize : pageSizeVector) {
nativePageSizeVector.push_back({
CopyString(pageSize.GetId()),
CopyString(pageSize.GetName()),
pageSize.GetWidth(),
pageSize.GetHeight()
});
}
nativePrinterInfo.capability.supportedPageSizes = CopyArray<Print_PageSize>(pageSizeVector,
nativePrinterInfo.capability.supportedPageSizes = CopyArray<Print_PageSize>(nativePageSizeVector,
nativePrinterInfo.capability.supportedPageSizesCount);
PRINT_HILOGI("nativePageSizeVector size = %{public}zu", nativePageSizeVector.size());
}
void ParseDefaultPageMargin(const nlohmann::json &cupsOpt, Print_Margin &defaultMargin)
@ -422,10 +415,8 @@ void ParseCupsOptions(const nlohmann::json &cupsOpt, Print_PrinterInfo &nativePr
ParseDefaultPageMargin(cupsOpt, nativePrinterInfo.defaultValue.defaultMargin);
ParseCupsCopyOpt(cupsOpt, nativePrinterInfo);
ParseMediaOpt(cupsOpt, nativePrinterInfo);
std::string keyword = "supportedPageSizeArray";
ParseJsonFieldAsArrayOpt(cupsOpt, keyword, nativePrinterInfo, PageSizeArrayConvert);
nlohmann::json advancedCapJson;
keyword = "multiple-document-handling-supported";
std::string keyword = "multiple-document-handling-supported";
AddJsonFieldStringToJsonObject(cupsOpt, keyword, advancedCapJson);
nativePrinterInfo.capability.advancedCapability = CopyString(advancedCapJson.dump().c_str());
}
@ -524,6 +515,7 @@ Print_PrinterInfo *ConvertToNativePrinterInfo(const PrinterInfo &info)
}
OHOS::Print::PrinterCapability cap;
info.GetCapability(cap);
PageSizeArrayConvert(cap, *nativePrinterInfo);
if (cap.HasOption() && json::accept(cap.GetOption())) {
nlohmann::json capJson = json::parse(cap.GetOption());
if (capJson.contains("cupsOptions") && capJson["cupsOptions"].is_object()) {

View File

@ -179,9 +179,7 @@ void ParsePageSizeAttributes(ipp_t *response, PrinterCapability &printerCaps)
std::string keyword = "media-supported";
std::vector<PrintPageSize> supportedPageSizes;
ParseAttributesToList<PrintPageSize>(response, keyword, supportedPageSizes, ConvertPrintPageSize);
std::string pageSizeJson = ConvertListToJson<PrintPageSize>(supportedPageSizes, ConvertPageSizeToJson);
printerCaps.SetSupportedPageSize(supportedPageSizes);
printerCaps.SetPrinterAttrNameAndValue("supportedPageSizeArray", pageSizeJson.c_str());
std::string defaultPageSizeId;
keyword = "media-default";

View File

@ -33,6 +33,7 @@
#include "print_constant.h"
#include "print_log.h"
#include "printer_info.h"
#include "print_service_converter.h"
#include "print_utils.h"
#include "string_wrapper.h"
#include "system_ability_definition.h"
@ -838,8 +839,10 @@ int32_t PrintServiceAbility::BuildPrinterPreference(PrinterCapability &cap, Prin
nlohmann::json capOpt = capJson["cupsOptions"];
std::string key = "id";
if (capOpt.contains("supportedPageSizeArray") && capOpt["supportedPageSizeArray"].is_string()) {
std::string supportedPageSizeOpts = capOpt["supportedPageSizeArray"].get<std::string>();
std::vector<PrintPageSize> supportedPageSizes;
cap.GetSupportedPageSize(supportedPageSizes);
if (supportedPageSizes.size() > 0) {
std::string supportedPageSizeOpts = ConvertListToJson<PrintPageSize>(supportedPageSizes, ConvertPageSizeToJson);
BuildPrinterPreferenceByOption(key, supportedPageSizeOpts, printPreference.pagesizeId);
}

View File

@ -480,8 +480,6 @@ bool UpdatePageSizeCapability(PrinterCapability &printerCap, const Print_Printer
capability->supportedPageSizes[i].height);
}
printerCap.SetSupportedPageSize(pageSizeList);
std::string pageSizeJson = ConvertListToJson<PrintPageSize>(pageSizeList, ConvertPageSizeToJson);
printerCap.SetPrinterAttrNameAndValue("supportedPageSizeArray", pageSizeJson.c_str());
return true;
}