]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - utils/lightdm-set-defaults.c
Add a dm-tool man page.
[sojka/lightdm.git] / utils / lightdm-set-defaults.c
1 /*
2  * Copyright (C) 2011 Didier Roche.
3  * Author: Didier Roche <didrocks@ubuntu.com>
4  * 
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
9  * license.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdlib.h>
17 #include <unistd.h>
18
19 #include <glib.h>
20 #include <glib/gi18n.h>
21
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"
31
32 #define IS_STRING_EMPTY(x) ((x)==NULL||(x)[0]=='\0')
33
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;
41
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;
50
51 static GOptionEntry entries[] =
52 {
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 },
64   { NULL }
65 };
66
67 void
68 show_nothing(const gchar   *log_domain,
69              GLogLevelFlags log_level,
70              const gchar   *message,
71              gpointer       unused_data) {};
72
73 int
74 update_boolean(const gboolean new_value,
75               gboolean     keep_old,
76               const gchar *key_group,
77               const gchar *key_name,
78               GKeyFile    *keyfile)
79 {
80     if (keep_old)
81         g_debug ("keep-old mode: keep previous default value");
82     else {
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);
85     }
86     return(0);
87 }
88
89 int
90 update_string(const gchar *default_value,
91               const gchar *new_value,
92               gboolean     keep_old,
93               gboolean     remove,
94               const gchar *key_group,
95               const gchar *key_name,
96               GKeyFile    *keyfile)
97 {
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);
101     }
102     else {
103         if (remove) {
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, "");
107                 return(0);
108             }
109             g_debug ("Can't remove: %s is not the default value for %s", default_value, key_name);
110             return(4);
111         }
112         else {
113             g_debug ("Found existing default value(%s) for %s", default_value, key_name);
114             if (keep_old)
115                 g_debug ("keep-old mode: keep previous default value");
116             else {
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);
119             }
120         }
121     }
122     return(0);
123 }
124
125 int 
126 str_to_bool(const gchar *str, gboolean *bool_out)
127 {
128     if (IS_STRING_EMPTY(str)) {
129         return -1;
130     }
131     else if (strncasecmp(str, "true", 4)==0) {
132         *bool_out = TRUE;
133         return 0;
134     }
135     else if (strncasecmp(str, "false", 5)==0) {
136         *bool_out = FALSE;
137         return 0;
138     }
139     else {
140         return -2;
141     }
142 }
143
144 int 
145 main (int argc, char *argv[])
146 {
147     GOptionContext *context = NULL;
148     GError         *error = NULL;
149
150     GKeyFile       *keyfile;
151     GKeyFileFlags   flags;
152     gchar          *s_data;
153     gsize           size;
154     const gchar    *gdm_conf_file = CONFIG_DIR "/lightdm.conf";
155
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;
161
162     bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
163     bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
164     textdomain (GETTEXT_PACKAGE);
165
166 #if !defined(GLIB_VERSION_2_36)
167     g_type_init ();
168 #endif
169
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);
176         return 1;
177     }
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);
181         return 1;
182     }
183     if (context)
184         g_option_context_free (context); 
185     if (!debug)
186         g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, show_nothing, NULL);
187
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);
193             error = NULL;
194     }
195
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);
201
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);
213         }
214         else {
215             g_printerr (N_("true and false are the only valid choices for hide-users\n"));
216             return 1;
217         }
218     }
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);
222         }
223         else {
224             g_printerr (N_("true and false are the only valid choices for allow-guest\n"));
225             return 1;
226         }
227     }
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);
231         }
232         else {
233             g_printerr (N_("true and false are the only valid choices for show-manual-login\n"));
234             return 1;
235         }
236     }
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);
240         }
241         else {
242             g_printerr (N_("true and false are the only valid choices for show-remote-login\n"));
243             return 1;
244         }
245     }
246
247     if (return_code == 0) {
248         s_data = g_key_file_to_data (keyfile, &size, &error);
249         if (!s_data) {
250             g_debug ("Can't convert data to string: %s", error->message);
251             g_error_free (error);
252             return_code = 1;
253         }
254         else {
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);
258                 return_code = 1;
259             }
260             g_free (s_data);
261          }
262     }
263
264     g_key_file_free (keyfile);
265
266     if (default_type)
267         g_free (default_type);
268     if (default_session)
269         g_free (default_session);
270     if (default_greeter)
271         g_free (default_greeter);
272     if (default_autologin)
273         g_free (default_autologin);
274
275     return return_code;
276
277 }