Merge pull request #455 from techee/filebrowser_icons

Show icons corresponding to MIME types in the file browser plugin
This commit is contained in:
Colomban Wendling 2015-06-18 16:49:37 +02:00
commit f427a3a6e1

View File

@ -67,6 +67,7 @@ enum
FILEVIEW_COLUMN_ICON = 0, FILEVIEW_COLUMN_ICON = 0,
FILEVIEW_COLUMN_NAME, FILEVIEW_COLUMN_NAME,
FILEVIEW_COLUMN_FILENAME, /* the full filename, including path for display as tooltip */ FILEVIEW_COLUMN_FILENAME, /* the full filename, including path for display as tooltip */
FILEVIEW_COLUMN_IS_DIR,
FILEVIEW_N_COLUMNS FILEVIEW_N_COLUMNS
}; };
@ -183,6 +184,39 @@ static gboolean check_filtered(const gchar *base_name)
} }
static GIcon *get_icon(const gchar *fname)
{
GIcon *icon = NULL;
gchar *ctype;
ctype = g_content_type_guess(fname, NULL, 0, NULL);
if (ctype)
{
icon = g_content_type_get_icon(ctype);
if (icon)
{
GtkIconInfo *icon_info;
icon_info = gtk_icon_theme_lookup_by_gicon(gtk_icon_theme_get_default(), icon, 16, 0);
if (!icon_info)
{
g_object_unref(icon);
icon = NULL;
}
else
gtk_icon_info_free(icon_info);
}
g_free(ctype);
}
if (!icon)
icon = g_themed_icon_new("text-x-generic");
return icon;
}
/* name is in locale encoding */ /* name is in locale encoding */
static void add_item(const gchar *name) static void add_item(const gchar *name)
{ {
@ -190,6 +224,7 @@ static void add_item(const gchar *name)
gchar *fname, *utf8_name, *utf8_fullname; gchar *fname, *utf8_name, *utf8_fullname;
const gchar *sep; const gchar *sep;
gboolean dir; gboolean dir;
GIcon *icon;
if (G_UNLIKELY(EMPTY(name))) if (G_UNLIKELY(EMPTY(name)))
return; return;
@ -225,11 +260,15 @@ static void add_item(const gchar *name)
gtk_list_store_append(file_store, &iter); gtk_list_store_append(file_store, &iter);
} }
icon = dir ? g_themed_icon_new("folder") : get_icon(utf8_name);
gtk_list_store_set(file_store, &iter, gtk_list_store_set(file_store, &iter,
FILEVIEW_COLUMN_ICON, (dir) ? GTK_STOCK_DIRECTORY : GTK_STOCK_FILE, FILEVIEW_COLUMN_ICON, icon,
FILEVIEW_COLUMN_NAME, utf8_name, FILEVIEW_COLUMN_NAME, utf8_name,
FILEVIEW_COLUMN_FILENAME, utf8_fullname, FILEVIEW_COLUMN_FILENAME, utf8_fullname,
FILEVIEW_COLUMN_IS_DIR, dir,
-1); -1);
g_object_unref(icon);
done: done:
g_free(utf8_name); g_free(utf8_name);
g_free(utf8_fullname); g_free(utf8_fullname);
@ -241,6 +280,7 @@ static void add_top_level_entry(void)
{ {
GtkTreeIter iter; GtkTreeIter iter;
gchar *utf8_dir; gchar *utf8_dir;
GIcon *icon;
if (EMPTY(g_path_skip_root(current_dir))) if (EMPTY(g_path_skip_root(current_dir)))
return; /* ignore 'C:\' or '/' */ return; /* ignore 'C:\' or '/' */
@ -251,11 +291,14 @@ static void add_top_level_entry(void)
gtk_list_store_prepend(file_store, &iter); gtk_list_store_prepend(file_store, &iter);
last_dir_iter = gtk_tree_iter_copy(&iter); last_dir_iter = gtk_tree_iter_copy(&iter);
icon = g_themed_icon_new("folder");
gtk_list_store_set(file_store, &iter, gtk_list_store_set(file_store, &iter,
FILEVIEW_COLUMN_ICON, GTK_STOCK_DIRECTORY, FILEVIEW_COLUMN_ICON, icon,
FILEVIEW_COLUMN_NAME, "..", FILEVIEW_COLUMN_NAME, "..",
FILEVIEW_COLUMN_FILENAME, utf8_dir, FILEVIEW_COLUMN_FILENAME, utf8_dir,
FILEVIEW_COLUMN_IS_DIR, TRUE,
-1); -1);
g_object_unref(icon);
g_free(utf8_dir); g_free(utf8_dir);
} }
@ -385,21 +428,15 @@ static gboolean is_folder_selected(GList *selected_items)
for (item = selected_items; item != NULL; item = g_list_next(item)) for (item = selected_items; item != NULL; item = g_list_next(item))
{ {
gchar *icon;
GtkTreeIter iter; GtkTreeIter iter;
GtkTreePath *treepath; GtkTreePath *treepath;
treepath = (GtkTreePath*) item->data; treepath = (GtkTreePath*) item->data;
gtk_tree_model_get_iter(model, &iter, treepath); gtk_tree_model_get_iter(model, &iter, treepath);
gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_ICON, &icon, -1); gtk_tree_model_get(model, &iter, FILEVIEW_COLUMN_IS_DIR, &dir_found, -1);
if (utils_str_equal(icon, GTK_STOCK_DIRECTORY)) if (dir_found)
{
dir_found = TRUE;
g_free(icon);
break; break;
}
g_free(icon);
} }
return dir_found; return dir_found;
} }
@ -828,7 +865,7 @@ static void prepare_file_view(void)
GtkTreeViewColumn *column; GtkTreeViewColumn *column;
GtkTreeSelection *selection; GtkTreeSelection *selection;
file_store = gtk_list_store_new(FILEVIEW_N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); file_store = gtk_list_store_new(FILEVIEW_N_COLUMNS, G_TYPE_ICON, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
gtk_tree_view_set_model(GTK_TREE_VIEW(file_view), GTK_TREE_MODEL(file_store)); gtk_tree_view_set_model(GTK_TREE_VIEW(file_view), GTK_TREE_MODEL(file_store));
g_object_unref(file_store); g_object_unref(file_store);
@ -837,7 +874,7 @@ static void prepare_file_view(void)
text_renderer = gtk_cell_renderer_text_new(); text_renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new(); column = gtk_tree_view_column_new();
gtk_tree_view_column_pack_start(column, icon_renderer, FALSE); gtk_tree_view_column_pack_start(column, icon_renderer, FALSE);
gtk_tree_view_column_set_attributes(column, icon_renderer, "stock-id", FILEVIEW_COLUMN_ICON, NULL); gtk_tree_view_column_set_attributes(column, icon_renderer, "gicon", FILEVIEW_COLUMN_ICON, NULL);
gtk_tree_view_column_pack_start(column, text_renderer, TRUE); gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
gtk_tree_view_column_set_attributes(column, text_renderer, "text", FILEVIEW_COLUMN_NAME, NULL); gtk_tree_view_column_set_attributes(column, text_renderer, "text", FILEVIEW_COLUMN_NAME, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(file_view), column); gtk_tree_view_append_column(GTK_TREE_VIEW(file_view), column);
@ -924,14 +961,14 @@ static GtkWidget *make_filterbar(void)
static gboolean completion_match_func(GtkEntryCompletion *completion, const gchar *key, static gboolean completion_match_func(GtkEntryCompletion *completion, const gchar *key,
GtkTreeIter *iter, gpointer user_data) GtkTreeIter *iter, gpointer user_data)
{ {
gchar *str, *icon; gchar *str;
gboolean is_dir;
gboolean result = FALSE; gboolean result = FALSE;
gtk_tree_model_get(GTK_TREE_MODEL(file_store), iter, gtk_tree_model_get(GTK_TREE_MODEL(file_store), iter,
FILEVIEW_COLUMN_ICON, &icon, FILEVIEW_COLUMN_NAME, &str, -1); FILEVIEW_COLUMN_IS_DIR, &is_dir, FILEVIEW_COLUMN_NAME, &str, -1);
if (str != NULL && icon != NULL && utils_str_equal(icon, GTK_STOCK_DIRECTORY) && if (str != NULL && is_dir && !g_str_has_suffix(key, G_DIR_SEPARATOR_S))
! g_str_has_suffix(key, G_DIR_SEPARATOR_S))
{ {
/* key is something like "/tmp/te" and str is a filename like "test", /* key is something like "/tmp/te" and str is a filename like "test",
* so strip the path from key to make them comparable */ * so strip the path from key to make them comparable */
@ -942,7 +979,6 @@ static gboolean completion_match_func(GtkEntryCompletion *completion, const gcha
g_free(str_lowered); g_free(str_lowered);
} }
g_free(str); g_free(str);
g_free(icon);
return result; return result;
} }