[easytag/wip/core-refactoring: 6/8] Use GFile in et_browser_select_dir()



commit aabdd636267eff48ded5fc935eb530ac6a9eb65f
Author: David King <amigadave amigadave com>
Date:   Thu Dec 31 19:35:45 2015 +0000

    Use GFile in et_browser_select_dir()

 src/application.c        |   60 ++++++++++++--------------
 src/application_window.c |    5 +-
 src/application_window.h |    4 +-
 src/browser.c            |  107 +++++++++++++++++++++++++++------------------
 src/browser.h            |   31 ++++++-------
 5 files changed, 111 insertions(+), 96 deletions(-)
---
diff --git a/src/application.c b/src/application.c
index 7d20d72..844b793 100644
--- a/src/application.c
+++ b/src/application.c
@@ -1,5 +1,5 @@
 /* EasyTAG - tag editor for audio files
- * Copyright (C) 2014  David King <amigadave amigadave com>
+ * Copyright (C) 2014-2015  David King <amigadave amigadave com>
  *
  * 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
@@ -33,7 +33,7 @@
 typedef struct
 {
     guint idle_handler;
-    gchar *init_directory;
+    GFile *init_directory;
 } EtApplicationPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (EtApplication, et_application, GTK_TYPE_APPLICATION)
@@ -400,7 +400,6 @@ et_application_open (GApplication *self,
     GFileInfo *info;
     GError *err = NULL;
     GFileType type;
-    gchar *path;
     gchar *display_path;
 
     priv = et_application_get_instance_private (ET_APPLICATION (self));
@@ -413,14 +412,17 @@ et_application_open (GApplication *self,
 
     check_for_hidden_path_in_tree (arg);
 
-    path = g_file_get_path (arg);
-    /* TODO: Use g_filename_display_basename? */
-    display_path = g_filename_display_name (path);
-    info = g_file_query_info (arg, G_FILE_ATTRIBUTE_STANDARD_TYPE,
+    info = g_file_query_info (arg, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME ","
+                              G_FILE_ATTRIBUTE_STANDARD_TYPE,
                               G_FILE_QUERY_INFO_NONE, NULL, &err);
 
     if (info == NULL)
     {
+        gchar *path;
+
+        path = g_file_get_path (arg);
+        display_path = g_filename_display_name (path);
+
         if (activated)
         {
             Log_Print (LOG_ERROR, _("Error while querying information for file ‘%s’: %s"),
@@ -446,16 +448,12 @@ et_application_open (GApplication *self,
         if (activated)
         {
             et_application_window_select_dir (ET_APPLICATION_WINDOW (window),
-                                              path);
-            g_free (path);
+                                              arg);
         }
         else
         {
-            priv->init_directory = path;
+            priv->init_directory = g_object_ref (arg);
         }
-
-        g_free (display_path);
-        g_object_unref (info);
     }
     else if (type == G_FILE_TYPE_REGULAR)
     {
@@ -464,43 +462,35 @@ et_application_open (GApplication *self,
 
         if (parent)
         {
-            g_free (display_path);
-            g_free (path);
-
             if (activated)
             {
-                gchar *parent_path;
-
-                parent_path = g_file_get_path (arg);
                 et_application_window_select_dir (ET_APPLICATION_WINDOW (window),
-                                                  parent_path);
-
-                g_free (parent_path);
+                                                  parent);
             }
             else
             {
-                priv->init_directory = g_file_get_path (parent);
+                priv->init_directory = parent;
             }
-
-            g_object_unref (parent);
-            g_object_unref (info);
         }
         else
         {
-            Log_Print (LOG_WARNING, _("Cannot open path ‘%s’"), display_path);
-            g_free (path);
-            g_free (display_path);
+            /* This is, in reality, the path of the file, not the directory. */
+            Log_Print (LOG_WARNING, _("Cannot open path ‘%s’"),
+                       g_file_info_get_display_name (info));
+            g_object_unref (info);
             return;
         }
     }
     else
     {
-        Log_Print (LOG_WARNING, _("Cannot open path ‘%s’"), display_path);
-        g_free (path);
-        g_free (display_path);
+        Log_Print (LOG_WARNING, _("Cannot open path ‘%s’"),
+                   g_file_info_get_display_name (info));
+        g_object_unref (info);
         return;
     }
 
+    g_object_unref (info);
+
     if (!activated)
     {
         common_init (ET_APPLICATION (self));
@@ -575,7 +565,11 @@ et_application_finalize (GObject *object)
     self = ET_APPLICATION (object);
     priv = et_application_get_instance_private (self);
 
-    g_free (priv->init_directory);
+    if (priv->init_directory)
+    {
+        g_object_unref (priv->init_directory);
+        priv->init_directory = NULL;
+    }
 
     G_OBJECT_CLASS (et_application_parent_class)->finalize (object);
 }
diff --git a/src/application_window.c b/src/application_window.c
index 67b4cfd..a254961 100644
--- a/src/application_window.c
+++ b/src/application_window.c
@@ -1948,13 +1948,14 @@ et_application_window_browser_clear_artist_model (EtApplicationWindow *self)
 }
 
 void
-et_application_window_select_dir (EtApplicationWindow *self, const gchar *path)
+et_application_window_select_dir (EtApplicationWindow *self,
+                                  GFile *file)
 {
     EtApplicationWindowPrivate *priv;
 
     priv = et_application_window_get_instance_private (self);
 
-    et_browser_select_dir (ET_BROWSER (priv->browser), path);
+    et_browser_select_dir (ET_BROWSER (priv->browser), file);
 }
 
 /*
diff --git a/src/application_window.h b/src/application_window.h
index c6f4bb0..e898a71 100644
--- a/src/application_window.h
+++ b/src/application_window.h
@@ -1,5 +1,5 @@
 /* EasyTAG - tag editor for audio files
- * Copyright (C) 2013  David King <amigadave amigadave com>
+ * Copyright (C) 2013-2015  David King <amigadave amigadave com>
  *
  * 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
@@ -66,7 +66,7 @@ void et_application_window_browser_set_sensitive (EtApplicationWindow *self, gbo
 void et_application_window_browser_clear (EtApplicationWindow *self);
 void et_application_window_browser_clear_album_model (EtApplicationWindow *self);
 void et_application_window_browser_clear_artist_model (EtApplicationWindow *self);
-void et_application_window_select_dir (EtApplicationWindow *self, const gchar *path);
+void et_application_window_select_dir (EtApplicationWindow *self, GFile *file);
 void et_application_window_select_file_by_et_file (EtApplicationWindow *self, ET_File *ETFile);
 const gchar * et_application_window_get_current_path (EtApplicationWindow *self);
 GtkWidget * et_application_window_get_scan_dialog (EtApplicationWindow *self);
diff --git a/src/browser.c b/src/browser.c
index dfcda5b..4e5ab28 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -242,7 +242,11 @@ static void et_browser_on_column_clicked (GtkTreeViewColumn *column,
 void
 et_browser_go_home (EtBrowser *self)
 {
-    et_browser_select_dir (self, g_get_home_dir ());
+    GFile *file;
+
+    file = g_file_new_for_path (g_get_home_dir ());
+    et_browser_select_dir (self, file);
+    g_object_unref (file);
 }
 
 /*
@@ -251,8 +255,11 @@ et_browser_go_home (EtBrowser *self)
 void
 et_browser_go_desktop (EtBrowser *self)
 {
-    et_browser_select_dir (self,
-                           g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP));
+    GFile *file;
+
+    file = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP));
+    et_browser_select_dir (self, file);
+    g_object_unref (file);
 }
 
 /*
@@ -261,8 +268,11 @@ et_browser_go_desktop (EtBrowser *self)
 void
 et_browser_go_documents (EtBrowser *self)
 {
-    et_browser_select_dir (self,
-                           g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS));
+    GFile *file;
+
+    file = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS));
+    et_browser_select_dir (self, file);
+    g_object_unref (file);
 }
 
 /*
@@ -271,8 +281,11 @@ et_browser_go_documents (EtBrowser *self)
 void
 et_browser_go_downloads (EtBrowser *self)
 {
-    et_browser_select_dir (self,
-                           g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD));
+    GFile *file;
+
+    file = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD));
+    et_browser_select_dir (self, file);
+    g_object_unref (file);
 }
 
 /*
@@ -281,8 +294,11 @@ et_browser_go_downloads (EtBrowser *self)
 void
 et_browser_go_music (EtBrowser *self)
 {
-    et_browser_select_dir (self,
-                           g_get_user_special_dir (G_USER_DIRECTORY_MUSIC));
+    GFile *file;
+
+    file = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_MUSIC));
+    et_browser_select_dir (self, file);
+    g_object_unref (file);
 }
 
 
@@ -528,13 +544,18 @@ et_browser_reload_directory (EtBrowser *self)
 
     if (priv->directory_view && priv->current_path != NULL)
     {
-        // Unselect files, to automatically reload the file of the directory
+        /* Unselect files, to automatically reload the file of the directory. */
+        GFile *file;
         GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->directory_view));
