diff --git a/services/print_service/include/print_service_ability.h b/services/print_service/include/print_service_ability.h index 8ddb3aa..640ff58 100644 --- a/services/print_service/include/print_service_ability.h +++ b/services/print_service/include/print_service_ability.h @@ -209,6 +209,7 @@ private: int32_t StartPrintJobInternal(const std::shared_ptr &printJob); int32_t QueryVendorPrinterInfo(const std::string &globalPrinterId, PrinterInfo &info); int32_t TryConnectPrinterByIp(const std::string ¶ms); + std::string RenamePrinterWhenAdded(const PrinterInfo &info); private: PrintSecurityGuardManager securityGuardManager_; diff --git a/services/print_service/src/print_service_ability.cpp b/services/print_service/src/print_service_ability.cpp index 20ab7ac..7d809f2 100644 --- a/services/print_service/src/print_service_ability.cpp +++ b/services/print_service/src/print_service_ability.cpp @@ -513,7 +513,7 @@ int32_t PrintServiceAbility::DestroyExtension() } PRINT_HILOGD("DestroyExtension started."); std::lock_guard lock(apiMutex_); - + for (auto extension : extensionStateList_) { if (extension.second < PRINT_EXTENSION_LOADING) { continue; @@ -524,7 +524,7 @@ int32_t PrintServiceAbility::DestroyExtension() PRINT_HILOGE("Destroy extension Not Register, BUT State is LOADED"); continue; } - + auto cbFunc = extCallbackMap_[cid]; if (cbFunc != nullptr) { cbFunc->OnCallback(); @@ -848,13 +848,13 @@ int32_t PrintServiceAbility::BuildPrinterPreference(PrinterCapability &cap, Prin std::string supportedOriOpts = capOpt["orientation-requested-supported"].get(); BuildPrinterPreferenceByOption(key, supportedOriOpts, printPreference.orientation); } - + key = "duplex"; if (capOpt.contains("sides-supported") && capOpt["sides-supported"].is_string()) { std::string supportedDeplexOpts = capOpt["sides-supported"].get(); BuildPrinterPreferenceByOption(key, supportedDeplexOpts, printPreference.duplex); } - + key = "quality"; if (capOpt.contains("print-quality-supported") && capOpt["print-quality-supported"].is_string()) { std::string supportedQualityOpts = capOpt["print-quality-supported"].get(); @@ -922,7 +922,7 @@ int32_t PrintServiceAbility::SetPrinterPreference(const std::string &printerId, } nlohmann::json objectJson = nlohmann::json::parse(printPreference); PrinterPreference oldPrintPreference = PrinterPreference::BuildPrinterPreferenceFromJson(objectJson); - + PRINT_HILOGD("printerSetting %{public}s", printerSetting.c_str()); nlohmann::json settingJson = nlohmann::json::parse(printerSetting); PreferenceSetting newSetting = PreferenceSetting::BuildPreferenceSettingFromJson(settingJson); @@ -999,7 +999,7 @@ bool PrintServiceAbility::WritePreferenceToFile() return false; } nlohmann::json printerMapJson = nlohmann::json::array(); - + for (auto& printPreference : printerIdAndPreferenceMap_) { if (json::accept(printPreference.second)) { nlohmann::json printPreferenceJson = nlohmann::json::parse(printPreference.second); @@ -1008,7 +1008,7 @@ bool PrintServiceAbility::WritePreferenceToFile() printerMapJson.push_back(objectJson); } } - + nlohmann::json jsonObject; jsonObject["printer_list"] = printerMapJson; std::string jsonString = jsonObject.dump(); @@ -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 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 diff --git a/test/unittest/fwk_print_service_ability_test/print_service_ability_test.cpp b/test/unittest/fwk_print_service_ability_test/print_service_ability_test.cpp index 6871eac..8205b77 100644 --- a/test/unittest/fwk_print_service_ability_test/print_service_ability_test.cpp +++ b/test/unittest/fwk_print_service_ability_test/print_service_ability_test.cpp @@ -542,7 +542,7 @@ HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0019, TestSize.Level1) std::string jobId ="1"; uint32_t state = PRINT_JOB_PREPARED; uint32_t subState = PRINT_JOB_COMPLETED_SUCCESS; - + state = PRINT_JOB_BLOCKED; service->notifyAdapterJobChanged(jobId, state, subState); } @@ -570,7 +570,7 @@ HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0021, TestSize.Level1) service->adapterListenersByJobId_[jobId] = listener; EXPECT_EQ(service->StartGetPrintFile(jobId, printAttributes, fd), E_PRINT_NONE); service->adapterListenersByJobId_.clear(); - + EXPECT_EQ(service->StartGetPrintFile(jobId, printAttributes, fd), E_PRINT_NONE); service->printAttributesList_[jobId] = printAttributes; EXPECT_EQ(service->StartGetPrintFile(jobId, printAttributes, fd), E_PRINT_NONE); @@ -1226,7 +1226,7 @@ HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0068, TestSize.Level1) std::string printerUri = "ipp://192.168.186.1:631/ipp/print"; EXPECT_EQ(service->QueryPrinterCapabilityByUri(printerUri, printerId, printerCaps), E_PRINT_NONE); } - + HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0069, TestSize.Level1) { auto service = std::make_shared(PRINT_SERVICE_ID, true); @@ -1978,7 +1978,7 @@ HWTEST_F(PrintServiceAbilityTest, PrintServiceAbilityTest_0130, TestSize.Level1) EXPECT_EQ(service->UpdatePrinterInDiscovery(info), E_PRINT_NONE); info.SetPrinterId("1234"); EXPECT_EQ(service->UpdatePrinterInDiscovery(info), E_PRINT_NONE); - + std::shared_ptr info1 = std::make_shared(); info1->SetPrinterId(DEFAULT_EXT_PRINTER_ID); service->printSystemData_.discoveredPrinterInfoList_[DEFAULT_EXT_PRINTER_ID] = info1; @@ -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(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