Unity 8
Greeter.cpp
1 /*
2  * Copyright (C) 2013 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  * Author: Michael Terry <michael.terry@canonical.com>
17  */
18 
19 #include "Greeter.h"
20 #include "GreeterPrivate.h"
21 #include <libintl.h>
22 
23 GreeterPrivate::GreeterPrivate(Greeter* parent)
24  : m_greeter(new QLightDM::Greeter(parent)),
25  m_active(false),
26  wasPrompted(false),
27  promptless(false),
28  q_ptr(parent)
29 {
30 }
31 
32 Greeter::Greeter(QObject* parent)
33  : QObject(parent),
34  d_ptr(new GreeterPrivate(this))
35 {
36  Q_D(Greeter);
37 
38  connect(d->m_greeter, &QLightDM::Greeter::showMessage,
39  this, &Greeter::showMessageFilter);
40  connect(d->m_greeter, &QLightDM::Greeter::showPrompt,
41  this, &Greeter::showPromptFilter);
42  connect(d->m_greeter, &QLightDM::Greeter::authenticationComplete,
43  this, &Greeter::authenticationCompleteFilter);
44 
45  d->m_greeter->connectSync();
46 }
47 
48 bool Greeter::isActive() const
49 {
50  Q_D(const Greeter);
51  return d->m_active;
52 }
53 
54 void Greeter::setIsActive(bool active)
55 {
56  Q_D(Greeter);
57  if (d->m_active != active) {
58  d->m_active = active;
59  Q_EMIT isActiveChanged();
60  }
61 }
62 
63 bool Greeter::isAuthenticated() const
64 {
65  Q_D(const Greeter);
66  return d->m_greeter->isAuthenticated();
67 }
68 
69 QString Greeter::authenticationUser() const
70 {
71  Q_D(const Greeter);
72  return d->m_greeter->authenticationUser();
73 }
74 
75 bool Greeter::promptless() const
76 {
77  Q_D(const Greeter);
78  return d->promptless;
79 }
80 
81 void Greeter::authenticate(const QString &username)
82 {
83  Q_D(Greeter);
84  d->wasPrompted = false;
85  if (d->promptless) {
86  d->promptless = false;
87  Q_EMIT promptlessChanged();
88  }
89 
90  d->m_greeter->authenticate(username);
91  Q_EMIT isAuthenticatedChanged();
92  Q_EMIT authenticationUserChanged(username);
93 }
94 
95 void Greeter::respond(const QString &response)
96 {
97  Q_D(Greeter);
98  d->m_greeter->respond(response);
99 }
100 
101 bool Greeter::startSessionSync(const QString &session)
102 {
103  Q_D(Greeter);
104  return d->m_greeter->startSessionSync(session);
105 }
106 
107 void Greeter::showPromptFilter(const QString &text, QLightDM::Greeter::PromptType type)
108 {
109  Q_D(Greeter);
110  d->wasPrompted = true;
111 
112  bool isDefaultPrompt = (text == dgettext("Linux-PAM", "Password: "));
113 
114  // Strip prompt of any colons at the end
115  QString trimmedText = text.trimmed();
116  if (trimmedText.endsWith(':') || trimmedText.endsWith(QStringLiteral(":"))) {
117  trimmedText.chop(1);
118  }
119 
120  Q_EMIT showPrompt(trimmedText, type == QLightDM::Greeter::PromptTypeSecret, isDefaultPrompt);
121 }
122 
123 void Greeter::showMessageFilter(const QString &text, QLightDM::Greeter::MessageType type)
124 {
125  Q_EMIT showMessage(text, type == QLightDM::Greeter::MessageTypeError);
126 }
127 
128 void Greeter::authenticationCompleteFilter()
129 {
130  Q_D(Greeter);
131  if (!d->wasPrompted) {
132  d->promptless = true;
133  Q_EMIT promptlessChanged();
134  }
135 
136  Q_EMIT isAuthenticatedChanged();
137  Q_EMIT authenticationComplete();
138 }