[glom] Added Utils::get_file_*_without_extension().



commit 7003fa456dcf6dc2936b723ac877944def1b78f4
Author: Murray Cumming <murrayc murrayc com>
Date:   Mon Oct 10 10:35:15 2011 +0200

    Added Utils::get_file_*_without_extension().
    
    	* glom/application.[h|cc]: Moved get_file_uri_without_extension() to
    	* glom/libglom/utils.[h|cc]: And added get_file_path_without_extension().

 ChangeLog             |    7 ++++++
 glom/application.cc   |   30 +--------------------------
 glom/application.h    |    2 -
 glom/libglom/utils.cc |   52 +++++++++++++++++++++++++++++++++++++++++++++++++
 glom/libglom/utils.h  |    8 +++++++
 5 files changed, 69 insertions(+), 30 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index cd4212b..aa374a8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-10-10  Murray Cumming  <murrayc murrayc com>
+
+	Added Utils::get_file_*_without_extension().
+	
+	* glom/application.[h|cc]: Moved get_file_uri_without_extension() to 
+	* glom/libglom/utils.[h|cc]: And added get_file_path_without_extension().
+
 2011-10-09  Murray Cumming  <murrayc murrayc com>
 
 	Fix make check with --enable-warnings=fatal.
diff --git a/glom/application.cc b/glom/application.cc
index 0b60d29..a5b759e 100644
--- a/glom/application.cc
+++ b/glom/application.cc
@@ -889,32 +889,6 @@ static bool uri_is_writable(const Glib::RefPtr<const Gio::File>& uri)
 }
 #endif // !GLOM_ENABLE_CLIENT_ONLY
 
-Glib::ustring Application::get_file_uri_without_extension(const Glib::ustring& uri)
-{
-  if(uri.empty())
-    return uri;
-
-  Glib::RefPtr<Gio::File> 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:
-    Glib::RefPtr<Gio::File> parent = file->get_parent();
-    Glib::RefPtr<Gio::File> file_without_extension = parent->get_child(filename_part_without_ext);
-
-    return file_without_extension->get_uri();
-  }
-}
-
 //TODO: Use Gio::Application? Is this even used?
 void Application::new_instance(const Glib::ustring& uri) //Override
 {
@@ -2440,7 +2414,7 @@ Glib::ustring Application::ui_file_select_save(const Glib::ustring& old_file_uri
       const Glib::ustring uri_chosen = fileChooser_Save->get_uri();
 
       //Change the URI, to put the file (and its data folder) in a folder:
-      const Glib::ustring uri = get_file_uri_without_extension(uri_chosen);
+      const Glib::ustring uri = Utils::get_file_uri_without_extension(uri_chosen);
 
       //Check whether the file exists, and that we have rights to it:
       Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(uri);
@@ -2645,7 +2619,7 @@ void Application::on_menu_developer_export_backup()
   // Start with a name based on the existing name.
   const Glib::ustring fileuri_old = document->get_file_uri();
   const Glib::RefPtr<const Gio::File> file_old =
-    Gio::File::create_for_uri( get_file_uri_without_extension(fileuri_old) );
+    Gio::File::create_for_uri( Utils::get_file_uri_without_extension(fileuri_old) );
   const std::string old_basename = file_old->get_basename();
   Glib::TimeVal timeval;
   timeval.assign_current_time();
diff --git a/glom/application.h b/glom/application.h
index 393025d..8770a7c 100644
--- a/glom/application.h
+++ b/glom/application.h
@@ -205,8 +205,6 @@ private:
   void open_browsed_document(const EpcServiceInfo* server, const Glib::ustring& service_name);
 #endif // !G_OS_WIN32
 
-  static Glib::ustring get_file_uri_without_extension(const Glib::ustring& uri);
-
   typedef GlomBakery::App_WithDoc_Gtk type_base;
 
   //Widgets:
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index f4f9763..cad1f49 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -1215,6 +1215,58 @@ Glib::ustring Utils::get_directory_child_with_suffix(const Glib::ustring& uri_di
   return Glib::ustring();
 }
 
+Glib::ustring Utils::get_file_uri_without_extension(const Glib::ustring& uri)
+{
+  if(uri.empty())
+    return uri;
+
+  Glib::RefPtr<Gio::File> 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:
+    Glib::RefPtr<Gio::File> parent = file->get_parent();
+    Glib::RefPtr<Gio::File> 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;
+
+  Glib::RefPtr<Gio::File> 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:
+    Glib::RefPtr<Gio::File> parent = file->get_parent();
+    Glib::RefPtr<Gio::File> 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;
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index 09a8826..8f37630 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -192,6 +192,14 @@ bool delete_directory(const std::string& uri);
  */
 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.
  */
 Glib::ustring get_list_of_layout_items_for_display(const LayoutGroup::type_list_items& list_layout_fields);



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