]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - tests/src/test-qt-greeter.cpp
First pass at reset support
[sojka/lightdm.git] / tests / src / test-qt-greeter.cpp
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <signal.h>
5 #include <glib-object.h>
6 #include <xcb/xcb.h>
7 #include <QLightDM/Greeter>
8 #include <QLightDM/Power>
9 #include <QLightDM/UsersModel>
10 #include <QtCore/QSettings>
11 #include <QtCore/QDebug>
12 #include <QtCore/QCoreApplication>
13
14 #include "test-qt-greeter.h"
15 #include "status.h"
16
17 static gchar *greeter_id;
18 static QCoreApplication *app = NULL;
19 static QSettings *config = NULL;
20 static QLightDM::PowerInterface *power = NULL;
21 static TestGreeter *greeter = NULL;
22 static QLightDM::UsersModel *users_model = NULL;
23
24 TestGreeter::TestGreeter ()
25 {
26     connect (this, SIGNAL(showMessage(QString, QLightDM::Greeter::MessageType)), SLOT(showMessage(QString, QLightDM::Greeter::MessageType)));
27     connect (this, SIGNAL(showPrompt(QString, QLightDM::Greeter::PromptType)), SLOT(showPrompt(QString, QLightDM::Greeter::PromptType)));
28     connect (this, SIGNAL(authenticationComplete()), SLOT(authenticationComplete()));
29     connect (this, SIGNAL(autologinTimerExpired()), SLOT(autologinTimerExpired()));
30 }
31
32 void TestGreeter::showMessage (QString text, QLightDM::Greeter::MessageType type)
33 {
34     status_notify ("%s SHOW-MESSAGE TEXT=\"%s\"", greeter_id, text.toAscii ().constData ());
35 }
36
37 void TestGreeter::showPrompt (QString text, QLightDM::Greeter::PromptType type)
38 {
39     status_notify ("%s SHOW-PROMPT TEXT=\"%s\"", greeter_id, text.toAscii ().constData ());
40 }
41
42 void TestGreeter::authenticationComplete ()
43 {
44     if (authenticationUser () != "")
45         status_notify ("%s AUTHENTICATION-COMPLETE USERNAME=%s AUTHENTICATED=%s",
46                        greeter_id,
47                        authenticationUser ().toAscii ().constData (), isAuthenticated () ? "TRUE" : "FALSE");
48     else
49         status_notify ("%s AUTHENTICATION-COMPLETE AUTHENTICATED=%s", greeter_id, isAuthenticated () ? "TRUE" : "FALSE");
50 }
51
52 void TestGreeter::autologinTimerExpired ()
53 {
54     status_notify ("%s AUTOLOGIN-TIMER-EXPIRED", greeter_id);
55 }
56
57 void TestGreeter::printHints ()
58 {
59     if (selectUserHint() != "")
60         status_notify ("%s SELECT-USER-HINT USERNAME=%s", greeter_id, greeter->selectUserHint ().toAscii ().constData ());
61     if (selectGuestHint())
62         status_notify ("%s SELECT-GUEST-HINT", greeter_id);
63     if (lockHint())
64         status_notify ("%s LOCK-HINT", greeter_id);
65     if (!hasGuestAccountHint ())
66         status_notify ("%s HAS-GUEST-ACCOUNT-HINT=FALSE", greeter_id);
67     if (hideUsersHint ())
68         status_notify ("%s HIDE-USERS-HINT", greeter_id);
69     if (showManualLoginHint ())
70         status_notify ("%s SHOW-MANUAL-LOGIN-HINT", greeter_id);
71     if (!showRemoteLoginHint ())
72         status_notify ("%s SHOW-REMOTE-LOGIN-HINT=FALSE", greeter_id);
73
74 }
75
76 void TestGreeter::idle ()
77 {
78     status_notify ("%s IDLE", greeter_id);
79 }
80
81 void TestGreeter::reset ()
82 {
83     status_notify ("%s RESET", greeter_id);
84     printHints ();
85 }
86
87 void TestGreeter::userRowsInserted (const QModelIndex & parent, int start, int end)
88 {
89     for (int i = start; i <= end; i++)
90     {
91         QString name = users_model->data (users_model->index (i, 0), QLightDM::UsersModel::NameRole).toString ();
92         status_notify ("%s USER-ADDED USERNAME=%s", greeter_id, qPrintable (name));
93     }
94 }
95
96 void TestGreeter::userRowsRemoved (const QModelIndex & parent, int start, int end)
97 {
98     for (int i = start; i <= end; i++)
99     {
100         QString name = users_model->data (users_model->index (i, 0), QLightDM::UsersModel::NameRole).toString ();
101         status_notify ("%s USER-REMOVED USERNAME=%s", greeter_id, qPrintable (name));
102     }
103 }
104
105 static void
106 signal_cb (int signum)
107 {
108     status_notify ("%s TERMINATE SIGNAL=%d", greeter_id, signum);
109     _exit (EXIT_SUCCESS);
110 }
111
112 static void
113 request_cb (const gchar *name, GHashTable *params)
114 {
115     gchar *r;
116
117     if (!name)
118     {
119         app->quit ();
120         return;
121     }
122   
123     if (strcmp (name, "AUTHENTICATE") == 0)
124     {
125         if (g_hash_table_lookup (params, "USERNAME"))
126             greeter->authenticate ((const gchar *) g_hash_table_lookup (params, "USERNAME"));
127         else
128             greeter->authenticate ();
129     }
130
131     else if (strcmp (name, "AUTHENTICATE-GUEST") == 0)
132         greeter->authenticateAsGuest ();
133   
134     else if (strcmp (name, "AUTHENTICATE-AUTOLOGIN") == 0)
135         greeter->authenticateAutologin ();
136
137     else if (strcmp (name, "AUTHENTICATE-REMOTE") == 0)
138         greeter->authenticateRemote ((const gchar *) g_hash_table_lookup (params, "SESSION"), NULL);
139
140     else if (strcmp (name, "RESPOND") == 0)
141         greeter->respond ((const gchar *) g_hash_table_lookup (params, "TEXT"));
142
143     else if (strcmp (name, "CANCEL-AUTHENTICATION") == 0)
144         greeter->cancelAuthentication ();
145
146     else if (strcmp (name, "START-SESSION") == 0)
147     {
148         if (g_hash_table_lookup (params, "SESSION"))
149         {
150             if (!greeter->startSessionSync ((const gchar *) g_hash_table_lookup (params, "SESSION")))
151                 status_notify ("%s SESSION-FAILED", greeter_id);
152         }
153         else
154         {
155             if (!greeter->startSessionSync ())
156                 status_notify ("%s SESSION-FAILED", greeter_id);
157         }
158     }
159
160     else if (strcmp (name, "LOG-USER-LIST-LENGTH") == 0)
161         status_notify ("%s LOG-USER-LIST-LENGTH N=%d", greeter_id, users_model->rowCount (QModelIndex ()));
162
163     else if (strcmp (name, "LOG-USER") == 0)
164     {
165         const gchar *username = (const gchar *) g_hash_table_lookup (params, "USERNAME");
166         for (int i = 0; i < users_model->rowCount (QModelIndex ()); i++)
167         {
168             QString name = users_model->data (users_model->index (i, 0), QLightDM::UsersModel::NameRole).toString ();
169             if (name == username)
170                 status_notify ("%s LOG-USER USERNAME=%s", greeter_id, qPrintable (name));
171         }
172     }
173
174     else if (strcmp (name, "LOG-USER-LIST") == 0)
175     {
176         for (int i = 0; i < users_model->rowCount (QModelIndex ()); i++)
177         {
178             QString name = users_model->data (users_model->index (i, 0), QLightDM::UsersModel::NameRole).toString ();
179             status_notify ("%s LOG-USER USERNAME=%s", greeter_id, qPrintable (name));
180         }
181     }
182
183     else if (strcmp (name, "GET-CAN-SUSPEND") == 0)
184     {
185         gboolean can_suspend = power->canSuspend ();
186         status_notify ("%s CAN-SUSPEND ALLOWED=%s", greeter_id, can_suspend ? "TRUE" : "FALSE");
187     }
188
189     else if (strcmp (name, "SUSPEND") == 0)
190     {
191         if (!power->suspend ())
192             status_notify ("%s FAIL-SUSPEND", greeter_id);
193     }
194
195     else if (strcmp (name, "GET-CAN-HIBERNATE") == 0)
196     {
197         gboolean can_hibernate = power->canHibernate ();
198         status_notify ("%s CAN-HIBERNATE ALLOWED=%s", greeter_id, can_hibernate ? "TRUE" : "FALSE");
199     }
200
201     else if (strcmp (name, "HIBERNATE") == 0)
202     {
203         if (!power->hibernate ())
204             status_notify ("%s FAIL-HIBERNATE", greeter_id);
205     }
206
207     else if (strcmp (name, "GET-CAN-RESTART") == 0)
208     {
209         gboolean can_restart = power->canRestart ();
210         status_notify ("%s CAN-RESTART ALLOWED=%s", greeter_id, can_restart ? "TRUE" : "FALSE");
211     }
212
213     else if (strcmp (name, "RESTART") == 0)
214     {
215         if (!power->restart ())
216             status_notify ("%s FAIL-RESTART", greeter_id);
217     }
218
219     else if (strcmp (name, "GET-CAN-SHUTDOWN") == 0)
220     {
221         gboolean can_shutdown = power->canShutdown ();
222         status_notify ("%s CAN-SHUTDOWN ALLOWED=%s", greeter_id, can_shutdown ? "TRUE" : "FALSE");
223     }
224
225     else if (strcmp (name, "SHUTDOWN") == 0)
226     {
227         if (!power->shutdown ())
228             status_notify ("%s FAIL-SHUTDOWN", greeter_id);
229     }
230 }
231
232 int
233 main(int argc, char *argv[])
234 {
235     gchar *display, *xdg_seat, *xdg_vtnr, *xdg_session_cookie, *xdg_session_class;
236     GString *status_text;   
237
238 #if !defined(GLIB_VERSION_2_36)
239     g_type_init ();
240 #endif
241
242     display = getenv ("DISPLAY");
243     xdg_seat = getenv ("XDG_SEAT");
244     xdg_vtnr = getenv ("XDG_VTNR");
245     xdg_session_cookie = getenv ("XDG_SESSION_COOKIE");
246     xdg_session_class = getenv ("XDG_SESSION_CLASS");
247     if (display)
248     {
249         if (display[0] == ':')
250             greeter_id = g_strdup_printf ("GREETER-X-%s", display + 1);
251         else
252             greeter_id = g_strdup_printf ("GREETER-X-%s", display);
253     }
254     else
255         greeter_id = g_strdup ("GREETER-?");
256
257     status_connect (request_cb, greeter_id);
258
259     app = new QCoreApplication (argc, argv);
260
261     signal (SIGINT, signal_cb);
262     signal (SIGTERM, signal_cb);
263
264     status_text = g_string_new ("");
265     g_string_printf (status_text, "%s START", greeter_id);
266     if (xdg_seat)
267         g_string_append_printf (status_text, " XDG_SEAT=%s", xdg_seat);
268     if (xdg_vtnr)
269         g_string_append_printf (status_text, " XDG_VTNR=%s", xdg_vtnr);
270     if (xdg_session_cookie)
271         g_string_append_printf (status_text, " XDG_SESSION_COOKIE=%s", xdg_session_cookie);
272     if (xdg_session_class)
273         g_string_append_printf (status_text, " XDG_SESSION_CLASS=%s", xdg_session_class);
274     status_notify (status_text->str);
275     g_string_free (status_text, TRUE);
276
277     config = new QSettings (g_build_filename (getenv ("LIGHTDM_TEST_ROOT"), "script", NULL), QSettings::IniFormat);
278
279     if (display)
280     {
281         xcb_connection_t *connection = xcb_connect (NULL, NULL);
282         if (xcb_connection_has_error (connection))
283         {
284             status_notify ("%s FAIL-CONNECT-XSERVER", greeter_id);
285             return EXIT_FAILURE;
286         }
287         status_notify ("%s CONNECT-XSERVER", greeter_id);
288     }
289
290     power = new QLightDM::PowerInterface();
291
292     greeter = new TestGreeter();
293
294     users_model = new QLightDM::UsersModel();
295     if (config->value ("test-greeter-config/log-user-changes", "false") == "true")
296     {
297         QObject::connect (users_model, SIGNAL(rowsInserted(const QModelIndex&, int, int)), greeter, SLOT(userRowsInserted(const QModelIndex&, int, int)));
298         QObject::connect (users_model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex&, int, int)), greeter, SLOT(userRowsRemoved(const QModelIndex&, int, int)));
299     }
300
301     if (config->value ("test-greeter-config/resettable", "false") == "true")
302     {
303         greeter->setResettable (true);
304         QObject::connect (greeter, SIGNAL(idle()), greeter, SLOT(idle()));
305         QObject::connect (greeter, SIGNAL(reset()), greeter, SLOT(reset()));
306     }
307
308     status_notify ("%s CONNECT-TO-DAEMON", greeter_id);
309     if (!greeter->connectSync())
310     {
311         status_notify ("%s FAIL-CONNECT-DAEMON", greeter_id);
312         return EXIT_FAILURE;
313     }
314
315     status_notify ("%s CONNECTED-TO-DAEMON", greeter_id);
316
317     greeter->printHints();
318
319     return app->exec();
320 }