[latexila/wip/latexila-next: 28/41] latexila_utils_replace_home_dir_with_tilde() in C



commit a3b9dd74a6b8408e4c896b99e62d68d7578a8df7
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed May 7 22:55:46 2014 +0200

    latexila_utils_replace_home_dir_with_tilde() in C
    
    The function comes from gedit.

 docs/reference/latexila-docs.xml     |    1 +
 docs/reference/latexila-sections.txt |    6 ++
 src/document.vala                    |    2 +-
 src/liblatexila/Makefile.am          |    4 +-
 src/liblatexila/latexila-utils.c     |   88 ++++++++++++++++++++++++++++++++++
 src/liblatexila/latexila-utils.h     |   31 ++++++++++++
 src/liblatexila/latexila.h           |    5 ++
 src/project_dialogs.vala             |    6 +-
 src/utils.vala                       |   13 +-----
 9 files changed, 139 insertions(+), 17 deletions(-)
---
diff --git a/docs/reference/latexila-docs.xml b/docs/reference/latexila-docs.xml
index db7b0ed..064f80a 100644
--- a/docs/reference/latexila-docs.xml
+++ b/docs/reference/latexila-docs.xml
@@ -17,6 +17,7 @@
     <xi:include href="xml/build-tools-default.xml"/>
     <xi:include href="xml/build-tools-personal.xml"/>
     <xi:include href="xml/build-view.xml"/>
+    <xi:include href="xml/utils.xml"/>
   </chapter>
 
   <chapter id="object-tree">
diff --git a/docs/reference/latexila-sections.txt b/docs/reference/latexila-sections.txt
index 85eae96..d0d729b 100644
--- a/docs/reference/latexila-sections.txt
+++ b/docs/reference/latexila-sections.txt
@@ -126,3 +126,9 @@ LATEXILA_TYPE_BUILD_VIEW
 LatexilaBuildViewPrivate
 latexila_build_view_get_type
 </SECTION>
+
+<SECTION>
+<FILE>utils</FILE>
+<TITLE>LatexilaUtils</TITLE>
+latexila_utils_replace_home_dir_with_tilde
+</SECTION>
diff --git a/src/document.vala b/src/document.vala
index e91cdb1..7d6f7d9 100644
--- a/src/document.vala
+++ b/src/document.vala
@@ -279,7 +279,7 @@ public class Document : Gtk.SourceBuffer
         if (location == null)
             return get_unsaved_document_name ();
 
-        return Utils.replace_home_dir_with_tilde (location.get_parse_name ());
+        return Latexila.utils_replace_home_dir_with_tilde (location.get_parse_name ());
     }
 
     public string get_short_name_for_display ()
diff --git a/src/liblatexila/Makefile.am b/src/liblatexila/Makefile.am
index 2f9a003..538c4e5 100644
--- a/src/liblatexila/Makefile.am
+++ b/src/liblatexila/Makefile.am
@@ -14,7 +14,8 @@ liblatexila_headers =                         \
        latexila-build-view.h                   \
        latexila-post-processor.h               \
        latexila-post-processor-all-output.h    \
-       latexila-types.h
+       latexila-types.h                        \
+       latexila-utils.h
 
 BUILT_SOURCES =                        \
        latexila-enum-types.c   \
@@ -29,6 +30,7 @@ liblatexila_la_SOURCES =                      \
        latexila-build-view.c                   \
        latexila-post-processor.c               \
        latexila-post-processor-all-output.c    \
+       latexila-utils.c                        \
        $(liblatexila_headers)                  \
        $(BUILT_SOURCES)
 
