GCC Code Coverage Report


Directory: ./
File: qtpdcom/src/LoginManager.cpp
Date: 2025-07-13 04:08:56
Exec Total Coverage
Lines: 54 78 69.2%
Branches: 19 43 44.2%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2009-2023 Bjarne von Horn <vh@igh.de>
4 *
5 * This file is part of the QtPdCom library.
6 *
7 * The QtPdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The QtPdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 ****************************************************************************/
21
22 #include "LoginManager.h"
23
24 #include "LoginManager_p.h"
25 #include <pdcom5/Exception.h>
26 #include <QDebug>
27
28 using QtPdCom::LoginManager;
29 using QtPdCom::LoginManagerPrivate;
30
31 4 LoginManager::LoginManager(QString server_name, QObject *parent):
32 QObject(parent),
33
3/6
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 4 times.
✗ Branch 16 not taken.
4 d_ptr(new LoginManagerPrivate(server_name.toStdString().c_str(), this))
34 4 {}
35
36 LoginManager::~LoginManager() = default;
37
38 1 void LoginManager::InitLibrary(const char *plugin_path)
39 {
40 1 PdCom::SimpleLoginManager::InitLibrary(plugin_path);
41 1 }
42
43 void LoginManager::FinalizeLibrary()
44 {
45 PdCom::SimpleLoginManager::FinalizeLibrary();
46 }
47
48 4 void LoginManager::setAuthName(QString name)
49 {
50 4 Q_D(LoginManager);
51 4 d->username = name.toStdString();
52 4 d->username_set = true;
53 4 }
54
55 4 void LoginManager::setPassword(QString password)
56 {
57 4 Q_D(LoginManager);
58 4 d->password = password.toStdString();
59 4 d->password_set = true;
60 4 }
61
62 3 void LoginManager::login()
63 {
64 3 Q_D(LoginManager);
65 try {
66
1/2
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 d->login();
67 }
68 catch (const PdCom::NotConnected &) {
69 qWarning() << "Login on not connected process!";
70 }
71 3 }
72
73 1 void LoginManager::logout()
74 {
75 1 Q_D(LoginManager);
76 try {
77
1/2
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 d->logout();
78 }
79 catch (const PdCom::NotConnected &) {
80 }
81 1 }
82
83 6 std::string LoginManagerPrivate::getAuthname()
84 {
85
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 5 times.
6 if (username_set) {
86 1 return username;
87 }
88 5 Q_Q(LoginManager);
89 5 emit q->needCredentials();
90 // username can be set from non-queued signal
91
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 2 times.
5 if (username_set) {
92 3 return username;
93 }
94 2 throw Cancel();
95 }
96
97 4 std::string LoginManagerPrivate::getPassword()
98 {
99
2/4
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
4 if (password_set) {
100 4 return password;
101 }
102 Q_Q(LoginManager);
103 emit q->needCredentials();
104 // username can be set from non-queued signal
105 if (password_set) {
106 return password;
107 }
108 throw Cancel();
109 }
110
111 6 void LoginManagerPrivate::completed(
112 PdCom::SimpleLoginManager::LoginResult result)
113 {
114 6 Q_Q(LoginManager);
115 6 loginResult = result;
116
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (result == LoginResult::Success) {
117 3 emit q->loginSuccessful();
118 }
119
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 else if (result == LoginResult::Error) {
120 1 clearCredentials();
121 1 emit q->loginFailed();
122 }
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 else if (result == LoginResult::NoSaslMechanism) {
124 emit q->loginFailed();
125 }
126 // LoginResult::Canceled is not an error.
127 6 }
128
129 2 void LoginManagerPrivate::clearCredentials()
130 {
131 2 username.clear();
132 2 password.clear();
133
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 password_set = username_set = false;
134 2 }
135
136 1 void LoginManager::clearCredentials()
137 {
138 1 Q_D(LoginManager);
139 1 d->clearCredentials();
140 1 }
141
142 QString LoginManager::getErrorMessage()
143 {
144 Q_D(LoginManager);
145
146 switch (d->loginResult) {
147 case PdCom::SimpleLoginManager::LoginResult::Success:
148 return tr("Login successful.");
149 case PdCom::SimpleLoginManager::LoginResult::Error:
150 return tr("Login failed: Please check"
151 " the given username and password.");
152 case PdCom::SimpleLoginManager::LoginResult::Canceled:
153 return tr("Login was aborted.");
154 case PdCom::SimpleLoginManager::LoginResult::NoSaslMechanism:
155 return tr("Login failed: No authentication mechanism found.");
156 default:
157 return tr("Unknown login error");
158 }
159 }
160