[gnome-games/wip/abhinavsingh/gamepad-config: 14/23] gamepad: Allow MappingsManager to save user mappings



commit 0dbbfe57417e0ca545a8a0e40e4ed00398f27c54
Author: theawless <theawless gmail com>
Date:   Wed Jun 21 05:24:35 2017 +0530

    gamepad: Allow MappingsManager to save user mappings
    
    This commit adds functionality in GamepadMappingsManager to allow it to
    save and delete user mappings in user's config directory.

 src/gamepad/gamepad-mappings-manager.c |  108 ++++++++++++++++++++++++++++++--
 src/gamepad/gamepad-mappings-manager.h |    7 ++-
 2 files changed, 109 insertions(+), 6 deletions(-)
---
diff --git a/src/gamepad/gamepad-mappings-manager.c b/src/gamepad/gamepad-mappings-manager.c
index dd20bde..ae8d621 100644
--- a/src/gamepad/gamepad-mappings-manager.c
+++ b/src/gamepad/gamepad-mappings-manager.c
@@ -14,6 +14,7 @@ struct _GamesGamepadMappingsManager {
   GHashTable *names;
   GHashTable *default_mappings;
   GHashTable *user_mappings;
+  gchar *user_mappings_uri;
 };
 
 G_DEFINE_TYPE (GamesGamepadMappingsManager, games_gamepad_mappings_manager, G_TYPE_OBJECT);
@@ -123,13 +124,68 @@ add_from_file_uri (GamesGamepadMappingsManager  *self,
   g_object_unref (file);
 }
 
+static void
+save_user_mappings (GamesGamepadMappingsManager *self,
+                    GError                     **error)
+{
+  g_return_if_fail (self != NULL);
+
+  GHashTableIter iter;
+  gpointer key, value;
+  gchar *guid;
+  const gchar *name;
+  gchar *sdl_string;
+  gchar *mapping_string;
+
+  GFile *file;
+  GFileOutputStream *stream;
+  GDataOutputStream *data_stream;
+  GError *inner_error = NULL;
+
+  file = g_file_new_for_uri (self->user_mappings_uri);
+  stream = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &inner_error);
+  if (G_UNLIKELY (inner_error != NULL)) {
+    g_propagate_error (error, inner_error);
+    g_object_unref (file);
+
+    return;
+  }
+  data_stream = g_data_output_stream_new (G_FILE_OUTPUT_STREAM (stream));
+
+  g_hash_table_iter_init (&iter, self->user_mappings);
+  while (g_hash_table_iter_next (&iter, &key, &value))
+  {
+    guid = (gchar *) key;
+    name = g_hash_table_lookup (self->names, guid);
+    sdl_string = (gchar *) value;
+
+    mapping_string = g_strconcat (guid, ",", name, ",", sdl_string, "\n", NULL);
+
+    g_data_output_stream_put_string (data_stream, mapping_string, NULL, &inner_error);
+    if (G_UNLIKELY (inner_error != NULL)) {
+      g_propagate_error (error, inner_error);
+      g_free (mapping_string);
+      g_object_unref (file);
+      g_object_unref (stream);
+      g_object_unref (data_stream);
+
+      return;
+    }
+
+    g_free (mapping_string);
+  }
+
+  g_object_unref (file);
+  g_object_unref (stream);
+  g_object_unref (data_stream);
+}
+
 static GamesGamepadMappingsManager *
 games_gamepad_mappings_manager_new (void)
 {
   GamesGamepadMappingsManager *self = NULL;
   gchar *dir;
   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;
 
@@ -152,18 +208,17 @@ games_gamepad_mappings_manager_new (void)
   // FIXME The gamepad module shouldn't have a hidden dependency on the
   // application.
   dir = games_application_get_config_dir ();
-  user_mappings_uri = g_strconcat ("file://",
+  self->user_mappings_uri = g_strconcat ("file://",
                                    dir,
                                    "/gamecontrollerdb.txt",
                                    NULL);
-  add_from_file_uri (self, user_mappings_uri, self->user_mappings, &inner_error);
+  add_from_file_uri (self, self->user_mappings_uri, self->user_mappings, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
-    g_warning (error_message, user_mappings_uri, inner_error->message);
+    g_warning (error_message, self->user_mappings_uri, inner_error->message);
     g_clear_error (&inner_error);
   }
 
   g_free (dir);
-  g_free (user_mappings_uri);
 
   return self;
 }
@@ -223,6 +278,48 @@ games_gamepad_mappings_manager_get_mapping (GamesGamepadMappingsManager *self,
   return g_strdup (mapping);
 }
 
+void
+games_gamepad_mappings_manager_save_mapping (GamesGamepadMappingsManager *self,
+                                             const gchar                 *guid,
+                                             const gchar                 *name,
+                                             const gchar                 *mapping)
+{
+  GError *inner_error = NULL;
+
+  g_return_if_fail (self != NULL);
+  g_return_if_fail (guid != NULL);
+  g_return_if_fail (name != NULL);
+  g_return_if_fail (mapping != NULL);
+
+  g_hash_table_insert (self->user_mappings, g_strdup (guid), g_strdup (mapping));
+  g_hash_table_insert (self->names, g_strdup (guid), g_strdup (name));
+
+  save_user_mappings (self, &inner_error);
+  if (G_UNLIKELY (inner_error != NULL)) {
+    g_warning ("GamepadMappingsManager: Can’t save user mappings: %s", inner_error->message);
+    g_clear_error (&inner_error);
+  }
+}
+
+void
+games_gamepad_mappings_manager_delete_mapping (GamesGamepadMappingsManager *self,
+                                               const gchar                 *guid)
+{
+  GError *inner_error = NULL;
+
+  g_return_if_fail (self != NULL);
+  g_return_if_fail (guid != NULL);
+
+  g_hash_table_remove (self->user_mappings, guid);
+  g_hash_table_remove (self->names, guid);
+
+  save_user_mappings (self, &inner_error);
+  if (G_UNLIKELY (inner_error != NULL)) {
+    g_warning ("GamepadMappingsManager: Can’t save user mappings: %s", inner_error->message);
+    g_clear_error (&inner_error);
+  }
+}
+
 /* Type */
 
 static void
@@ -233,6 +330,7 @@ finalize (GObject *object)
   g_hash_table_unref (self->names);
   g_hash_table_unref (self->default_mappings);
   g_hash_table_unref (self->user_mappings);
+  g_free (self->user_mappings_uri);
 
   G_OBJECT_CLASS (games_gamepad_mappings_manager_parent_class)->finalize (object);
 }
diff --git a/src/gamepad/gamepad-mappings-manager.h b/src/gamepad/gamepad-mappings-manager.h
index 82d344e..e64196b 100644
--- a/src/gamepad/gamepad-mappings-manager.h
+++ b/src/gamepad/gamepad-mappings-manager.h
@@ -16,7 +16,12 @@ gchar *games_gamepad_mappings_manager_get_user_mapping (GamesGamepadMappingsMana
                                                         const gchar                 *guid);
 gchar *games_gamepad_mappings_manager_get_mapping (GamesGamepadMappingsManager *self,
                                                    const gchar                 *guid);
-
+void games_gamepad_mappings_manager_save_mapping (GamesGamepadMappingsManager *self,
+                                                  const gchar                 *guid,
+                                                  const gchar                 *name,
+                                                  const gchar                 *mapping);
+void games_gamepad_mappings_manager_delete_mapping (GamesGamepadMappingsManager *self,
+                                                    const gchar                 *guid);
 G_END_DECLS
 
 #endif /* GAMES_GAMEPAD_MAPPINGS_MANAGER_H */


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