#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# fuss-launcher command line testing tool
#
# Copyright (C) 2010 Enrico Zini <enrico@truelite.it>
# Copyright (C) 2010 Christopher R. Gabriel <cgabriel@truelite.it>
# Copyright (C) 2010 The Fuss Project <info@fuss.bz.it>
#
# Authors: Christopher R. Gabriel <cgabriel@truelite.it>
#          Enrico Zini <enrico@truelite.it>
#
# Sponsored by the Fuss Project: http://www.fuss.bz.it/
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

from fusslauncher import cmdline
from fusslauncher import conf

parser = cmdline.Parser(usage="usage: %prog [options] menufile",
                description="Dump merged menu structures")
#parser.add_option("-q", "--quiet", action="store_true", help="quiet mode: only output fatal errors")
parser.add_option("-v", "--verbose", action="store_true", help="verbose mode")
parser.add_option("-d", "--debug", action="store_true", help="debug mode")
#parser.add_option("-u", "--uninstalled", action="store_true", help="debug mode")
(opts, args) = parser.parse_args()

import sys
import xdg.Menu
import gettext
_ = gettext.gettext

def count_entries(menu):
    res = 0
    for x in menu.getEntries():
        if hasattr(x, "DesktopEntry"):
            res += 1
    return res

def dump(menu, prefix=""):
    print "%s%s: %d leaf entries" % (prefix, menu.getName(), count_entries(menu))
    for e in menu.getEntries():
        if hasattr(e, "getEntries"):
            dump(e, prefix + " ")


gettext.bindtextdomain(conf.GETTEXT_PACKAGE)
gettext.textdomain(conf.GETTEXT_PACKAGE)

if not args:
    print >>sys.stderr, "Please specify one menu file"
    sys.exit(1)

if len(args) > 1:
    print >>sys.stderr, "Please specify only one menu file"
    sys.exit(1)

menu = xdg.Menu.parse(args[0])
dump(menu)

sys.exit(0)