diff --git a/src/liblatexila/latexila-utils.c b/src/liblatexila/latexila-utils.c
new file mode 100644
index 0000000..a16d984
--- /dev/null
+++ b/src/liblatexila/latexila-utils.c
@@ -0,0 +1,88 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * From gedit-utils.c:
+ * Copyright (C) 1998, 1999 - Alex Roberts, Evan Lawrence
+ * Copyright (C) 2000, 2002 - Chema Celorio, Paolo Maggi
+ * Copyright (C) 2003-2005 - Paolo Maggi
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila 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 LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/**
+ * SECTION:utils
+ * @title: LatexilaUtils
+ * @short_description: Utilities functions
+ *
+ * Various utilities functions.
+ */
+
+#include "latexila-utils.h"
+#include <string.h>
+
+/**
+ * latexila_utils_replace_home_dir_with_tilde:
+ * @filename: the filename.
+ *
+ * Replaces the home directory with a tilde, if the home directory is present in
+ * the @filename.
+ *
+ * This function comes from gedit.
+ *
+ * Returns: the new filename. Free with g_free().
+ */
+gchar *
+latexila_utils_replace_home_dir_with_tilde (const gchar *filename)
+{
+  gchar *tmp;
+  gchar *home;
+
+  g_return_val_if_fail (filename != NULL, NULL);
+
+  /* Note that g_get_home_dir returns a const string */
+  tmp = (gchar *) g_get_home_dir ();
+
+  if (tmp == NULL)
+    {
+      return g_strdup (filename);
+    }
+
+  home = g_filename_to_utf8 (tmp, -1, NULL, NULL, NULL);
+  if (home == NULL)
+    {
+      return g_strdup (filename);
+    }
+
+  if (strcmp (filename, home) == 0)
+    {
+      g_free (home);
+      return g_strdup ("~");
+    }
+
+  tmp = home;
+  home = g_strdup_printf ("%s/", tmp);
+  g_free (tmp);
+
+  if (g_str_has_prefix (filename, home))
+    {
+      gchar *res = g_strdup_printf ("~/%s", filename + strlen (home));
+      g_free (home);
+      return res;
+    }
+
+  g_free (home);
+  return g_strdup (filename);
+}
diff --git a/src/liblatexila/latexila-utils.h b/src/liblatexila/latexila-utils.h
new file mode 100644
index 0000000..f96abe1
--- /dev/null
+++ b/src/liblatexila/latexila-utils.h
@@ -0,0 +1,31 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * LaTeXila 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila 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 LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __LATEXILA_UTILS_H__
+#define __LATEXILA_UTILS_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+gchar *         latexila_utils_replace_home_dir_with_tilde      (const gchar *filename);
+
+G_END_DECLS
+
+#endif /* __LATEXILA_UTILS_H__ */
diff --git a/src/liblatexila/latexila.h b/src/liblatexila/latexila.h
index f284c63..0d799f0 100644
--- a/src/liblatexila/latexila.h
+++ b/src/liblatexila/latexila.h
@@ -20,12 +20,17 @@
 #ifndef __LATEXILA_H__
 #define __LATEXILA_H__
 
+#include "latexila-types.h"
+#include "latexila-enum-types.h"
+
 #include "latexila-build-job.h"
 #include "latexila-build-tool.h"
 #include "latexila-build-tools.h"
 #include "latexila-build-tools-default.h"
 #include "latexila-build-tools-personal.h"
+#include "latexila-build-view.h"
 #include "latexila-post-processor.h"
 #include "latexila-post-processor-all-output.h"
+#include "latexila-utils.h"
 
 #endif /* __LATEXILA_H__ */
diff --git a/src/project_dialogs.vala b/src/project_dialogs.vala
index 353821e..eb6e0d7 100644
--- a/src/project_dialogs.vala
+++ b/src/project_dialogs.vala
@@ -99,7 +99,7 @@ namespace ProjectDialogs
                 MessageType.ERROR,
                 ButtonsType.OK,
                 _("There is a conflict with the project \"%s\"."),
-                Utils.replace_home_dir_with_tilde (conflict.get_parse_name ()) + "/");
+                Latexila.utils_replace_home_dir_with_tilde (conflict.get_parse_name ()) + "/");
             error_dialog.run ();
             error_dialog.destroy ();
         }
@@ -125,7 +125,7 @@ namespace ProjectDialogs
 
         // directory
         string project_dir = project.directory.get_parse_name ();
-        project_dir = Utils.replace_home_dir_with_tilde (project_dir) + "/";
+        project_dir = Latexila.utils_replace_home_dir_with_tilde (project_dir) + "/";
         Label location = new Label (project_dir);
         location.set_line_wrap (true);
         location.set_halign (Align.START);
@@ -341,7 +341,7 @@ namespace ProjectDialogs
             string uri_directory = project.directory.get_parse_name ();
             string uri_main_file = project.main_file.get_parse_name ();
 
-            string dir = Utils.replace_home_dir_with_tilde (uri_directory) + "/";
+            string dir = Latexila.utils_replace_home_dir_with_tilde (uri_directory) + "/";
 
             // relative path
             string main_file =
diff --git a/src/utils.vala b/src/utils.vala
index 00d122b..b8ac9e3 100644
--- a/src/utils.vala
+++ b/src/utils.vala
@@ -71,24 +71,13 @@ namespace Utils
     /*************************************************************************/
     // URI, File or Path utilities
 
-    public string replace_home_dir_with_tilde (string uri)
-    {
-        return_val_if_fail (uri != null, null);
-        string home = Environment.get_home_dir ();
-        if (uri == home)
-            return "~";
-        if (uri.has_prefix (home))
-            return "~" + uri[home.length:uri.length];
-        return uri;
-    }
-
     public string? uri_get_dirname (string uri)
     {
         return_val_if_fail (uri != null, null);
         string dir = Path.get_dirname (uri);
         if (dir == ".")
             return null;
-        return replace_home_dir_with_tilde (dir);
+        return Latexila.utils_replace_home_dir_with_tilde (dir);
     }
 
     /* Returns a string suitable to be displayed in the UI indicating


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