2 * Copyright (C) 2011 Didier Roche.
3 * Author: Didier Roche <didrocks@ubuntu.com>
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
20 #include <glib/gi18n.h>
22 #define SEATDEFAULT_KEY_GROUP "SeatDefaults"
23 #define TYPE_KEY_NAME "type"
24 #define SESSION_KEY_NAME "user-session"
25 #define GREETER_KEY_NAME "greeter-session"
26 #define AUTOLOGIN_KEY_NAME "autologin-user"
27 #define HIDE_USERS_KEY_NAME "greeter-hide-users"
28 #define MANUAL_LOGIN_KEY_NAME "greeter-show-manual-login"
29 #define REMOTE_LOGIN_KEY_NAME "greeter-show-remote-login"
30 #define ALLOW_GUEST_KEY_NAME "allow-guest"
32 #define IS_STRING_EMPTY(x) ((x)==NULL||(x)[0]=='\0')
34 static gboolean debug = FALSE;
35 static gboolean keep_old = FALSE;
36 static gboolean remove = FALSE;
37 static gboolean hide_users = FALSE;
38 static gboolean show_manual_login = FALSE;
39 static gboolean show_remote_login = FALSE;
40 static gboolean allow_guest = FALSE;
42 static char *type = NULL;
43 static char *session = NULL;
44 static char *greeter = NULL;
45 static char *autologin = NULL;
46 static char *str_hide_users = NULL;
47 static char *str_show_manual_login = NULL;
48 static char *str_show_remote_login = NULL;
49 static char *str_allow_guest = NULL;
51 static GOptionEntry entries[] =
53 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging"), NULL },
54 { "keep-old", 'k', 0, G_OPTION_ARG_NONE, &keep_old, N_("Only update if no default already set"), NULL },
55 { "remove", 'r', 0, G_OPTION_ARG_NONE, &remove, N_("Remove default value if it's the current one"), NULL },
56 { "type", 't', 0, G_OPTION_ARG_STRING, &type, N_("Set default seat type"), NULL },
57 { "session", 's', 0, G_OPTION_ARG_STRING, &session, N_("Set default session"), NULL },
58 { "greeter", 'g', 0, G_OPTION_ARG_STRING, &greeter, N_("Set default greeter"), NULL },
59 { "autologin",'a', 0, G_OPTION_ARG_STRING, &autologin, N_("Set autologin user"), NULL },
60 { "hide-users",'i', 0, G_OPTION_ARG_STRING, &str_hide_users, N_("Set greeter-hide-users to true or false"), NULL },
61 { "show-manual-login",'m', 0, G_OPTION_ARG_STRING, &str_show_manual_login, N_("Set show-manual-login to true or false"), NULL },
62 { "show-remote-login",'R', 0, G_OPTION_ARG_STRING, &str_show_remote_login, N_("Set show-remote-login to true or false"), NULL },
63 { "allow-guest",'l', 0, G_OPTION_ARG_STRING, &str_allow_guest, N_("Set allow-guest to true or false"), NULL },
68 show_nothing(const gchar *log_domain,
69 GLogLevelFlags log_level,
71 gpointer unused_data) {};
74 update_boolean(const gboolean new_value,
76 const gchar *key_group,
77 const gchar *key_name,
81 g_debug ("keep-old mode: keep previous default value");
83 g_debug ("Update to %d for %s", new_value, key_name);
84 g_key_file_set_boolean (keyfile, key_group, key_name, new_value);
90 update_string(const gchar *default_value,
91 const gchar *new_value,
94 const gchar *key_group,
95 const gchar *key_name,
98 if (!(default_value) || (strlen(default_value) < 1)) {
99 g_debug ("No existing valid value for %s. Set to %s", key_name, new_value);
100 g_key_file_set_string (keyfile, key_group, key_name, new_value);
104 if (g_strcmp0 (default_value, new_value) == 0) {
105 g_debug ("Remove %s as default value for %s", default_value, key_name);
106 g_key_file_set_string (keyfile, key_group, key_name, "");
109 g_debug ("Can't remove: %s is not the default value for %s", default_value, key_name);
113 g_debug ("Found existing default value(%s) for %s", default_value, key_name);
115 g_debug ("keep-old mode: keep previous default value");
117 g_debug ("Update to %s for %s", new_value, key_name);
118 g_key_file_set_string (keyfile, key_group, key_name, new_value);
126 str_to_bool(const gchar *str, gboolean *bool_out)
128 if (IS_STRING_EMPTY(str)) {
131 else if (strncasecmp(str, "true", 4)==0) {
135 else if (strncasecmp(str, "false", 5)==0) {
145 main (int argc, char *argv[])
147 GOptionContext *context = NULL;
148 GError *error = NULL;
154 const gchar *gdm_conf_file = CONFIG_DIR "/lightdm.conf";
156 gchar *default_type = NULL;
157 gchar *default_session = NULL;
158 gchar *default_greeter = NULL;
159 gchar *default_autologin = NULL;
160 gint return_code = 0;
162 bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
163 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
164 textdomain (GETTEXT_PACKAGE);
166 #if !defined(GLIB_VERSION_2_36)
170 context = g_option_context_new (N_("- set lightdm default values"));
171 g_option_context_add_main_entries (context, entries, NULL);
172 if (!g_option_context_parse (context, &argc, &argv, &error)) {
173 g_printerr (N_("option parsing failed: %s\n"), error->message);
174 g_option_context_free (context);
175 g_error_free (error);
178 if (IS_STRING_EMPTY (type) && IS_STRING_EMPTY (session) && IS_STRING_EMPTY (greeter) && IS_STRING_EMPTY (autologin) && IS_STRING_EMPTY(str_hide_users) && IS_STRING_EMPTY(str_show_manual_login) && IS_STRING_EMPTY(str_show_remote_login) && IS_STRING_EMPTY(str_allow_guest)) {
179 g_printerr (N_("Wrong usage of the command\n%s"), g_option_context_get_help (context, FALSE, NULL));
180 g_option_context_free (context);
184 g_option_context_free (context);
186 g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, show_nothing, NULL);
188 keyfile = g_key_file_new ();
189 flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
190 if (!(g_key_file_load_from_file (keyfile, gdm_conf_file, flags, &error))) {
191 g_debug ("File doesn't seem to exist or can't be read: create one (%s)", error->message);
192 g_error_free (error);
196 // try to get the right keys
197 default_type = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, TYPE_KEY_NAME, NULL);
198 default_session = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, SESSION_KEY_NAME, NULL);
199 default_greeter = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, GREETER_KEY_NAME, NULL);
200 default_autologin = g_key_file_get_string (keyfile, SEATDEFAULT_KEY_GROUP, AUTOLOGIN_KEY_NAME, NULL);
202 if (!(IS_STRING_EMPTY (type)))
203 return_code = update_string (default_type, type, keep_old, remove, SEATDEFAULT_KEY_GROUP, TYPE_KEY_NAME, keyfile);
204 if (!(IS_STRING_EMPTY (session)))
205 return_code = update_string (default_session, session, keep_old, remove, SEATDEFAULT_KEY_GROUP, SESSION_KEY_NAME, keyfile);
206 if (!(IS_STRING_EMPTY (greeter)) && (return_code == 0))
207 return_code = update_string (default_greeter, greeter, keep_old, remove, SEATDEFAULT_KEY_GROUP, GREETER_KEY_NAME, keyfile);
208 if (!(IS_STRING_EMPTY (autologin)) && (return_code == 0))
209 return_code = update_string (default_autologin, autologin, keep_old, remove, SEATDEFAULT_KEY_GROUP, AUTOLOGIN_KEY_NAME, keyfile);
210 if (!(IS_STRING_EMPTY(str_hide_users)) && (return_code == 0)) {
211 if (str_to_bool(str_hide_users, &hide_users) == 0) {
212 return_code = update_boolean (hide_users, keep_old, SEATDEFAULT_KEY_GROUP, HIDE_USERS_KEY_NAME, keyfile);
215 g_printerr (N_("true and false are the only valid choices for hide-users\n"));
219 if (!(IS_STRING_EMPTY(str_allow_guest)) && (return_code == 0)) {
220 if (str_to_bool(str_allow_guest, &allow_guest) == 0) {
221 return_code = update_boolean (allow_guest, keep_old, SEATDEFAULT_KEY_GROUP, ALLOW_GUEST_KEY_NAME, keyfile);
224 g_printerr (N_("true and false are the only valid choices for allow-guest\n"));
228 if (!(IS_STRING_EMPTY(str_show_manual_login)) && (return_code == 0)) {
229 if (str_to_bool(str_show_manual_login, &show_manual_login) == 0) {
230 return_code = update_boolean (show_manual_login, keep_old, SEATDEFAULT_KEY_GROUP, MANUAL_LOGIN_KEY_NAME, keyfile);
233 g_printerr (N_("true and false are the only valid choices for show-manual-login\n"));
237 if (!(IS_STRING_EMPTY(str_show_remote_login)) && (return_code == 0)) {
238 if (str_to_bool(str_show_remote_login, &show_remote_login) == 0) {
239 return_code = update_boolean (show_remote_login, keep_old, SEATDEFAULT_KEY_GROUP, REMOTE_LOGIN_KEY_NAME, keyfile);
242 g_printerr (N_("true and false are the only valid choices for show-remote-login\n"));
247 if (return_code == 0) {
248 s_data = g_key_file_to_data (keyfile, &size, &error);
250 g_debug ("Can't convert data to string: %s", error->message);
251 g_error_free (error);
255 if(!g_file_set_contents (gdm_conf_file, s_data, size, &error)) {
256 g_printerr ("Can't update: %s\n", error->message);
257 g_error_free (error);
264 g_key_file_free (keyfile);
267 g_free (default_type);
269 g_free (default_session);
271 g_free (default_greeter);
272 if (default_autologin)
273 g_free (default_autologin);