This file is indexed.

/usr/bin/fvwm-menu-desktop is in fvwm 1:2.6.7-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#!/usr/bin/python
 
# Modification History

# Changed on 16/12/31 by Jaimos Skriletz:
# - Added check for FVWM_USERDIR env variable.
# - Added check for python-xdg module to print less errors if not found.
# - Added option -e/--menu-error to output phython-xdg not found as
#   a menu for the default-config.

# Changed on 16/10/27 by Jaimos Skrietz:
# - Renamed default menu to XDGMenu and changed the name of the
#   FvwmForm to FvwmForm-XDGMenu-Config
# - Modified the FvwmForm and added the abilty to load defaults from
#   the Form's data file.
# - Changed default to generate menu titles. Disable with --without-titles
# - The top level menu now has two additional items:
#     'Regenerate' - Regenerates menu.
#     'Configure' - Opens up FvwmForm-XDGMenu-Config.
# - Added --regen-cmd "CMD" for a fvwm CMD to use on the Regenerate item.
#   Default: PipeRead `fvwm-menu-desktop`
# - Added --include-items [config|regenerate|both|none] option
#   to control if the additional items are included in the menu.
# - Added --dynamic option to be used with dynamic menus.
# - Added --all-menus option to generate all menus and not try to determine
#   which one is best
# - Changed default behavior to include menu titles.
# - Added new option --without-titles

# Changed on 25/02/14 by Thomas Funk:
# - Converting of icons always to png

# Changed on 06/10/13 by Thomas Funk:
# Some Bugfixes:
# - DecodeEncodeErrors in menu names
# - no output appears with 'fvwm-menu-desktop --get-menus all|desktop'
# - No entry "Regenerate XDG menu(s)" appears with
#   'fvwm-menu-desktop --insert-in-menu MenuRoot'
# - exchange all tabs with spaces to prevent indention errors
# - add two new options: --app-icon --dir-icon
#   to handle default icons for not available app/dir icons
# - fix bug in convert icon routine that background of svg icons are
#   transparent

# Changed on 15/06/13 by Thomas Funk:
# support for python-xdg > 0.19.
# add gettext localization.

# Changed on 10/01/12 by Thomas Funk:
# Unicode support.

# Changed on 01/26/12 by Dan Espen (dane):
# Make compatible with fvwm-menu-desktop.
# Restored DestroyMenu, needed for reload menus.
# Remove bug, was printing iconpath on converted icons
# Replace obsolete optparse, use getopt instead
# Change from command line arg for applications.menu
# change to using ?$XDG_MENU_PREFIX or theme? fixme
# - use "Exec exec" for all commands, remove option.

# fixme, fix documentation, FvwmForm-Desktop, usage prompt is wrong
# change, mini icons are enabled by default.
# there are rescalable icons.

# Author: Piotr Zielinski (http://www.cl.cam.ac.uk/~pz215/)
# Licence: GPL 2
# Date: 03.12.2005

# This script takes names of menu files conforming to the XDG Desktop
# Menu Specification, and outputs their FVWM equivalents to the
# standard output.
#
#   http://standards.freedesktop.org/menu-spec/latest/

# This script requires the python-xdg module, which in Debian can be
# installed by typing
#
#   apt-get install python-xdg
#
# On Fedora, python-xdg is installed by default.

import sys
import getopt
import os.path
import os
import fnmatch
import time

# Test for python-xdg
try:
    import xdg.Menu
except ImportError:
    xdg_import_error = True
else:
    xdg_import_error = False
    import xdg.IconTheme
    import xdg.Locale
    from xdg.DesktopEntry import *
    from xdg.BaseDirectory import *