+
         if (selection)
         {
             gtk_tree_selection_unselect_all(selection);
         }
-        et_browser_select_dir (self, priv->current_path);
+
+        file = g_file_new_for_path (priv->current_path);
+        et_browser_select_dir (self, file);
+        g_object_unref (file);
     }
 }
 
@@ -568,7 +589,6 @@ Browser_Entry_Activated (EtBrowser *self,
     EtBrowserPrivate *priv;
     const gchar *parse_name;
     GFile *file;
-    gchar *path;
 
     priv = et_browser_get_instance_private (self);
 
@@ -576,11 +596,10 @@ Browser_Entry_Activated (EtBrowser *self,
     Add_String_To_Combo_List (GTK_LIST_STORE (priv->entry_model), parse_name);
 
     file = g_file_parse_name (parse_name);
-    path = g_file_get_path (file);
 
-    et_browser_select_dir (self, path);
+    et_browser_select_dir (self, file);
+
     g_object_unref (file);
-    g_free (path);
 }
 
 /*
@@ -606,22 +625,23 @@ et_browser_entry_set_text (EtBrowser *self, const gchar *text)
 void
 et_browser_go_parent (EtBrowser *self)
 {
-    gchar *parent_dir, *path;
+    GFile *file;
+    GFile *parent;
 
-    /* TODO: Replace this with g_file_get_parent(). */
-    parent_dir = g_strdup (et_browser_get_current_path (self));
+    file = g_file_new_for_path (et_browser_get_current_path (self));
+    parent = g_file_get_parent (file);
 
-    if (strlen(parent_dir)>1)
+    if (parent)
     {
-        if ( parent_dir[strlen(parent_dir)-1]==G_DIR_SEPARATOR )
-            parent_dir[strlen(parent_dir)-1] = '\0';
-        path = g_path_get_dirname(parent_dir);
-
-        et_browser_select_dir (self, path);
-        g_free(path);
+        et_browser_select_dir (self, parent);
+        g_object_unref (parent);
+    }
+    else
+    {
+        g_debug ("%s", "No parent found for current browser path");
     }
 
-    g_free (parent_dir);
+    g_object_unref (file);
 }
 
 /*
@@ -1002,16 +1022,17 @@ et_browser_win32_get_drive_root (EtBrowser *self,
 
 
 /*
- * et_browser_select_dir: Select the directory corresponding to the 'path' in
- * the tree browser, but it doesn't read it!
- * Check if path is correct before selecting it.
+ * et_browser_select_dir:
  *
- * - "current_path" is in file system encoding (not UTF-8)
+ * Select the directory corresponding to the 'path' in the tree browser, but it
+ * doesn't read it! Check if path is correct before selecting it.
  */
 void
