get_icon_lookup_path: only return readable/existing dirs

This commit is contained in:
Daniel Hahler 2015-10-11 19:53:00 +02:00
parent fbfa6b8995
commit 7afd8e3179
1 changed files with 31 additions and 11 deletions

View File

@ -67,29 +67,48 @@ local function is_format_supported(icon_file)
end end
local icon_lookup_path = nil local icon_lookup_path = nil
--- Get a list of icon lookup paths.
-- @treturn table A list of directories, without trailing slash.
local function get_icon_lookup_path() local function get_icon_lookup_path()
if not icon_lookup_path then if not icon_lookup_path then
local add_if_readable = function(t, path)
if awful_util.dir_readable(path) then
table.insert(t, path)
end
end
icon_lookup_path = {} icon_lookup_path = {}
local icon_theme_paths = {} local icon_theme_paths = {}
local icon_theme = theme.icon_theme local icon_theme = theme.icon_theme
local paths = glib.get_system_data_dirs() local paths = glib.get_system_data_dirs()
table.insert(paths, 1, glib.get_user_data_dir()) table.insert(paths, 1, glib.get_user_data_dir())
table.insert(paths, 1, glib.get_home_dir() .. '/.icons') table.insert(paths, 1, glib.build_filenamev({glib.get_home_dir(),
'.icons'}))
for k,dir in ipairs(paths) do for k,dir in ipairs(paths) do
local icons_dir = glib.build_filenamev({dir, 'icons'})
if awful_util.dir_readable(icons_dir) then
if icon_theme then if icon_theme then
table.insert(icon_theme_paths, dir..'/icons/' .. icon_theme .. '/') add_if_readable(icon_theme_paths,
glib.build_filenamev({icons_dir,
icon_theme}))
end
-- Fallback theme.
add_if_readable(icon_theme_paths,
glib.build_filenamev({icons_dir, 'hicolor'}))
end end
table.insert(icon_theme_paths, dir..'/icons/hicolor/') -- fallback theme
end end
for i, icon_theme_directory in ipairs(icon_theme_paths) do for i, icon_theme_directory in ipairs(icon_theme_paths) do
for j, size in ipairs(all_icon_sizes) do for j, size in ipairs(all_icon_sizes) do
table.insert(icon_lookup_path, icon_theme_directory .. size .. '/apps/') add_if_readable(icon_lookup_path,
glib.build_filenamev({icon_theme_directory,
size, 'apps'}))
end end
end end
for k,dir in ipairs(paths)do for k,dir in ipairs(paths)do
-- lowest priority fallbacks -- lowest priority fallbacks
table.insert(icon_lookup_path, dir..'/pixmaps/') add_if_readable(icon_lookup_path,
table.insert(icon_lookup_path, dir..'/icons/') glib.build_filenamev({dir, 'pixmaps'}))
add_if_readable(icon_lookup_path,
glib.build_filenamev({dir, 'icons'}))
end end
end end
return icon_lookup_path return icon_lookup_path
@ -109,14 +128,15 @@ function utils.lookup_icon(icon_file)
return awful_util.file_readable(icon_file) and icon_file or nil return awful_util.file_readable(icon_file) and icon_file or nil
else else
for i, directory in ipairs(get_icon_lookup_path()) do for i, directory in ipairs(get_icon_lookup_path()) do
if is_format_supported(icon_file) and awful_util.file_readable(directory .. icon_file) then if is_format_supported(icon_file) and
return directory .. icon_file awful_util.file_readable(directory .. "/" .. icon_file) then
return directory .. "/" .. icon_file
else else
-- Icon is probably specified without path and format, -- Icon is probably specified without path and format,
-- like 'firefox'. Try to add supported extensions to -- like 'firefox'. Try to add supported extensions to
-- it and see if such file exists. -- it and see if such file exists.
for _, format in ipairs(icon_formats) do for _, format in ipairs(icon_formats) do
local possible_file = directory .. icon_file .. "." .. format local possible_file = directory .. "/" .. icon_file .. "." .. format
if awful_util.file_readable(possible_file) then if awful_util.file_readable(possible_file) then
return possible_file return possible_file
end end