# Main Function
def main ():

    description = """
Generate Fvwm Menu from xdg files.
Standard output is a series Fvwm commands."""

    obs_args=['check-app',
              'enable-style',
              'enable-tran-style',
              'fvwm-icons',
              'kde_config',
              'mini-icon-path',
              'merge-user-menu',
              'su_gui',
              'utf8',
              'wm-icons']
    dashed_obs_args=[]
    for a in obs_args :
        dashed_obs_args.append('--'+a)

    obs_parms=['check-icons',
               'check-mini-icon',
               'destroy-type',
               'dir',
               'icon-app',
               'icon-folder',
               'icon-style',
               'icon-title',
               'icon-toptitle',
               'icons-path',
               'lang',
               'menu-style',
               'name',
               'png-icons-path',
               'submenu-name-prefix',
               'time-limit',
               'tran-icons-path',
               'tran-mini-icons-path',
               'type',
               'uniconv-exec',
               'uniconv',
               'xterm']
    equaled_obs_parms=[]
    for a in obs_parms :
        equaled_obs_parms.append(a+'=')
    dashed_obs_parms=[]
    for a in obs_parms :
        dashed_obs_parms.append('--'+a)

    try:
        opts, args = getopt.getopt(sys.argv[1:], "hs:t:vwe",
                                   ["help", "verbose", "enable-mini-icons", "with-titles", "without-titles", "version",
                                    "desktop=", "size=", "theme=", "install-prefix=", "menu-type=", "regen-cmd=",
                                    "title=", "get-menus=", "set-menus=", "insert-in-menu=", "mini-icon-dir=", "menu-error",
                                    "app-icon=", "dir-icon=", "include-items=", "dynamic", "all-menus"]+obs_args+equaled_obs_parms)
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        print usage
        sys.exit(2)
    global verbose, force, size, current_theme, icon_dir, top, install_prefix, menu_type, menu_list_length 
    global with_titles, menu_entry_count, get_menus, timestamp, set_menus, printmode, insert_in_menu, previous_theme
    global default_app_icon, default_dir_icon, include_items, config_menus, regen_cmd, dynamic_menu, build_all_menus
    version = "2.3.3"
    verbose = False
    force = False
    desktop=''
    size=24
    current_theme='gnome'
    previous_theme='gnome'
    icon_dir="~/.fvwm/icons"
    top='XDGMenu'
    insert_in_menu = False
    install_prefix = ''
    menu_type = ''
    with_titles = True
    menu_entry_count = 0
    menu_list_length = 0
    get_menus = ''
    printmode = True
    set_menus = []
    build_all_menus = False
    config_menus = []
    default_app_icon = "gnome-applications"
    default_dir_icon = "gnome-fs-directory"
    include_items = 'both'
    regen_cmd = 'PipeRead `fvwm-menu-desktop`'
    dynamic_menu = False
    menu_error = False

    # Loads config options from $FVWM_USERDIR/.FvwmForm-XDGMenu-Config
    if "FVWM_USERDIR" in os.environ:
        config_file = "%s/.FvwmForm-XDGMenu-Config" % os.environ['FVWM_USERDIR']
    else:
        config_file = "%s/.fvwm/.FvwmForm-XDGMenu-Config" % os.environ['HOME']
    if os.path.isfile(config_file):
        fvwmform_config = open(config_file, "r")

        for l in fvwmform_config:
            o = l.split()
            if len(o)>2 and o[0] != '#':
                if o[1][:3] == 'MEN' and o[2] == 'on':
                    config_menus.append(o[1][3:])
                if o[1] == 'IconsOn' and o[2] == 'on':
                    force = True
                elif o[1] == 'Size':
                    size = o[2]
                elif o[1] == 'TitlesOn' and o[2] == 'on':
                    with_titles = True
                elif o[1] == 'Theme':
                    current_theme = o[2]
                elif o[1] == 'Title':
                    top = o[2]
                elif o[1] == 'InsertInto':
                    top = o[2]
                    insert_in_menu = True
                elif o[1] == 'Installprefix':
                    install_prefix = o[2]
                elif o[1] == 'IconDir':
                    icon_dir = o[2]
                elif o[1] == 'DirIcon':
                    default_dir_icon = o[2]
                elif o[1] == 'AppIcon':
                    default_app_icon = o[2]
                elif o[1] == 'IncludeConfig' and o[2] == 'on':
                    include_items = "config"
                elif o[1] == 'IncludeRegen' and o[2] == 'on':
                    include_items = "regenerate"
                elif o[1] == 'IncludeBoth' and o[2] == 'on':
                    include_items = "both"
                elif o[1] == 'IncludeNone' and o[2] == 'on':
                    include_items = "none"
        fvwmform_config.close()

    for o, a in opts:
        if o in ("-v", "--verbose"):
            verbose = True
            if os.path.isfile(config_file):
                vprint("Defaults loaded from %s\n" % config_file)
            else:
                vprint("Config file not found: %s\nUsing built-in defaults.\n" % config_file)
        elif o in ("-h", "--help") :
            print usage
            sys.exit()
        elif o in ("--version") :
            print "fvwm-menu-desktop version " + version
            sys.exit()
        elif o in ("-e", "--menu-error") :
            menu_error = True
        elif o in ("--enable-mini-icons") :
            force=True
        elif o in ("--insert-in-menu") :
            top=a
            insert_in_menu = True
        elif o in ("--desktop") :
            desktop=a
        elif o in ("-t", "--title") :
            top=a
        elif o in ("--get-menus") :
            if a == 'all' or a == 'desktop' :
                get_menus=a
                printmode = False
            else :
                sys.stderr.write( "--get-menus argument must be 'all' or 'desktop' found "+a )
                print usage
                sys.exit(1)
        elif o in ("-s","--size") :
            size = int(a)
        elif o in ("--mini-icon-dir") :
            icon_dir = a
        elif o in ("--set-menus") :
            if a[-1] == ' ':
                a = a[:-1]
            set_menus=a.split(' ')
        elif o in ("--install-prefix") :
            if a and not os.path.isabs(a):
                assert False, "install-prefix must be an absolute path"
            # add trailing slash if not there already
            if not a[-1] == '/' : # trailing slash
                a=a + '/'
            install_prefix = a
        elif o in ("--theme") :
            current_theme = a
        elif o in ("--menu-type") :
            menu_type = a
        elif o in ("-w", "--with-titles") :
            with_titles = True
        elif o in ("--without-titles") :
            with_titles = False
        elif o in ("--app-icon") :
            default_app_icon = a
        elif o in ("--dir-icon") :
            default_dir_icon = a
        elif o in ("--include-items") :
            if a in ("both", "none", "config", "regenerate") :
                include_items = a
            else:
                sys.stderr.write( "--include-items argument must be 'config', 'regenerate', 'both' or 'none' found "+a )
                print usage
                sys.exit(1)
        elif o in ("--regen-cmd") :
            regen_cmd = a
        elif o in ("--dynamic") :
            dynamic_menu = True
        elif o in ("--all-menus") :
            build_all_menus = True
        elif o in (str(dashed_obs_args+dashed_obs_parms)) :
            # Ignore
            sys.stderr.write( "Warning: Arg "+o+" is obsolete and ignored\n" )
        else:
            assert False, "unhandled option"

    # Exit if python-xdg not found
    if xdg_import_error:
        if menu_error:
            printtext('DestroyMenu "%s"' % top)
            printtext('AddToMenu "%s" "%s" Title' % (top, top))
            printtext('+ "Error: python-xdg not found" Nop')
            printtext('+ "" Nop')
            printtext('+ "Regenerate" PipeRead `fvwm-menu-desktop -e`')
        else:
            sys.stderr.write('Python module python-xdg not found.')
        sys.exit(1)


    timestamp = time.time()
    
    if len(set_menus) == 0:
        xdg_menu_prefix = ((os.environ['XDG_MENU_PREFIX'] if os.environ.has_key('XDG_MENU_PREFIX') else ''))
    
        # First check if no user presettings made
        if desktop == '':
            # check if $XDG_MENU_PREFIX is set
            if not xdg_menu_prefix == '':
                desktop = xdg_menu_prefix.replace('-', '').lower()
        
        vprint("Parameters for creating menu list:")
        vprint(" XDG_MENU_PREFIX:  \'%s\'" %xdg_menu_prefix)
        vprint(" --install-prefix: \'%s\'" %install_prefix)
        vprint(" --desktop:        \'%s\'" %desktop)
        vprint(" --menu-type:      \'%s\'" %menu_type)
        
        vprint("\nStart search ...")
        menulist, desktop_temp = getmenulist(desktop, menu_type)
        if not desktop_temp == '':
            desktop = desktop_temp

    else:
        menulist = set_menus
    
    vprint(" Menu list: %s\n" %menulist)
    menu_list_length =  len(menulist)       
        
    if menu_list_length == 0:
        if not desktop == '':
            desktop = desktop + '-'
	if menu_error:
            printtext('DestroyMenu "%s"' % top)
            printtext('AddToMenu "%s" "%s" Title' % (top, top))
            printtext('+ "Error: No menus found" Nop')
            printtext('+ "" Nop')
            printtext('+ "Regenerate" PipeRead `fvwm-menu-desktop -e`')
        else:
            sys.stderr.write(install_prefix+desktop+menu_type+".menu not available on this system. Exiting...\n")
        sys.exit(1)
    else:
        # set previous_theme if <icon_dir>/.theme exist
        if os.path.exists(os.path.join(os.path.expanduser(icon_dir), ".theme")):
            previous_theme = next(open(os.path.join(os.path.expanduser(icon_dir), ".theme"), 'r')).replace('\n', '')
        vprint(" Previous used theme: %s" %previous_theme)
        vprint(" Current used theme: %s" %current_theme)
        
        parsemenus(menulist, desktop)

    # write current_theme to <icon_dir>/.theme if --enable-mini-icons and printmode is set
    if printmode and force:
        fh = open(os.path.join(os.path.expanduser(icon_dir), ".theme"), "w")
        fh.write(current_theme)
        fh.close()
              
    vprint("\nProcess took " + str(time.time()-timestamp) + " seconds")
             
