--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- Asynchronous XDG data aggregator
local menu_utils = require("menubar.utils")
local menu_gen = require("menubar.menu_gen")
local awful = require("awful")
local gears = require("gears")
menu_utils.wm_name = ""

-- Directories to scan for .desktop files
local desktop_dirs = {}
local desktop_dirs_complete = 0
local icon_dirs = {}
((table.concat(gears.filesystem.get_xdg_data_dirs(),":") or 
 "/usr/share:/usr/local/share")..":"..os.getenv("HOME").."/.local/share"):gsub("[^:]*",function(path)
    if gears.filesystem.dir_readable(path.."/applications") then
        table.insert(desktop_dirs, path.."/applications")
    end
    if gears.filesystem.dir_readable(path.."/icons") then
        table.insert(icon_dirs, path.."/icons")
    end
end)


-- Global xdg data cache 
xdg = {
    apps = {},
    categories = {
        Other = {
            icon = "applications-other",
            apps = {}
        },
        Wine = {
            icon = "wine",
            apps = {}
        }
    }
}

for k,v in pairs(menu_gen.all_categories) do
    xdg.categories[v.app_type] = {
        name = v.name,
        icon = v.icon_name,
        apps = {}
    }
end

-- Asynchronous scanning process
for k,v in pairs(desktop_dirs) do
    awful.spawn.with_line_callback("find "..tostring(v).." -name *.desktop",{
        stdout = function(line)
            local data = menu_utils.parse_desktop_file(line)
            if data.NoDisplay then
                return
            end
            local appdata = {
                name = data.Name,
                category = "Other",
                exec = data.Exec,
                icon = (data.Icon and menu_utils.lookup_icon(data.Icon)),
                description = data.Comment
            }
            -- Match first available cateogry for sorting
            for k,v in pairs(data.Categories or {"Other"}) do
                if xdg.categories[v] then
                    appdata.category = v
                    break
                end
                -- Oh how do I love those Wine applications and their categories
                if v:match("^Wine") then
                    appdata.category = "Wine"
                    break
                end
            end
            -- Open terminal apps in the terminal (duh)
            if data.Terminal then
                appdata.exec = global.terminal.." -e "..appdata.exec
            end
            -- Just for you, Wine - special case because you're being a shit
            if (exec and exec:find("%W?wine ")) then
                appdata.category = "Wine"
            end
            table.insert(xdg.apps,appdata)
            table.insert(xdg.categories[appdata.category].apps,appdata)
        end,
        output_done = function()
            -- Call a global signal 
            desktop_dirs_complete = desktop_dirs_complete + 1
            awesome.emit_signal("xdg::dir_finished",v)
        end
    })
end
awesome.connect_signal("xdg::dir_finished",function(dir)
    if desktop_dirs_complete == #desktop_dirs then
        awesome.emit_signal("xdg::all_finished")
        -- Clean up empty categories
        for k,v in pairs(xdg.categories) do
            if #v.apps == 0 then
                xdg.categories[k] = nil
            end
        end
    end
end)
