!415 重命名重复名称打印机

Merge pull request !415 from 单海洋/myfeature
This commit is contained in:
openharmony_ci 2024-10-16 11:47:05 +00:00 committed by Gitee
commit 052e103cf4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 77 additions and 11 deletions

View File

@ -209,6 +209,7 @@ private:
int32_t StartPrintJobInternal(const std::shared_ptr<PrintJob> &printJob);
int32_t QueryVendorPrinterInfo(const std::string &globalPrinterId, PrinterInfo &info);
int32_t TryConnectPrinterByIp(const std::string &params);
std::string RenamePrinterWhenAdded(const PrinterInfo &info);
private:
PrintSecurityGuardManager securityGuardManager_;

View File

@ -3059,6 +3059,10 @@ bool PrintServiceAbility::AddVendorPrinterToDiscovery(const std::string &globalV
PRINT_HILOGW("allocate printer info fail");
return false;
}
OHOS::Print::CupsPrinterInfo cupsPrinter;
if (printSystemData_.QueryCupsPrinterInfoByPrinterId(globalPrinterId, cupsPrinter)) {
printerInfo->SetPrinterName(cupsPrinter.name);
}
printerInfo->SetPrinterId(globalPrinterId);
printerInfo->SetPrinterState(PRINTER_ADDED);
printSystemData_.AddPrinterToDiscovery(printerInfo);
@ -3100,6 +3104,10 @@ bool PrintServiceAbility::UpdateVendorPrinterToDiscovery(const std::string &glob
printerInfo->SetPrinterId(globalPrinterId);
}
}
OHOS::Print::CupsPrinterInfo cupsPrinter;
if (printSystemData_.QueryCupsPrinterInfoByPrinterId(globalPrinterId, cupsPrinter)) {
printerInfo->SetPrinterName(cupsPrinter.name);
}
printerInfo->SetPrinterState(PRINTER_UPDATE_CAP);
SendPrinterDiscoverEvent(PRINTER_UPDATE_CAP, *printerInfo);
SendPrinterEvent(*printerInfo);
@ -3127,6 +3135,7 @@ bool PrintServiceAbility::AddVendorPrinterToCupsWithPpd(const std::string &globa
PRINT_HILOGW("empty capability or invalid printer info");
return false;
}
printerInfo->SetPrinterName(RenamePrinterWhenAdded(*printerInfo));
CupsPrinterInfo info;
info.name = printerInfo->GetPrinterName();
info.uri = printerInfo->GetUri();
@ -3386,4 +3395,30 @@ bool PrintServiceAbility::UpdateAddedPrinterInCups(const std::string &printerId,
}
return false;
}
std::string PrintServiceAbility::RenamePrinterWhenAdded(const PrinterInfo &info)
{
static uint32_t repeatNameLimit = 10;
std::vector<std::string> printerNameList;
printSystemData_.GetAddedPrinterListFromSystemData(printerNameList);
uint32_t nameIndex = 1;
auto printerName = info.GetPrinterName();
auto iter = printerNameList.begin();
auto end = printerNameList.end();
do {
iter = std::find(iter, end, printerName);
if (iter == end) {
break;
}
printerName = info.GetPrinterName();
printerName += " ";
printerName += std::to_string(nameIndex);
if (nameIndex == repeatNameLimit) {
break;
}
++nameIndex;
iter = printerNameList.begin();
} while (iter != end);
return printerName;
}
} // namespace OHOS::Print

View File

@ -2068,4 +2068,34 @@ HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0135, TestSize.Level1)
param = "{\"protocol\":\"ipp\",\"ip\":\"a.b.c.d\"}";
EXPECT_EQ(service->TryConnectPrinterByIp(param), E_PRINT_SERVER_FAILURE);
}
HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0136, TestSize.Level1)
{
auto service = std::make_shared<PrintServiceAbility>(PRINT_SERVICE_ID, true);
std::string vendorName = "fwk.driver";
std::string printerId = "testprinter";
std::string ppdData;
PrinterInfo info;
info.SetPrinterName(vendorName);
info.SetPrinterId(printerId);
PrinterCapability cap;
info.SetCapability(cap);
info.SetUri("uri");
info.SetPrinterMake("maker");
ppdData = "ppd";
EXPECT_TRUE(service->AddVendorPrinterToDiscovery(vendorName, info));
service->AddVendorPrinterToCupsWithPpd(vendorName, printerId, ppdData);
std::string vendorName1 = "fwk.driver";
std::string printerId1 = "testprinter1";
PrinterInfo info1;
info1.SetPrinterName(vendorName1);
info1.SetPrinterId(printerId1);
PrinterCapability cap1;
info1.SetCapability(cap1);
info1.SetUri("uri1");
info1.SetPrinterMake("maker1");
EXPECT_TRUE(service->RenamePrinterWhenAdded(info1) == "fwk.driver 1");
service->RemoveVendorPrinterFromCups(vendorName, printerId);
}
} // namespace OHOS::Print