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