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