def getmenulist(desktop, menu_type):
    menudict = {} 
    config_dirs = []
    if not install_prefix == '':
        config_dirs = [install_prefix]
    else:
        config_dirs = xdg_config_dirs # xdg_config_dirs is a built-in list from python-xdg

    found_menus = 0
    for dir in config_dirs:
        if install_prefix == '':
            dir = os.path.join(dir, 'menus')
        # skipping all paths which not available
        if os.path.exists(dir):
            filelist = set([])
            dir_list = os.listdir(dir)
            #pattern = '*'+desktop+'*'+menu_type+'*.menu'
            # Always find all menus
            pattern = '*.menu'
            for filename in fnmatch.filter(dir_list, pattern):
                filelist.add(filename)
            
            # the menudict dictionary has a unsorted list (set) for the values.
            # set is easier to use then a list for removing items 
            menudict[dir] = filelist
            found_menus += len(filelist)
            vprint(" found in %s: %s" %(dir, list(filelist)))
    
    desktop_dict = {}
    if not found_menus == 0:
        all_menus = []
        # remove all menus in /etc/xdg/menus if exist in user dir
        for path in menudict.keys():
            if not path == '/etc/xdg/menus':
                if path == os.path.join(os.getenv("HOME"), '.config/menus'):
                    menudict['/etc/xdg/menus'] = menudict['/etc/xdg/menus'] - menudict[path]
                #else:
                #    menudict[path] = menudict[path] - menudict['/etc/xdg/menus']
                for menu in list(menudict[path]):
                    all_menus.append(path + '/' + menu)
        
        if not menudict['/etc/xdg/menus'] == 0:
            for menu in list(menudict['/etc/xdg/menus']):
                all_menus.append('/etc/xdg/menus/' + menu) 
         
        if get_menus == 'all' or (build_all_menus and desktop == '' and menu_type == ''):
            return all_menus, ''

        # get menus selected in config file
        if len(config_menus) > 0:
            config_menulist = []
            for i in config_menus:
                for j in all_menus:
                    if fnmatch.fnmatch( j, "*%s.menu" % i ):
                        config_menulist.append(j)
            vprint("\n Selected menus from config file: %s " % list(config_menulist))
            # Use config file if --dekstop not set
            if len(config_menulist) == 0:
                vprint(" No menus in config found. Using all menus.")
            elif desktop == '' and menu_type == '':
                vprint(" Using menus from config file.")
                return config_menulist, ''
            else:
                vprint(" Ignoring menus in config file, due to --desktop or --menu-type.")
        
        # filter --desktop and --menu-type options
        if desktop != '' or menu_type != '':
            vprint("\n Filtering menus according to --desktop %s and --menu-type %s" % (desktop, menu_type) )
            pattern = '*'+desktop+'*'+menu_type+'*'
            for path in menudict.keys():
                for menu in list(menudict[path]):
                    if not fnmatch.fnmatch( menu, pattern ):
                        menudict[path].remove(menu)
                if menudict[path] == set([]):
                    del menudict[path]
            if menudict == {}:
                sys.stderr.write("No menus found matching --desktop %s and --menu-type %s. Exiting...\n" % (desktop, menu_type) )
                sys.exit(1)

        vprint("\n Finding best menu in Menu List: %s" % menudict )
        if build_all_menus:
            all_menus = []
            for key in menudict:
                for i in menudict[key]:
                  all_menus.append(key+'/'+i)
            return all_menus, ''

        # sort menus related to desktops and create a weighting
        vprint("\n DE weighting search: DE => [user menus, system menus, overall]")
        weight_dict = {}
        if desktop == '':
            # first the desktops, then debian (shouldn't appear in others) then others holding
            # all other non DE menus e.g. tools and at the end the nones without prefixes
            # If there're other prefixes from other WMs - should be added BEFORE debian
            DEs = ['gnome', 'kde', 'xfce', 'lxde', 'cinnamon', 'mate', 'debian', 'others', 'none']
        else:
            DEs = [desktop]
        for de in DEs:
            menus = set([])
            user_menus = 0
            system_menus = 0
            filled = False
            for path in menudict.keys():
                if de == 'none':
                    pattern = '*'
                elif de == 'others':
                    pattern = '*-*'
                else:
                    pattern = '*'+de+'*'
                # fnmatch.filter returns a list of files the pattern match
                menu_names = fnmatch.filter(menudict[path], pattern)
                if not len(menu_names) == 0:
                    filled = True
                for name in menu_names:
                    menus.add(path+'/'+name)
                # delete each found DE menu from the actual path. So, the menus will be reduced loop by loop.
                menudict[path] = menudict[path]-set(menu_names)
                # count the menus found in the users and systems menu path for later weighting
                if not path == '/etc/xdg/menus':
                    user_menus = len(menu_names)
                else:
                    system_menus = len(menu_names)
            if filled:
                desktop_dict[de] = menus
                filled = False
            # fill the weight dictionary with the counts
            weight_dict[de] = [user_menus, system_menus, user_menus+system_menus]
            vprint(" %s => %s" %(de, weight_dict[de]))
    
        # get the highest rated desktop
        highest = 0
        de_highest = ''
        for de in sorted(weight_dict.keys()):
            de_user = weight_dict[de][0]
            de_system = weight_dict[de][1]
            de_total = weight_dict[de][2]
            higher = False
            if not de_highest == '':
                # don't weight 'none' and 'others cause both not DEs
                if not de == 'none' and not de == 'others':
                    highest_user = weight_dict[de_highest][0]
                    highest_system = weight_dict[de_highest][1]
                    highest_total = weight_dict[de_highest][2]
                    # first compare the total counts
                    if highest < de_total:
                        higher = True
                    elif highest == de_total:
                        # if the totals equal compare the users
                        if highest_user < de_user:
                            higher = True
                        elif highest_user == de_user:
                            # it the users equal compare the system menus
                            if highest_system < de_system:
                                higher = True
                            # if the systems equal the last wins
                            elif highest_system == de_system:
                                higher = True # fixme, should be biunique. -but how? With atime?
            else:
                higher = True
                
            if higher:
                highest = de_total
                de_highest = de

	if highest == 0 :	# no dev environments?
	    de_highest = 'others' # use 'others'
        vprint( "\n Winner: %s" %de_highest)
        
        # Perhaps there're a global menus available which are not in the highest rated list
        if desktop_dict.has_key('none'):
            for menu in desktop_dict['none']:
                name = menu.replace('.menu', '').split('/')
                # the fnmatch.filter will be used to find NO match because then
                # the menu is not in the list
                found = fnmatch.filter(desktop_dict[de_highest], '*'+name[-1]+'*')
                if found == []:
                    desktop_dict[de_highest].add(menu)
        
        # Add 'others' menus to list, because these could be tool menus like yast, etc
        if desktop_dict.has_key('others'):
            for menu in desktop_dict['others']:
                desktop_dict[de_highest].add(menu)
                
    if len(desktop_dict) == 0:
        return [], ''
    else:
        return list(desktop_dict[de_highest]), de_highest

