diff --git a/configure.ac b/configure.ac index 37ce8d7856e4709fadee3441e9e1615811d5db5c..f33c9fbcf0d997674e929e90b21bb95d09b4b15c 100644 --- a/configure.ac +++ b/configure.ac @@ -27,6 +27,7 @@ PKG_CHECK_MODULES(FIVE_OR_MORE, [ glib-2.0 >= $GLIB_REQUIRED gio-2.0 >= $GLIB_REQUIRED gtk+-3.0 >= $GTK_REQUIRED + libgnome-games-support-1 librsvg-2.0 >= $RSVG_REQUIRED ]) diff --git a/meson.build b/meson.build index e704fb989eafd1d3dd35c713de67c8577c279f91..14d9d3ce79ac378e057897b2faf4e69e2c1700b6 100644 --- a/meson.build +++ b/meson.build @@ -15,11 +15,12 @@ pkgdata_dir = join_paths (data_dir, 'five-or-more') # Dependencies glib_min_version = '2.32' -librsvg = dependency('librsvg-2.0', version: '>= 2.32.0') gio = dependency('gio-2.0', version: '>= ' + glib_min_version) glib = dependency('glib-2.0', version: '>= ' + glib_min_version) gmodule_export = dependency('gmodule-export-2.0') gtk = dependency('gtk+-3.0', version: '>= 3.20.0') +libgnome_games_support = dependency('libgnome-games-support-1') +librsvg = dependency('librsvg-2.0', version: '>= 2.32.0') # Libraries cc = meson.get_compiler('c') diff --git a/po/POTFILES.in b/po/POTFILES.in index 980c5766635d0b2d3a882d9f613bbd1bb463141c..8f101220b739a5acca7215f8b19174fa4ddfef68 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -8,7 +8,6 @@ data/menu.ui data/org.gnome.five-or-more.gschema.xml data/translatable_themes.h src/five-or-more.c -src/games-scores-dialog.c src/five-or-more-app.c src/game-area.c src/balls-preview.c diff --git a/src/Makefile.am b/src/Makefile.am index 79d7d957eba803ca95524c2384572622045c01e8..8bd07fc145c3572654fe99b1447cafece4121c1a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,14 +8,6 @@ five_or_more_SOURCES = \ games-gridframe.h \ games-preimage.c \ games-preimage.h \ - games-score.h \ - games-score.c \ - games-scores.c \ - games-scores.h \ - games-scores-dialog.c \ - games-scores-dialog.h \ - games-scores-backend.c \ - games-scores-backend.h \ five-or-more-app.h \ five-or-more-app.c \ balls-preview.c \ diff --git a/src/five-or-more-app.c b/src/five-or-more-app.c index 8c94ddc7a1428595215188fa2731708f12de3d7e..839c95fad0aeaddb447c23a949de075ab54e176a 100644 --- a/src/five-or-more-app.c +++ b/src/five-or-more-app.c @@ -33,12 +33,10 @@ #include #include #include +#include #include "games-file-list.h" #include "games-preimage.h" -#include "games-gridframe.h" -#include "games-scores.h" -#include "games-scores-dialog.h" #include "game-area.h" #include "five-or-more-app.h" #include "balls-preview.h" @@ -50,7 +48,12 @@ #define KEY_BACKGROUND_COLOR "background-color" #define KEY_BALL_THEME "ball-theme" -static const GamesScoresCategory scorecats[] = { +typedef struct { + gchar *key; + gchar *name; +} key_value; + +static const key_value scorecats[] = { { "Small", NC_("board size", "Small") }, { "Medium", NC_("board size", "Medium") }, { "Large", NC_("board size", "Large") } @@ -77,7 +80,11 @@ static GtkWidget *size_radio_s, *size_radio_m, *size_radio_l; static int move_timeout = 100; static gboolean window_is_fullscreen = FALSE, window_is_maximized = FALSE; static gint window_width = 0, window_height = 0; -static GamesScores *highscores; + +static gint no_categories = 3; +static gchar *score_current_category = NULL; +static GamesScoresContext *highscores; + static GSettings *settings; static GtkBuilder *builder; static GtkBuilder *builder_preferences; @@ -93,12 +100,6 @@ get_settings () return &settings; } -const GamesScoresCategory * -get_scorecats () -{ - return scorecats; -} - gint * get_game_size () { @@ -129,12 +130,6 @@ get_move_timeout () return move_timeout; } -GamesScores * -get_highscores() -{ - return highscores; -} - void update_score (guint value) { @@ -170,9 +165,11 @@ set_backgnd_color (const gchar * str) } static void -show_scores (gint pos) +show_scores () { - static GtkWidget *dialog; + games_scores_context_run_dialog (highscores); + +/* static GtkWidget *dialog; if (dialog == NULL) { dialog = games_scores_dialog_new (GTK_WINDOW (app), highscores, _("Five or More Scores")); @@ -186,7 +183,7 @@ show_scores (gint pos) gtk_window_present (GTK_WINDOW (dialog)); gtk_dialog_run (GTK_DIALOG (dialog)); - gtk_widget_hide (dialog); + gtk_widget_hide (dialog);*/ } static void @@ -205,15 +202,46 @@ load_properties (void) load_theme (); } +static void +add_score_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) +{ + GamesScoresContext *context = GAMES_SCORES_CONTEXT (source_object); + GError *error = NULL; + + games_scores_context_add_score_finish (context, res, &error); + if (error != NULL) { + g_warning ("Failed to add score: %s", error->message); + g_error_free (error); + } +} + +const gchar * +category_name_from_key (const gchar *key) +{ + int i; + for (i = 0; i < no_categories; i++) { + if (g_strcmp0 (scorecats[i].key, key) == 0) + return scorecats[i].name; + } + return NULL; +} + void game_over (void) { - int pos; set_status_message (_("Game Over!")); - if (score > 0) - pos = games_scores_add_plain_score (highscores, score); - show_scores (pos); + if (score > 0) { + const gchar *name = category_name_from_key (score_current_category); + GamesScoresCategory *current_category = games_scores_category_new (score_current_category, name); + games_scores_context_add_score (highscores, + score, + current_category, + NULL, + add_score_cb, + NULL); + } + show_scores (); } static void @@ -263,6 +291,8 @@ conf_value_changed_cb (GSettings *settings, gchar *key) if (size_tmp != game_size) { set_sizes (size_tmp); + score_current_category = scorecats[size_tmp - 1].key; + reset_game (); start_game (); } @@ -282,6 +312,7 @@ init_config (void) game_size = CLAMP (game_size, SMALL, MAX_SIZE - 1); set_sizes (game_size); + score_current_category = scorecats[game_size - 1].key; } static gboolean @@ -318,7 +349,7 @@ game_top_ten_callback (GSimpleAction *action, GVariant *parameter, gpointer user_data) { - show_scores (0); + show_scores (); } @@ -561,6 +592,16 @@ game_quit_callback (GSimpleAction *action, g_application_quit (G_APPLICATION (user_data)); } +static GamesScoresCategory * +create_category_from_key (const char *key, gpointer user_data) +{ + const gchar *name = category_name_from_key (key); + if (name == NULL) + return NULL; + + return games_scores_category_new (key, name); +} + void startup_cb (GApplication *application) { @@ -570,6 +611,7 @@ startup_cb (GApplication *application) GtkWidget *new_game_button; guint i; GError *error = NULL; + GamesScoresDirectoryImporter *importer; GActionEntry app_actions[] = { { "new-game", game_new_callback }, @@ -588,11 +630,6 @@ startup_cb (GApplication *application) settings = g_settings_new ("org.gnome.five-or-more"); - highscores = games_scores_new ("five-or-more", - scorecats, G_N_ELEMENTS (scorecats), - "board size", NULL, - 0 /* default category */, - GAMES_SCORES_STYLE_PLAIN_DESCENDING); init_config (); builder = gtk_builder_new (); ui_path = g_build_filename (DATA_DIRECTORY, "menu.ui", NULL); @@ -644,13 +681,22 @@ startup_cb (GApplication *application) GtkWidget *draw_area = game_area_init (); - gridframe = games_grid_frame_new (get_hfieldsize(), get_vfieldsize()); + gridframe = GTK_WIDGET (games_grid_frame_new (get_hfieldsize(), get_vfieldsize())); games_grid_frame_set_padding (GAMES_GRID_FRAME (gridframe), 1, 1); gtk_container_add (GTK_CONTAINER (gridframe), draw_area); gtk_box_pack_start (GTK_BOX (hbox), gridframe, TRUE, TRUE, 0); new_game_button = GTK_WIDGET (gtk_builder_get_object (builder, "new_game_button")); + importer = games_scores_directory_importer_new (); + highscores = games_scores_context_new_with_importer ("five-or-more", + _("Board Size: "), + GTK_WINDOW (app), + create_category_from_key, + NULL, + GAMES_SCORES_STYLE_POINTS_GREATER_IS_BETTER, + GAMES_SCORES_IMPORTER (importer)); + load_properties (); gtk_builder_connect_signals (builder, NULL); @@ -692,4 +738,4 @@ set_application_callbacks(GtkApplication *application) g_signal_connect (application, "startup", G_CALLBACK (startup_cb), NULL); g_signal_connect (application, "activate", G_CALLBACK (activate_cb), NULL); g_signal_connect (application, "shutdown", G_CALLBACK (shutdown_cb), NULL); -} \ No newline at end of file +} diff --git a/src/five-or-more-app.h b/src/five-or-more-app.h index 6e9a6b6b8ff2ea362a032d24647dd50a04776672..6f853f7c7e451cb88e61b547204cd62e031a7b9c 100644 --- a/src/five-or-more-app.h +++ b/src/five-or-more-app.h @@ -24,7 +24,6 @@ #define FIVE_OR_MORE_APP_H #include -#include "games-scores.h" typedef struct _background background; struct _background{ @@ -33,7 +32,6 @@ struct _background{ gint set; } ; -GamesScores *get_highscores (); GtkWidget *get_gridframe (); char *get_ball_filename (); background get_backgnd (); @@ -41,11 +39,10 @@ void set_status_message (gchar * message); void game_over (void); void set_application_callbacks (GtkApplication *application); gint *get_game_size (); -const GamesScoresCategory *get_scorecats (); GSettings **get_settings (); int get_move_timeout (); void game_props_callback (GSimpleAction *action, GVariant *parameter, gpointer user_data); void update_score (guint value); -#endif \ No newline at end of file +#endif diff --git a/src/five-or-more.c b/src/five-or-more.c index cc9e1fec03189f130939770770906d407b2e5564..4a68b1108eb028f81b366ac36d6205170dbd67b8 100644 --- a/src/five-or-more.c +++ b/src/five-or-more.c @@ -51,7 +51,6 @@ main (int argc, char *argv[]) bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); - games_scores_startup (); GRand **rgen = get_rgen(); *rgen = g_rand_new (); diff --git a/src/game-area.c b/src/game-area.c index 7a3043ba0ba8099886b4473667fc2ee26d6e82e2..d4c70f46c3b3710e4700d054e11da7e23ba33779 100644 --- a/src/game-area.c +++ b/src/game-area.c @@ -33,8 +33,8 @@ #include #include #include +#include -#include "games-gridframe.h" #include "game-area.h" #include "five-or-more-app.h" #include "balls-preview.h" @@ -82,8 +82,7 @@ static const gint field_sizes[MAX_SIZE][4] = { }; static scoretable sctab[] = - { {5, 10}, {6, 12}, {7, 18}, {8, 28}, {9, 42}, {10, 82}, {11, 108}, {12, - 138}, + { {5, 10}, {6, 12}, {7, 18}, {8, 28}, {9, 42}, {10, 82}, {11, 108}, {12, 138}, {13, 172}, {14, 210}, {0, 0} }; static gchar *warning_message = NULL; @@ -98,8 +97,6 @@ set_sizes (gint size) npieces = field_sizes[size][3]; gint *game_size = get_game_size(); *game_size = size; - GamesScoresCategory *scorecats = get_scorecats(); - games_scores_set_category (get_highscores(), scorecats[size - 1].key); g_settings_set_int (*(get_settings()), KEY_SIZE, size); GtkWidget *gridframe = get_gridframe(); @@ -321,7 +318,6 @@ check_gameover (void) static int addscore (int num) { - gchar string[20]; int i = 0; int retval; @@ -670,7 +666,6 @@ cell_clicked (GtkWidget * widget, int fx, int fy) static gint button_press_event (GtkWidget * widget, GdkEvent * event) { - int x, y; int fx, fy; if (inmove) @@ -1054,4 +1049,4 @@ game_area_init (void) gtk_widget_set_can_focus (draw_area, TRUE); gtk_widget_grab_focus (draw_area); return draw_area; -} \ No newline at end of file +} diff --git a/src/games-gridframe.c b/src/games-gridframe.c deleted file mode 100644 index 371fed1b630e35e8fbb0c634183ee298958be19b..0000000000000000000000000000000000000000 --- a/src/games-gridframe.c +++ /dev/null @@ -1,276 +0,0 @@ -/* games-gridframe.c: Create a container that guarantees that the internal - * allocated space is a fixed multiple of an integer. - * - * Copyright 2004 by Callum McKenzie - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see . - */ - -/* A lot of this was written by following the sorce for GtkFrame and - * GtkAspectFrame. */ - -#include - -#include -#include - -#include "games-gridframe.h" - -enum { - PROP_0, - PROP_X_PADDING, - PROP_Y_PADDING, - PROP_WIDTH, - PROP_HEIGHT, - PROP_X_ALIGN, - PROP_Y_ALIGN -}; - -G_DEFINE_TYPE (GamesGridFrame, games_grid_frame,GTK_TYPE_BIN) - -struct GamesGridFramePrivate { - gint xmult; - gint ymult; - - gint xpadding; - gint ypadding; - - gfloat xalign; - gfloat yalign; - - GtkAllocation old_allocation; -}; - -void -games_grid_frame_set (GamesGridFrame * frame, gint newxmult, gint newymult) -{ - if (newxmult > 0) - frame->priv->xmult = newxmult; - if (newymult > 0) - frame->priv->ymult = newymult; - - gtk_widget_queue_resize (GTK_WIDGET (frame)); -} - -void -games_grid_frame_set_padding (GamesGridFrame * frame, gint newxpadding, - gint newypadding) -{ - if (newxpadding >= 0) - frame->priv->xpadding = newxpadding; - - if (newypadding >= 0) - frame->priv->ypadding = newypadding; - - gtk_widget_queue_resize (GTK_WIDGET (frame)); -} - - -void -games_grid_frame_set_alignment (GamesGridFrame * frame, gfloat xalign, - gfloat yalign) -{ - if (xalign < 0.0) - xalign = 0.0; - else if (xalign > 1.0) - xalign = 1.0; - - if (yalign < 0.0) - yalign = 0.0; - else if (yalign > 1.0) - yalign = 1.0; - - frame->priv->xalign = xalign; - frame->priv->yalign = yalign; - - gtk_widget_queue_resize (GTK_WIDGET (frame)); -} - -static void -games_grid_frame_set_property (GObject * object, guint prop_id, - const GValue * value, GParamSpec * pspec) -{ - GamesGridFrame *frame = GAMES_GRID_FRAME (object); - - switch (prop_id) { - case PROP_X_PADDING: - games_grid_frame_set_padding (frame, g_value_get_int (value), -1); - break; - case PROP_Y_PADDING: - games_grid_frame_set_padding (frame, -1, g_value_get_int (value)); - break; - case PROP_X_ALIGN: - games_grid_frame_set_alignment (frame, g_value_get_float (value), - frame->priv->yalign); - break; - case PROP_Y_ALIGN: - games_grid_frame_set_alignment (frame, frame->priv->xalign, - g_value_get_float (value)); - break; - case PROP_WIDTH: - games_grid_frame_set (frame, g_value_get_int (value), -1); - break; - case PROP_HEIGHT: - games_grid_frame_set (frame, -1, g_value_get_int (value)); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -games_grid_frame_get_property (GObject * object, guint prop_id, - GValue * value, GParamSpec * pspec) -{ - GamesGridFrame *frame = GAMES_GRID_FRAME (object); - - switch (prop_id) { - case PROP_X_PADDING: - g_value_set_int (value, frame->priv->xpadding); - break; - case PROP_Y_PADDING: - g_value_set_int (value, frame->priv->ypadding); - break; - case PROP_X_ALIGN: - g_value_set_float (value, frame->priv->xalign); - break; - case PROP_Y_ALIGN: - g_value_set_float (value, frame->priv->yalign); - break; - case PROP_WIDTH: - g_value_set_int (value, frame->priv->xmult); - break; - case PROP_HEIGHT: - g_value_set_int (value, frame->priv->ymult); - break; - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); - break; - } -} - -static void -games_grid_frame_size_allocate (GtkWidget * widget, - GtkAllocation * allocation) -{ - GamesGridFrame *frame = GAMES_GRID_FRAME (widget); - GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget)); - GtkAllocation child_allocation; - gint xsize, ysize, size; - - gtk_widget_set_allocation (widget, allocation); - - xsize = MAX (1, (allocation->width - frame->priv->xpadding) / frame->priv->xmult); - ysize = MAX (1, (allocation->height - frame->priv->ypadding) / frame->priv->ymult); - - size = MIN (xsize, ysize); - - child_allocation.width = size * frame->priv->xmult + frame->priv->xpadding; - child_allocation.height = size * frame->priv->ymult + frame->priv->ypadding; - - child_allocation.x = - (allocation->width - child_allocation.width) * frame->priv->xalign + - allocation->x; - child_allocation.y = - (allocation->height - child_allocation.height) * frame->priv->yalign + - allocation->y; - - if (gtk_widget_get_mapped (widget) && - (child_allocation.x != frame->priv->old_allocation.x || - child_allocation.y != frame->priv->old_allocation.y || - child_allocation.width != frame->priv->old_allocation.width || - child_allocation.height != frame->priv->old_allocation.height)) - gdk_window_invalidate_rect (gtk_widget_get_window (widget), allocation, FALSE); - - if (child && gtk_widget_get_visible (child)) - gtk_widget_size_allocate (child, &child_allocation); - - frame->priv->old_allocation = child_allocation; -} - -static void -games_grid_frame_class_init (GamesGridFrameClass * class) -{ - GObjectClass *object_class; - GtkWidgetClass *widget_class; - - object_class = G_OBJECT_CLASS (class); - widget_class = GTK_WIDGET_CLASS (class); - - object_class->set_property = games_grid_frame_set_property; - object_class->get_property = games_grid_frame_get_property; - - widget_class->size_allocate = games_grid_frame_size_allocate; - - g_type_class_add_private (object_class, sizeof (GamesGridFramePrivate)); - - g_object_class_install_property (object_class, PROP_X_PADDING, - g_param_spec_int ("x_padding", "X Padding", "X Padding", - 0, G_MAXINT, 0, - G_PARAM_READABLE | - G_PARAM_WRITABLE)); - g_object_class_install_property (object_class, PROP_Y_PADDING, - g_param_spec_int ("y_padding", "Y Padding", "Y Padding", - 0, G_MAXINT, 0, - G_PARAM_READABLE | - G_PARAM_WRITABLE)); - g_object_class_install_property (object_class, PROP_WIDTH, - g_param_spec_int ("width_multiple", "Width Multiple", "Width Multiple", - 1, G_MAXINT, 1, - G_PARAM_READABLE | - G_PARAM_WRITABLE)); - g_object_class_install_property (object_class, PROP_HEIGHT, - g_param_spec_int ("height_multiple", "Height Multiple", "Height Multiple", - 1, G_MAXINT, 1, - G_PARAM_READABLE | - G_PARAM_WRITABLE)); - g_object_class_install_property (object_class, PROP_X_ALIGN, - g_param_spec_float ("xalign", "X Alignment", "X Alignment", - 0.0, 1.0, 0.5, - G_PARAM_READABLE | - G_PARAM_WRITABLE)); - g_object_class_install_property (object_class, PROP_Y_ALIGN, - g_param_spec_float ("yalign", "Y Alignment", "Y Alignment", - 0.0, 1.0, 0.5, - G_PARAM_READWRITE | - G_PARAM_WRITABLE)); -} - -static void -games_grid_frame_init (GamesGridFrame * frame) -{ - frame->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame, GAMES_TYPE_GRID_FRAME, GamesGridFramePrivate); - - frame->priv->xmult = 1; - frame->priv->ymult = 1; - - frame->priv->xalign = 0.5; - frame->priv->yalign = 0.5; -} - -GtkWidget * -games_grid_frame_new (gint width, gint height) -{ - GamesGridFrame *frame; - - frame = g_object_new (GAMES_TYPE_GRID_FRAME, NULL); - - frame->priv->xmult = MAX (width, 1); - frame->priv->ymult = MAX (height, 1); - - return GTK_WIDGET (frame); -} - -/* EOF */ diff --git a/src/games-gridframe.h b/src/games-gridframe.h deleted file mode 100644 index 645966255610718fc0b6549bf2e806dc02104d13..0000000000000000000000000000000000000000 --- a/src/games-gridframe.h +++ /dev/null @@ -1,61 +0,0 @@ -/* games-gridframe.h: Create a container that guarantees that the internal - * allocated space is a fixed multiple of an integer. - * - * Copyright 2004 by Callum McKenzie - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see . - */ - -#ifndef GAMES_GRID_FRAME_H -#define GAMES_GRID_FRAME_H - -#include - -G_BEGIN_DECLS - -#define GAMES_TYPE_GRID_FRAME (games_grid_frame_get_type ()) -#define GAMES_GRID_FRAME(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_GRID_FRAME, GamesGridFrame)) -#define GAMES_GRID_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_GRID_FRAME, GamesGridFrameClass)) -#define GAMES_IS_GRID_FRAME(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_GRID_FRAME)) -#define GAMES_IS_GRID_FRAME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_GRID_FRAME)) -#define GAMES_GRID_FRAME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_GRID_FRAME)) - -typedef struct GamesGridFramePrivate GamesGridFramePrivate; - -typedef struct { - GtkBin bin; - /*< private >*/ - GamesGridFramePrivate *priv; -} GamesGridFrame; - -typedef struct { - GtkBinClass parent; -} GamesGridFrameClass; - -GType games_grid_frame_get_type (void); -GtkWidget *games_grid_frame_new (gint width, - gint height); -void games_grid_frame_set (GamesGridFrame * frame, - gint width, - gint height); -void games_grid_frame_set_padding (GamesGridFrame * frame, - gint xpadding, - gint ypadding); -void games_grid_frame_set_alignment (GamesGridFrame * frame, - gfloat xalign, - gfloat yalign); - -G_END_DECLS -#endif /* GAMES_GRID_FRAME_H */ -/* EOF */ diff --git a/src/games-score.c b/src/games-score.c deleted file mode 100644 index 9f486313aacfd6ee951fe0e84069658ffd8a1a89..0000000000000000000000000000000000000000 --- a/src/games-score.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (C) 2005 Callum McKenzie - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see . - */ - -#include - -#include "games-score.h" - -G_DEFINE_TYPE (GamesScore, games_score, G_TYPE_OBJECT) - -struct GamesScorePrivate { - union { - guint32 plain; - gdouble time_double; /* minutes.seconds */ - } value; - time_t time; -}; - -/** - * games_score_new: - * - * Creates a new score object. - * - * Return value: the new #GamesScore - **/ -GamesScore * -games_score_new () -{ - return g_object_new (GAMES_TYPE_SCORE, NULL); -} - -/** - * games_score_new_plain: - * @value: The value of the score. - * - * Creates a new score object. - * - * Return value: the new #GamesScore - **/ -GamesScore * -games_score_new_plain (guint32 value) -{ - GamesScore *score = g_object_new (GAMES_TYPE_SCORE, NULL); - score->priv->value.plain = value; - return score; -} - -/** - * games_score_new_time: - * @value: The timer value of the score. - * - * Creates a new score object. - * - * Return value: the new #GamesScore - **/ -GamesScore * -games_score_new_time (gdouble value) -{ - GamesScore *score = g_object_new (GAMES_TYPE_SCORE, NULL); - score->priv->value.time_double = value; - return score; -} - -time_t -games_score_get_time (GamesScore *score) -{ - return score->priv->time; -} - -void -games_score_set_time (GamesScore *score, time_t time) -{ - score->priv->time = time; -} - -guint32 -games_score_get_value_as_plain (GamesScore *score) -{ - return score->priv->value.plain; -} - -gdouble -games_score_get_value_as_time (GamesScore *score) -{ - return score->priv->value.time_double; -} - -gint -games_score_compare (GamesScoreStyle style, - GamesScore * a, - GamesScore * b) -{ - switch (style) { - case GAMES_SCORES_STYLE_PLAIN_DESCENDING: - if (a->priv->value.plain > b->priv->value.plain) - return +1; - if (a->priv->value.plain < b->priv->value.plain) - return -1; - return 0; - case GAMES_SCORES_STYLE_PLAIN_ASCENDING: - if (a->priv->value.plain > b->priv->value.plain) - return -1; - if (a->priv->value.plain < b->priv->value.plain) - return +1; - return 0; - case GAMES_SCORES_STYLE_TIME_DESCENDING: - if (a->priv->value.time_double > b->priv->value.time_double) - return +1; - if (a->priv->value.time_double < b->priv->value.time_double) - return -1; - return 0; - case GAMES_SCORES_STYLE_TIME_ASCENDING: - if (a->priv->value.time_double > b->priv->value.time_double) - return -1; - if (a->priv->value.time_double < b->priv->value.time_double) - return +1; - return 0; - default: - g_warning - ("Uknown score style in games_score_compare - treating as equal."); - return 0; - } -} - -static void -games_score_finalize (GObject * object) -{ - GamesScore *score = GAMES_SCORE (object); - - G_OBJECT_CLASS (games_score_parent_class)->finalize (object); -} - -static void -games_score_class_init (GamesScoreClass * klass) -{ - GObjectClass *object_class = (GObjectClass *) klass; - - object_class->finalize = games_score_finalize; - - g_type_class_add_private (object_class, sizeof (GamesScorePrivate)); -} - -static void -games_score_init (GamesScore *score) -{ - const gchar* name; - - score->priv = G_TYPE_INSTANCE_GET_PRIVATE (score, GAMES_TYPE_SCORE, GamesScorePrivate); - - score->priv->time = time (NULL); -} diff --git a/src/games-score.h b/src/games-score.h deleted file mode 100644 index 45dd3e596c44430a27dd6e24c3e2af5a90e7feec..0000000000000000000000000000000000000000 --- a/src/games-score.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2005 Callum McKenzie - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see . - */ - -#ifndef GAMES_SCORE_H -#define GAMES_SCORE_H - -#include -#include - -G_BEGIN_DECLS - -#define GAMES_TYPE_SCORE (games_score_get_type ()) -#define GAMES_SCORE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_SCORE, GamesScore)) -#define GAMES_SCORE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAMES_TYPE_SCORE, GamesScoreClass)) -#define GAMES_IS_SCORE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_SCORE)) -#define GAMES_IS_SCORE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_SCORE)) - -typedef struct GamesScorePrivate GamesScorePrivate; - -/* GamesScore and GamesScoresStyle should be kept in sync. */ -typedef enum { - GAMES_SCORES_STYLE_PLAIN_DESCENDING, - GAMES_SCORES_STYLE_PLAIN_ASCENDING, - GAMES_SCORES_STYLE_TIME_DESCENDING, - GAMES_SCORES_STYLE_TIME_ASCENDING, -} GamesScoreStyle; - -typedef struct { - GObject parent; - /*< private >*/ - GamesScorePrivate *priv; -} GamesScore; - -typedef struct { - GObjectClass parent_class; -} GamesScoreClass; - -GType games_score_get_type (void); -GamesScore *games_score_new (void); -GamesScore *games_score_new_plain (guint32 value); -GamesScore *games_score_new_time (gdouble value); -time_t games_score_get_time (GamesScore *score); -void games_score_set_time (GamesScore *score, time_t time); -guint32 games_score_get_value_as_plain (GamesScore *score); -gdouble games_score_get_value_as_time (GamesScore *score); -gint games_score_compare (GamesScoreStyle style, - GamesScore * a, - GamesScore * b); - -G_END_DECLS - -#endif /* GAMES_SCORE_H */ diff --git a/src/games-scores-backend.c b/src/games-scores-backend.c deleted file mode 100644 index 5880e5f5b64a46a59c718e23da875075a86b39e2..0000000000000000000000000000000000000000 --- a/src/games-scores-backend.c +++ /dev/null @@ -1,339 +0,0 @@ -/* games-scores-backend.c - * - * Copyright (C) 2005 Callum McKenzie - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see . - */ - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "games-score.h" -#include "games-scores.h" -#include "games-scores-backend.h" - -struct GamesScoresBackendPrivate { - GList *scores_list; - GamesScoreStyle style; - time_t timestamp; - gchar *filename; - gint fd; -}; - -G_DEFINE_TYPE (GamesScoresBackend, games_scores_backend, G_TYPE_OBJECT); - -void -games_scores_backend_startup (void) -{ - /* Retained for compatibility */ -} - -static void -games_scores_backend_finalize (GObject *object) -{ - GamesScoresBackend *backend = GAMES_SCORES_BACKEND (object); - GamesScoresBackendPrivate *priv = backend->priv; - - g_free (priv->filename); - /* FIXME: more to do? */ - - G_OBJECT_CLASS (games_scores_backend_parent_class)->finalize (object); -} - -static void -games_scores_backend_class_init (GamesScoresBackendClass * klass) -{ - GObjectClass *oclass = G_OBJECT_CLASS (klass); - - g_type_class_add_private (klass, sizeof (GamesScoresBackendPrivate)); - oclass->finalize = games_scores_backend_finalize; -} - -static void -games_scores_backend_init (GamesScoresBackend * backend) -{ - backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend, - GAMES_TYPE_SCORES_BACKEND, - GamesScoresBackendPrivate); -} - -GamesScoresBackend * -games_scores_backend_new (GamesScoreStyle style, - char *base_name, - char *name) -{ - GamesScoresBackend *backend; - char *pkguserdatadir; - - backend = GAMES_SCORES_BACKEND (g_object_new (GAMES_TYPE_SCORES_BACKEND, - NULL)); - - backend->priv->timestamp = 0; - backend->priv->style = style; - backend->priv->scores_list = NULL; - pkguserdatadir = g_build_filename (g_get_user_data_dir (), base_name, NULL); - backend->priv->filename = g_build_filename (pkguserdatadir, name, NULL); - - if (access (pkguserdatadir, O_RDWR) == -1) { - /* Don't return NULL because games-scores.c does not - * expect it, and can't do anything about it anyway. */ - mkdir (pkguserdatadir, 0775); - } - backend->priv->fd = -1; - - return backend; -} - - -/* Get a lock on the scores file. Block until it is available. - * This also supplies the file descriptor we need. The return value - * is whether we were succesful or not. */ -static gboolean -games_scores_backend_get_lock (GamesScoresBackend * self) -{ - gint error; - struct flock lock; - - if (self->priv->fd != -1) { - /* Assume we already have the lock and rewind the file to - * the beginning. */ - lseek (self->priv->fd, 0, SEEK_SET); - return TRUE; /* Assume we already have the lock. */ - } - - self->priv->fd = open (self->priv->filename, O_RDWR | O_CREAT, 0755); - if (self->priv->fd == -1) { - return FALSE; - } - - lock.l_type = F_WRLCK; - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 0; - - error = fcntl (self->priv->fd, F_SETLKW, &lock); - - if (error == -1) { - close (self->priv->fd); - self->priv->fd = -1; - return FALSE; - } - - return TRUE; -} - -/* Release the lock on the scores file and dispose of the fd. */ -/* We ignore errors, there is nothing we can do about them. */ -static void -games_scores_backend_release_lock (GamesScoresBackend * self) -{ - struct flock lock; - - /* We don't have a lock, ignore this call. */ - if (self->priv->fd == -1) - return; - - lock.l_type = F_UNLCK; - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 0; - - fcntl (self->priv->fd, F_SETLKW, &lock); - - close (self->priv->fd); - - self->priv->fd = -1; -} - -/** - * games_scores_backend_get_scores: - * @self: the backend to get the scores from - * - * You can alter the list returned by this function, but you must - * make sure you set it again with the _set_scores method or discard it - * with with the _discard_scores method. Otherwise deadlocks will ensue. - * - * Return value: (transfer none) (allow-none) (element-type GnomeGamesSupport.Score): The list of scores - */ -GList * -games_scores_backend_get_scores (GamesScoresBackend * self) -{ - gchar *buffer; - gchar *eol; - gchar *scorestr; - gchar *timestr; - GamesScore *newscore; - struct stat info; - int error; - ssize_t length, target; - GList *t; - - /* Check for a change in the scores file and update if necessary. */ - error = stat (self->priv->filename, &info); - - /* If an error occurs then we give up on the file and return NULL. */ - if (error != 0) { - return NULL; - } - - if ((info.st_mtime > self->priv->timestamp) || (self->priv->scores_list == NULL)) { - self->priv->timestamp = info.st_mtime; - - /* Dump the old list of scores. */ - t = self->priv->scores_list; - while (t != NULL) { - g_object_unref (t->data); - t = g_list_next (t); - } - g_list_free (self->priv->scores_list); - self->priv->scores_list = NULL; - - /* Lock the file and get the list. */ - if (!games_scores_backend_get_lock (self)) - return NULL; - - buffer = g_malloc (info.st_size + 1); - if (buffer == NULL) { - games_scores_backend_release_lock (self); - return NULL; - } - - target = info.st_size; - length = 0; - do { - target -= length; - length = read (self->priv->fd, buffer, info.st_size); - if (length == -1) { - games_scores_backend_release_lock (self); - g_free (buffer); - return NULL; - } - } while (length < target); - - buffer[info.st_size] = '\0'; - - /* FIXME: These details should be in a sub-class. */ - - /* Parse the list. We start by breaking it into lines. */ - /* Since the buffer is null-terminated - * we can do the string stuff reasonably safely. */ - eol = strchr (buffer, '\n'); - scorestr = buffer; - while (eol != NULL) { - *eol++ = '\0'; - timestr = strchr (scorestr, ' '); - if (timestr == NULL) - break; - *timestr++ = '\0'; - /* At this point we have two strings, both null terminated. All - * part of the original buffer. */ - switch (self->priv->style) { - case GAMES_SCORES_STYLE_PLAIN_DESCENDING: - case GAMES_SCORES_STYLE_PLAIN_ASCENDING: - newscore = games_score_new_plain (g_ascii_strtod (scorestr, NULL)); - break; - case GAMES_SCORES_STYLE_TIME_DESCENDING: - case GAMES_SCORES_STYLE_TIME_ASCENDING: - newscore = games_score_new_time (g_ascii_strtod (scorestr, NULL)); - break; - default: - g_assert_not_reached (); - } - games_score_set_time (newscore, g_ascii_strtoull (timestr, NULL, 10)); - self->priv->scores_list = g_list_append (self->priv->scores_list, newscore); - /* Setup again for the next time around. */ - scorestr = eol; - eol = strchr (eol, '\n'); - } - - g_free (buffer); - } - - /* FIXME: Sort the scores! We shouldn't rely on the file being sorted. */ - - return self->priv->scores_list; -} - -gboolean -games_scores_backend_set_scores (GamesScoresBackend * self, GList * list) -{ - GList *s; - GamesScore *d; - gchar *buffer; - gint output_length = 0; - gchar dtostrbuf[G_ASCII_DTOSTR_BUF_SIZE]; - - if (!games_scores_backend_get_lock (self)) - return FALSE; - - self->priv->scores_list = list; - - s = list; - while (s != NULL) { - gdouble rscore; - guint64 rtime; - - d = (GamesScore *) s->data; - rscore = 0.0; - switch (self->priv->style) { - case GAMES_SCORES_STYLE_PLAIN_DESCENDING: - case GAMES_SCORES_STYLE_PLAIN_ASCENDING: - rscore = games_score_get_value_as_plain (d); - break; - case GAMES_SCORES_STYLE_TIME_DESCENDING: - case GAMES_SCORES_STYLE_TIME_ASCENDING: - rscore = games_score_get_value_as_time(d); - break; - default: - g_assert_not_reached (); - } - rtime = games_score_get_time (d); - - buffer = g_strdup_printf ("%s %"G_GUINT64_FORMAT"\n", - g_ascii_dtostr (dtostrbuf, sizeof (dtostrbuf), - rscore), rtime); - write (self->priv->fd, buffer, strlen (buffer)); - output_length += strlen (buffer); - /* Ignore any errors and blunder on. */ - g_free (buffer); - - s = g_list_next (s); - } - - /* Remove any content in the file that hasn't yet been overwritten. */ - ftruncate (self->priv->fd, output_length--); - - /* Update the timestamp so we don't reread the scores unnecessarily. */ - self->priv->timestamp = time (NULL); - - games_scores_backend_release_lock (self); - - return TRUE; -} - -void -games_scores_backend_discard_scores (GamesScoresBackend * self) -{ - games_scores_backend_release_lock (self); -} diff --git a/src/games-scores-backend.h b/src/games-scores-backend.h deleted file mode 100644 index 9cc35d2d3894e2afdb96511c2bf9f902067c3542..0000000000000000000000000000000000000000 --- a/src/games-scores-backend.h +++ /dev/null @@ -1,61 +0,0 @@ - /* games-scores-backend.c - * - * Copyright (C) 2005 Callum McKenzie - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see . - */ - -#ifndef GAMES_SCORES_BACKEND_H -#define GAMES_SCORES_BACKEND_H - -#include -#include - -#include - -#include "games-score.h" - -G_BEGIN_DECLS - -#define GAMES_TYPE_SCORES_BACKEND (games_scores_backend_get_type ()) -#define GAMES_SCORES_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GAMES_TYPE_SCORES_BACKEND, GamesScoresBackend)) -#define GAMES_SCORES_BACKEND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAMES_TYPE_SCORES_BACKEND, GamesScoresBackendClass)) -#define GAMES_IS_SCORES_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GAMES_TYPE_SCORES_BACKEND)) -#define GAMES_IS_SCORES_BACKEND_CLASS(kls) (G_TYPE_CHECK_CLASS_TYPE ((kls), GAMES_TYPE_SCORES_BACKEND)) -#define GAMES_GET_SCORES_BACKEND_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAMES_TYPE_SCORES_BACKEND, GamesScoresBackendClass)) - -typedef struct GamesScoresBackendPrivate GamesScoresBackendPrivate; - -typedef struct { - GObject object; - /*< private >*/ - GamesScoresBackendPrivate *priv; -} GamesScoresBackend; - -typedef struct { - GObjectClass parent_class; -} GamesScoresBackendClass; - -void games_scores_backend_startup (void); -GType games_scores_backend_get_type (void); -GamesScoresBackend *games_scores_backend_new (GamesScoreStyle style, - char *base_name, - char *name); -GList *games_scores_backend_get_scores (GamesScoresBackend * self); -gboolean games_scores_backend_set_scores (GamesScoresBackend * self, - GList * list); -void games_scores_backend_discard_scores (GamesScoresBackend * self); - -G_END_DECLS -#endif /* GAMES_SCORES_BACKEND_H */ diff --git a/src/games-scores-dialog.c b/src/games-scores-dialog.c deleted file mode 100644 index e49326f17d2d94b6df8696e189c6dedfd5208274..0000000000000000000000000000000000000000 --- a/src/games-scores-dialog.c +++ /dev/null @@ -1,507 +0,0 @@ -/* -*- mode: C -*- - - games-scores-dialog.c - Copyright 2004, 2005, 2006 Callum McKenzie - - This library is free software; you can redistribute it and'or modify - it under the terms of the GNU Library General Public License as published - by the Free Software Foundation; either version 2, or (at your option) - any later version. - - This library 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 Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; if not, see . */ - -/* Authors: Callum McKenzie */ - -#include - -#include -#include - -#include "games-scores-dialog.h" - -G_DEFINE_TYPE (GamesScoresDialog, games_scores_dialog, GTK_TYPE_DIALOG); - -struct GamesScoresDialogPrivate { - GtkWidget *message; - GtkWidget *hdiv; - GtkWidget *combo; - GtkWidget *label; - GtkWidget *catbar; - GtkListStore *list; - GtkTreeView *treeview; - GtkTreeViewColumn *column; - GtkTreeViewColumn *timecolumn; - GamesScores *scores; - GHashTable *categories; - GHashTable *catindices; - gint catcounter; - gint hilight; - gboolean preservehilight; - - /* FIXME: This should be a property. */ - gint style; -}; - -static void -games_scores_dialog_finalize (GObject *o) -{ - GamesScoresDialog *dialog = GAMES_SCORES_DIALOG (o); - - if (dialog->priv->scores) - g_object_unref (dialog->priv->scores); - - if (dialog->priv->categories) - g_hash_table_destroy (dialog->priv->categories); - if (dialog->priv->catindices) - g_hash_table_destroy (dialog->priv->catindices); - - G_OBJECT_CLASS (games_scores_dialog_parent_class)->finalize (o); -} - -static void -games_scores_dialog_class_init (GamesScoresDialogClass *klass) -{ - g_type_class_add_private (klass, sizeof (GamesScoresDialogPrivate)); - - G_OBJECT_CLASS (klass)->finalize = games_scores_dialog_finalize; -} - -/** - * add_category: - * @self: a pointer to a GamesScoresDialog - * @key: an identifier for the category. This should also be a valid - * score category for the gnome_score system. - * @name: the category name - * - * Adds a new category to combo box selector. - * - **/ -static void games_scores_dialog_add_category (GamesScoresDialog *self, - const gchar *key, - const gchar *name) -{ - gchar *k; - - k = g_strdup (key); - - g_hash_table_insert (self->priv->categories, k, - GINT_TO_POINTER (self->priv->catcounter)); - g_hash_table_insert (self->priv->catindices, - GINT_TO_POINTER (self->priv->catcounter), - k); - self->priv->catcounter++; - gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (self->priv->combo), name); -} - -/* This is a helper function for loading the initial list of categories - * in a _foreach function. If only C had lambda... */ -static void games_scores_dialog_load_categories (GamesScoresCategory *cat, - GamesScoresDialog *self) -{ - /* category->name is already translated here! */ - games_scores_dialog_add_category (self, cat->key, cat->name); -} - -/** - * set_style: - * @self: a pointer to a GamesScoresDialog - * @style: the style to use - * - * Sets the style of score displayed. e.g. GAMES_SCORES_STYLE_TIME - * displays the scores as times. Note that the order of the scores - * is determined at the gnome_score layer but their interpretation - * is at this layer. - * - **/ -static void games_scores_dialog_set_style (GamesScoresDialog *self, GamesScoreStyle style) -{ - const gchar *header; - - self->priv->style = style; - switch (style) { - case GAMES_SCORES_STYLE_TIME_DESCENDING: - case GAMES_SCORES_STYLE_TIME_ASCENDING: - /* Score dialog column header for the score time (e.g. 1 minute) */ - header = C_("score-dialog", "Time"); - break; - case GAMES_SCORES_STYLE_PLAIN_DESCENDING: - case GAMES_SCORES_STYLE_PLAIN_ASCENDING: - default: - /* Score dialog column header for the score value (e.g. 30 moves) */ - header = C_("score-dialog", "Score"); - } - - gtk_tree_view_column_set_title (self->priv->column, header); -} - -/** - * set_category: - * @self: a pointer to a GamesScoresDialog - * @key: the category to change to - * - * Sets the category the scores dialog is displaying. - * - **/ -static void games_scores_dialog_set_category (GamesScoresDialog *self, - const gchar *key) -{ - gpointer value; - int idx; - - value = g_hash_table_lookup (self->priv->categories, key); - idx = GPOINTER_TO_INT (value); - - self->priv->preservehilight = TRUE; - gtk_combo_box_set_active (GTK_COMBO_BOX (self->priv->combo), idx); -} - -/** - * new: - * @domain: the scores domain to use, usually the application name - * @title: the title for the dialog - * - * Creates a new high scores dialog. Use gtk_dialog_run and - * gtk_widget_destroy to manage it. - * - * Returns: a new widget - * - **/ -GtkWidget * games_scores_dialog_new (GtkWindow *parent_window, GamesScores *scores, const gchar *title) -{ - GamesScoresDialog *dialog = GAMES_SCORES_DIALOG (g_object_new (GAMES_TYPE_SCORES_DIALOG, NULL)); - - dialog->priv->scores = g_object_ref (scores); - games_scores_dialog_set_style (dialog, games_scores_get_style (scores)); - dialog->priv->preservehilight = FALSE; - - gtk_window_set_title (GTK_WINDOW (dialog), title); - gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent_window)); - - _games_scores_category_foreach (scores, - (GamesScoresCategoryForeachFunc) games_scores_dialog_load_categories, - dialog); - - if (dialog->priv->catcounter <= 1) { - gtk_widget_hide (dialog->priv->catbar); - } - - return (GtkWidget *)dialog; -} - -/* Load up the list with the current set of scores. */ -static void games_scores_dialog_redraw (GamesScoresDialog *self) { - GtkTreeIter iter; - const gchar *name; - gint score; - gchar *ss; - gint timestamp; - GDateTime *datetime; - gchar *time; - gdouble dscore; - GList *scorelist; - - gtk_list_store_clear (self->priv->list); - - scorelist = games_scores_get (self->priv->scores); - - while (scorelist) { - timestamp = games_score_get_time ((GamesScore *)scorelist->data); - datetime = g_date_time_new_from_unix_local (timestamp); - time = g_date_time_format (datetime,"%d/%m/%y"); - - switch (self->priv->style) { - case GAMES_SCORES_STYLE_TIME_ASCENDING: - case GAMES_SCORES_STYLE_TIME_DESCENDING: - dscore = games_score_get_value_as_time ((GamesScore *)scorelist->data); - score = (int) (100.0 * dscore + 0.5); - ss = g_strdup_printf (/* Score format for time based scores. %1$d is the time in minutes, %2$d is the time in seconds */ - C_("score-dialog", "%1$dm %2$ds"), - score/100, score%100); - break; - case GAMES_SCORES_STYLE_PLAIN_ASCENDING: - case GAMES_SCORES_STYLE_PLAIN_DESCENDING: - default: - score = games_score_get_value_as_plain ((GamesScore *)scorelist->data); - ss = g_strdup_printf ("%d", score); - } - gtk_list_store_append (self->priv->list, &iter); - gtk_list_store_set (self->priv->list, &iter, 0, time, 1, ss, -1); - g_free (ss); - g_free (time); - scorelist = g_list_next (scorelist); - } -} - -/* Change the currently viewed score category. There is a little bit - * of silly-buggers here to make sure the change is temporary and - * we end up on the right page next time. */ -static void games_scores_dialog_change_category (GtkComboBox *widget, - GamesScoresDialog *self) -{ - gchar *catcopy; - gint idx; - gchar *newcat; - - /* This seems like a bit of a hack, but since we're trying to - * temporarily change the category it sort of makes sense. */ - - catcopy = g_strdup (games_scores_get_category (self->priv->scores)); - idx = gtk_combo_box_get_active (widget); - newcat = g_hash_table_lookup (self->priv->catindices, - GINT_TO_POINTER (idx)); - - games_scores_set_category (self->priv->scores, newcat); - if (self->priv->preservehilight) { - self->priv->preservehilight = FALSE; - } else { - self->priv->hilight = 0; - } - games_scores_dialog_redraw (self); - games_scores_set_category (self->priv->scores, catcopy); - - g_free (catcopy); -} - -/* This is to make sure we update ourselves when the window goes through - * a hide/show cycle. */ -/* FIXME: We should monitor the high scores list (or get games-scores to - * send us a signal. */ -static void games_scores_dialog_show (GamesScoresDialog *self) -{ - const gchar *cat; - - cat = games_scores_get_category (self->priv->scores); - if (cat) - games_scores_dialog_set_category (self, cat); - games_scores_dialog_redraw (self); -} - -/* This is the other half of ensuring the hide/show cycle works properly. */ -static void games_scores_dialog_hide (GamesScoresDialog *self) { - self->priv->hilight = 0; - gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (self->priv->treeview)); -} - -/** - * set_category_description: - * @self: a pointer to a GamesScoresDialog - * @description: A description of the categories - * - * Sets the category description label. i.e. the widget to the - * left of the category combo box. - * - **/ -void games_scores_dialog_set_category_description (GamesScoresDialog *self, - const gchar *description) -{ - gchar *lstr; - - lstr = g_strdup_printf ("%s", description); - gtk_label_set_markup (GTK_LABEL (self->priv->label), lstr); - gtk_label_set_use_underline (GTK_LABEL (self->priv->label), TRUE); - g_free(lstr); -} - -/** - * set_message: - * @self: a pointer to a GamesScoresDialog - * @message: the message - * - * Sets the message at the top of the dialog. Pango markup is understood. - * - **/ -void games_scores_dialog_set_message (GamesScoresDialog *self, - const gchar *message) -{ - if ((message == NULL) || (*message == '\0')) { - gtk_widget_hide (self->priv->message); - gtk_widget_hide (self->priv->hdiv); - } else { - gtk_widget_show (self->priv->message); - gtk_widget_show (self->priv->hdiv); - gtk_label_set_label (GTK_LABEL (self->priv->message), message); - } -} - -/** - * set_category_description: - * @self: a pointer to a GamesScoresDialog - * @pos: the position in the high score list to hilight. Should be in the - * range 1 to 10. - * - * Hilights an entry in the high score list. This is suitable for indicating - * to the player where the game they just played is. - * - **/ -void games_scores_dialog_set_hilight (GamesScoresDialog *self, guint pos) -{ - if ((pos < 1) || (pos > GAMES_SCORES_SIGNIFICANT)) - return; - - self->priv->hilight = pos; -} - -/** - * set_buttons: - * @self: a pointer to a GamesScoresDialog - * @buttons: An or-ed list of GamesScoresButtons - * - * Changes the button sets at the buttom of the dialog - * - **/ -void games_scores_dialog_set_buttons (GamesScoresDialog *self, guint buttons) -{ - /* Remove an existing buttons. */ - gtk_container_foreach (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (self))), - (GtkCallback) (gtk_widget_destroy), NULL); - - /* The default is a single close button, suitable for the scores - menu item. */ - if (buttons == 0) - buttons = GAMES_SCORES_CLOSE_BUTTON; - - if (buttons & GAMES_SCORES_QUIT_BUTTON) { - gtk_dialog_add_button (GTK_DIALOG (self), _("_Quit"), - GTK_RESPONSE_REJECT); - gtk_dialog_set_default_response (GTK_DIALOG (self), - GTK_RESPONSE_REJECT); - } - - if (buttons & GAMES_SCORES_UNDO_BUTTON) { - gtk_dialog_add_button (GTK_DIALOG (self), _("_Undo"), - GTK_RESPONSE_DELETE_EVENT); - gtk_dialog_set_default_response (GTK_DIALOG (self), - GTK_RESPONSE_DELETE_EVENT); - } - - if (buttons & GAMES_SCORES_NEW_GAME_BUTTON) { - gtk_dialog_add_button (GTK_DIALOG (self), _("_New Game"), - GTK_RESPONSE_ACCEPT); - gtk_dialog_set_default_response (GTK_DIALOG (self), - GTK_RESPONSE_ACCEPT); - } - - if (buttons & GAMES_SCORES_CLOSE_BUTTON) { - gtk_dialog_add_button (GTK_DIALOG (self), _("_Close"), - GTK_RESPONSE_CLOSE); - gtk_dialog_set_default_response (GTK_DIALOG (self), - GTK_RESPONSE_CLOSE); - } -} - -static void games_scores_dialog_init (GamesScoresDialog *self) -{ - GtkWidget *vbox; - GtkWidget *scroll; - GtkWidget *listview; - GtkTreeViewColumn *column; - GtkTreeViewColumn *timecolumn; - GtkCellRenderer *renderer; - GtkCellRenderer *timerenderer; - - self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GAMES_TYPE_SCORES_DIALOG, - GamesScoresDialogPrivate); - - self->priv->style = GAMES_SCORES_STYLE_PLAIN_DESCENDING; - /* These two hashes are the reverse of each other. As an optimisation - * they share the same set of strings (as keys in the first case and - * as data in the second). The first hash is responsible for - * deallocating the memory for the strings. These two are only - * valid as a pair. */ - self->priv->categories = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, NULL); - self->priv->catindices = g_hash_table_new (g_direct_hash, g_direct_equal); - self->priv->catcounter = 0; - self->priv->hilight = 0; - gtk_container_set_border_width (GTK_CONTAINER (self), 5); - gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), 2); - - g_signal_connect (G_OBJECT (self), "show", - G_CALLBACK (games_scores_dialog_show), NULL); - g_signal_connect (G_OBJECT (self), "hide", - G_CALLBACK (games_scores_dialog_hide), NULL); - - vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6); - gtk_container_set_border_width (GTK_CONTAINER (vbox), 5); - gtk_box_pack_end (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (self))), - vbox, TRUE, TRUE, 0); - - scroll = gtk_scrolled_window_new (NULL, NULL); - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll), - GTK_POLICY_AUTOMATIC, - GTK_POLICY_AUTOMATIC); - gtk_widget_set_size_request (scroll, 200, 265); - gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroll), - GTK_SHADOW_ETCHED_IN); - gtk_box_pack_end (GTK_BOX (vbox), scroll, TRUE, TRUE, 0); - - self->priv->message = gtk_label_new (""); - gtk_label_set_use_markup (GTK_LABEL (self->priv->message), TRUE); - gtk_label_set_justify (GTK_LABEL (self->priv->message), - GTK_JUSTIFY_CENTER); - gtk_box_pack_start (GTK_BOX (vbox), self->priv->message, FALSE, FALSE, 0); - - self->priv->hdiv = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL); - gtk_box_pack_start (GTK_BOX (vbox), self->priv->hdiv, FALSE, FALSE, 0); - - self->priv->catbar = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12); - gtk_box_pack_start (GTK_BOX (vbox), self->priv->catbar, FALSE, FALSE, 0); - - self->priv->label = gtk_label_new (NULL); - gtk_label_set_use_markup (GTK_LABEL (self->priv->label), TRUE); - gtk_box_pack_start (GTK_BOX (self->priv->catbar), self->priv->label, - FALSE, FALSE, 0); - - self->priv->combo = gtk_combo_box_text_new (); - gtk_widget_set_focus_on_click (GTK_WIDGET (self->priv->combo), FALSE); - gtk_box_pack_start (GTK_BOX (self->priv->catbar), - self->priv->combo, TRUE, TRUE, 0); - gtk_label_set_mnemonic_widget (GTK_LABEL (self->priv->label), self->priv->combo); - - g_signal_connect (G_OBJECT (self->priv->combo), "changed", - G_CALLBACK (games_scores_dialog_change_category), self); - - self->priv->list = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING); - - listview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (self->priv->list)); - self->priv->treeview = GTK_TREE_VIEW (listview); - - timerenderer = gtk_cell_renderer_text_new (); - timecolumn = gtk_tree_view_column_new_with_attributes (/* Score dialog column header for the date the score was recorded */ - _("Date"), - timerenderer, - "text", 0, - NULL); - g_object_set (G_OBJECT (timerenderer), "xalign", 1.0, NULL); - gtk_tree_view_append_column (GTK_TREE_VIEW (listview), - GTK_TREE_VIEW_COLUMN (timecolumn)); - self->priv->timecolumn = timecolumn; - - renderer = gtk_cell_renderer_text_new (); - column = gtk_tree_view_column_new_with_attributes ("", renderer, "text", 1, NULL); - g_object_set (G_OBJECT (renderer), "xalign", 1.0, NULL); - gtk_tree_view_append_column (GTK_TREE_VIEW (listview), - GTK_TREE_VIEW_COLUMN (column)); - self->priv->column = column; - - gtk_container_add (GTK_CONTAINER (scroll), listview); - - games_scores_dialog_set_buttons (self, GAMES_SCORES_CLOSE_BUTTON); - - gtk_window_set_destroy_with_parent (GTK_WINDOW (self), TRUE); - - gtk_widget_grab_focus (self->priv->combo); - - gtk_widget_show_all (vbox); - gtk_widget_hide (self->priv->hdiv); - gtk_widget_hide (self->priv->message); -} - -/* FIXME: There is basically no range checking. */ - diff --git a/src/games-scores-dialog.h b/src/games-scores-dialog.h deleted file mode 100644 index 24de6cac9ad8afe0a081d03064c975c6a29e8bef..0000000000000000000000000000000000000000 --- a/src/games-scores-dialog.h +++ /dev/null @@ -1,80 +0,0 @@ -/* -*- mode: C -*- - - games-scores-dialog.h - Copyright 2004, 2005, 2006 Callum McKenzie - - This library is free software; you can redistribute it and'or modify - it under the terms of the GNU Library General Public License as published - by the Free Software Foundation; either version 2, or (at your option) - any later version. - - This library 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 Library General Public License for more details. - - You should have received a copy of the GNU Library General Public License - along with this library; if not, see . */ - -/* Authors: Callum McKenzie */ - -#ifndef GAMES_SCORES_DIALOG_H -#define GAMES_SCORES_DIALOG_H - -#include - -#include "games-score.h" /* For GamesScoreStyle. */ -#include "games-scores.h" - -G_BEGIN_DECLS -#define GAMES_TYPE_SCORES_DIALOG (games_scores_dialog_get_type ()) -#define GAMES_SCORES_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), \ - GAMES_TYPE_SCORES_DIALOG, \ - GamesScoresDialog)) -#define GAMES_SCORES_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), \ - GAMES_TYPE_SCORES_DIALOG, \ - GamesScoresDialogClass)) -#define GAMES_IS_SCORES_DIALOG(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \ - GAMES_TYPE_SCORES_DIALOG)) -#define GAMES_IS_SCORES_DIALOG_CLASS(kls) (G_TYPE_CHECK_CLASS_TYPE ((kls), \ - GAMES_TYPE_SCORES_DIALOG)) -#define GAMES_GET_SCORES_DIALOG_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \ - GAMES_TYPE_SCORES_DIALOG, \ - GamesScoresDialogClass)) - -/* This enumeration is to provide common buttons for the dialog. */ - -typedef enum { - GAMES_SCORES_CLOSE_BUTTON = 1, - GAMES_SCORES_NEW_GAME_BUTTON = 2, - GAMES_SCORES_UNDO_BUTTON = 4, - GAMES_SCORES_QUIT_BUTTON = 8, -} GamesScoresButtons; - -typedef struct GamesScoresDialogPrivate GamesScoresDialogPrivate; - -typedef struct { - GtkDialog dialog; - /*< private >*/ - GamesScoresDialogPrivate *priv; -} GamesScoresDialog; - -typedef struct { - GtkDialogClass parent_class; -} GamesScoresDialogClass; - -GType games_scores_dialog_get_type (void); -GtkWidget *games_scores_dialog_new (GtkWindow *parent_window, - GamesScores *scores, - const gchar *title); -void games_scores_dialog_set_category_description (GamesScoresDialog *self, - const gchar *description); -void games_scores_dialog_set_hilight (GamesScoresDialog *self, - guint pos); -void games_scores_dialog_set_message (GamesScoresDialog *self, - const gchar *message); -void games_scores_dialog_set_buttons (GamesScoresDialog *self, - guint buttons); - -G_END_DECLS -#endif diff --git a/src/games-scores.c b/src/games-scores.c deleted file mode 100644 index 5dd9870e854a52b9ddd96eab69721d71f19fa47a..0000000000000000000000000000000000000000 --- a/src/games-scores.c +++ /dev/null @@ -1,445 +0,0 @@ -/* - * Copyright 2005 Callum McKenzie - * - * This library is free software; you can redistribute it and'or modify - * it under the terms of the GNU Library General Public License as published - * by the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This library 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 Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; if not, see . - * - */ - -/* Authors: Callum McKenzie */ - -/* FIXME: Document */ - -#include - -#include -#include - -#include - -#include "games-scores-backend.h" -#include "games-score.h" -#include "games-scores.h" - -/* The local version of the GamesScoresCategory. */ -typedef struct { - GamesScoresCategory category; - GamesScoresBackend *backend; -} GamesScoresCategoryInternal; - -struct GamesScoresPrivate { - GHashTable *categories; - GSList *catsordered; - gchar *currentcat; - gchar *defcat; - gchar *basename; - gboolean last_score_significant; - gint last_score_position; - GamesScore *last_score; - GamesScoreStyle style; - GamesScoresCategoryInternal dummycat; -}; - -void -games_scores_startup (void) -{ - games_scores_backend_startup (); -} - -static void -games_scores_category_free (GamesScoresCategoryInternal *cat) -{ - g_free (cat->category.key); - g_free (cat->category.name); - if (cat->backend) - g_object_unref (cat->backend); - g_free (cat); -} - -/** - * games_scores_get_current: - * @self: A scores object. - * - * Retrieves the current category and make sure it is in a state to be used. - * - **/ -static GamesScoresCategoryInternal * -games_scores_get_current (GamesScores * self) -{ - GamesScoresPrivate *priv = self->priv; - GamesScoresCategoryInternal *cat; - - if (priv->currentcat == NULL) { - /* We have a single, anonymous, category. */ - cat = &(priv->dummycat); - } else { - cat = g_hash_table_lookup (priv->categories, priv->currentcat); - if (!cat) - return NULL; - } - - if (cat->backend == NULL) { - cat->backend = games_scores_backend_new (priv->style, priv->basename, - cat->category.key); - } - - return cat; -} - -/* FIXME: Static games_score_init function to initialise the setgid stuff. */ -/* FIXME: This is actually an argument for a helper-app since this function - * won't know what files we are after until _new is called. */ - -G_DEFINE_TYPE (GamesScores, games_scores, G_TYPE_OBJECT); - -/** - * games_scores_new: - * @app_name: the (old) app name (for backward compatibility), - * used as the basename of the category filenames - * @categories: (allow-none): the score categories, or %NULL to use an anonymous category - * @n_categories: (allow-none): the number of category entries in @categories - * @categories_context: (allow-none): the translation context to use for the category names, - * or %NULL to use no translation context - * @categories_domain: (allow-none): the translation domain to use for the category names, - * or %NULL to use the default domain - * @default_category_index: (allow-none): the key of the default category, or %NULL - * @style: the category style - * - * - * Returns: a new #GamesScores object - */ -GamesScores * -games_scores_new (const char *app_name, - const GamesScoresCategory *categories, - int n_categories, - const char *categories_context, - const char *categories_domain, - int default_category_index, - GamesScoreStyle style) -{ - GamesScores *self; - - self = GAMES_SCORES (g_object_new (GAMES_TYPE_SCORES, NULL)); - - /* FIXME: Input sanity checks. */ - - /* catsordered is a record of the ordering of the categories. - * Its data is shared with the hash table. */ - self->priv->catsordered = NULL; - - if (n_categories > 0) { - int i; - - g_return_val_if_fail (default_category_index >= 0 && default_category_index < n_categories, NULL); - - for (i = 0; i < n_categories; ++i) { - const GamesScoresCategory *category = &categories[i]; - const char *display_name; - - if (categories_context) { - display_name = g_dpgettext2 (categories_domain, categories_context, category->name); - } else { - display_name = dgettext (categories_domain, category->name); - } - - games_scores_add_category (self, category->key, display_name); - } - - self->priv->defcat = g_strdup (categories[default_category_index].key); - self->priv->currentcat = g_strdup (self->priv->defcat); - } - - self->priv->basename = g_strdup (app_name); - /* FIXME: Do some sanity checks on the default and the like. */ - - self->priv->style = style; - - /* Set up the anonymous category for use when no categories are specified. */ - self->priv->dummycat.category.key = (char *) ""; - self->priv->dummycat.category.name = (char *) ""; - - return self; -} - -/** - * games_scores_add_category: - * @self: - * @key: the key for the new category - * @name: the user visible label for the new category - * - * Add a new category after initialisation. key and name are copied into - * internal structures. The scores dialog is not currently updated. - * - **/ -void -games_scores_add_category (GamesScores *self, - const char *key, - const char *name) -{ - GamesScoresPrivate *priv = self->priv; - GamesScoresCategoryInternal *cat; - - cat = g_new (GamesScoresCategoryInternal, 1); - cat->category.key = g_strdup (key); - cat->category.name = g_strdup (name); - cat->backend = NULL; - - g_hash_table_insert (priv->categories, g_strdup (key), cat); - priv->catsordered = g_slist_append (priv->catsordered, cat); -} - -/** - * games_scores_set_category: - * @self: A scores object. - * @category: A string identifying the category to use (the key in - * the GamesScoresCategory structure). - * - * This function sets the scores category to use. e.g. whether we are playing - * on hard, medium or easy. It should be used at the time that the game - * itself switches between difficulty levels. The category determines where - * scores are to be stored and read from. - * - **/ -void -games_scores_set_category (GamesScores * self, const gchar * category) -{ - GamesScoresPrivate *priv = self->priv; - - g_return_if_fail (self != NULL); - - if (category == NULL) - category = priv->defcat; - - g_free (priv->currentcat); - priv->currentcat = g_strdup (category); - - /* FIXME: Check validity of category (Null, the same as current, - * is actually a category) then just set it in the structure. */ -} - -/** - * games_scores_add_score: - * @self: A scores object. - * @score: A #GamesScore - it is up to the caller to convert their - * raw value to one of the supported types. - * - * Add a score to the set of scores. Retention of anything but the - * top-ten scores is undefined. It returns either the place in the top ten - * or zero if no place was achieved. It can therefore be treated as a - * boolean if desired. - * - **/ -gint -games_scores_add_score (GamesScores * self, GamesScore *score) -{ - GamesScoresPrivate *priv = self->priv; - GamesScoresCategoryInternal *cat; - gint place, n; - GList *s, *scores_list; - - g_return_val_if_fail (self != NULL, 0); - - cat = games_scores_get_current (self); - - scores_list = games_scores_backend_get_scores (cat->backend); - - s = scores_list; - place = 0; - n = 0; - - while (s != NULL) { - GamesScore *oldscore = s->data; - - n++; - - /* If beat someone in the list, add us there. */ - if (games_score_compare (priv->style, oldscore, score) < 0) { - scores_list = g_list_insert_before (scores_list, s, - g_object_ref (score)); - place = n; - break; - } - - s = g_list_next (s); - } - - /* If we haven't placed anywhere and the list still has - * room to grow, put us on the end. - * This also handles the empty-file case. */ - if ((place == 0) && (n < GAMES_SCORES_SIGNIFICANT)) { - place = n + 1; - scores_list = g_list_append (scores_list, g_object_ref (score)); - } - - if (g_list_length (scores_list) > GAMES_SCORES_SIGNIFICANT) { - s = g_list_nth (scores_list, GAMES_SCORES_SIGNIFICANT - 1); - /* Note that we are guaranteed to only need to remove one link - * and it is also guaranteed not to be the first one. */ - g_object_unref (g_list_next (s)->data); - g_list_free (g_list_next (s)); - s->next = NULL; - } - - if (games_scores_backend_set_scores (cat->backend, scores_list) == FALSE) - place = 0; - - priv->last_score_significant = place > 0; - priv->last_score_position = place; - g_object_unref (priv->last_score); - priv->last_score = g_object_ref (score); - - return place; -} - -gint -games_scores_add_plain_score (GamesScores * self, guint32 value) -{ - return games_scores_add_score (self, games_score_new_plain (value)); -} - -gint -games_scores_add_time_score (GamesScores * self, gdouble value) -{ - return games_scores_add_score (self, games_score_new_time (value)); -} - -/** - * games_scores_get: - * @self: A scores object. - * - * Get a list of GamesScore objects for the current category. The list - * is still owned by the GamesScores object and is not guaranteed to - * be the either the same or accurate after any games_scores call - * except games_scores_get. Do not alter the data either. - * - * Returns: (element-type GnomeGamesSupport.Score) (transfer none): A list of GamesScore objects. - **/ -GList * -games_scores_get (GamesScores * self) -{ - GamesScoresCategoryInternal *cat; - GList *scores; - - g_return_val_if_fail (self != NULL, NULL); - - cat = games_scores_get_current (self); - - scores = games_scores_backend_get_scores (cat->backend); - /* Tell the backend that we won't be altering the scores so it - * can release the lock. */ - games_scores_backend_discard_scores (cat->backend); - - return scores; -} - -/** - * _games_scores_category_foreach: - * @self: A scores object. - * @func: A function to call. - * @userdata: Arbitrary data. - * - * This function will iterate over the list of categories calling the - * supplied function with the category and userdata as arguments. - * The ordering of the categories is the order they were added. - * - **/ -void -_games_scores_category_foreach (GamesScores * self, - GamesScoresCategoryForeachFunc func, - gpointer userdata) -{ - GamesScoresPrivate *priv = self->priv; - GSList *l; - - g_return_if_fail (self != NULL); - - for (l = priv->catsordered; l != NULL; l = l->next) { - func ((GamesScoresCategory*) l->data, userdata); - } -} - -/** - * games_scores_get_style: - * @self: A scores object. - * - * Returns the style of the scores. - * - **/ -GamesScoreStyle -games_scores_get_style (GamesScores * self) -{ - GamesScoresPrivate *priv = self->priv; - - g_return_val_if_fail (self != NULL, 0); - - return priv->style; -} - -/** - * games_scores_get_category: - * @self: A scores object. - * - * Returns the current category key. It is owned by the GamesScores object and - * should not be altered. This will be NULL if no category is current (this - * will typically happen if no categories have been added to the GamesScore). - * - **/ -const gchar * -games_scores_get_category (GamesScores * self) -{ - GamesScoresPrivate *priv = self->priv; - - g_return_val_if_fail (self != NULL, NULL); - - return priv->currentcat; -} - -static void -games_scores_init (GamesScores * self) -{ - GamesScoresPrivate *priv; - - /* Most of the work is done in the _new method. */ - - priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GAMES_TYPE_SCORES, GamesScoresPrivate); - - priv->last_score_significant = FALSE; - priv->last_score_position = 0; - priv->last_score = games_score_new (); - priv->categories = g_hash_table_new_full (g_str_hash, g_str_equal, - g_free, - (GDestroyNotify) games_scores_category_free); -} - -static void -games_scores_finalize (GObject * object) -{ - GamesScores *scores = GAMES_SCORES (object); - - g_hash_table_unref (scores->priv->categories); - g_slist_free (scores->priv->catsordered); - g_free (scores->priv->currentcat); - g_free (scores->priv->defcat); - g_free (scores->priv->basename); - g_object_unref (scores->priv->last_score); - - G_OBJECT_CLASS (games_scores_parent_class)->finalize (object); -} - -static void -games_scores_class_init (GamesScoresClass * klass) -{ - GObjectClass *object_class = (GObjectClass *) klass; - object_class->finalize = games_scores_finalize; - g_type_class_add_private (klass, sizeof (GamesScoresPrivate)); -} diff --git a/src/games-scores.h b/src/games-scores.h deleted file mode 100644 index 98028d5f41283a3b733f03121fcf7ad1897c9c98..0000000000000000000000000000000000000000 --- a/src/games-scores.h +++ /dev/null @@ -1,89 +0,0 @@ -/* Games Scores Dialog - Display high scores - * - * Copyright (c) 2005 by Callum McKenzie - * - * This library is free software; you can redistribute it and'or modify - * it under the terms of the GNU Library General Public License as published - * by the Free Software Foundation; either version 2, or (at your option) - * any later version. - * - * This library 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 Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this library; if not, see . - * - */ - -/* Authors: Callum McKenzie */ - -#ifndef GAMES_SCORES_H -#define GAMES_SCORES_H - -#include -#include - -G_BEGIN_DECLS - -#include "games-score.h" -#include "games-scores-backend.h" - -/* How many scores get counted as significant. */ -#define GAMES_SCORES_SIGNIFICANT 10 - -typedef struct { - gchar *key; /* A unique identifier (warning: this is used to generate the - * scores file name, so it should match the old domains) */ - gchar *name; /* A human-readable description. */ -} GamesScoresCategory; - -typedef void (*GamesScoresCategoryForeachFunc) (GamesScoresCategory * cat, - gpointer data); - -#define GAMES_TYPE_SCORES (games_scores_get_type()) -#define GAMES_SCORES(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), games_scores_get_type(), GamesScores) -#define GAMES_SCORES_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), games_scores_get_type(), GamesScores const) -#define GAMES_SCORES_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), games_scores_get_type(), GamesScoresClass) -#define GAMES_IS_SCORES(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), games_scores_get_type ()) -#define GAMES_SCORES_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), games_scores_get_type(), GamesScoresClass) - -typedef struct GamesScoresPrivate GamesScoresPrivate; - -typedef struct { - GObject parent; - /*< private >*/ - GamesScoresPrivate *priv; -} GamesScores; - -typedef struct { - GObjectClass parent; -} GamesScoresClass; - -void games_scores_startup (void); -GType games_scores_get_type (void); -GamesScores *games_scores_new (const char *app_name, - const GamesScoresCategory *categories, - int n_categories, - const char *categories_context, - const char *categories_domain, - int default_category_index, - GamesScoreStyle style); -void games_scores_set_category (GamesScores * self, const gchar * category); -gint games_scores_add_score (GamesScores * self, GamesScore *score); -gint games_scores_add_plain_score (GamesScores * self, guint32 value); -gint games_scores_add_time_score (GamesScores * self, gdouble value); -GList * games_scores_get (GamesScores * self); -void _games_scores_category_foreach (GamesScores * self, - GamesScoresCategoryForeachFunc func, - gpointer userdata); -GamesScoreStyle games_scores_get_style (GamesScores * self); -const gchar *games_scores_get_category (GamesScores * self); -void games_scores_add_category (GamesScores *self, - const char *key, - const char *name); - -G_END_DECLS - -#endif /* GAMES_SCORES_H */ diff --git a/src/meson.build b/src/meson.build index 190d20a066f2fb22622d33117980a7ba4fed4d73..441eb865620d9ee230a95e90bd8f45ed3c26c436 100644 --- a/src/meson.build +++ b/src/meson.build @@ -10,18 +10,8 @@ five_or_more_c_sources = [ 'balls-preview.h', 'games-file-list.c', 'games-file-list.h', - 'games-gridframe.c', - 'games-gridframe.h', 'games-preimage.c', - 'games-preimage.h', - 'games-score.c', - 'games-score.h', - 'games-scores-backend.c', - 'games-scores-backend.h', - 'games-scores-dialog.c', - 'games-scores-dialog.h', - 'games-scores.c', - 'games-scores.h' + 'games-preimage.h' ] five_or_more_sources = [ @@ -29,11 +19,12 @@ five_or_more_sources = [ ] five_or_more_deps = [ - libmath, - librsvg, glib, gio, - gtk + gtk, + libgnome_games_support, + libmath, + librsvg ] five_or_more_includes = [