Line data Source code
1 : // SPDX-FileCopyrightText: 2016 Anne Jan Brouwer
2 : // SPDX-License-Identifier: GPL-3.0-or-later
3 : /**
4 : * @file
5 : * @brief Implements PassBackendFactory. The backends are held through
6 : * pointers and constructed on first use, so nothing depends on the
7 : * initialisation order of globals.
8 : */
9 :
10 : #include "passbackendfactory.h"
11 : #include "appsettings.h"
12 : #include "pass.h"
13 : #include "qtpasssettings.h"
14 : #include "util.h"
15 :
16 : #include <QDir>
17 :
18 : Pass *PassBackendFactory::pass = nullptr;
19 : QScopedPointer<RealPass> PassBackendFactory::realPass;
20 : QScopedPointer<ImitatePass> PassBackendFactory::imitatePass;
21 :
22 508 : auto PassBackendFactory::getPass() -> Pass * {
23 508 : if (!pass) {
24 187 : AppSettings s = QtPassSettings::load();
25 : // Ensure store directory exists (first-run side effect — load() normalises
26 : // the path but does not create the directory).
27 187 : if (!QDir(s.passStore).exists())
28 1 : Util::mkpathForStore(s.passStore);
29 187 : if (s.usePass) {
30 3 : pass = getRealPass();
31 : } else {
32 184 : pass = getImitatePass();
33 : }
34 187 : if (pass) {
35 187 : pass->init(s);
36 : }
37 187 : }
38 508 : return pass;
39 : }
40 :
41 288 : auto PassBackendFactory::getRealPass() -> RealPass * {
42 288 : if (realPass.isNull()) {
43 5 : realPass.reset(new RealPass());
44 : }
45 288 : return realPass.data();
46 : }
47 :
48 595 : auto PassBackendFactory::getImitatePass() -> ImitatePass * {
49 595 : if (imitatePass.isNull()) {
50 6 : imitatePass.reset(new ImitatePass());
51 6 : AppSettings s = QtPassSettings::load();
52 6 : imitatePass->init(s);
53 6 : }
54 595 : return imitatePass.data();
55 : }
56 :
57 161 : auto PassBackendFactory::isIdle() -> bool {
58 322 : return (realPass.isNull() || realPass->isIdle()) &&
59 161 : (imitatePass.isNull() || imitatePass->isIdle());
60 : }
61 :
62 904 : void PassBackendFactory::invalidate() { pass = nullptr; }
|