-et_browser_select_dir (EtBrowser *self, const gchar *current_path)
+et_browser_select_dir (EtBrowser *self,
+                       GFile *file)
 {
     EtBrowserPrivate *priv;
+    gchar *current_path;
     GtkTreePath *rootPath = NULL;
     GtkTreeIter parentNode, currentNode;
     gint index = 1; // Skip the first token as it is NULL due to leading /
@@ -1023,18 +1044,14 @@ et_browser_select_dir (EtBrowser *self, const gchar *current_path)
 
     g_return_if_fail (priv->directory_view != NULL);
 
-    /* Load current_path */
-    if (et_str_empty (current_path))
-    {
-        return;
-    }
-
     /* Don't check here if the path is valid. It will be done later when
      * selecting a node in the tree */
 
+    current_path = g_file_get_path (file);
     et_browser_set_current_path (self, current_path);
 
     parts = g_strsplit(current_path, G_DIR_SEPARATOR_S, 0);
+    g_free (current_path);
 
     // Expand root node (fill parentNode and rootPath)
 #ifdef G_OS_WIN32
@@ -1071,7 +1088,7 @@ et_browser_select_dir (EtBrowser *self, const gchar *current_path)
         if (!gtk_tree_model_iter_children(GTK_TREE_MODEL(priv->directory_model), &currentNode, &parentNode))
         {
             gchar *path, *parent_path;
-            GFile *file;
+            GFile *directory;
 
             gtk_tree_model_get (GTK_TREE_MODEL (priv->directory_model),
                                 &parentNode, TREE_COLUMN_FULL_PATH,
@@ -1079,11 +1096,11 @@ et_browser_select_dir (EtBrowser *self, const gchar *current_path)
             path = g_build_filename (parent_path, parts[index], NULL);
             g_free (parent_path);
 
-            file = g_file_new_for_path (path);
+            directory = g_file_new_for_path (path);
 
             /* As dir name was not found in any node, check whether it exists
              * or not. */
-            if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, NULL)
+            if (g_file_query_file_type (directory, G_FILE_QUERY_INFO_NONE, NULL)
                 == G_FILE_TYPE_DIRECTORY)
             {
                 /* It exists and is readable permission of parent directory is executable */
@@ -1106,12 +1123,12 @@ et_browser_select_dir (EtBrowser *self, const gchar *current_path)
             }
             else
             {
-                g_object_unref (file);
+                g_object_unref (directory);
                 g_free (path);
                 break;
             }
 
-            g_object_unref (file);
+            g_object_unref (directory);
             g_free (path);
         }
 
@@ -3182,11 +3199,15 @@ et_browser_reload (EtBrowser *self)
 
     if (selection)
     {
+        GFile *file;
+
         g_signal_handlers_block_by_func (selection,
                                          G_CALLBACK (Browser_Tree_Node_Selected),
                                          self);
         Browser_Tree_Initialize (self);
-        et_browser_select_dir (self, current_path);
+        file = g_file_new_for_path (current_path);
+        et_browser_select_dir (self, file);
+        g_object_unref (file);
         g_signal_handlers_unblock_by_func (selection,
                                            G_CALLBACK (Browser_Tree_Node_Selected),
                                            self);
diff --git a/src/browser.h b/src/browser.h
index c617c0f..00db58a 100644
--- a/src/browser.h
+++ b/src/browser.h
@@ -1,23 +1,22 @@
-/*
- *  EasyTAG - Tag editor for MP3 and Ogg Vorbis files
- *  Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
+/* EasyTAG - tag editor for audio files
+ * Copyright (C) 2014-2015  David King <amigadave amigadave com>
+ * Copyright (C) 2000-2003  Jerome Couderc <easytag gmail com>
  *
- *  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 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.
+ * 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.
+ * 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 ET_BROWSER_H_
 #define ET_BROWSER_H_
 
@@ -69,7 +68,7 @@ enum
 void et_browser_clear_album_model (EtBrowser *self);
 void et_browser_clear_artist_model (EtBrowser *self);
 
-void et_browser_select_dir (EtBrowser *self, const gchar *current_path);
+void et_browser_select_dir (EtBrowser *self, GFile *file);
 void et_browser_reload (EtBrowser *self);
 void et_browser_collapse (EtBrowser *self);
 void et_browser_set_sensitive (EtBrowser *self, gboolean sensitive);


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