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 wasPrompted;
29  bool promptless;
30 
31 protected:
32  Greeter * const q_ptr;
33 
34 private:
35  Q_DECLARE_PUBLIC(Greeter)
36 };
37 
38 
39 GreeterPrivate::GreeterPrivate(Greeter* parent)
40  : m_greeter(new QLightDM::Greeter(parent)),
41  wasPrompted(false),
42  promptless(false),
43  q_ptr(parent)
44 {
45 }
46 
47 Greeter::Greeter(QObject* parent)
48  : QObject(parent),
49  d_ptr(new GreeterPrivate(this))
50 {
51  Q_D(Greeter);
52 
53  connect(d->m_greeter, SIGNAL(showMessage(QString, QLightDM::Greeter::MessageType)),
54  this, SLOT(showMessageFilter(QString, QLightDM::Greeter::MessageType)));
55  connect(d->m_greeter, SIGNAL(showPrompt(QString, QLightDM::Greeter::PromptType)),
56  this, SLOT(showPromptFilter(QString, QLightDM::Greeter::PromptType)));
57  connect(d->m_greeter, SIGNAL(authenticationComplete()),
58  this, SLOT(authenticationCompleteFilter()));
59 
60  d->m_greeter->connectSync();
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 authenticationUserChanged(username);
92 }
93 
94 void Greeter::respond(const QString &response)
95 {
96  Q_D(Greeter);
97  d->m_greeter->respond(response);
98 }
99 
100 bool Greeter::startSessionSync(const QString &session)
101 {
102  Q_D(Greeter);
103  return d->m_greeter->startSessionSync(session);
104 }
105 
106 void Greeter::showPromptFilter(const QString &text, QLightDM::Greeter::PromptType type)
107 {
108  Q_D(Greeter);
109  d->wasPrompted = true;
110 
111  // Strip prompt of any colons at the end
112  QString trimmedText = text.trimmed();
113  if (trimmedText.endsWith(":") || trimmedText.endsWith(":")) {
114  trimmedText.chop(1);
115  }
116 
117  Q_EMIT showPrompt(trimmedText, type == QLightDM::Greeter::PromptTypeSecret);
118 }
119 
120 void Greeter::showMessageFilter(const QString &text, QLightDM::Greeter::MessageType type)
121 {
122  Q_EMIT showMessage(text, type == QLightDM::Greeter::MessageTypeError);
123 }
124 
125 void Greeter::authenticationCompleteFilter()
126 {
127  Q_D(Greeter);
128  if (!d->wasPrompted) {
129  d->promptless = true;
130  Q_EMIT promptlessChanged();
131  }
132 
133  Q_EMIT authenticationComplete();
134 }