Files
ptprnt/src/core/PrinterDriverFactory.cpp
Moritz Martinius 2d37f6fcfb
All checks were successful
Build ptprnt / build (push) Successful in 3m50s
cli-parser-cleanup (#15)
Reviewed-on: moritz/ptouch-prnt#15
2025-10-13 19:23:27 +00:00

69 lines
2.4 KiB
C++

/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "PrinterDriverFactory.hpp"
#include <algorithm>
#include <spdlog/spdlog.h>
#include <memory>
#include "libusbwrap/LibUsbTypes.hpp"
#include "printers/FakePrinter.hpp"
#include "printers/P700Printer.hpp"
namespace ptprnt {
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::create(libusbwrap::usbId id) {
if (printer::P700Printer::mInfo.usbId == id) {
spdlog::info("Found printer P700!");
return std::make_shared<printer::P700Printer>();
} else {
spdlog::trace("{:04x}:{:04x} is not a compatible printer", id.first, id.second);
}
return nullptr;
}
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createFakePrinter() {
spdlog::info("Creating FakePrinter (virtual test printer)");
return std::make_shared<printer::FakePrinter>();
}
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::createByName(const std::string& driverName) {
// Convert to lowercase for case-insensitive comparison
std::string nameLower = driverName;
std::ranges::transform(nameLower, nameLower.begin(), [](unsigned char c) { return std::tolower(c); });
if (nameLower == "p700" || nameLower == "p700printer") {
spdlog::info("Creating P700 printer driver by name");
return std::make_shared<printer::P700Printer>();
} else if (nameLower == "fakeprinter" || nameLower == "fake") {
return createFakePrinter();
}
spdlog::warn("Unknown printer driver name: {}", driverName);
return nullptr;
}
std::vector<std::string> PrinterDriverFactory::listAllDrivers() const {
return {std::string(printer::P700Printer::mInfo.driverName), std::string(printer::FakePrinter::mInfo.driverName)};
}
} // namespace ptprnt