Debian Codegraph @

src/inputname.c

raw symbols SCIP
1 /* inputname.c - dialog for entering users name
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 */
8 #include <gtk/gtk.h>
9 #include <string.h>
10 #include <time.h>
11 #include <stdlib.h>
12
13 #include "gtkballs.h"
14 #include "gtkutils.h"
15 #include "scoreboard.h"
16 #include "halloffame.h"
17 #include "game.h"
18
19 static gchar _last_player_name[50] = "";
20 static gint _saved_score = 0;
21
22 static void read_entry (GtkEntry *entry, gpointer data)
23 {
24 struct score_board current_entry;
25 time_t current_time;
26 struct tm *timeptr;
27 gint pos, fbn;
28 gsize br, bw;
29 gchar *tstr = NULL;
30 struct score_board b[10];
31 struct score_board_full *bf = NULL;
32
33 tstr = (gchar*)gtk_entry_get_text (entry);
34 strncpy(current_entry.name, tstr && *tstr ? tstr : _("Anonymous"), sizeof(current_entry.name)-1);
35 strncpy(_last_player_name, current_entry.name, sizeof(_last_player_name)-1);
36 current_entry.score = _saved_score;
37 current_time = time(NULL);
38 timeptr = localtime(&current_time);
39
40 if (!timeptr) {
41 ut_simple_message_box(_("Unable to determine current date.\n"));
42 strncpy(current_entry.date, _("Unknown"), sizeof(current_entry.date)-1);
43 } else {
44 if (!strftime(current_entry.date, sizeof(current_entry.date), _("%a %b %d %H:%M %Y"), timeptr)) {
45 ut_simple_message_box(_("Unable to determine current date.\n"));
46 strncpy(current_entry.date, _("Unknown"), sizeof(current_entry.date)-1);
47 } else {
48 tstr = g_locale_to_utf8(current_entry.date, -1, &br, &bw, NULL);
49 if (!tstr) {
50 strncpy(current_entry.date, _("Unknown"), sizeof(current_entry.date)-1);
51 } else {
52 strncpy(current_entry.date, tstr, sizeof(current_entry.date)-1);
53 g_free(tstr);
54 }
55 }
56 }
57
58 if (!read_score(b, &bf, &fbn)) {
59 ut_simple_message_box (_("Unable to read score.\n"));
60 } else {
61 pos = insert_entry_in_score_board(b, current_entry);
62 if (!write_score(b, bf, fbn)) {
63 ut_simple_message_box(_("Unable to save score.\n"));
64 }
65 /*FIXME: free bf here */
66 free_score_board_full(bf, fbn);
67 if (pos != -1) {
68 pos++;
69 show_hall_of_fame(NULL, GINT_TO_POINTER (pos), b);
70 }
71 }
72
73 gtk_widget_destroy (GTK_WIDGET (data));
74 /* show scores to let user see if (s)he's on top ;) */
75 }
76
77 static void input_name_response (GtkDialog * dlg, int response, gpointer user_data)
78 {
79 if (response == GTK_RESPONSE_OK) {
80 read_entry (GTK_ENTRY (user_data), dlg);
81 }
82 }
83
84 static void input_name_esc (GtkDialog * dlg, gpointer data)
85 {
86 g_signal_stop_emission_by_name (dlg, "close"); // ignore Esc
87 }
88
89 void input_name_dialog(void)
90 {
91 GtkWidget * dialog, * prompt_label, * vbox;
92 GtkWidget * name;
93 gchar * s;
94
95 /* we have to save score, because they will be set to 0 in new_game() */
96 _saved_score = game_get_score();
97
98 dialog = gtkutil_dialog_new (NULL, main_window, TRUE, &vbox);
99
100 prompt_label = gtk_label_new(_("Enter your name"));
101 gtk_box_pack_start (GTK_BOX(vbox), prompt_label, TRUE, TRUE, 0);
102
103 name = gtk_entry_new();
104 gtk_entry_set_max_length(GTK_ENTRY(name), 14);
105 g_signal_connect(G_OBJECT(name), "activate", G_CALLBACK(read_entry), dialog);
106
107 /* restore the last player's name */
108 if (!(*_last_player_name)) {
109 if ((s = getenv("USER"))) {
110 strncpy(_last_player_name, s, sizeof(_last_player_name)-1);
111 }
112 }
113 gtk_entry_set_text(GTK_ENTRY(name), _last_player_name);
114 gtk_editable_select_region(GTK_EDITABLE(name), 0, -1);
115 gtk_box_pack_start (GTK_BOX(vbox), name, TRUE, TRUE, 5);
116
117 gtk_dialog_add_button (GTK_DIALOG (dialog), "gtk-ok", GTK_RESPONSE_OK);
118 g_signal_connect (dialog, "response", G_CALLBACK (input_name_response), name);
119 g_signal_connect (dialog, "close", G_CALLBACK (input_name_esc), name);
120
121 gtk_widget_show_all (dialog);
122 }