menubar: look for .desktop files recursively

It was missing apps/entries from /usr/share/applications/kde4.

This patch also makes sure that entries are unique (by Exec/Name).

Closes https://github.com/awesomeWM/awesome/pull/711.
This commit is contained in:
Daniel Hahler 2016-02-26 22:19:35 +01:00
parent b84f214144
commit 25eff81878
2 changed files with 32 additions and 29 deletions

View File

@ -25,7 +25,7 @@ if not data_dir then
end end
--- Specifies all directories where menubar should look for .desktop --- Specifies all directories where menubar should look for .desktop
-- files. The search is not recursive. -- files. The search is recursive.
menu_gen.all_menu_dirs = { data_dir .. 'applications/', '/usr/share/applications/', '/usr/local/share/applications/' } menu_gen.all_menu_dirs = { data_dir .. 'applications/', '/usr/share/applications/', '/usr/local/share/applications/' }
--- Specify the mapping of .desktop Categories section to the --- Specify the mapping of .desktop Categories section to the
@ -83,40 +83,43 @@ local function trim(s)
end end
--- Generate an array of all visible menu entries. --- Generate an array of all visible menu entries.
-- @return all menu entries. -- @treturn table All menu entries.
function menu_gen.generate() function menu_gen.generate()
-- Update icons for category entries -- Update icons for category entries
menu_gen.lookup_category_icons() menu_gen.lookup_category_icons()
local result = {} local result = {}
local unique_entries = {}
for _, dir in ipairs(menu_gen.all_menu_dirs) do for _, dir in ipairs(menu_gen.all_menu_dirs) do
local entries = utils.parse_dir(dir) for _, entry in ipairs(utils.parse_dir(dir)) do
for _, program in ipairs(entries) do
-- Check whether to include program in the menu -- Check whether to include program in the menu
if program.show and program.Name and program.cmdline then if entry.show and entry.Name and entry.cmdline then
local target_category = nil local unique_key = entry.Name .. '\0' .. entry.cmdline
-- Check if the program falls at least to one of the if not unique_entries[unique_key] then
-- usable categories. Set target_category to be the id local target_category = nil
-- of the first category it finds. -- Check if the program falls into at least one of the
if program.categories then -- usable categories. Set target_category to be the id
for _, category in pairs(program.categories) do -- of the first category it finds.
local cat_key, cat_use = if entry.categories then
for _, category in pairs(entry.categories) do
local cat_key, cat_use =
get_category_name_and_usage_by_type(category) get_category_name_and_usage_by_type(category)
if cat_key and cat_use then if cat_key and cat_use then
target_category = cat_key target_category = cat_key
break break
end
end end
end end
end if target_category then
if target_category then local name = trim(entry.Name) or ""
local name = trim(program.Name) or "" local cmdline = trim(entry.cmdline) or ""
local cmdline = trim(program.cmdline) or "" local icon = entry.icon_path or nil
local icon = program.icon_path or nil table.insert(result, { name = name,
table.insert(result, { name = name, cmdline = cmdline,
cmdline = cmdline, icon = icon,
icon = icon, category = target_category })
category = target_category }) unique_entries[unique_key] = true
end
end end
end end
end end

View File

@ -240,12 +240,12 @@ function utils.parse(file)
return program return program
end end
--- Parse a directory with .desktop files --- Parse a directory with .desktop files recursively.
-- @param dir The directory. -- @tparam string dir The directory.
-- @return A table with all .desktop entries. -- @treturn table Paths of found .desktop files.
function utils.parse_dir(dir) function utils.parse_dir(dir)
local programs = {} local programs = {}
local files = io.popen('find '.. dir ..' -maxdepth 1 -name "*.desktop" 2>/dev/null') local files = io.popen('find '.. dir .." -name '*.desktop' 2>/dev/null")
for file in files:lines() do for file in files:lines() do
local program = utils.parse(file) local program = utils.parse(file)
if program then if program then