[cheese] cheese-effects: implement loading cheese effects in libcheese



commit df620b39cf78f38eaaa897ec330028d1198662b5
Author: Raluca Elena Podiuc <ralucaelena1985 gmail com>
Date:   Sun Aug 14 12:40:26 2011 +0300

    cheese-effects: implement loading cheese effects in libcheese
    
    We need to load cheese effects from Empathy. To not have the same code
    written once in Vala (Cheese), once in C (Empathy), and again in each
    app that wants to use Cheese effects, I reimplemented this
    functionality and moved it to libcheese.

 libcheese/cheese-effect.c       |  132 +++++++++++++++++++++++++++++++++++++++
 libcheese/cheese-effect.h       |    3 +
 src/cheese-effects-manager.vala |   76 +---------------------
 src/vapi/cheese-common.vapi     |    3 +
 4 files changed, 141 insertions(+), 73 deletions(-)
---
diff --git a/libcheese/cheese-effect.c b/libcheese/cheese-effect.c
index 7045cf1..24e9fbb 100644
--- a/libcheese/cheese-effect.c
+++ b/libcheese/cheese-effect.c
@@ -156,3 +156,135 @@ cheese_effect_new (void)
 {
   return g_object_new (CHEESE_TYPE_EFFECT, NULL);
 }
