Unity 8
Greeter.cpp
1 /*
2  * Copyright (C) 2013, 2015 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "Greeter.h"
19 #include "GreeterPrivate.h"
20 #include <libintl.h>
21 
22 GreeterPrivate::GreeterPrivate(Greeter* parent)
23  : m_greeter(new QLightDM::Greeter(parent)),
24  m_active(false),
25  wasPrompted(false),
26  promptless(false),
27  q_ptr(parent)
28 {
29 }
30 
31 Greeter::Greeter(QObject* parent)
32  : QObject(parent),
33  d_ptr(new GreeterPrivate(this))
34 {
35  Q_D(Greeter);
36 
37  connect(d->m_greeter, &QLightDM::Greeter::showMessage,
38  this, &Greeter::showMessageFilter);
39  connect(d->m_greeter, &QLightDM::Greeter::showPrompt,
40  this, &Greeter::showPromptFilter);
41  connect(d->m_greeter, &QLightDM::Greeter::authenticationComplete,
42  this, &Greeter::authenticationCompleteFilter);
43 
44  d->m_greeter->connectSync();
45 }
46 
47 bool Greeter::isActive() const
48 {
49  Q_D(const Greeter);
50  return d->m_active;
51 }
52 
53 void Greeter::setIsActive(bool active)
54 {
55  Q_D(Greeter);
56  if (d->m_active != active) {
57  d->m_active = active;
58  Q_EMIT isActiveChanged();
59  }
60 }
61 
62 bool Greeter::isAuthenticated() const
63 {
64  Q_D(const Greeter);
65  return d->m_greeter->isAuthenticated();
66 }
67 
68 QString Greeter::authenticationUser() const
69 {
70  Q_D(const Greeter);
71  return d->m_greeter->authenticationUser();
72 }
73 
74 QString Greeter::defaultSessionHint() const
75 {
76  Q_D(const Greeter);
77  return d->m_greeter->defaultSessionHint();
78 }
79 
80 bool Greeter::promptless() const
81 {
82  Q_D(const Greeter);
83  return d->promptless;
84 }
85 
86 QString Greeter::selectUser() const
87 {
88  Q_D(const Greeter);
89  return d->m_greeter->selectUserHint();
90 }
91 
92 void Greeter::authenticate(const QString &username)
93 {
94  Q_D(Greeter);
95  d->wasPrompted = false;
96  if (d->promptless) {
97  d->promptless = false;
98  Q_EMIT promptlessChanged();
99  }
100 
101  d->m_greeter->authenticate(username);
102  Q_EMIT isAuthenticatedChanged();
103  Q_EMIT authenticationUserChanged(username);
104 }
105 
106 void Greeter::respond(const QString &response)
107 {
108  Q_D(Greeter);
109  d->m_greeter->respond(response);
110 }
111 
112 bool Greeter::startSessionSync(const QString &session)
113 {
114  Q_D(Greeter);
115  return d->m_greeter->startSessionSync(session);
116 }
117 
118 void Greeter::showPromptFilter(const QString &text, QLightDM::Greeter::PromptType type)
119 {
120  Q_D(Greeter);
121  d->wasPrompted = true;
122 
123  bool isDefaultPrompt = (text == dgettext("Linux-PAM", "Password: "));
124 
125  // Strip prompt of any colons at the end
126  QString trimmedText = text.trimmed();
127  if (trimmedText.endsWith(':') || trimmedText.endsWith(QStringLiteral(":"))) {
128  trimmedText.chop(1);
129  }
130 
131  Q_EMIT showPrompt(trimmedText, type == QLightDM::Greeter::PromptTypeSecret, isDefaultPrompt);
132 }
133 
134 void Greeter::showMessageFilter(const QString &text, QLightDM::Greeter::MessageType type)
135 {
136  Q_EMIT showMessage(text, type == QLightDM::Greeter::MessageTypeError);
137 }
138 
139 void Greeter::authenticationCompleteFilter()
140 {
141  Q_D(Greeter);
142  if (!d->wasPrompted) {
143  d->promptless = true;
144  Q_EMIT promptlessChanged();
145  }
146 
147  Q_EMIT isAuthenticatedChanged();
148  Q_EMIT authenticationComplete();
149 }