[gnome-nibbles] Revert "Remove more of the broken scores system"



commit 3609a54d828cb4d942c278c25bcb64c594795f36
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Tue Nov 5 20:47:33 2013 -0600

    Revert "Remove more of the broken scores system"
    
    This reverts commit 4bf47d434b4491b6641abddeccacede791e35813.

 src/Makefile.am            |    2 +
 src/games-scores-backend.c |  133 ++++++++++++++++++++++++++++++++++++++++++++
 src/games-scores-backend.h |   63 +++++++++++++++++++++
 src/games-scores.c         |   29 ++++++++++
 src/games-scores.h         |    1 +
 src/main.c                 |    2 +
 6 files changed, 230 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 28982fb..aa2b861 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,8 @@ gnome_nibbles_SOURCES = \
        games-scores.h                  \
        games-scores-dialog.c   \
        games-scores-dialog.h   \
+       games-scores-backend.c  \
+       games-scores-backend.h  \
        gnibbles.h \
        gnibbles.c \
        properties.h \
diff --git a/src/games-scores-backend.c b/src/games-scores-backend.c
new file mode 100644
index 0000000..6d138a9
--- /dev/null
+++ b/src/games-scores-backend.c
@@ -0,0 +1,133 @@
+/* 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, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <config.h>
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <string.h>
+#include <stdlib.h>
+
+#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)
+{
+
+}
+
+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;
+  gchar *fullname;
+
+  backend = GAMES_SCORES_BACKEND (g_object_new (GAMES_TYPE_SCORES_BACKEND,
+                                                NULL));
+
+  if (name[0] == '\0')                /* Name is "" */
+    fullname = g_strjoin (".", base_name, "scores", NULL);
+  else
+    fullname = g_strjoin (".", base_name, name, "scores", NULL);
+
+  backend->priv->timestamp = 0;
+  backend->priv->style = style;
+  backend->priv->scores_list = NULL;
+  backend->priv->filename = g_build_filename (SCORESDIR, fullname, NULL);
+  g_free (fullname);
+
+  backend->priv->fd = -1;
+
+  return backend;
+}
+
+
+
+/**
+ * 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)
+{
+  return NULL;
+}
+
+gboolean
+games_scores_backend_set_scores (GamesScoresBackend * self, GList * list)
+{
+  return FALSE;
+}
+
+void
+games_scores_backend_discard_scores (GamesScoresBackend * self)
+{
+
+}
diff --git a/src/games-scores-backend.h b/src/games-scores-backend.h
new file mode 100644
index 0000000..c094431
--- /dev/null
+++ b/src/games-scores-backend.h
@@ -0,0 +1,63 @@
+ /* 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, write to the
+  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+  * Boston, MA 02111-1307, USA.
+  */
+
+#ifndef GAMES_SCORES_BACKEND_H
+#define GAMES_SCORES_BACKEND_H
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <time.h>
+
+#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.c b/src/games-scores.c
index be003ac..8b097e4 100644
--- a/src/games-scores.c
+++ b/src/games-scores.c
@@ -28,12 +28,14 @@
 
 #include <glib/gi18n.h>
 
+#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 {
@@ -49,12 +51,19 @@ struct GamesScoresPrivate {
   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);
 }
 
@@ -80,6 +89,11 @@ games_scores_get_current (GamesScores * self)
       return NULL;
   }
 
+  if (cat->backend == NULL) {
+    cat->backend = games_scores_backend_new (priv->style, priv->basename,
+                                             cat->category.key);
+  }
+
   return cat;
 }
 
@@ -179,6 +193,7 @@ games_scores_add_category (GamesScores *self,
   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);
@@ -237,6 +252,8 @@ games_scores_add_score (GamesScores * self, GamesScore *score)
 
   cat = games_scores_get_current (self);
 
+  scores_list = games_scores_backend_get_scores (cat->backend);
+
   s = scores_list;
   place = 0;
   n = 0;
@@ -274,6 +291,9 @@ games_scores_add_score (GamesScores * self, GamesScore *score)
     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);
@@ -329,6 +349,8 @@ games_scores_update_score_name (GamesScores * self, gchar * new_name, gchar * ol
 
   cat = games_scores_get_current (self);
 
+  scores_list = games_scores_backend_get_scores (cat->backend);
+
   s = g_list_last (scores_list);
   n = g_list_length (scores_list);
 
@@ -349,6 +371,8 @@ games_scores_update_score_name (GamesScores * self, gchar * new_name, gchar * ol
     n--;
   }
 
+  games_scores_backend_set_scores (cat->backend, scores_list);
+
   g_free (old_name);
 }
 
@@ -390,6 +414,11 @@ games_scores_get (GamesScores * self)
 
   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;
 }
 
diff --git a/src/games-scores.h b/src/games-scores.h
index f24a3f0..02e31c6 100644
--- a/src/games-scores.h
+++ b/src/games-scores.h
@@ -29,6 +29,7 @@
 G_BEGIN_DECLS
 
 #include "games-score.h"
+#include "games-scores-backend.h"
 
 /* How many scores get counted as significant. */
 #define GAMES_SCORES_SIGNIFICANT 10
diff --git a/src/main.c b/src/main.c
index 71a6be2..b3419d3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -743,6 +743,8 @@ main (int argc, char **argv)
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
 
+  games_scores_startup ();
+
   /*
    * Required because the binary doesn't match the desktop file.
    * Has to be before the call to g_option_context_parse.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]