def vprint(text):
    if verbose:
        sys.stderr.write(text+"\n")
    
def printtext(text, encodetext=True):
    if encodetext:
        print text.encode("utf-8")
    else:
        print text

# fix unicode decode problem like
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position xyz: ordinal not in range(128) 
def decodetext(text):
    old_stdout = sys.stdout
    fnull = open(os.devnull, 'w')
    sys.stdout = fnull
    try:
        new_text = text.encode("utf-8")
        printtext('%s' % text)
    except UnicodeDecodeError:
        try:
            unicode_text = text.decode("iso-8859-15")
            text = "%s" % unicode_text
            new_text = text.encode("iso-8859-15")
            print new_text
        except:
            new_text = None
    fnull.close()
    sys.stdout = old_stdout
    return new_text
    
def geticonfile(icon):
    iconpath = xdg.IconTheme.getIconPath(icon, size, current_theme, ["png", "xpm", "svg"])

    if not iconpath == None:
        extension = os.path.splitext(iconpath)[1]

    if not iconpath:
        return None

    if not force:
        return iconpath
    
    if iconpath.find("%ix%i" % (size, size)) >= 0: # ugly hack!!!
        return iconpath

    if not os.path.isdir(os.path.expanduser(icon_dir)):
        os.makedirs(os.path.expanduser(icon_dir))

    iconfile = os.path.join(os.path.expanduser(icon_dir),
                            "%ix%i-" % (size, size) + 
                            os.path.basename(iconpath))

    if os.path.exists(iconpath):
        iconfile = iconfile.replace(extension, '.png')
        if extension == '.svg':
            os.system("if test \\( \\( ! -f '%s' \\) -o \\( '%s' -nt '%s' \\) \\) -o \\( '%s' != '%s' \\); then convert -background none '%s' -resize %i '%s' ; fi"% 
                          (iconfile, iconpath, iconfile, current_theme, previous_theme, iconpath, size, iconfile))
        else:
            os.system("if test \\( \\( ! -f '%s' \\) -o \\( '%s' -nt '%s' \\) \\) -o \\( '%s' != '%s' \\); then convert '%s' -resize %i '%s' ; fi"% 
                          (iconfile, iconpath, iconfile, current_theme, previous_theme, iconpath, size, iconfile))
    else:
        sys.stderr.write("%s not found! Using default icon.\n" % iconpath)
        return None
    return iconfile

    
