-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_account.c
368 lines (310 loc) · 10.2 KB
/
ui_account.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/***********************************************************************
Theater Ticket Management System
Copyright(C) 2017 hepangda
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, either version 3 of the License, or
3 any later version.
This program is distributed in the hope that it will be useful,
buf WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY of FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have recived a copy of the GNU Gereral Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author: hepangda
E-mail: pangda@xiyoulinux.org
*************************************************************************/
#include"include/ui.h"
#include"include/frame.h"
#include"include/global.h"
#include"include/tty.h"
#include"include/service.h"
#include<unistd.h>
#include<sys/ioctl.h>
#include<string.h>
extern int (*next_ui)();
int ui_draw_account_changepwd() {
char input[200], buf[200], buf2[200];
ui_clearlines(23, 28);
link_t ret = srv_find_user_name(this_user.username);
if (ret == NULL) {
ui_draw_highlight(23, 8, "Unexcepted Error");
return -1;
}
user_t *this = (user_t *)ret->data;
ui_draw(23, 8, "New Password: ");
ui_scanf(input, "%s", buf);
ui_draw(24, 8, "Input New Password Again: ");
ui_scanf(input, "%s", buf2);
ui_clearlines(23, 28);
if (strcmp(buf, buf2) != 0) {
ui_draw_highlight(23, 8, "Update Failed, input is not the same.");
return -1;
} else {
strcpy(this->passwd, buf2);
ui_draw_highlight(23, 8, "Update Succeed!");
}
return RET_SUCCEED;
}
int ui_account_changepwd() {
ui_draw_account_changepwd();
sleep(2);
next_ui = ui_account_lowpower;
}
int ui_draw_account_lowpower(int select) {
ui_window("TTMS Account Management");
static const char *SEPLINE = "================================================================================================";
static const ui_pos_t selections[] = { {23, 8}, {24, 8} };
static const char *slt_string[] = {
"1. Change My Password", "2. Return to Main Menu"
};
ui_draw(3, 6, SEPLINE);
ui_draw(21, 6, SEPLINE);
ui_draw(10, 20, "You are not administrator, you can only change your password.");
ui_clearlines(23, 28);
for (int i = 0; i < 2; i++) {
if (select == i) {
ui_draw_item(selections[i].x, selections[i].y - 1, ">%s", slt_string[i]);
} else {
ui_draw(selections[i].x, selections[i].y, slt_string[i]);
}
}
}
int ui_account_lowpower() {
static int select = 0;
int err_draw = 0;
int ret;
do {
ui_draw_account_lowpower(select);
if (err_draw) {
err_draw = 0;
ui_bottom("Your choice is out of range.");
}
ret = ui_request("You aren\'t administrator, you can only change your password.");
if (ret >= '1' && ret <= '2') {
select = ret - '1';
} else if (ret == 'w' || ret == 's') {
select = (select == 0) ? 1 : 0;
} else if (ret == 10) {
next_ui = (select) ? ui_main_menu : ui_account_changepwd;
return RET_SUCCEED;
} else {
err_draw = 1;
}
} while (1);
return RET_SUCCEED;
}
int ui_draw_account(int select, int pages) {
ui_window("TTMS Account Management");
static const char *SEPLINE = "================================================================================================";
static const ui_pos_t selections[] = {
{23, 8}, {23, 38}, {23, 68}, {24, 8},
{24, 38}, {24, 68}, {25, 8}
};
static const char *slt_string[] = {
"1. Prev Page", "2. Next Page",
"3. Add User", "4. Delete User",
"5. Update User", "6. Find User",
"7. Return to Main Menu"
};
static const char *chart_title_format = "%-30s %-20s";
static const char *chart_list_format = "%-30s %-6d";
ui_draw(3, 6, SEPLINE);
ui_draw(5, 6, SEPLINE);
ui_draw(21, 6, SEPLINE);
ui_draw(4, 8, chart_title_format, "User Name", "User Type");
ui_draw_highlight(22, 8, "Page: %d", pages);
link_t first = ui_pager(g_user, pages);
for (int i = 0; i < UI_ITEM_PERPAGE && first != NULL; i++, first = first->next) {
user_t *this = (user_t *)first->data;
ui_draw(6 + i, 8, chart_list_format, this->username, this->type)
}
for (int i = 0; i < 7; i++) {
if (select == i) {
ui_draw_item(selections[i].x, selections[i].y - 1, ">%s", slt_string[i]);
} else {
ui_draw(selections[i].x, selections[i].y, slt_string[i]);
}
}
}
int ui_account() {
static int select = 0;
static int pages = 1;
int max_page = ui_pager_maxpage(g_user);
int err_draw = 0;
int ret;
do {
ui_draw_account(select, pages);
if (err_draw) {
err_draw = 0;
ui_bottom("Your choice is out of range.");
}
ret = ui_request("Please Select(0 ~ 6):");
if (ret >= '1' && ret <= '7') {
select = ret - '1';
} else if (ret == 'w') {
if (select == 0) {
select = 6;
} else {
select--;
}
} else if (ret == 's') {
if (select == 6) {
select = 0;
} else {
select++;
}
} else if (ret == 10) {
switch (select) {
case 0:
pages = (pages > 1) ? pages - 1 : pages;
break;
case 1:
pages = (pages <= max_page) ? pages + 1 : pages;
break;
case 2:
next_ui = ui_account_add;
return 0;
case 3:
next_ui = ui_account_delete;
return 0;
case 4:
next_ui = ui_account_update;
return 0;
case 5:
next_ui = ui_account_find;
return 0;
case 6:
next_ui = ui_main_menu;
return 0;
}
} else {
err_draw = 1;
}
} while (1);
}
int ui_draw_account_update() {
static const ui_pos_t selections[] = {
{23, 8}, {24, 8}
};
static const char *slt_string[] = {
"New Password", "New Usertype"
};
ui_clearlines(23, 28);
const char *tips = "Enter the username: ";
tty_cursor_gotoxy(23, 8);
ui_write(tips);
char input[200];
ui_getstring(input);
link_t ret = srv_find_user_name(input);
ui_clearlines(23, 28);
if (ret == NULL) {
ui_draw_highlight(23, 8, "There\'s no user named %s!", input);
return -1;
}
user_t *this = (user_t *)ret->data;
ui_draw(selections[0].x, selections[0].y, "%s :", slt_string[0], this->passwd);
ui_scanf(input, "%s", &this->passwd);
ui_draw(selections[1].x, selections[1].y,
"%s[0 for Admin, 1 for Manager, 2 for Saler][Origin %d]: ", slt_string[1], this->type);
ui_scanf(input, "%d", &this->type);
ui_clearlines(23, 28);
if (this->type < 0 || this->type > 2) {
ui_draw_highlight(23, 8, "User type error! Update error");
return -1;
}
ui_draw_highlight(23, 8, "Update Succeed!");
return RET_SUCCEED;
}
int ui_account_update() {;
ui_draw_account_update();
sleep(2);
next_ui = ui_account;
}
int ui_draw_account_delete() {
ui_clearlines(23, 28);
ui_draw(23, 8, "Enter the username: ");
char input[200];
ui_getstring(input);
link_t ret = srv_find_user_name(input);
ui_clearlines(23, 28);
tty_highlight;
tty_cursor_gotoxy(23, 8);
if (ret == NULL) {
ui_draw_highlight(23, 8, "There\'s no user named %s. Delete Failed.", input);
return -1;
} else {
ll_deletea(g_user, input, srv_user_equname);
ui_draw_highlight(23, 8, "Delete Succeed!");
return 0;
}
}
int ui_account_delete() {;
ui_draw_account_delete();
sleep(2);
next_ui = ui_account;
return RET_SUCCEED;
}
int ui_draw_account_add() {
static const ui_pos_t selections[] = {
{23, 8}, {24, 8}, {25, 8}
};
static const char *slt_string[] = {
"Username: ", "Password: ",
"User Type[0 for Admin, 1 for Manager, 2 for Saler]: "
};
ui_clearlines(23, 28);
char input[200];
user_t newuser;
ui_draw(selections[0].x, selections[0].y, slt_string[0]);
ui_scanf(input, "%s", newuser.username);
ui_draw(selections[1].x, selections[1].y, slt_string[1]);
ui_scanf(input, "%s", &newuser.passwd);
ui_draw(selections[2].x, selections[2].y, slt_string[2]);
ui_scanf(input, "%d", &newuser.type);
ui_clearlines(23, 28);
if (newuser.type < 0 || newuser.type > 2) {
ui_draw_highlight(23, 8, "User type error! Add Failed.");
return -1;
}
int ret = srv_user_add(newuser);
if (ret == 0) {
ui_draw_highlight(23, 8, "Add Succeed!");
} else {
ui_draw_highlight(23, 8, "Add Error!");
};
return ret;
}
int ui_account_add() {;
ui_draw_account_add();
sleep(2);
next_ui = ui_account;
}
int ui_draw_account_find() {
ui_clearlines(23, 28);
const char *tips = "Enter the username: ";
tty_cursor_gotoxy(23, 8);
ui_write(tips);
char input[200];
ui_getstring(input);
link_t ret = srv_find_user_name(input);
ui_clearlines(23, 28);
tty_highlight;
if (ret == NULL) {
ui_draw_highlight(23, 8, "There\'s no user named %s, find failed.", input);
ui_draw(25, 8, "Press any key to continue...");
int rets = ui_request("Press any key..");
return -1;
} else {
user_t *this = (user_t *)ret->data;
ui_draw_highlight(23, 8, "Find Result: %s, user type: %d", this->username, this->type);
ui_draw(25, 8, "Press any key to continue...");
int rets = ui_request("Press any key..");
return RET_SUCCEED;
}
}
int ui_account_find() {;
ui_draw_account_find();
next_ui = ui_account;
return RET_SUCCEED;
}