[gnome-games/wip/abhinavsingh/gamepad-config: 20/25] gamepad: Use file uris in GamesMappingsManager



commit 93a98a686e7f1725208f7e2c0f782ba4b3c5007e
Author: theawless <theawless gmail com>
Date:   Wed Jun 21 02:08:21 2017 +0530

    gamepad: Use file uris in GamesMappingsManager
    
    This will simplify the code and increase reusability.

 src/gamepad/gamepad-mappings-manager.c |   77 +++++++------------------------
 1 files changed, 18 insertions(+), 59 deletions(-)
---
diff --git a/src/gamepad/gamepad-mappings-manager.c b/src/gamepad/gamepad-mappings-manager.c
index a43e95e..32bb2b9 100644
--- a/src/gamepad/gamepad-mappings-manager.c
+++ b/src/gamepad/gamepad-mappings-manager.c
@@ -19,8 +19,6 @@ G_DEFINE_TYPE (GamesGamepadMappingsManager, games_gamepad_mappings_manager, G_TY
 
 static GamesGamepadMappingsManager *instance = NULL;
 
-#define MAPPINGS_FILE_NAME "gamecontrollerdb.txt"
-
 // FIXME The gamepad module shouldn't have a hidden dependency on the
 // application.
 gchar *games_application_get_config_dir (void);
@@ -72,7 +70,6 @@ add_from_input_stream (GamesGamepadMappingsManager  *self,
                                                     NULL, NULL,
                                                     &inner_error);
     if (G_UNLIKELY (inner_error != NULL)) {
-      g_assert (mapping_string == NULL);
       g_propagate_error (error, inner_error);
       g_object_unref (data_stream);
 
@@ -83,54 +80,24 @@ add_from_input_stream (GamesGamepadMappingsManager  *self,
       break;
 
     add_mapping (self, mapping_string);
+    g_free (mapping_string);
   }
   g_object_unref (data_stream);
 }
 
 static void
-add_from_resource (GamesGamepadMappingsManager  *self,
-                   const gchar                  *path,
+add_from_file_uri (GamesGamepadMappingsManager  *self,
+                   const gchar                  *file_uri,
                    GError                      **error)
 {
-  GInputStream *stream;
-  GError *inner_error = NULL;
-
-  g_return_if_fail (self != NULL);
-  g_return_if_fail (path != NULL);
-
-  stream = g_resources_open_stream (path,
-                                    G_RESOURCE_LOOKUP_FLAGS_NONE,
-                                    &inner_error);
-  if (G_UNLIKELY (inner_error != NULL)) {
-    g_propagate_error (error, inner_error);
-
-    return;
-  }
-
-  add_from_input_stream (self, G_INPUT_STREAM (stream), &inner_error);
-  if (G_UNLIKELY (inner_error != NULL)) {
-    g_propagate_error (error, inner_error);
-    g_object_unref (stream);
-
-    return;
-  }
-
-  g_object_unref (stream);
-}
-
-static void
-add_from_file (GamesGamepadMappingsManager  *self,
-               const gchar                  *file_name,
-               GError                      **error)
-{
   GFile *file;
   GFileInputStream *stream;
   GError *inner_error = NULL;
 
   g_return_if_fail (self != NULL);
-  g_return_if_fail (file_name != NULL);
+  g_return_if_fail (file_uri != NULL);
 
-  file = g_file_new_for_path (file_name);
+  file = g_file_new_for_uri (file_uri);
   stream = g_file_read (file, NULL, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
     g_propagate_error (error, inner_error);
@@ -157,7 +124,9 @@ games_gamepad_mappings_manager_new (void)
 {
   GamesGamepadMappingsManager *self = NULL;
   gchar *dir;
-  gchar *path;
+  const gchar *default_mappings_uri = "resource:///org/gnome/Games/gamepads/gamecontrollerdb.txt";
+  gchar *user_mappings_uri;
+  const gchar *error_message = "GamepadMappingsManager: Can’t add mappings from %s: %s";
   GError *inner_error = NULL;
 
   self = (GamesGamepadMappingsManager*) g_object_new (GAMES_TYPE_GAMEPAD_MAPPINGS_MANAGER, NULL);
@@ -168,37 +137,27 @@ games_gamepad_mappings_manager_new (void)
   if (self->mappings == NULL)
     self->mappings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
-  add_from_resource (self,
-                     "/org/gnome/Games/gamepads/gamecontrollerdb.txt",
-                     &inner_error);
+  add_from_file_uri (self, default_mappings_uri, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
-    g_warning ("GamepadMappingsManager: Can’t find resource gamecontrollerdb.txt: %s",
-               inner_error->message);
+    g_warning (error_message, default_mappings_uri, inner_error->message);
     g_clear_error (&inner_error);
   }
 
   // FIXME The gamepad module shouldn't have a hidden dependency on the
   // application.
   dir = games_application_get_config_dir ();
-  path = g_build_filename (dir, MAPPINGS_FILE_NAME, NULL);
-
-  g_free (dir);
-
-  if (!g_file_test (path, G_FILE_TEST_EXISTS)) {
-    g_free (path);
-
-    return self;
-  }
-
-  add_from_file (self, path, &inner_error);
+  user_mappings_uri = g_strconcat ("file://",
+                                   dir,
+                                   "/gamecontrollerdb.txt",
+                                   NULL);
+  add_from_file_uri (self, user_mappings_uri, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
-    g_warning ("GamepadMappingsManager: Can’t add from user’s config dir’s %s: %s",
-               MAPPINGS_FILE_NAME,
-               inner_error->message);
+    g_warning (error_message, user_mappings_uri, inner_error->message);
     g_clear_error (&inner_error);
   }
 
-  g_free (path);
+  g_free (dir);
+  g_free (user_mappings_uri);
 
   return self;
 }


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