Debian Codegraph @

src/new_highscore_dialog.cc

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /* Groundhog -- a simple logic game
2 * Copyright (C) 1998-2002 Maurits Rijk
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include <pwd.h>
20 #include <unistd.h>
21
22 #include "game.h"
23 #include "icons.h"
24 #include "intl.h"
25 #include "new_highscore_dialog.h"
26
27 NewHighScoreDialog::NewHighScoreDialog(Game* game) :
28 Dialog(_("New Highscore")), _game(game)
29 {
30 GtkWidget* table = gtk_table_new(2, 2, FALSE);
31 gtk_container_border_width(GTK_CONTAINER(table), 10);
32 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(_window)->vbox), table, TRUE, TRUE,
33 10);
34 gtk_table_set_row_spacings(GTK_TABLE(table), 10);
35 gtk_table_set_col_spacings(GTK_TABLE(table), 5);
36
37 // Create champagne icon.
38 GtkWidget* icon = gtk_image_new_from_stock(GROUNDHOG_STOCK_CHAMPAGNE,
39 GTK_ICON_SIZE_DIALOG);
40 gtk_table_attach_defaults(GTK_TABLE(table), icon, 0, 1, 0, 1);
41
42 GtkWidget* label = gtk_label_new(_("Congratulations, a new highscore!"));
43 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
44
45 GtkWidget* name_label = gtk_label_new(_("Name:"));
46 gtk_table_attach_defaults(GTK_TABLE(table), name_label, 0, 1, 1, 2);
47
48 _name = gtk_entry_new();
49 gtk_table_attach_defaults(GTK_TABLE(table), _name, 1, 2, 1, 2);
50 }
51
52 void
53 NewHighScoreDialog::OnOk()
54 {
55 _game->AddHighScore(gtk_entry_get_text(GTK_ENTRY(_name)));
56 _game->ShowHighScore();
57 }
58
59 char*
60 NewHighScoreDialog::GetLoginName()
61 {
62 char* login_name;
63 if (!(login_name = getenv("LOGNAME"))
64 && !(login_name = getlogin())
65 && !(login_name = getenv("USER"))) {
66 struct passwd * passwd_entry = getpwuid(getuid());
67 if (!passwd_entry) passwd_entry = getpwuid(geteuid());
68 login_name = passwd_entry ? passwd_entry->pw_name : (char*) "nobody";
69 }
70 return login_name;
71 }
72
73 std::string
74 NewHighScoreDialog::GetUserName()
75 {
76 char* login_name = GetLoginName();
77 struct passwd* passwd_entry = login_name ? getpwnam(login_name) : 0;
78 std::string gecos_field = (passwd_entry && passwd_entry->pw_gecos)
79 ? passwd_entry->pw_gecos : (login_name ? login_name : "Anonymous");
80
81 size_t pos = gecos_field.find(',');
82 return (pos == std::string::npos)
83 ? gecos_field : gecos_field.substr(0, pos);
84 }
85
86 void
87 NewHighScoreDialog::DoDialog()
88 {
89 std::string user_name = GetUserName();
90 gtk_entry_set_text(GTK_ENTRY(_name), user_name.c_str());
91 Dialog::DoDialog();
92 gtk_widget_grab_focus(_name);
93 }