Skip to content
This repository has been archived by the owner on Oct 10, 2020. It is now read-only.

Commit

Permalink
Look for icons in all directories from the specification
Browse files Browse the repository at this point in the history
According to Icon Theme Specification Version 0.11[1], "apps
should look [for icons] in $HOME/.icons (for backwards compatibility),
in $XDG_DATA_DIRS/icons and in /usr/share/pixmaps (in that order)."

Fix icon size handling. The assignment of icon_sizes to isizes in
the lookup_icon() function, is not necessary and caused the former
table to grow rapidly.

[1] http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-0.11.html
  • Loading branch information
steelman committed May 5, 2015
1 parent e514e74 commit 0d94177
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 12 deletions.
85 changes: 85 additions & 0 deletions freedesktop/dirs.lua
@@ -0,0 +1,85 @@
-- -*- lua-indent-level: 4; indent-tabs-mode: nil -*-
-- Grab environment

local string = string
local io = io
local os = os
local table = table
local type = type
local ipairs = ipairs
local pairs = pairs

module("freedesktop.dirs")

local _xdg_dirs = {}
local _xdg_home = {}

local xdg_default_dirs = {
DATA = "/usr/local/share:/usr/share",
CONFIG = "/etc/xdg",
}

local xdg_default_home = {
CACHE = ".cache/",
CONFIG = ".config/",
DATA = ".local/share/",
}

local function xdg_dirs(d)
local env = os.getenv('XDG_' .. d .. '_DIRS')
local ret = {}
if not env or env == "" then
env = xdg_default_dirs[d]
end
for i in string.gmatch(env, "[^:]+") do
i = string.gsub(i,'/*$', '')
table.insert(ret, i .. '/')
end
_xdg_dirs[d] = ret
return ret
end

function xdg_config_dirs()
if _xdg_dirs['CONFIG'] then
return _xdg_dirs['CONFIG']
end
return xdg_dirs('CONFIG')
end

function xdg_data_dirs()
if _xdg_dirs['DATA'] then
return _xdg_dirs['DATA']
end
return xdg_dirs("DATA")
end

local function xdg_home(d)
local ret = os.getenv('XDG_' .. d .. '_HOME')
if not ret or ret == "" then
local home = string.gsub(os.getenv('HOME'),'/*$', '')
ret = home .. '/' .. xdg_default_home[d]
end
_xdg_home[d] = ret
return ret
end

function xdg_cache_home()
if _xdg_home['CACHE'] then
return _xdg_home['CACHE']
end
return xdg_home('CACHE')
end

function xdg_config_home()
if _xdg_home['CONFIG'] then
return _xdg_home['CONFIG']
end
return xdg_home('CONFIG')
end

function xdg_data_home()
if _xdg_home['DATA'] then
return _xdg_home['DATA']
end
return xdg_home('DATA')
end
58 changes: 46 additions & 12 deletions freedesktop/utils.lua
@@ -1,3 +1,4 @@
-- -*- lua-indent-level: 4; indent-tabs-mode: nil -*-
-- Grab environment

local io = io
Expand All @@ -6,6 +7,7 @@ local table = table
local type = type
local ipairs = ipairs
local pairs = pairs
local dirs = require("freedesktop.dirs")

module("freedesktop.utils")

Expand Down Expand Up @@ -34,12 +36,24 @@ all_icon_types = {
'status',
'mimetypes'
}
all_icon_paths = { os.getenv("HOME") .. '/.icons/', '/usr/share/icons/' }

icon_sizes = {}
all_icon_paths = {}

local mime_types = {}

local function _init_all_icon_paths()
for _,dir in ipairs({ os.getenv("HOME") .. '/.icons/',
dirs.xdg_data_home() .. 'icons/'}) do
if directory_exists(dir) then
table.insert(all_icon_paths, dir)
end
end
for _,dir in ipairs(dirs.xdg_data_dirs()) do
if directory_exists(dir .. 'icons/') then
table.insert(all_icon_paths, dir .. 'icons/')
end
end
end

function get_lines(...)
local f = io.popen(...)
return function () -- iterator
Expand All @@ -49,6 +63,17 @@ function get_lines(...)
end
end

function directory_exists(filename)
local file = io.open(filename)
local result = (file ~= nil)
if result then
local a,b,c = file:read(0)
file:close()
result = (c == 21)
end
return result
end

function file_exists(filename)
local file = io.open(filename, 'r')
local result = (file ~= nil)
Expand All @@ -67,34 +92,42 @@ function lookup_icon(arg)
local icon_themes = {}
local icon_theme_paths = {}
if icon_theme and type(icon_theme) == 'table' then
icon_themes = icon_theme
icon_themes = {}
for k,v in pairs(icon_theme) do
icon_themes[k] = v
end
elseif icon_theme then
icon_themes = { icon_theme }
end
table.insert(icon_themes, 'hicolor')
for i, theme in ipairs(icon_themes) do
for j, path in ipairs(all_icon_paths) do
table.insert(icon_theme_paths, path .. theme .. '/')
if directory_exists(path .. theme) then
table.insert(icon_theme_paths, path .. theme .. '/')
end
end
-- TODO also look in parent icon themes, as in freedesktop.org specification
end
table.insert(icon_theme_paths, '/usr/share/icons/hicolor/') -- fallback theme cf spec

local isizes = icon_sizes
local isizes = {}
for i, sz in ipairs(all_icon_sizes) do
table.insert(isizes, sz)
end

for i, icon_theme_directory in ipairs(icon_theme_paths) do
for j, size in ipairs(arg.icon_sizes or isizes) do
for k, icon_type in ipairs(all_icon_types) do
table.insert(icon_path, icon_theme_directory .. size .. '/' .. icon_type .. '/')
if directory_exists(icon_theme_directory .. size) then
for k, icon_type in ipairs(all_icon_types) do
local p = icon_theme_directory .. size .. '/' .. icon_type
if directory_exists(p) then
table.insert(icon_path, p .. '/')
end
end
end
end
end
-- lowest priority fallbacks
-- lowest priority fallback
table.insert(icon_path, '/usr/share/pixmaps/')
table.insert(icon_path, '/usr/share/icons/')
table.insert(icon_path, '/usr/share/app-install/icons/')

for i, directory in ipairs(icon_path) do
if (arg.icon:find('.+%.png') or arg.icon:find('.+%.xpm')) and file_exists(directory .. arg.icon) then
Expand Down Expand Up @@ -253,3 +286,4 @@ function parse_dirs_and_files(arg)
return files
end

_init_all_icon_paths()

0 comments on commit 0d94177

Please sign in to comment.