[glom] utils: Move some functions into file_utils



commit 1318cb5770953fa4f1f904e5ca09b5fcf489c030
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Mar 16 23:22:46 2016 +0100

    utils: Move some functions into file_utils

 CMakeLists.txt                                     |    2 +
 glom/appwindow.cc                                  |    1 +
 glom/glom_create_from_example.cc                   |    1 +
 .../connectionpool_backends/postgres_self.cc       |    1 +
 glom/libglom/connectionpool_backends/sqlite.cc     |    1 +
 .../data_structure/layout/layoutitem_image.cc      |    1 +
 glom/libglom/document/document.cc                  |    1 +
 glom/libglom/file_utils.cc                         |  326 ++++++++++++++++++++
 glom/libglom/file_utils.h                          |   84 +++++
 glom/libglom/filelist.am                           |    2 +
 glom/libglom/report_builder.cc                     |    1 +
 glom/libglom/utils.cc                              |  285 -----------------
 glom/libglom/utils.h                               |   42 ---
 glom/main.cc                                       |    3 +-
 glom/utility_widgets/imageglom.cc                  |    1 +
 tests/import/utils.cc                              |    2 +-
 .../python/test_python_execute_func_with_record.cc |    1 +
 tests/test_document_autosave.cc                    |    2 +-
 tests/test_document_load_and_save.cc               |    2 +-
 tests/test_selfhosting_new_then_backup_restore.cc  |    2 +-
 tests/test_selfhosting_utils.cc                    |    1 +
 tests/translations_po/test_document_export_po.cc   |    2 +-
 22 files changed, 430 insertions(+), 334 deletions(-)
