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



commit 2ec895072c409c645291571f69b3cc64a14572f4
Author: theawless <theawless gmail com>
Date:   Fri Jul 28 02:14:19 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 |  107 ++++++++++++++++++++++++++++++--
 src/gamepad/gamepad-mappings-manager.h |    6 ++
 2 files changed, 108 insertions(+), 5 deletions(-)
---
diff --git a/src/gamepad/gamepad-mappings-manager.c b/src/gamepad/gamepad-mappings-manager.c
index 2930993..c02fe17 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);
@@ -126,13 +127,67 @@ add_from_file_uri (GamesGamepadMappingsManager  *self,
   g_object_unref (file);
 }
 
+static void
+save_user_mappings (GamesGamepadMappingsManager  *self,
+                    GError                      **error)
+{
+  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;
+
+  g_return_if_fail (self != 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_strdup_printf ("%s,%s,%s\n", guid, name, sdl_string);
+
+    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;
   gchar *path;
-  gchar *user_mappings_uri;
   GError *inner_error = NULL;
 
   self = (GamesGamepadMappingsManager*) g_object_new (GAMES_TYPE_GAMEPAD_MAPPINGS_MANAGER, NULL);
@@ -157,7 +212,7 @@ games_gamepad_mappings_manager_new (void)
   // application.
   dir = games_application_get_config_dir ();
   path = g_build_filename (dir, MAPPINGS_FILE_NAME, NULL);
-  user_mappings_uri = g_filename_to_uri (path, NULL, &inner_error);
+  self->user_mappings_uri = g_filename_to_uri (path, NULL, &inner_error);
   if (G_UNLIKELY (inner_error != NULL)) {
     g_debug ("GamepadMappingsManager: Can't build path for user config: %s",
              inner_error->message);
@@ -168,17 +223,16 @@ games_gamepad_mappings_manager_new (void)
     return self;
   }
 
-  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_debug ("GamepadMappingsManager: Can’t add mappings from %s: %s",
-             user_mappings_uri,
+             self->user_mappings_uri,
              inner_error->message);
     g_clear_error (&inner_error);
   }
 
   g_free (dir);
   g_free (path);
-  g_free (user_mappings_uri);
 
   return self;
 }
@@ -238,6 +292,48 @@ games_gamepad_mappings_manager_get_mapping (GamesGamepadMappingsManager *self,
   return 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
@@ -248,6 +344,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..87056fd 100644
--- a/src/gamepad/gamepad-mappings-manager.h
+++ b/src/gamepad/gamepad-mappings-manager.h
@@ -16,6 +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
 


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