[gnome-builder] content-type: Check for conflicting plain files and markdown files



commit 393d1ce317bd0a304e1aba839e9f60691b0f54f3
Author: vanadiae <vanadiae35 gmail com>
Date:   Thu Dec 24 17:36:04 2020 +0100

    content-type: Check for conflicting plain files and markdown files
    
    Currently when a file is named i.e. COPYING or README, it is tagged a content type
    text/x-copying, so it receives the correct symbolic icon.
    
    But when the file is i.e. COPYING.md or README.md, then it's detected as
    a markdown file and so it uses the markdown icon instead of the correct
    bundled icon (copyright logo).
    
    So this commit checks for common file names that might be put as
    markdown (or another format, for what it's worth), and use the correct
    bundled icon for those files. This work can't be done in the
    shared-mime-info database because it's mostly based on the use-case of
    the file, not its format, so there would be conflicts between multiple
    content type.

 src/libide/editor/ide-editor-page.c             |  2 +-
 src/libide/io/ide-content-type.c                | 25 ++++++++++++++++++++++++-
 src/libide/io/ide-content-type.h                |  5 +++--
 src/libide/projects/ide-project-file.c          |  2 +-
 src/plugins/file-search/gbp-file-search-index.c |  2 +-
 5 files changed, 30 insertions(+), 6 deletions(-)
---
diff --git a/src/libide/editor/ide-editor-page.c b/src/libide/editor/ide-editor-page.c
index 08cd60bf3..67f5c162d 100644
--- a/src/libide/editor/ide-editor-page.c
+++ b/src/libide/editor/ide-editor-page.c
@@ -117,7 +117,7 @@ ide_editor_page_update_icon (IdeEditorPage *self)
   content_type = g_content_type_guess (name, (const guchar *)sniff, strlen (sniff), NULL);
 
   /* Update icon to match guess */
-  icon = ide_g_content_type_get_symbolic_icon (content_type);
+  icon = ide_g_content_type_get_symbolic_icon (content_type, name);
   ide_page_set_icon (IDE_PAGE (self), icon);
 }
 
diff --git a/src/libide/io/ide-content-type.c b/src/libide/io/ide-content-type.c
index df2a88dc7..829c0ae76 100644
--- a/src/libide/io/ide-content-type.c
+++ b/src/libide/io/ide-content-type.c
@@ -37,9 +37,26 @@
  * Since: 3.32
  */
 GIcon *
-ide_g_content_type_get_symbolic_icon (const gchar *content_type)
+ide_g_content_type_get_symbolic_icon (const gchar *content_type,
+                                      const gchar *filename)
 {
   static GHashTable *bundled;
+  /* This ensures those files get a proper icon when they end with .md (markdown files).
+   * It can't be fixed in the shared-mime-info db because otherwise they wouldn't get detected as
+   * markdown anymore.
+   */
+  static const struct {
+    const gchar *searched_prefix;
+    const gchar *icon_name;
+  } bundled_check_by_name_prefix[] = {
+    {"README", "text-x-readme-symbolic"},
+    {"NEWS", "text-x-changelog-symbolic"},
+    {"CHANGELOG", "text-x-changelog-symbolic"},
+    {"COPYING", "text-x-copying-symbolic"},
+    {"LICENSE", "text-x-copying-symbolic"},
+    {"AUTHORS", "text-x-authors-symbolic"},
+    {"MAINTAINERS", "text-x-authors-symbolic"},
+  };
   g_autoptr(GIcon) icon = NULL;
 
   g_return_val_if_fail (content_type != NULL, NULL);
@@ -93,6 +110,12 @@ ide_g_content_type_get_symbolic_icon (const gchar *content_type)
     {
       const gchar * const *names = g_themed_icon_get_names (G_THEMED_ICON (icon));
 
+      for (guint j = 0; j < G_N_ELEMENTS (bundled_check_by_name_prefix); j++)
+        {
+          if (g_str_has_prefix (filename, bundled_check_by_name_prefix[j].searched_prefix))
+            return g_icon_new_for_string (bundled_check_by_name_prefix[j].icon_name, NULL);
+        }
+
       if (names != NULL)
         {
           gboolean fallback = FALSE;
diff --git a/src/libide/io/ide-content-type.h b/src/libide/io/ide-content-type.h
index 6304b69c1..7311ebed7 100644
--- a/src/libide/io/ide-content-type.h
+++ b/src/libide/io/ide-content-type.h
@@ -24,8 +24,9 @@
 
 G_BEGIN_DECLS
 
-IDE_AVAILABLE_IN_3_32
-GIcon *ide_g_content_type_get_symbolic_icon (const gchar *content_type);
+IDE_AVAILABLE_IN_3_40
+GIcon *ide_g_content_type_get_symbolic_icon (const gchar *content_type,
+                                             const gchar *filename);
 
 G_END_DECLS
 
diff --git a/src/libide/projects/ide-project-file.c b/src/libide/projects/ide-project-file.c
index 05dfb8748..627ec0b1b 100644
--- a/src/libide/projects/ide-project-file.c
+++ b/src/libide/projects/ide-project-file.c
@@ -389,7 +389,7 @@ ide_project_file_get_symbolic_icon (IdeProjectFile *self)
         {
           g_autoptr(GIcon) override = NULL;
 
-          if ((override = ide_g_content_type_get_symbolic_icon (content_type)))
+          if ((override = ide_g_content_type_get_symbolic_icon (content_type, g_file_info_get_display_name 
(priv->info))))
             g_file_info_set_symbolic_icon (priv->info, override);
         }
     }
diff --git a/src/plugins/file-search/gbp-file-search-index.c b/src/plugins/file-search/gbp-file-search-index.c
index 5f12790f2..f7e4b9c20 100644
--- a/src/plugins/file-search/gbp-file-search-index.c
+++ b/src/plugins/file-search/gbp-file-search-index.c
@@ -395,7 +395,7 @@ gbp_file_search_index_populate (GbpFileSearchIndex *self,
            * Sniffing would be way too slow here.
            */
           if ((content_type = g_content_type_guess (filename, NULL, 0, NULL)))
-            themed_icon = ide_g_content_type_get_symbolic_icon (content_type);
+            themed_icon = ide_g_content_type_get_symbolic_icon (content_type, filename);
 
           result = g_object_new (GBP_TYPE_FILE_SEARCH_RESULT,
                                  "context", context,


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