Skip to content

Commit

Permalink
- Added locale support, via File::DesktopEntry.
Browse files Browse the repository at this point in the history
When `File::DesktopEntry` is installed ( https://metacpan.org/pod/File::DesktopEntry ), `obmenu-generator` will use it to resolve the locale name of each menu entry.

- Removed the `name_keys` config option. The correct name key is now automatically resolved by "File::DesktopEntry".
- "Linux::DesktopFiles" >= 0.23 is now required.
- Minor internal simplifications.
  • Loading branch information
trizen committed Oct 21, 2017
1 parent 0f15af9 commit 3564424
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions obmenu-generator
Expand Up @@ -24,18 +24,18 @@
# Name: obmenu-generator
# License: GPLv3
# Created: 25 March 2011
# Latest edit: 04 October 2017
# Latest edit: 21 October 2017
# https://github.com/trizen/obmenu-generator

use 5.014;

#use strict;
#use warnings;

require Linux::DesktopFiles; # >= 0.09
require Linux::DesktopFiles; # >= 0.23

my $pkgname = 'obmenu-generator';
my $version = '0.81';
my $version = '0.82-alpha';

our ($CONFIG, $SCHEMA);
my $output_h = \*STDOUT;
Expand Down Expand Up @@ -109,8 +109,8 @@ my $config_help = <<"HELP";
| substitutions : Substitute, by using a regex, in the values of the desktop files.
Example: [
{key => 'Exec', re => qr/xterm/, value => 'sakura'},
{key => 'Exec', re => qr/\\\\\\\\/, value => '\\\\', global => 1}, # for wine apps
{key => 'Exec', re => qr/xterm/, value => 'sakura', global => 1},
{key => 'Name', re => qr/^GNU I\w+ M\w+ P\w+/, value => 'GIMP'},
],
|| ICON SETTINGS
Expand All @@ -120,10 +120,6 @@ my $config_help = <<"HELP";
| generic_fallback : Try to shorten icon name at '-' characters before looking at inherited themes. (default: 0)
| force_icon_size : Always get the icon scaled to the requested size. (default: 0)
|| KEYS
| name_keys : Valid keys for application name.
Example: ['Name[fr]', 'GenericName[fr]', 'Name'], # french menu
|| PATHS
| desktop_files_paths : Absolute paths which contain .desktop files.
Example: [
Expand Down Expand Up @@ -242,7 +238,6 @@ my %CONFIG = (

},

name_keys => ['Name'],
terminal => 'xterm',
editor => 'geany',
missing_icon => 'gtk-missing-image',
Expand Down Expand Up @@ -310,8 +305,6 @@ else {
warnings.pm
warnings/register.pm
Tie/Hash.pm
Carp.pm
Exporter.pm
)
} = ();
}
Expand All @@ -321,8 +314,7 @@ my $desk_obj = Linux::DesktopFiles->new(

categories => [map $_->{cat}[0], grep exists $_->{cat}, @$SCHEMA],

keys_to_keep => [@{$CONFIG{name_keys}},
'Exec',
keys_to_keep => ['Name', 'Exec',
($with_icons ? 'Icon' : ()),
(
defined($CONFIG{'Linux::DesktopFiles'}{skip_entry})
Expand Down Expand Up @@ -500,18 +492,36 @@ foreach my $file ($desk_obj->get_desktop_files) {
my $cache_ok = (%info and $info{__MTIME__} == $mtime);

if (not $cache_ok) {
$desk_obj->parse(\my %cats, $file);

%info = %cats ? (%{(values %cats)[0][0]}, __MTIME__ => $mtime) : do {
$cache_db{$file} = join("\0\1\0", __IGNORE__ => 1);
next;
my %entry = $desk_obj->parse_desktop_file($file);

%info = (
%entry
? (
Name => $entry{Name},
Exec => $entry{Exec},

(
$with_icons
? (Icon => check_icon($entry{Icon}))
: ()
),

__CATEGORIES__ => join(';', @{$entry{Categories}}),
__MTIME__ => $mtime,
)
: do {
$cache_db{$file} = join("\0\1\0", __IGNORE__ => 1);
next;
}
);

eval {
require Encode;
require File::DesktopEntry;
my $fde = File::DesktopEntry->new($file);
$info{Name} = Encode::encode_utf8($fde->get('Name'));
};

if ($with_icons) {
$info{Icon} = check_icon($info{Icon});
}

$info{__CATEGORIES__} = join(';', keys %cats);
$cache_db{$file} = join("\0\1\0", %info);
}

Expand All @@ -537,16 +547,9 @@ foreach my $schema (@$SCHEMA) {
map [lc($_) => $_],
map {

my $name;
my $name = $_->{Name};
my $exec = $_->{Exec};

foreach my $key (@{$CONFIG{name_keys}}) {
if ($_->{$key}) {
$name = $_->{$key};
last;
}
}

# encode the entities
$name =~ tr/"&_//
&& $name =~ s/([&"_])/$entities{$1}/g;
Expand Down

0 comments on commit 3564424

Please sign in to comment.