2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Menu generation module for menubar
|
2014-05-19 13:37:13 +02:00
|
|
|
--
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @author Antonio Terceiro
|
|
|
|
-- @copyright 2009, 2011-2012 Antonio Terceiro, Alexander Yakushev
|
2014-05-19 13:37:13 +02:00
|
|
|
-- @module menubar.menu_gen
|
2012-03-08 20:24:21 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Grab environment
|
2017-03-18 01:14:58 +01:00
|
|
|
local gtable = require("gears.table")
|
|
|
|
local gfilesystem = require("gears.filesystem")
|
2012-03-08 20:24:21 +01:00
|
|
|
local utils = require("menubar.utils")
|
|
|
|
local pairs = pairs
|
|
|
|
local ipairs = ipairs
|
|
|
|
local table = table
|
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
local menu_gen = {}
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
-- Options section
|
|
|
|
|
2017-03-18 01:14:58 +01:00
|
|
|
--- Get the path to the directories where XDG menu applications are installed.
|
|
|
|
local function get_xdg_menu_dirs()
|
|
|
|
local dirs = gfilesystem.get_xdg_data_dirs()
|
|
|
|
table.insert(dirs, 1, gfilesystem.get_xdg_data_home())
|
|
|
|
return gtable.map(function(dir) return dir .. 'applications/' end, dirs)
|
2014-06-30 15:08:43 +02:00
|
|
|
end
|
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Specifies all directories where menubar should look for .desktop
|
2016-02-26 22:19:35 +01:00
|
|
|
-- files. The search is recursive.
|
2017-03-18 01:14:58 +01:00
|
|
|
menu_gen.all_menu_dirs = get_xdg_menu_dirs()
|
2012-03-08 20:24:21 +01:00
|
|
|
|
2013-12-29 15:10:31 +01:00
|
|
|
--- Specify the mapping of .desktop Categories section to the
|
2012-03-08 20:24:21 +01:00
|
|
|
-- categories in the menubar. If "use" flag is set to false then any of
|
|
|
|
-- the applications that fall only to this category will not be shown.
|
2012-06-12 10:36:28 +02:00
|
|
|
menu_gen.all_categories = {
|
2012-03-08 20:24:21 +01:00
|
|
|
multimedia = { app_type = "AudioVideo", name = "Multimedia",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-multimedia", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
development = { app_type = "Development", name = "Development",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-development", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
education = { app_type = "Education", name = "Education",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-science", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
games = { app_type = "Game", name = "Games",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-games", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
graphics = { app_type = "Graphics", name = "Graphics",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-graphics", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
office = { app_type = "Office", name = "Office",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-office", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
internet = { app_type = "Network", name = "Internet",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-internet", use = true },
|
2019-01-21 18:58:44 +01:00
|
|
|
science = { app_type = "Science", name="Science",
|
|
|
|
icon_name = "applications-science", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
settings = { app_type = "Settings", name = "Settings",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-utilities", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
tools = { app_type = "System", name = "System Tools",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-system", use = true },
|
2012-03-08 20:24:21 +01:00
|
|
|
utility = { app_type = "Utility", name = "Accessories",
|
2015-09-21 10:17:20 +02:00
|
|
|
icon_name = "applications-accessories", use = true }
|
2012-03-08 20:24:21 +01:00
|
|
|
}
|
|
|
|
|
2022-07-05 10:37:14 +02:00
|
|
|
-- Find icons for category entries.
|
2012-06-12 10:36:28 +02:00
|
|
|
function menu_gen.lookup_category_icons()
|
|
|
|
for _, v in pairs(menu_gen.all_categories) do
|
2017-11-16 13:12:59 +01:00
|
|
|
v.icon = utils.lookup_icon(v.icon_name)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Get category key name and whether it is used by its app_type.
|
2012-03-08 20:24:21 +01:00
|
|
|
-- @param app_type Application category as written in .desktop file.
|
|
|
|
-- @return category key name in all_categories, whether the category is used
|
|
|
|
local function get_category_name_and_usage_by_type(app_type)
|
2012-06-12 10:36:28 +02:00
|
|
|
for k, v in pairs(menu_gen.all_categories) do
|
2012-03-08 20:24:21 +01:00
|
|
|
if app_type == v.app_type then
|
|
|
|
return k, v.use
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Generate an array of all visible menu entries.
|
2016-03-01 03:17:13 +01:00
|
|
|
-- @tparam function callback Will be fired when all menu entries were parsed
|
|
|
|
-- with the resulting list of menu entries as argument.
|
|
|
|
-- @tparam table callback.entries All menu entries.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct menubar.menu_gen.generate
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2016-03-01 03:17:13 +01:00
|
|
|
function menu_gen.generate(callback)
|
2012-03-08 20:24:21 +01:00
|
|
|
-- Update icons for category entries
|
2012-06-30 21:51:11 +02:00
|
|
|
menu_gen.lookup_category_icons()
|
2012-03-08 20:24:21 +01:00
|
|
|
|
|
|
|
local result = {}
|
2016-02-26 22:19:35 +01:00
|
|
|
local unique_entries = {}
|
2016-03-01 03:17:13 +01:00
|
|
|
local dirs_parsed = 0
|
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
for _, dir in ipairs(menu_gen.all_menu_dirs) do
|
2016-03-01 03:17:13 +01:00
|
|
|
utils.parse_dir(dir, function(entries)
|
|
|
|
entries = entries or {}
|
|
|
|
for _, entry in ipairs(entries) do
|
|
|
|
-- Check whether to include program in the menu
|
|
|
|
if entry.show and entry.Name and entry.cmdline then
|
|
|
|
local unique_key = entry.Name .. '\0' .. entry.cmdline
|
|
|
|
if not unique_entries[unique_key] then
|
|
|
|
local target_category = nil
|
|
|
|
-- Check if the program falls into at least one of the
|
|
|
|
-- usable categories. Set target_category to be the id
|
|
|
|
-- of the first category it finds.
|
|
|
|
if entry.categories then
|
|
|
|
for _, category in pairs(entry.categories) do
|
|
|
|
local cat_key, cat_use =
|
|
|
|
get_category_name_and_usage_by_type(category)
|
|
|
|
if cat_key and cat_use then
|
|
|
|
target_category = cat_key
|
|
|
|
break
|
|
|
|
end
|
2016-02-26 22:19:35 +01:00
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
2017-09-28 22:58:14 +02:00
|
|
|
|
|
|
|
local name = utils.rtrim(entry.Name) or ""
|
|
|
|
local cmdline = utils.rtrim(entry.cmdline) or ""
|
|
|
|
local icon = entry.icon_path or nil
|
|
|
|
table.insert(result, { name = name,
|
|
|
|
cmdline = cmdline,
|
|
|
|
icon = icon,
|
|
|
|
category = target_category })
|
|
|
|
unique_entries[unique_key] = true
|
2016-02-26 22:19:35 +01:00
|
|
|
end
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
2016-03-01 03:17:13 +01:00
|
|
|
dirs_parsed = dirs_parsed + 1
|
|
|
|
if dirs_parsed == #menu_gen.all_menu_dirs then
|
|
|
|
callback(result)
|
|
|
|
end
|
|
|
|
end)
|
2012-03-08 20:24:21 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:36:28 +02:00
|
|
|
return menu_gen
|
|
|
|
|
2012-03-08 20:24:21 +01:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|