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



commit 037c43908c9a4a78a1a916ac87dc0e35d48b906a
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 user mappings in user config directory.

 src/gamepad/gamepad-mappings-manager.c |   89 ++++++++++++++++++++++++++++++--
 src/gamepad/gamepad-mappings-manager.h |    4 ++
 2 files changed, 88 insertions(+), 5 deletions(-)
---
diff --git a/src/gamepad/gamepad-mappings-manager.c b/src/gamepad/gamepad-mappings-manager.c
index af0a66b..027fa21 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 (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;
 }
@@ -209,6 +264,29 @@ 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);
+  }
+}
+
 /* Type */
 
 static void
@@ -219,6 +297,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 aa39b63..83a7cbc 100644
--- a/src/gamepad/gamepad-mappings-manager.h
+++ b/src/gamepad/gamepad-mappings-manager.h
@@ -14,6 +14,10 @@ gchar *games_gamepad_mappings_manager_get_default_mapping (GamesGamepadMappingsM
                                                            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);
 
 G_END_DECLS
 


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