def getdefaulticonfile(command):
    if command.startswith("Popup"):
        return geticonfile(default_dir_icon)
    else:
        return geticonfile(default_app_icon)    

def printmenu(name, icon, command):
    iconfile = ''
    if force :
        iconfile = geticonfile(icon) or getdefaulticonfile(command) or icon
        if not (iconfile == '' or iconfile == None):
            iconfile = '%'+iconfile+'%'
        else:
            sys.stderr.write("%s icon or default icon not found!\n")
    name = decodetext(name)
    command = decodetext(command)
    iconfile = decodetext(iconfile)
    if not (name == None or command == None or iconfile == None):
        printtext('+ "%s%s" %s' % (name, iconfile, command), False)
    else:
        sys.stderr.write("A menu entry cannot decode! Skipping ...\n")
        printtext('# Decode Error! Menu entry skipped.')

def parsemenus(menulist, desktop):
    global menu_entry_count
    if menu_list_length == 1:
        new_menulist = menulist
        # user defines only one special menu
        parsemenu(xdg.Menu.parse(menulist[0]), top)
    else:
        # create a top title list
        top_titles = []
        for file in menulist:
            # extract and split the filename and set first char of each word to capital
            name_parts = file.replace('.menu', '').split('/')[-1].split('-')
            name_parts = [name[0].replace(name[0], name[0].upper())+name[1:] for name in name_parts]
            top_titles.append(' '.join(name_parts))
        
        # create the submenus
        new_toptitles = []
        new_menulist = []
        for title, menu in zip(top_titles, menulist):
            name = 'Fvwm'+title
            vprint("Create submenu \'%s\' from \'%s\'" %(name, menu))
            parsemenu(xdg.Menu.parse(menu), name, title)
            # remove a menu if no menu entry was created in its sub menus
            if not menu_entry_count == 0:
                new_toptitles.append(title)
                new_menulist.append(menu)
                menu_entry_count = 0
            else:
                vprint(" Menu is empty - won't be used!")
        
        # create the root menu
        if printmode:
            if not insert_in_menu:
                if dynamic_menu:
                    printtext('DestroyMenu recreate "%s"' % top)
                else:
                    printtext('DestroyMenu "%s"' % top)
            if with_titles and not insert_in_menu:
                printtext('AddToMenu "%s" "%s" Title' % (top, top))
            else:
                printtext('AddToMenu "%s"' % top)
    
            for title in sorted(new_toptitles):
                name = 'Fvwm'+title
                printmenu(title, '', 'Popup "%s"' % name)

            if include_items != 'none':
                printtext('+ "" Nop')
                if include_items in ("both", "regenerate"):
                    printmenu("$[gt.Regenerate]", "system-software-update", regen_cmd )
                if include_items in ("both", "config"):
                    printmenu("$[gt.Configure]", "system-software-update", "Module FvwmPerl -l fvwm-menu-desktop-config.fpl" )

    if not get_menus == '':
        printtext('%s' % ' '.join(new_menulist))

