]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - greeters/qt/lightdm-example-qt-greeter.cpp
Add example to the name of all the greeters
[sojka/lightdm.git] / greeters / qt / lightdm-example-qt-greeter.cpp
1 #include "lightdm-example-qt-greeter.h"
2
3 #include <QtGui/QApplication>
4 #include <QtGui/QLabel>
5 #include <QtGui/QLineEdit>
6 #include <QtGui/QPushButton>
7 #include <QtGui/QGridLayout>
8
9 #include "ldmgreeter.h"
10
11 LoginDialog::LoginDialog() : QDialog()
12 {
13     label = new QLabel("Username:", this);
14     entry = new QLineEdit(this);
15     connect(entry, SIGNAL(returnPressed()), this, SLOT(onLogin()));  
16
17     QPushButton *button = new QPushButton("Login", this);
18     connect(button, SIGNAL(clicked()), this, SLOT(onLogin()));
19
20     QGridLayout *layout = new QGridLayout(this);
21     layout->addWidget(label, 0, 0, 1, 1);
22     layout->addWidget(entry, 1, 0, 2, 1);
23     layout->addWidget(button, 2, 0, 3, 1);
24     setLayout(layout);
25
26     greeter = new LdmGreeter; //FIXME this LEAKS! Either finish the QWidget subclass plan, or add parent arg to LdmGreeter.
27     connect(greeter, SIGNAL(showPrompt(QString)), this, SLOT(showPrompt(QString)));
28     connect(greeter, SIGNAL(showMessage(QString)), this, SLOT(showMessage(QString)));
29     connect(greeter, SIGNAL(showError(QString)), this, SLOT(showError(QString)));
30     connect(greeter, SIGNAL(authenticationComplete()), this, SLOT(authenticationComplete()));
31     connect(greeter, SIGNAL(quit()), this, SLOT(quit()));
32     greeter->connectToServer();
33 }
34
35 void LoginDialog::onLogin()
36 {
37     if(greeter->inAuthentication()) {
38         if(inPrompt) {
39             greeter->provideSecret(entry->text());
40         }
41         inPrompt = false;
42         entry->setText("");
43         entry->setEchoMode(QLineEdit::Normal);
44     }
45     else {
46         greeter->startAuthentication(entry->text());
47     }
48 }
49
50 void LoginDialog::showPrompt(QString text)
51 {
52     entry->setText("");
53     entry->setEchoMode(QLineEdit::Password);
54     label->setText(text);
55     inPrompt = true;
56 }
57
58 void LoginDialog::showMessage(QString text)
59 {    
60     label->setText(text);
61 }
62
63 void LoginDialog::showError(QString text)
64 {
65      label->setText(text);
66 }
67
68 void LoginDialog::authenticationComplete()
69 {
70     entry->setText("");
71     if(greeter->isAuthenticated()) {
72         greeter->login(greeter->authenticationUser(), greeter->defaultSession(), greeter->defaultLanguage());
73     }
74     else {
75         label->setText("Failed to authenticate");
76     }
77 }
78
79 void LoginDialog::quit()
80 {
81     exit(0);
82 }
83
84 int main(int argc, char *argv[])
85 {
86     QApplication a(argc, argv);
87
88     LoginDialog d;
89     d.show();
90
91     return a.exec();
92 }