[latexila/wip/templates-revamp: 1/2] Rewrite create_parent_directories() util function in C



commit e99c0949f8ab4b6079eb70db7de64c3f0ce16652
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed May 27 14:35:03 2015 +0200

    Rewrite create_parent_directories() util function in C
    
    It'll be used for the templates.

 docs/reference/latexila-sections.txt |    1 +
 src/liblatexila/latexila-utils.c     |   45 ++++++++++++++++++++++++++++++++++
 src/liblatexila/latexila-utils.h     |    3 ++
 src/utils.vala                       |   26 +------------------
 4 files changed, 51 insertions(+), 24 deletions(-)
---
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index 8e8ad02..0d4a234 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -268,4 +268,5 @@ latexila_utils_file_query_exists_async
 latexila_utils_file_query_exists_finish
 latexila_utils_show_uri
 latexila_utils_get_dialog_component
+latexila_utils_create_parent_directories
 </SECTION>
diff --git a/src/liblatexila/latexila-utils.c b/src/liblatexila/latexila-utils.c
index 9dc4cc7..5ad85a8 100644
--- a/src/liblatexila/latexila-utils.c
+++ b/src/liblatexila/latexila-utils.c
@@ -377,3 +377,48 @@ latexila_utils_get_dialog_component (const gchar *title,
   g_free (markup);
   return GTK_WIDGET (grid);
 }
+
+/**
+ * latexila_utils_create_parent_directories:
+ * @file: a file
+ * @error: (out) (optional): a location to a %NULL #GError, or %NULL.
+ *
+ * Synchronously creates parent directories of @file, so that @file can be
+ * saved.
+ *
+ * Returns: whether the directories are correctly created. %FALSE is returned on
+ * error.
+ */
+gboolean
+latexila_utils_create_parent_directories (GFile   *file,
+                                          GError **error)
+{
+  GFile *parent;
+  GError *my_error = NULL;
+
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+
+  parent = g_file_get_parent (file);
+
+  if (parent == NULL)
+    return TRUE;
+
+  g_file_make_directory_with_parents (parent, NULL, &my_error);
+  g_object_unref (parent);
+
+  if (my_error != NULL)
+    {
+      if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+        {
+          g_error_free (my_error);
+          return TRUE;
+        }
+      else
+        {
+          g_propagate_error (error, my_error);
+          return FALSE;
+        }
+    }
+
+  return TRUE;
+}
diff --git a/src/liblatexila/latexila-utils.h b/src/liblatexila/latexila-utils.h
index edac6df..729f393 100644
--- a/src/liblatexila/latexila-utils.h
+++ b/src/liblatexila/latexila-utils.h
@@ -55,6 +55,9 @@ void            latexila_utils_show_uri                         (GdkScreen    *s
 GtkWidget *     latexila_utils_get_dialog_component             (const gchar *title,
                                                                  GtkWidget   *widget);
 
+gboolean        latexila_utils_create_parent_directories        (GFile   *file,
+                                                                 GError **error);
+
 G_END_DECLS
 
 #endif /* __LATEXILA_UTILS_H__ */
diff --git a/src/utils.vala b/src/utils.vala
index d26ad9a..67fb7a5 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -121,34 +121,12 @@ namespace Utils
         }
     }
 
-    public bool create_parent_directories (File file)
-    {
-        File parent = file.get_parent ();
-
-        if (parent == null || parent.query_exists ())
-            return true;
-
-        try
-        {
-            parent.make_directory_with_parents ();
-        }
-        catch (Error e)
-        {
-            warning ("Failed to create directory parents for the file '%s': %s",
-                file.get_parse_name (), e.message);
-            return false;
-        }
-
-        return true;
-    }
-
     public bool save_file (File file, string contents, bool make_backup = false)
     {
-        if (! create_parent_directories (file))
-            return false;
-
         try
         {
+            Latexila.utils_create_parent_directories (file);
+
             file.replace_contents (contents.data, null, make_backup,
                 FileCreateFlags.NONE, null);
         }


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