def parsemenu(menu, name="", title=""):
    global menu_entry_count
    m = re.compile('%[A-Z]?', re.I) # Pattern for %A-Z (meant for %U)
    if not name :
        name = menu.getPath()
    if not title:
        title = name
    if printmode:
        name = decodetext(name)
        title = decodetext(title)
        if not (name == None or title == None):
            if not insert_in_menu or not (insert_in_menu and name == top and menu_list_length == 1):
                if name == top and dynamic_menu:
                    printtext('DestroyMenu recreate "%s"' % name, False)
                else:
                    printtext('DestroyMenu "%s"' % name, False)
            if with_titles:
                # for insert-in-menu AddToMenu doesn't have a title for top menu
                # because this will appear then in the other menu
                if insert_in_menu and name == top and menu_list_length == 1:
                    printtext('AddToMenu "%s"' % name, False)
                else:
                    printtext('AddToMenu "%s" "%s" Title' % (name, title), False)
            else:
                printtext('AddToMenu "%s"' % name, False)
        else:
            sys.stderr.write("A menu entry cannot decode! Skipping ...\n")
            printtext('# Decode Error! Menu entry skipped.')
    for entry in menu.getEntries():
        if isinstance(entry, xdg.Menu.Menu):
            if printmode:
                printmenu(entry.getName(), entry.getIcon(), 'Popup "%s"' % entry.getPath())
        elif isinstance(entry, xdg.Menu.MenuEntry):
            if printmode:
                desktop = DesktopEntry(entry.DesktopEntry.getFileName())
                # eliminate '%U' etc behind execute string
                execProgram = m.sub('', desktop.getExec())
                printmenu(desktop.getName(), desktop.getIcon(), "Exec exec " + " " + execProgram)
            menu_entry_count += 1
        else:
            if printmode:
                printtext('# not supported: ' + str(entry))

    if printmode:
        # should only appear in a single menu. For more it will insert in parsemenus() when the top menu will built
        if menu_list_length == 1 and name == top and include_items != 'none':
            printtext('+ "" Nop')
            if include_items in ("both", "regenerate"):
                printmenu("$[gt.Regenerate]", "system-software-update", regen_cmd )
            if include_items in ("both", "config"):
                printmenu("$[gt.Configure]", "system-software-update", "Module FvwmPerl -l fvwm-menu-desktop-config.fpl" )
        printtext('')

    for entry in menu.getEntries():
        if isinstance(entry, xdg.Menu.Menu):
            parsemenu(entry)