+
+/**
+ * cheese_effect_load_from_file: load effect from file
+ * @fname: (type filename): name of the file containing effect specification
+ *
+ * Returns: (transfer full): a #CheeseEffect
+ */
+CheeseEffect*
+cheese_effect_load_from_file (const gchar *fname)
+{
+  const gchar  *GROUP_NAME = "Effect";
+  gchar        *name, *desc;
+  GError       *err = NULL;
+  CheeseEffect *eff = NULL;
+  GKeyFile     *kf = g_key_file_new ();
+
+  g_key_file_load_from_file (kf, fname, G_KEY_FILE_NONE, &err);
+  if (err != NULL)
+    goto err_kf_load;
+
+  name = g_key_file_get_locale_string (kf, GROUP_NAME, "Name", NULL, &err);
+  if (err != NULL)
+    goto err_name;
+
+  desc = g_key_file_get_string (kf, GROUP_NAME, "PipelineDescription", &err);
+  if (err != NULL)
+    goto err_desc;
+
+  g_key_file_free (kf);
+
+  eff = cheese_effect_new ();
+  g_object_set (eff, "name", name, NULL);
+  g_object_set (eff, "pipeline-desc", desc, NULL);
+  g_free (name);
+  g_free (desc);
+
+  return eff;
+
+err_desc:
+    g_free (name);
+err_name:
+err_kf_load:
+    g_key_file_free (kf);
+    g_warning ("CheeseEffect: couldn't load file %s: %s", fname, err->message);
+    g_clear_error (&err);
+    return NULL;
+}
+
+/* Returns list of effects loaded from files from @directory.
+   Only parses files ending with the '.effects' extension */
+static GList*
+cheese_effect_load_effects_from_directory (const gchar* directory)
+{
+  gboolean retval;
+  GError   *err = NULL;
+  GDir     *dir = NULL;
+  GList    *list = NULL;
+
+  retval = g_file_test (directory, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
+  if (!retval)
+    return NULL;
+
+  dir = g_dir_open (directory, (guint) 0, &err);
+  if (err != NULL)
+  {
+    g_warning ("CheeseEffect: g_dir_open: %s\n", err->message);
+    g_clear_error (&err);
+    return NULL;
+  }
+
+  while (TRUE) {
+    CheeseEffect *effect;
+    gchar        *abs_path;
+    const gchar  *fname = g_dir_read_name (dir);
+
+    /* no more files */
+    if (fname == NULL)
+      break;
+
+    if (!g_str_has_suffix (fname, ".effect"))
+      continue;
+
+    abs_path = g_build_filename (directory, fname, NULL);
+    effect = cheese_effect_load_from_file (abs_path);
+    if (effect != NULL)
+      list = g_list_append (list, effect);
+    g_free (abs_path);
+  }
+  g_dir_close (dir);
+
+  return list;
+}
+
+static GList*
+cheese_effect_load_effects_from_subdirectory (const gchar* directory,
+                                              const gchar* subdirectory)
+{
+  GList *list;
+  gchar *path = g_build_filename (directory, subdirectory, NULL);
+  list = cheese_effect_load_effects_from_directory (path);
+  g_free (path);
+  return list;
+}
+
+/**
+ * cheese_effect_load_effects: load effects from known standard directories.
+ *
+ * Returns: (element-type Cheese.Effect) (transfer full): List of #CheeseEffect
+ */
+GList*
+cheese_effect_load_effects ()
+{
+  const gchar *const*data_dirs, *dir;
+  GList *ret = NULL, *l;
+
+  dir = g_get_user_data_dir (); /* value returned owned by GLib */
+  l = cheese_effect_load_effects_from_subdirectory (dir, "gnome-video-effects");
+  ret = g_list_concat (ret, l);
+
+  data_dirs = g_get_system_data_dirs (); /* value returned owned by GLib */
+  if (!data_dirs)
+    return ret;
+
+  for (; *data_dirs; data_dirs++)
+  {
+    dir = *data_dirs;
+    l = cheese_effect_load_effects_from_subdirectory (dir, "gnome-video-effects");
+    ret = g_list_concat (ret, l);
+  }
+
+  return ret;
+}
diff --git a/libcheese/cheese-effect.h b/libcheese/cheese-effect.h
index 0bd72c7..225d2fc 100644
--- a/libcheese/cheese-effect.h
+++ b/libcheese/cheese-effect.h
@@ -58,6 +58,9 @@ gboolean      cheese_effect_is_preview_connected (CheeseEffect *self);
 void          cheese_effect_enable_preview (CheeseEffect *self);
 void          cheese_effect_disable_preview (CheeseEffect *self);
 
+CheeseEffect *cheese_effect_load_from_file (const gchar *fname);
+GList        *cheese_effect_load_effects ();
+
 G_END_DECLS
 
 #endif /* _CHEESE_EFFECT_H_ */
diff --git a/src/cheese-effects-manager.vala b/src/cheese-effects-manager.vala
index 8bf0f53..42fc51c 100644
--- a/src/cheese-effects-manager.vala
+++ b/src/cheese-effects-manager.vala
@@ -26,71 +26,8 @@ const string GROUP_NAME = "Effect";
 
 internal class Cheese.EffectsManager : GLib.Object
 {
-  public static Cheese.Effect? parse_effect_file (string filename)
-  {
-    KeyFile kf     = new KeyFile ();
-    Effect  eff    = new Effect ();
-    var     locale = Intl.setlocale (LocaleCategory.ALL, "");
-
-    try
-    {
-      kf.load_from_file (filename, KeyFileFlags.NONE);
-      eff.name          = kf.get_locale_string (GROUP_NAME, "Name", locale);
-      eff.pipeline_desc = kf.get_string (GROUP_NAME, "PipelineDescription");
-    }
-    catch (KeyFileError err)
-    {
-      warning ("Error: %s\n", err.message);
-      return null;
-    }
-    catch (FileError err)
-    {
-      warning ("Error: %s\n", err.message);
-      return null;
-    }
-
-    return eff;
-  }
-
   public ArrayList<Effect> effects;
 
-  private ArrayList<Effect> ? load_effects_from_directory (string directory)
-  {
-    ArrayList<Effect> list = new ArrayList<Effect>();
-
-    if (FileUtils.test (directory, FileTest.EXISTS | FileTest.IS_DIR))
-    {
-      Dir    dir;
-      string cur_file;
-      try
-      {
-        dir = Dir.open (directory);
-      }
-      catch (FileError err)
-      {
-        warning ("Error: %s\n", err.message);
-        return null;
-      }
-
-
-      cur_file = dir.read_name ();
-      while (cur_file != null)
-      {
-        if (cur_file.has_suffix (".effect"))
-        {
-          Effect effect = EffectsManager.parse_effect_file (GLib.Path.build_filename (directory, cur_file));
-          if (!effects.contains (effect))
-          {
-            message ("Found %s (%s)", effect.name, effect.pipeline_desc);
-            list.add (effect);
-          }
-        }
-        cur_file = dir.read_name ();
-      }
-    }
-    return list;
-  }
-
   public EffectsManager ()
   {
     effects = new ArrayList<Effect>((EqualFunc) cmp_value);
@@ -98,16 +35,9 @@ internal class Cheese.EffectsManager : GLib.Object
 
   public void load_effects ()
   {
-    string system_effects;
-
-    foreach (string dir in Environment.get_system_data_dirs ())
-    {
-      system_effects = GLib.Path.build_filename (dir, "gnome-video-effects");
-      effects.add_all (load_effects_from_directory (system_effects));
-    }
-
-    string user_effects = GLib.Path.build_filename (Environment.get_user_data_dir (), "gnome-video-effects");
-    effects.add_all (load_effects_from_directory (user_effects));
+    GLib.List<Cheese.Effect> effect_list = Cheese.Effect.load_effects ();
+    for (int i = 0; i < effect_list.length (); i++)
+      effects.add (effect_list<Cheese.Effect>.nth (i).data);
 
     effects.sort ((CompareFunc) sort_value);
 
diff --git a/src/vapi/cheese-common.vapi b/src/vapi/cheese-common.vapi
index d5d8bf2..be6c51b 100644
--- a/src/vapi/cheese-common.vapi
+++ b/src/vapi/cheese-common.vapi
@@ -18,6 +18,9 @@ namespace Cheese
     public void enable_preview();
     public void disable_preview();
     public bool is_preview_connected();
+
+    public static Cheese.Effect load_from_file (string fname);
+    public static GLib.List<Cheese.Effect> load_effects ();
   }
 
   [CCode (cheader_filename = "cheese-camera.h")]



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