---
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 18b2d33..5f552f7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -252,6 +252,8 @@ set(SOURCE_FILES
         glom/libglom/standard_table_prefs_fields.h
         glom/libglom/translations_po.cc
         glom/libglom/translations_po.h
+        glom/libglom/file_utils.cc
+        glom/libglom/file_utils.h
         glom/libglom/string_utils.cc
         glom/libglom/string_utils.h
         glom/libglom/utils.cc
diff --git a/glom/appwindow.cc b/glom/appwindow.cc
index f631da4..c61d2b9 100644
--- a/glom/appwindow.cc
+++ b/glom/appwindow.cc
@@ -24,6 +24,7 @@
 #include <glom/dialog_existing_or_new.h>
 #include <glom/bakery/dialog_offersave.h>
 #include <libglom/string_utils.h>
+#include <libglom/file_utils.h>
 
 #ifndef GLOM_ENABLE_CLIENT_ONLY
 #include <glom/mode_design/translation/dialog_change_language.h>
diff --git a/glom/glom_create_from_example.cc b/glom/glom_create_from_example.cc
index f4878dd..974b948 100644
--- a/glom/glom_create_from_example.cc
+++ b/glom/glom_create_from_example.cc
@@ -32,6 +32,7 @@
 #include <libglom/init.h>
 #include <libglom/privs.h>
 #include <libglom/db_utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/utils.h>
 #include <giomm/file.h>
 #include <glibmm/optioncontext.h>
diff --git a/glom/libglom/connectionpool_backends/postgres_self.cc 
b/glom/libglom/connectionpool_backends/postgres_self.cc
index f300174..8d8753b 100644
--- a/glom/libglom/connectionpool_backends/postgres_self.cc
+++ b/glom/libglom/connectionpool_backends/postgres_self.cc
@@ -21,6 +21,7 @@
 #include <libglom/connectionpool_backends/postgres_self.h>
 #include <libglom/utils.h>
 #include <libglom/db_utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/spawn_with_feedback.h>
 #include <giomm/file.h>
 #include <glib/gstdio.h> // For g_remove
diff --git a/glom/libglom/connectionpool_backends/sqlite.cc b/glom/libglom/connectionpool_backends/sqlite.cc
index 579f4e0..a9f07dd 100644
--- a/glom/libglom/connectionpool_backends/sqlite.cc
+++ b/glom/libglom/connectionpool_backends/sqlite.cc
@@ -22,6 +22,7 @@
 #include <libglom/libglom_config.h>
 #include <libglom/connectionpool_backends/sqlite.h>
 #include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/algorithms_utils.h>
 #include <libglom/db_utils.h>
 #include <giomm/file.h>
diff --git a/glom/libglom/data_structure/layout/layoutitem_image.cc 
b/glom/libglom/data_structure/layout/layoutitem_image.cc
index 78a7dcb..e1e58be 100644
--- a/glom/libglom/data_structure/layout/layoutitem_image.cc
+++ b/glom/libglom/data_structure/layout/layoutitem_image.cc
@@ -20,6 +20,7 @@
 
 #include <libglom/data_structure/layout/layoutitem_image.h>
 #include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/data_structure/glomconversions.h>
 #include <glibmm/i18n.h>
 
diff --git a/glom/libglom/document/document.cc b/glom/libglom/document/document.cc
index 4fa7dee..5cfe53f 100644
--- a/glom/libglom/document/document.cc
+++ b/glom/libglom/document/document.cc
@@ -22,6 +22,7 @@
 #include <libglom/xml_utils.h>
 #include <libglom/algorithms_utils.h>
 #include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/string_utils.h>
 //#include <libglom/data_structure/glomconversions.h>
 #include <libglom/data_structure/layout/report_parts/layoutitem_summary.h>
diff --git a/glom/libglom/file_utils.cc b/glom/libglom/file_utils.cc
new file mode 100644
index 0000000..c56b202
--- /dev/null
+++ b/glom/libglom/file_utils.cc
@@ -0,0 +1,326 @@
+/* Glom
+ *
+ * Copyright (C) 2001-2016 Murray Cumming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ */
+
+#include <libglom/file_utils.h>
+#include <libglom/string_utils.h>
+#include <glibmm/miscutils.h>
+#include <glibmm/fileutils.h>
+#include <glibmm/convert.h>
+#include <giomm/resource.h>
+#include <libglom/utils.h>
+#include <fstream>
+#include <iostream>
+
+namespace Glom
+{
+
+namespace Utils
+{
+
+bool file_exists(const Glib::ustring& uri)
+{
+  if(uri.empty())
+     return false;
+
+  //Check whether file exists already:
+  // Try to examine the input file.
+  auto file = Gio::File::create_for_uri(uri);
+  return file_exists(file);
+}
+
+bool file_exists(const Glib::RefPtr<Gio::File>& file)
+{
+  try
+  {
+    return file->query_exists();
+  }
+  catch(const Gio::Error& /* ex */)
+  {
+    return false; //Something went wrong. It does not exist.
+  }
+}
+
+bool delete_directory(const Glib::RefPtr<Gio::File>& directory)
+{
+  try
+  {
+    if(!(directory->query_exists()))
+      return true;
+
+    //(Recursively) Delete any child files and directories,
+    //so we can delete this directory.
+    auto enumerator = directory->enumerate_children();
+
+    auto info = enumerator->next_file();
+    while(info)
+    {
+      auto child = directory->get_child(info->get_name());
+      bool removed_child = false;
+      if(child->query_file_type() == Gio::FILE_TYPE_DIRECTORY)
+        removed_child = delete_directory(child);
+      else
+        removed_child = child->remove();
+
+      if(!removed_child)
+        return false;
+
+      info = enumerator->next_file();
+    }
+
+    //Delete the actual directory:
+    if(!directory->remove())
+      return false;
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception from Gio::File: " << ex.what() << std::endl;
+    return false;
+  }
+
+  return true;
+}
+
+bool delete_directory(const std::string& uri)
+{
+  auto file = Gio::File::create_for_uri(uri);
+  return delete_directory(file);
+}
+
+bool delete_file(const std::string& uri)
+{
+  auto file = Gio::File::create_for_uri(uri);
+  if(file->query_file_type() == Gio::FILE_TYPE_DIRECTORY)
+  {
+    std::cerr << G_STRFUNC << ": The file is a directory.\n";
+    return false;
+  }
+
+  try
+  {
+    if(!file->remove())
+      return false;
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception from Gio::File: " << ex.what() << std::endl;
+    return false;
+  }
+
+  return true;
+}
+
+/** For instance, to find the first file in the directory with a .glom extension.
+ */
+Glib::ustring get_directory_child_with_suffix(const Glib::ustring& uri_directory, const std::string& suffix, 
bool recursive)
+{
+  auto directory = Gio::File::create_for_uri(uri_directory);
+  auto enumerator = directory->enumerate_children();
+
+  auto info = enumerator->next_file();
+  while(info)
+  {
+    Glib::RefPtr<const Gio::File> child = directory->get_child(info->get_name());
+
+    const Gio::FileType file_type = child->query_file_type();
+    if(file_type == Gio::FILE_TYPE_REGULAR)
+    {
+      //Check the filename:
+      const std::string basename = child->get_basename();
+      if(string_remove_suffix(basename, suffix) != basename)
+        return child->get_uri();
+    }
+    else if(recursive && file_type == Gio::FILE_TYPE_DIRECTORY)
+    {
+      //Look in sub-directories too:
+      const Glib::ustring result = get_directory_child_with_suffix(child->get_uri(), suffix, recursive);
+      if(!result.empty())
+        return result;
+    }
+
+    info = enumerator->next_file();
+  }
+
+  return Glib::ustring();
+}
+
+Glib::ustring get_file_uri_without_extension(const Glib::ustring& uri)
+{
+  if(uri.empty())
+    return uri;
+
+  auto file = Gio::File::create_for_uri(uri);
+  if(!file)
+    return uri; //Actually an error.
+
+  const Glib::ustring filename_part = file->get_basename();
+
+  const Glib::ustring::size_type pos_dot = filename_part.rfind(".");
+  if(pos_dot == Glib::ustring::npos)
+    return uri; //There was no extension, so just return the existing URI.
+  else
+  {
+    const Glib::ustring filename_part_without_ext = filename_part.substr(0, pos_dot);
+
+    //Use the Gio::File API to manipulate the URI:
+    auto parent = file->get_parent();
+    auto file_without_extension = parent->get_child(filename_part_without_ext);
+
+    return file_without_extension->get_uri();
+  }
+}
+
+std::string get_file_path_without_extension(const std::string& filepath)
+{
+  if(filepath.empty())
+    return filepath;
+
+  auto file = Gio::File::create_for_path(filepath);
+  if(!file)
+    return filepath; //Actually an error.
+
+  const Glib::ustring filename_part = file->get_basename();
+
+  const Glib::ustring::size_type pos_dot = filename_part.rfind(".");
+  if(pos_dot == Glib::ustring::npos)
+    return filepath; //There was no extension, so just return the existing URI.
+  else
+  {
+    const Glib::ustring filename_part_without_ext = filename_part.substr(0, pos_dot);
+
+    //Use the Gio::File API to manipulate the URI:
+    auto parent = file->get_parent();
+    auto file_without_extension = parent->get_child(filename_part_without_ext);
+
+    return file_without_extension->get_path();
+  }
+}
+
+std::string get_temp_file_path(const std::string& prefix, const std::string& extension)
+{
+  //Get a temporary file path:
+  std::string filepath;
+  try
+  {
+    const std::string prefix_pattern = prefix + "XXXXXX" + extension;
+    const int filehandle = Glib::file_open_tmp(filepath, prefix_pattern);
+    ::close(filehandle);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() failed\n";
+    return filepath;
+  }
+
+  if(filepath.empty())
+  {
+    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() returned an empty filepath\n";
+  }
+
+  return filepath;
+}
+
+Glib::ustring get_temp_file_uri(const std::string& prefix, const std::string& extension)
+{
+  try
+  {
+    const std::string filepath = get_temp_file_path(prefix, extension);
+    return Glib::filename_to_uri(filepath);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception from filename_to_uri(): " << ex.what() << std::endl;
+    return std::string();
+  }
+}
+
+std::string get_temp_directory_path(const std::string& prefix)
+{
+  std::string result;
+
+  const std::string pattern = Glib::build_filename(
+          Glib::get_tmp_dir(), prefix + "XXXXXX");
+
+  //We must copy the pattern, because mkdtemp() modifies it:
+  char* c_pattern = g_strdup(pattern.c_str());
+
+  const char* filepath = g_mkdtemp(c_pattern);
+  if(filepath)
+    result = filepath;
+
+  return result;
+}
+
+Glib::ustring get_temp_directory_uri(const std::string& prefix)
+{
+  try
+  {
+    const std::string filepath = get_temp_directory_path(prefix);
+    return Glib::filename_to_uri(filepath);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": Exception from filename_to_uri(): " << ex.what() << std::endl;
+    return Glib::ustring();
+  }
+}
+
+
+Glib::ustring create_local_image_uri(const Gnome::Gda::Value& value)
+{
+  static guint m_temp_image_uri_number = 0;
+
+  Glib::ustring result;
+
+  if(value.get_value_type() == GDA_TYPE_BINARY)
+  {
+    long size = 0;
+    gconstpointer pData = value.get_binary(size);
+    if(size && pData)
+    {
+      // Note that this is regular binary data, not escaped text representing the data:
+
+      //Save the image to a temporary file and provide the file URI.
+      char pchExtraNum[10];
+      sprintf(pchExtraNum, "%d", m_temp_image_uri_number);
+      result = ("/tmp/glom_report_image_" + Glib::ustring(pchExtraNum) + ".png");
+      ++m_temp_image_uri_number;
+
+      std::fstream the_stream(result, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
+      if(the_stream)
+      {
+        the_stream.write((char*)pData, size);
+      }
+    }
+    else
+      std::cerr << G_STRFUNC << ": binary GdaValue contains no data.\n";
+  }
+  //else
+  //  std::cerr << G_STRFUNC << ": type != BINARY\n";
+
+  if(result.empty())
+    result = "/tmp/glom_report_image_invalid.png";
+
+  return ("file://" + result);
+}
+
+
+} //namespace Utils
+
+} //namespace Glom
diff --git a/glom/libglom/file_utils.h b/glom/libglom/file_utils.h
new file mode 100644
index 0000000..9a80579
--- /dev/null
+++ b/glom/libglom/file_utils.h
@@ -0,0 +1,84 @@
+/* Glom
+ *
+ * Copyright (C) 2001-2016 Murray Cumming
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ */
+
+#ifndef GLOM_FILE_UTILS_H
+#define GLOM_FILE_UTILS_H
+
+#include <giomm/file.h>
+#include <libgdamm/value.h>
+
+namespace Glom
+{
+
+namespace Utils
+{
+
+bool file_exists(const Glib::ustring& uri);
+
+bool file_exists(const Glib::RefPtr <Gio::File>& file);
+
+
+Glib::ustring create_local_image_uri(const Gnome::Gda::Value& value);
+
+/** Delete a directory, if it exists, and its contents.
+ * Unlike g_file_delete(), this does not fail if the directory is not empty.
+ */
+bool delete_directory(const Glib::RefPtr<Gio::File>& directory);
+
+/** Delete a directory, if it exists, and its contents.
+ * Unlike g_file_delete(), this does not fail if the directory is not empty.
+ * See also delete_file().
+ */
+bool delete_directory(const std::string& uri);
+
+/** Delete a file, if it exists.
+ * See also delete_directory().
+ */
+bool delete_file(const std::string& uri);
+
+/** For instance, to find the first file in the directory with a .glom extension.
+ */
+Glib::ustring get_directory_child_with_suffix(const Glib::ustring& uri_directory, const std::string& suffix, 
bool recursive);
+
+/** Get a URI with the extension (any extension, not just .glom) removed.
+ */
+Glib::ustring get_file_uri_without_extension(const Glib::ustring& uri);
+
+/** Get a filepath with the extension (any extension, not just .glom) removed.
+ */
+std::string get_file_path_without_extension(const std::string& filepath);
+
+
+std::string get_temp_file_path(const std::string& prefix = std::string(), const std::string& extension = 
std::string());
+Glib::ustring get_temp_file_uri(const std::string& prefix = std::string(), const std::string& extension = 
std::string());
+
+/** This actually creates the directory.
+ */
+std::string get_temp_directory_path(const std::string& prefix = std::string());
+
+/** This actually creates the directory.
+ */
+Glib::ustring get_temp_directory_uri(const std::string& prefix = std::string());
+
+} //namespace Utils
+
+} //namespace Glom
+
+#endif //GLOM_FILE_UTILS_H
diff --git a/glom/libglom/filelist.am b/glom/libglom/filelist.am
index fecc488..a2de35f 100644
--- a/glom/libglom/filelist.am
+++ b/glom/libglom/filelist.am
@@ -108,6 +108,8 @@ libglom_sources =                                                   \
        glom/libglom/db_utils.h                                         \
        glom/libglom/db_utils_export.cc                                 \
        glom/libglom/db_utils_export.h                                          \
+       glom/libglom/file_utils.cc \
+       glom/libglom/file_utils.h \
        glom/libglom/glom_postgres.cc                                   \
        glom/libglom/glom_postgres.h                                    \
        glom/libglom/init.cc                                            \
diff --git a/glom/libglom/report_builder.cc b/glom/libglom/report_builder.cc
index 42af551..a9444bf 100644
--- a/glom/libglom/report_builder.cc
+++ b/glom/libglom/report_builder.cc
@@ -22,6 +22,7 @@
 #include <libglom/utils.h>
 #include <libglom/data_structure/glomconversions.h>
 #include <libglom/db_utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/xsl_utils.h>
 #include <libglom/xml_utils.h>
 #include <iostream>
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index 4446ba6..5216d81 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -577,67 +577,6 @@ Glib::ustring Utils::locale_language_id(const Glib::ustring& locale_id)
   }
 }
 
