"Fossies" - the Fresh Open Source Software Archive 
Member "jpilot-2_0_1/install_user.c" (3 Apr 2021, 9608 Bytes) of package /linux/privat/jpilot-2_0_1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*******************************************************************************
2 * install_user.c
3 * A module of J-Pilot http://jpilot.org
4 *
5 * Copyright (C) 1999-2014 by Judd Montgomery
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
20
21 /********************************* Includes ***********************************/
22 #include "config.h"
23 #include <gtk/gtk.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pwd.h>
27
28 #include "i18n.h"
29 #include "utils.h"
30 #include "sync.h"
31 #include "prefs.h"
32 #include "jpilot.h"
33 #include "install_user.h"
34
35 /****************************** Prototypes ************************************/
36 struct install_dialog_data {
37 GtkWidget *user_entry;
38 GtkWidget *ID_entry;
39 int button_hit;
40 char user[128];
41 unsigned long id;
42 };
43
44 /****************************** Main Code *************************************/
45 /* Allocates memory and returns pointer, or NULL */
46 static char *jp_user_or_whoami(void)
47 {
48 struct passwd *pw_ent;
49 uid_t uid;
50 const char *svalue;
51 long char_set;
52
53 get_pref(PREF_CHAR_SET, &char_set, NULL);
54 get_pref(PREF_USER, NULL, &svalue);
55 if ((svalue) && (svalue[0])) {
56 /* Convert User Name stored in Palm character set */
57 return charset_p2newj(svalue, -1, char_set);
58 }
59
60 uid = geteuid();
61 pw_ent = getpwuid(uid);
62 if ((pw_ent) && (pw_ent->pw_name)) {
63 return strdup(pw_ent->pw_name);
64 }
65 return NULL;
66 }
67
68 static void cb_install_user_button(GtkWidget *widget, gpointer data)
69 {
70 GtkWidget *w;
71 struct install_dialog_data *Pdata;
72
73 w = gtk_widget_get_toplevel(widget);
74 Pdata = g_object_get_data(G_OBJECT(w), "install_dialog_data");
75 if (Pdata) {
76 Pdata->button_hit = GPOINTER_TO_INT(data);
77 if (Pdata->button_hit == DIALOG_SAID_1) {
78 g_strlcpy(Pdata->user,
79 gtk_entry_get_text(GTK_ENTRY(Pdata->user_entry)),
80 sizeof(Pdata->user));
81 sscanf(gtk_entry_get_text(GTK_ENTRY(Pdata->ID_entry)), "%lu",
82 &(Pdata->id));
83 }
84 }
85
86 gtk_widget_destroy(w);
87 }
88
89 static gboolean cb_destroy_dialog(GtkWidget *widget)
90 {
91 gtk_main_quit();
92
93 return FALSE;
94 }
95
96 static int dialog_install_user(GtkWindow *main_window,
97 char *user, int user_len,
98 unsigned long *user_id)
99 {
100 GtkWidget *button, *label;
101 GtkWidget *user_entry, *ID_entry;
102 GtkWidget *install_user_dialog;
103 GtkWidget *vbox;
104 GtkWidget *hbox;
105 /* object data */
106 struct install_dialog_data data;
107 unsigned long id;
108 char s_id[32];
109 char *whoami;
110
111 data.button_hit=0;
112
113 install_user_dialog = gtk_widget_new(GTK_TYPE_WINDOW,
114 "type", GTK_WINDOW_TOPLEVEL,
115 "window_position", GTK_WIN_POS_MOUSE,
116 "title", _("Install User"),
117 NULL);
118 gtk_window_set_modal(GTK_WINDOW(install_user_dialog), TRUE);
119 if (main_window) {
120 gtk_window_set_transient_for(GTK_WINDOW(install_user_dialog), GTK_WINDOW(main_window));
121 }
122
123 g_signal_connect(G_OBJECT(install_user_dialog), "destroy",
124 G_CALLBACK(cb_destroy_dialog), install_user_dialog);
125
126 g_object_set_data(G_OBJECT(install_user_dialog),
127 "install_dialog_data", &data);
128
129 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
130
131 gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
132
133 gtk_container_add(GTK_CONTAINER(install_user_dialog), vbox);
134
135 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
136 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
137 label = gtk_label_new(_("A PalmOS(c) device needs a user name and a user ID in order to sync properly."));
138 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
139 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
140 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
141
142 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
143 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
144 label = gtk_label_new(_("If you want to sync more than 1 PalmOS(c) device each one should have a different ID and preferably a different user name."));
145 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
146 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
147 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
148
149 srandom(time(NULL));
150 /* RAND_MAX is 32768 on Solaris machines for some reason.
151 * If someone knows how to fix this, let me know.
152 */
153 if (RAND_MAX==32768) {
154 id = 1+(2000000000.0*random()/(2147483647+1.0));
155 } else {
156 id = 1+(2000000000.0*random()/(RAND_MAX+1.0));
157 }
158 g_snprintf(s_id, 30, "%ld", id);
159
160 /* User Name entry */
161
162 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
163 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
164
165 /* Instruction label */
166 label = gtk_label_new(_("Most people choose their name or nickname for the user name."));
167 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
168 gtk_label_set_line_wrap(GTK_LABEL(label), FALSE);
169 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
170
171 /* User Name */
172 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
173 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
174 label = gtk_label_new(_("User Name"));
175 user_entry = gtk_entry_new();
176 gtk_entry_set_max_length(GTK_ENTRY(user_entry), 128);
177 entry_set_multiline_truncate(GTK_ENTRY(user_entry), TRUE);
178 data.user_entry = user_entry;
179 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
180 gtk_box_pack_start(GTK_BOX(hbox), user_entry, TRUE, TRUE, 2);
181
182 /* Instruction label */
183 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
184 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 0);
185 label = gtk_label_new(_("The ID should be a random number."));
186 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
187 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
188 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
189
190 /* User ID */
191 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
192 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
193 label = gtk_label_new(_("User ID"));
194 ID_entry = gtk_entry_new();
195 gtk_entry_set_max_length(GTK_ENTRY(ID_entry), 32);
196 entry_set_multiline_truncate(GTK_ENTRY(ID_entry), TRUE);
197 data.ID_entry = ID_entry;
198 gtk_entry_set_text(GTK_ENTRY(ID_entry), s_id);
199 whoami = jp_user_or_whoami();
200 if (whoami) {
201 gtk_entry_set_text(GTK_ENTRY(user_entry), whoami);
202 free(whoami);
203 }
204 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 2);
205 gtk_box_pack_start(GTK_BOX(hbox), ID_entry, TRUE, TRUE, 2);
206
207 /* Cancel/Install buttons */
208 hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
209 gtk_button_box_set_layout(GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
210 gtk_box_set_spacing(GTK_BOX(hbox), 6);
211 gtk_container_set_border_width(GTK_CONTAINER(hbox), 5);
212 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 2);
213
214 button = gtk_button_new_with_label("Cancel");
215 g_signal_connect(G_OBJECT(button), "clicked",
216 G_CALLBACK(cb_install_user_button),
217 GINT_TO_POINTER(DIALOG_SAID_2));
218 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 1);
219
220 button = gtk_button_new_with_label(_("Install User"));
221 g_signal_connect(G_OBJECT(button), "clicked",
222 G_CALLBACK(cb_install_user_button),
223 GINT_TO_POINTER(DIALOG_SAID_1));
224 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 1);
225 gtk_widget_set_can_default(button,TRUE);
226 gtk_widget_grab_default(button);
227 gtk_widget_grab_focus(button);
228
229 gtk_widget_show_all(install_user_dialog);
230
231 gtk_main();
232
233 g_strlcpy(user, data.user, user_len);
234 *user_id = data.id;
235
236 return data.button_hit;
237 }
238
239 int install_user_gui(GtkWidget *main_window)
240 {
241 int r;
242 char user[MAX_PREF_LEN];
243 unsigned long user_id;
244 const char *svalue;
245 long old_user_id;
246 char *old_user;
247
248 r = dialog_install_user(GTK_WINDOW(main_window), user, MAX_PREF_LEN, &user_id);
249 if (r == DIALOG_SAID_1) {
250 /* Temporarily set the user and user ID */
251 get_pref(PREF_USER, NULL, &svalue);
252 get_pref(PREF_USER_ID, &old_user_id, NULL);
253 if (svalue) {
254 old_user = strdup(svalue);
255 } else {
256 old_user = strdup("");
257 }
258
259 set_pref(PREF_USER, 0, user, FALSE);
260 set_pref(PREF_USER_ID, user_id, NULL, TRUE);
261
262 jp_logf(JP_LOG_DEBUG, "user is %s\n", user);
263 jp_logf(JP_LOG_DEBUG, "user id is %ld\n", user_id);
264
265 r = setup_sync(SYNC_NO_PLUGINS | SYNC_INSTALL_USER);
266
267 jp_logf(JP_LOG_DEBUG, "old user is %s\n", old_user);
268 jp_logf(JP_LOG_DEBUG, "old user id is %ld\n", old_user_id);
269 set_pref(PREF_USER, 0, old_user, FALSE);
270 set_pref(PREF_USER_ID, old_user_id, NULL, TRUE);
271 free(old_user);
272
273 return r;
274 }
275
276 return -1;
277 }
278