[tepl] Utils: add create_parent_directories()



commit 8b1424d9354898aa01d9a2e150655f53ce99459d
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Mar 31 11:07:50 2020 +0200

    Utils: add create_parent_directories()
    
    Copied from gnome-latex, added the GCancellable param.

 docs/reference/tepl-sections.txt |  1 +
 tepl/tepl-utils.c                | 51 +++++++++++++++++++++++++++++++++++++++-
 tepl/tepl-utils.h                |  6 ++++-
 3 files changed, 56 insertions(+), 2 deletions(-)
---
diff --git a/docs/reference/tepl-sections.txt b/docs/reference/tepl-sections.txt
index 6f686cc..a705151 100644
--- a/docs/reference/tepl-sections.txt
+++ b/docs/reference/tepl-sections.txt
@@ -429,6 +429,7 @@ tepl_utils_get_file_extension
 tepl_utils_get_file_shortname
 tepl_utils_replace_home_dir_with_tilde
 tepl_utils_decode_uri
+tepl_utils_create_parent_directories
 tepl_utils_show_warning_dialog
 </SECTION>
 
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index 8dc386c..ea78318 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -6,7 +6,7 @@
  * Copyright 2000, 2002 - Chema Celorio, Paolo Maggi
  * Copyright 2003-2005 - Paolo Maggi
  *
- * Copyright 2016, 2017 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016-2020 - Sébastien Wilmet <swilmet gnome org>
  *
  * Tepl is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -509,6 +509,55 @@ _tepl_utils_get_fallback_basename_for_display (GFile *location)
        return basename;
 }
 
+/**
+ * tepl_utils_create_parent_directories:
+ * @file: a file
+ * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
+ * @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.
+ * Since: 4.6
+ */
+gboolean
+tepl_utils_create_parent_directories (GFile         *file,
+                                     GCancellable  *cancellable,
+                                     GError       **error)
+{
+       GFile *parent;
+       GError *my_error = NULL;
+
+       g_return_val_if_fail (G_IS_FILE (file), FALSE);
+       g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
+       g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+       parent = g_file_get_parent (file);
+
+       if (parent == NULL)
+       {
+               return TRUE;
+       }
+
+       g_file_make_directory_with_parents (parent, cancellable, &my_error);
+       g_clear_object (&parent);
+
+       if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+       {
+               g_error_free (my_error);
+               return TRUE;
+       }
+       if (my_error != NULL)
+       {
+               g_propagate_error (error, my_error);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
 GtkWidget *
 _tepl_utils_create_close_button (void)
 {
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index a586936..4e7a559 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -1,7 +1,7 @@
 /*
  * This file is part of Tepl, a text editor library.
  *
- * Copyright 2016, 2017 - Sébastien Wilmet <swilmet gnome org>
+ * Copyright 2016-2020 - Sébastien Wilmet <swilmet gnome org>
  *
  * Tepl is free software; you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the
@@ -58,6 +58,10 @@ gboolean     tepl_utils_decode_uri                           (const gchar  *uri,
 G_GNUC_INTERNAL
 gchar *                _tepl_utils_get_fallback_basename_for_display   (GFile *location);
 
+gboolean       tepl_utils_create_parent_directories            (GFile         *file,
+                                                                GCancellable  *cancellable,
+                                                                GError       **error);
+
 /* Widget utilities */
 
 G_GNUC_INTERNAL


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