-Glib::ustring Utils::create_local_image_uri(const Gnome::Gda::Value& value)
-{
-  static guint m_temp_image_uri_number = 0;
-
-  Glib::ustring result;
-
-  if(value.get_value_type() == GDA_TYPE_BINARY)
-  {
-    long size = 0;
-    gconstpointer pData = value.get_binary(size);
-    if(size && pData)
-    {
-      // Note that this is regular binary data, not escaped text representing the data:
-
-      //Save the image to a temporary file and provide the file URI.
-      char pchExtraNum[10];
-      sprintf(pchExtraNum, "%d", m_temp_image_uri_number);
-      result = ("/tmp/glom_report_image_" + Glib::ustring(pchExtraNum) + ".png");
-      ++m_temp_image_uri_number;
-
-      std::fstream the_stream(result, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
-      if(the_stream)
-      {
-        the_stream.write((char*)pData, size);
-      }
-    }
-    else
-       std::cerr << G_STRFUNC << ": binary GdaValue contains no data.\n";
-  }
-  //else
-  //  std::cerr << G_STRFUNC << ": type != BINARY\n";
-
-  if(result.empty())
-    result = "/tmp/glom_report_image_invalid.png";
-
-  return ("file://" + result);
-}
-
-bool Utils::file_exists(const Glib::ustring& uri)
-{
-  if(uri.empty())
-     return false;
-
-  //Check whether file exists already:
-  // Try to examine the input file.
-  auto file = Gio::File::create_for_uri(uri);
-  return file_exists(file);
-}
-
-bool Utils::file_exists(const Glib::RefPtr<Gio::File>& file)
-{
-  try
-  {
-    return file->query_exists();
-  }
-  catch(const Gio::Error& /* ex */)
-  {
-    return false; //Something went wrong. It does not exist.
-  }
-}
-
 //TODO: This is a duplicate of the one in db_utils.cc:
 //Merge all db utilities into db_utils in glom 1.24:
 static Glib::RefPtr<Gnome::Gda::Connection> get_connection()
@@ -841,161 +780,6 @@ Glib::RefPtr<Gnome::Gda::SqlBuilder> Utils::build_sql_update_with_where_clause(
   return builder;
 }
 
-bool Utils::delete_directory(const Glib::RefPtr<Gio::File>& directory)
-{
-  try
-  {
-    if(!(directory->query_exists()))
-      return true;
-
-    //(Recursively) Delete any child files and directories,
-    //so we can delete this directory.
-    auto enumerator = directory->enumerate_children();
-
-    auto info = enumerator->next_file();
-    while(info)
-    {
-      auto child = directory->get_child(info->get_name());
-      bool removed_child = false;
-      if(child->query_file_type() == Gio::FILE_TYPE_DIRECTORY)
-        removed_child = delete_directory(child);
-      else
-        removed_child = child->remove();
-
-      if(!removed_child)
-         return false;
-
-      info = enumerator->next_file();
-    }
-
-    //Delete the actual directory:
-    if(!directory->remove())
-      return false;
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << G_STRFUNC << ": Exception from Gio::File: " << ex.what() << std::endl;
-    return false;
-  }
-
-  return true;
-}
-
-bool Utils::delete_directory(const std::string& uri)
-{
-  auto file = Gio::File::create_for_uri(uri);
-  return delete_directory(file);
-}
-
-bool Utils::delete_file(const std::string& uri)
-{
-  auto file = Gio::File::create_for_uri(uri);
-  if(file->query_file_type() == Gio::FILE_TYPE_DIRECTORY)
-  {
-    std::cerr << G_STRFUNC << ": The file is a directory.\n";
-    return false;
-  }
-
-  try
-  {
-    if(!file->remove())
-      return false;
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << G_STRFUNC << ": Exception from Gio::File: " << ex.what() << std::endl;
-    return false;
-  }
-
-  return true;
-}
-
-/** For instance, to find the first file in the directory with a .glom extension.
- */
-Glib::ustring Utils::get_directory_child_with_suffix(const Glib::ustring& uri_directory, const std::string& 
suffix, bool recursive)
-{
-  auto directory = Gio::File::create_for_uri(uri_directory);
-  auto enumerator = directory->enumerate_children();
-
-  auto info = enumerator->next_file();
-  while(info)
-  {
-    Glib::RefPtr<const Gio::File> child = directory->get_child(info->get_name());
-
-    const Gio::FileType file_type = child->query_file_type();
-    if(file_type == Gio::FILE_TYPE_REGULAR)
-    {
-      //Check the filename:
-      const std::string basename = child->get_basename();
-      if(string_remove_suffix(basename, suffix) != basename)
-        return child->get_uri();
-    }
-    else if(recursive && file_type == Gio::FILE_TYPE_DIRECTORY)
-    {
-      //Look in sub-directories too:
-      const Glib::ustring result = get_directory_child_with_suffix(child->get_uri(), suffix, recursive);
-      if(!result.empty())
-        return result;
-    }
-
-    info = enumerator->next_file();
-  }
-
-  return Glib::ustring();
-}
-
-Glib::ustring Utils::get_file_uri_without_extension(const Glib::ustring& uri)
-{
-  if(uri.empty())
-    return uri;
-
-  auto file = Gio::File::create_for_uri(uri);
-  if(!file)
-    return uri; //Actually an error.
-
-  const Glib::ustring filename_part = file->get_basename();
-
-  const Glib::ustring::size_type pos_dot = filename_part.rfind(".");
-  if(pos_dot == Glib::ustring::npos)
-    return uri; //There was no extension, so just return the existing URI.
-  else
-  {
-    const Glib::ustring filename_part_without_ext = filename_part.substr(0, pos_dot);
-
-    //Use the Gio::File API to manipulate the URI:
-    auto parent = file->get_parent();
-    auto file_without_extension = parent->get_child(filename_part_without_ext);
-
-    return file_without_extension->get_uri();
-  }
-}
-
-std::string Utils::get_file_path_without_extension(const std::string& filepath)
-{
-  if(filepath.empty())
-    return filepath;
-
-  auto file = Gio::File::create_for_path(filepath);
-  if(!file)
-    return filepath; //Actually an error.
-
-  const Glib::ustring filename_part = file->get_basename();
-
-  const Glib::ustring::size_type pos_dot = filename_part.rfind(".");
-  if(pos_dot == Glib::ustring::npos)
-    return filepath; //There was no extension, so just return the existing URI.
-  else
-  {
-    const Glib::ustring filename_part_without_ext = filename_part.substr(0, pos_dot);
-
-    //Use the Gio::File API to manipulate the URI:
-    auto parent = file->get_parent();
-    auto file_without_extension = parent->get_child(filename_part_without_ext);
-
-    return file_without_extension->get_path();
-  }
-}
-
 Glib::ustring Utils::get_list_of_layout_items_for_display(const LayoutGroup::type_list_items& 
list_layout_fields)
 {
   Glib::ustring result;
@@ -1040,75 +824,6 @@ Glib::ustring Utils::get_list_of_sort_fields_for_display(const Formatting::type_
   return text;
 }
 
-std::string Utils::get_temp_file_path(const std::string& prefix, const std::string& extension)
-{
-  //Get a temporary file path:
-  std::string filepath;
-  try
-  {
-    const std::string prefix_pattern = prefix + "XXXXXX" + extension;
-    const int filehandle = Glib::file_open_tmp(filepath, prefix_pattern);
-    ::close(filehandle);
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() failed\n";
-    return filepath;
-  }
-  
-  if(filepath.empty())
-  {
-    std::cerr << G_STRFUNC << ": Glib::file_open_tmp() returned an empty filepath\n";
-  }
-
-  return filepath;
-}
-
-Glib::ustring Utils::get_temp_file_uri(const std::string& prefix, const std::string& extension)
-{
-  try
-  {
-    const std::string filepath = get_temp_file_path(prefix, extension);
-    return Glib::filename_to_uri(filepath);
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << G_STRFUNC << ": Exception from filename_to_uri(): " << ex.what() << std::endl;
-    return std::string();
-  }
-}
-
-std::string Utils::get_temp_directory_path(const std::string& prefix)
-{
-  std::string result;
-
-  const std::string pattern = Glib::build_filename(
-    Glib::get_tmp_dir(), prefix + "XXXXXX");
-
-  //We must copy the pattern, because mkdtemp() modifies it:
-  char* c_pattern = g_strdup(pattern.c_str());
-  
-  const char* filepath = g_mkdtemp(c_pattern);
-  if(filepath)
-    result = filepath;
-
-  return result;
-}
-
-Glib::ustring Utils::get_temp_directory_uri(const std::string& prefix)
-{
-  try
-  {
-    const std::string filepath = get_temp_directory_path(prefix);
-    return Glib::filename_to_uri(filepath);
-  }
-  catch(const Glib::Error& ex)
-  {
-    std::cerr << G_STRFUNC << ": Exception from filename_to_uri(): " << ex.what() << std::endl;
-    return Glib::ustring();
-  }
-}
-
 LayoutGroup::type_list_const_items Utils::get_layout_items_plus_primary_key(const 
LayoutGroup::type_list_const_items& items, const std::shared_ptr<const Document>& document, const 
Glib::ustring& table_name)
 {
   if(!document)
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index 8e97020..57b4668 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -145,38 +145,6 @@ Glib::ustring locale_simplify(const Glib::ustring& locale_id);
  */
 Glib::ustring locale_language_id(const Glib::ustring& locale_id);
 
-Glib::ustring create_local_image_uri(const Gnome::Gda::Value& value);
-
-bool file_exists(const Glib::ustring& uri);
-bool file_exists(const Glib::RefPtr<Gio::File>& file);
-
-/** Delete a directory, if it exists, and its contents.
- * Unlike g_file_delete(), this does not fail if the directory is not empty.
- */
-bool delete_directory(const Glib::RefPtr<Gio::File>& directory);
-
-/** Delete a directory, if it exists, and its contents.
- * Unlike g_file_delete(), this does not fail if the directory is not empty.
- * See also delete_file().
- */
-bool delete_directory(const std::string& uri);
-
-/** Delete a file, if it exists.
- * See also delete_directory().
- */
-bool delete_file(const std::string& uri);
-
-/** For instance, to find the first file in the directory with a .glom extension.
- */
-Glib::ustring get_directory_child_with_suffix(const Glib::ustring& uri_directory, const std::string& suffix, 
bool recursive);
-
-/** Get a URI with the extension (any extension, not just .glom) removed.
- */
-Glib::ustring get_file_uri_without_extension(const Glib::ustring& uri);
-
-/** Get a filepath with the extension (any extension, not just .glom) removed.
- */
-std::string get_file_path_without_extension(const std::string& filepath);
 
 /** Get a string to display to the user, as a representation of a list of layout items.
  */
@@ -203,16 +171,6 @@ LayoutGroup::type_list_items get_layout_items_plus_primary_key(const LayoutGroup
 
 type_vecConstLayoutFields get_table_fields_to_show_for_sequence(const std::shared_ptr<const Document>& 
document, const Glib::ustring& table_name, const Document::type_list_layout_groups& mapGroupSequence);
 
-std::string get_temp_file_path(const std::string& prefix = std::string(), const std::string& extension = 
std::string());
-Glib::ustring get_temp_file_uri(const std::string& prefix = std::string(), const std::string& extension = 
std::string());
-
-/** This actually creates the directory.
- */
-std::string get_temp_directory_path(const std::string& prefix = std::string());
-
-/** This actually creates the directory.
- */
-Glib::ustring get_temp_directory_uri(const std::string& prefix = std::string());
 
 /** @returns true if the script is OK, or 
  * false if the script uses pygtk2, which would cause a crash,
diff --git a/glom/main.cc b/glom/main.cc
index f07411f..5b9a501 100644
--- a/glom/main.cc
+++ b/glom/main.cc
@@ -31,8 +31,7 @@
 #include <glom/libglom/init.h>
 #include <glom/libglom/connectionpool.h>
 #include <glom/libglom/utils.h>
-
-#include <giomm/file.h>
+#include <libglom/file_utils.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
 
diff --git a/glom/utility_widgets/imageglom.cc b/glom/utility_widgets/imageglom.cc
index 53f1e15..59b6c9b 100644
--- a/glom/utility_widgets/imageglom.cc
+++ b/glom/utility_widgets/imageglom.cc
@@ -28,6 +28,7 @@
 #include <glom/utility_widgets/dialog_image_load_progress.h>
 #include <glom/utility_widgets/dialog_image_save_progress.h>
 #include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <gtkmm/appchooserdialog.h>
 #include <gtkmm/filechooserdialog.h>
 #include <giomm/file.h>
diff --git a/tests/import/utils.cc b/tests/import/utils.cc
index 08cdbb0..c5fc981 100644
--- a/tests/import/utils.cc
+++ b/tests/import/utils.cc
@@ -1,5 +1,5 @@
 #include <tests/import/utils.h>
-#include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <glibmm/convert.h>
 #include <glibmm/fileutils.h>
 #include <glibmm/main.h>
diff --git a/tests/python/test_python_execute_func_with_record.cc 
b/tests/python/test_python_execute_func_with_record.cc
index 35620dd..3a3a121 100644
--- a/tests/python/test_python_execute_func_with_record.cc
+++ b/tests/python/test_python_execute_func_with_record.cc
@@ -2,6 +2,7 @@
 #include <glom/libglom/connectionpool.h>
 #include <glom/python_embed/glom_python.h>
 #include <libglom/data_structure/glomconversions.h>
+#include <libglom/file_utils.h>
 #include <libglom/utils.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
diff --git a/tests/test_document_autosave.cc b/tests/test_document_autosave.cc
index 5d22a44..caa11b6 100644
--- a/tests/test_document_autosave.cc
+++ b/tests/test_document_autosave.cc
@@ -20,7 +20,7 @@
 
 #include <libglom/document/document.h>
 #include <libglom/init.h>
-#include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <giomm/file.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
diff --git a/tests/test_document_load_and_save.cc b/tests/test_document_load_and_save.cc
index d76f5ab..24ffa6d 100644
--- a/tests/test_document_load_and_save.cc
+++ b/tests/test_document_load_and_save.cc
@@ -20,7 +20,7 @@
 
 #include <libglom/document/document.h>
 #include <libglom/init.h>
-#include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>
 
diff --git a/tests/test_selfhosting_new_then_backup_restore.cc 
b/tests/test_selfhosting_new_then_backup_restore.cc
index 60ef0a4..68a00d8 100644
--- a/tests/test_selfhosting_new_then_backup_restore.cc
+++ b/tests/test_selfhosting_new_then_backup_restore.cc
@@ -20,7 +20,7 @@
 
 #include "tests/test_selfhosting_utils.h"
 #include <libglom/init.h> 
-#include <libglom/utils.h>  
+#include <libglom/file_utils.h>
 #include <glib.h> //For g_assert()
 #include <iostream>
 #include <cstdlib> //For EXIT_SUCCESS and EXIT_FAILURE
diff --git a/tests/test_selfhosting_utils.cc b/tests/test_selfhosting_utils.cc
index b533e0c..07efa10 100644
--- a/tests/test_selfhosting_utils.cc
+++ b/tests/test_selfhosting_utils.cc
@@ -26,6 +26,7 @@
 #include <libglom/init.h>
 #include <libglom/privs.h>
 #include <libglom/db_utils.h>
+#include <libglom/file_utils.h>
 #include <libglom/utils.h>
 #include <giomm/file.h>
 #include <glibmm/convert.h>
diff --git a/tests/translations_po/test_document_export_po.cc 
b/tests/translations_po/test_document_export_po.cc
index 0e2cf2b..4a7af78 100644
--- a/tests/translations_po/test_document_export_po.cc
+++ b/tests/translations_po/test_document_export_po.cc
@@ -23,7 +23,7 @@
 #include <libglom/document/document.h>
 #include <libglom/translations_po.h>
 #include <libglom/init.h>
-#include <libglom/utils.h>
+#include <libglom/file_utils.h>
 #include <giomm/file.h>
 #include <glibmm/convert.h>
 #include <glibmm/miscutils.h>


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