usage="""
A script which parses xdg menu definitions to build
the corresponding fvwm menus.

Usage: $0 [OPTIONS]
Options:
    -h, --help                show this help and exit.
    --version                 show version and exit.
    --install-prefix DIR      install prefix of the desktop menu files.
                              Per default not set. For system wide menus
                              use /etc/xdg/menus/.
    --desktop NAME            use menus that include NAME in the file name:
                              gnome, kde, xfce, lxde, debian, etc.
    --menu-type NAME          use menus that include NAME in the file name:
                              applications, settings, preferences, etc. When
                              used with --desktop only menus whose file name
                              mathces '*desktop*menutype*' are used.
    --theme NAME              icon theme: gnome (default), oxygen, etc. Don't
                              use hicolor. It's the default fallback theme if
                              no icon is found.
    -w, --with-titles         generate menus with titles. Default.
    --without-titles          generate menus without titles.
    --enable-mini-icons       enable mini-icons in menu.
    -s, --size NUM            set size of mini-icons in menu. Default is 24.
    --mini-icon-dir DIR       set directory for mini-icons.
                              Default is ~/.fvwm/icons.
    --app-icon NAME           set default application icon if no others found.
                              Default is 'gnome-applications'.
    --dir-icon NAME           set default directory icon if no others found.
                              Default is 'gnome-fs-directory'.
    -t, --title NAME          menu title of the top menu used by Popup command.
                              Default is XDGMenu.
    --insert-in-menu NAME     generates a menu to place it in the root level
                              of the menu NAME.
    --get-menus all|desktop   prints a space separated list of full menu paths.
                              'all' is all menus on the system except empty
                              ones. 'desktop' list the menus that would have been
                              generated. No menu generation is done.
    --set-menus menu_paths    expects a space separated list of full menu paths
                              to generate user specified menus.
    --all-menus               generate all menus found.
    --include-items NAME      include additional menu items NAME in top level
                              menu. NAME can be 'config', 'regenerate', 'both'
                              or 'none'. Default both.
    --regen-cmd ACTION        The fvwm ACTION for the 'Regenerate' menu item.
                              Default: 'PipeRead `fvwm-menu-desktop`'
    --dynamic                 used with dynamic menus.
    -e, --menu-error          out python-xdg not found error in menu.
    -v, --verbose             run and display debug info on STDERR."""

if __name__ == "__main__":
    main()

# Local Variables:
# mode: python
# compile-command: "python fvwm-menu-desktop.in --enable-mini-icons --with-titles"
# End: