1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qplatformprintplugin.h" |
5 | #include "qplatformprintersupport.h" |
6 | #include "qprinterinfo.h" |
7 | #include "private/qfactoryloader_p.h" |
8 | #include <qcoreapplication.h> |
9 | #include <qdebug.h> |
10 | |
11 | #ifndef QT_NO_PRINTER |
12 | |
13 | #if defined(Q_OS_MACOS) |
14 | Q_IMPORT_PLUGIN(QCocoaPrinterSupportPlugin) |
15 | #elif defined(Q_OS_WIN) |
16 | Q_IMPORT_PLUGIN(QWindowsPrinterSupportPlugin) |
17 | #endif |
18 | |
19 | QT_BEGIN_NAMESPACE |
20 | |
21 | using namespace Qt::StringLiterals; |
22 | |
23 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, |
24 | (QPlatformPrinterSupportFactoryInterface_iid, "/printsupport"_L1 , Qt::CaseInsensitive)) |
25 | |
26 | QPlatformPrinterSupportPlugin::QPlatformPrinterSupportPlugin(QObject *parent) |
27 | : QObject(parent) |
28 | { |
29 | } |
30 | |
31 | QPlatformPrinterSupportPlugin::~QPlatformPrinterSupportPlugin() |
32 | { |
33 | } |
34 | |
35 | static QPlatformPrinterSupport *printerSupport = nullptr; |
36 | |
37 | static void cleanupPrinterSupport() |
38 | { |
39 | delete printerSupport; |
40 | printerSupport = nullptr; |
41 | } |
42 | |
43 | /*! |
44 | \internal |
45 | |
46 | Returns a lazily-initialized singleton. Ownership is granted to the |
47 | QPlatformPrinterSupportPlugin, which is never unloaded or destroyed until |
48 | application exit, i.e. you can expect this pointer to always be valid and |
49 | multiple calls to this function will always return the same pointer. |
50 | */ |
51 | QPlatformPrinterSupport *QPlatformPrinterSupportPlugin::get() |
52 | { |
53 | if (!printerSupport) { |
54 | const QMultiMap<int, QString> keyMap = loader()->keyMap(); |
55 | QMultiMap<int, QString>::const_iterator it = keyMap.cbegin(); |
56 | if (!qEnvironmentVariableIsEmpty(varName: "QT_PRINTER_MODULE" )) { |
57 | QString module = QString::fromLocal8Bit(ba: qgetenv(varName: "QT_PRINTER_MODULE" )); |
58 | QMultiMap<int, QString>::const_iterator it2 = std::find_if(first: keyMap.cbegin(), last: keyMap.cend(), pred: [module](const QString &value){ return value == module; }); |
59 | if (it2 == keyMap.cend()) |
60 | qWarning() << "Unable to load printer plugin" << module; |
61 | else |
62 | it = it2; |
63 | } |
64 | if (it != keyMap.cend()) |
65 | printerSupport = qLoadPlugin<QPlatformPrinterSupport, QPlatformPrinterSupportPlugin>(loader: loader(), key: it.value()); |
66 | if (printerSupport) |
67 | qAddPostRoutine(cleanupPrinterSupport); |
68 | } |
69 | return printerSupport; |
70 | } |
71 | |
72 | QT_END_NAMESPACE |
73 | |
74 | #include "moc_qplatformprintplugin.cpp" |
75 | |
76 | #endif |
77 | |