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