Skip to content

Commit

Permalink
Switched to Gtk2::IconTheme for locating icon files.
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Jul 18, 2017
1 parent ae2f0f3 commit 71b1333
Showing 1 changed file with 83 additions and 83 deletions.
166 changes: 83 additions & 83 deletions obmenu-generator
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@
# Latest edit on: 26 March 2017
# Website: https://github.com/trizen/obmenu-generator

#use 5.014;
use 5.014;

#use strict;
#use warnings;

require Linux::DesktopFiles; # >= 0.09

my $pkgname = 'obmenu-generator';
my $version = 0.71;
my $version = 0.99;

our ($CONFIG, $SCHEMA);
my $output_h = \*STDOUT;

my ($pipe, $static, $icons, $reconfigure, $update_config, $reconf_openbox);
my ($pipe, $static, $with_icons, $reconfigure, $update_config, $reconf_openbox);

my $home_dir =
$ENV{HOME}
Expand All @@ -56,7 +57,6 @@ my $schema_file = "$config_dir/schema.pl";
my $config_file = "$config_dir/config.pl";
my $openbox_conf = "$xdg_config_home/openbox";
my $menufile = "$openbox_conf/menu.xml";
my $icons_db = "$config_dir/icons.db";
my $cache_db = "$config_dir/cache.db";

sub usage {
Expand All @@ -76,7 +76,7 @@ other:
-o <file> : path to the menu.xml file
-u : update the config file
-r : regenerate the config file
-d : regenerate icons.db
-d : regenerate the cache file
-c : reconfigure openbox automatically
-R : reconfigure openbox and exit
-h : print this message and exit
Expand Down Expand Up @@ -111,33 +111,13 @@ my $config_help = <<"HELP";
{key => 'Exec', re => qr/\\\\\\\\/, value => '\\\\', global => 1}, # for wine apps
],
|| ICON SETTINGS
| icon_dirs_first : When looking for icons, look in this directories first,
before looking in the directories of the current icon theme.
Example: [
"\$ENV{HOME}/My icons",
],
| icon_dirs_second : Look in this directories after looked in the directories of the
current icon theme. (Before /usr/share/pixmaps)
Example: [
"/usr/share/icons/gnome",
],
| icon_dirs_last : Look in this directories at the very last, after looked in
/usr/share/pixmaps, /usr/share/icons/hicolor and some other
directories.
Example: [
"/usr/share/icons/Tango",
],
| strict_icon_dirs : A true value will make the script to look only inside the directories
specified by you in either one of the above three options.
| gtk_rc_filename : Absolute path to the GTK configuration file.
| missing_image : Use this icon for missing icons (default: gtk-missing-image)
| missing_icon : Use this icon for missing icons (default: gtk-missing-image)
| icon_size : Prefered size for icons. (default: 32)
| skip_svg_icons : Ignore SVG icons. (default: 0)
| force_svg_icons : Use only SVG icons. (default: 0)
| force_icon_size : Use only icons at the prefered icon size, if possible. (default: 0)
|| KEYS
| name_keys : Valid keys for application name.
Expand All @@ -162,7 +142,7 @@ HELP
if (@ARGV) {
while (defined(my $arg = shift @ARGV)) {
if ($arg eq '-i') {
$icons = 1;
$with_icons = 1;
}
elsif ($arg eq '-p') {
$pipe = 1;
Expand All @@ -175,7 +155,6 @@ if (@ARGV) {
}
elsif ($arg eq '-d') {
unlink $cache_db;
unlink $icons_db;
}
elsif ($arg eq '-u') {
$update_config = 1;
Expand Down Expand Up @@ -256,19 +235,19 @@ my %CONFIG = (
],
#>>>

icon_dirs_first => undef,
icon_dirs_second => undef,
icon_dirs_last => undef,
strict_icon_dirs => undef,

skip_svg_icons => 0,
},

name_keys => ['Name'],
terminal => 'xterm',
editor => 'geany',
missing_icon => 'gtk-missing-image',
VERSION => $version,

icon_size => 32,
skip_svg_icons => 0,
force_svg_icons => 0,
force_icon_size => 0,

VERSION => $version,
);

sub dump_configuration {
Expand Down Expand Up @@ -307,7 +286,13 @@ if ($CONFIG{VERSION} != $version) {
$CONFIG{VERSION} = $version;
}

if (!$pipe) {
# Regenerate the cache db if the config or schema file has been modified
if ((-M $config_file) < (-M $cache_db) or (-M _) > (-M $schema_file)) {
print STDERR "** Regenerating the cache DB...\n";
unlink $cache_db;
}

if (!$with_icons) {

# Performance improvement
# XXX: don't do this in production code!
Expand All @@ -325,31 +310,26 @@ if (!$pipe) {
*warnings::warnif = sub { };
}

# Regenerate the cache db if the config or schema file has been modified
if ((-M $config_file) < (-M $cache_db) or (-M _) > (-M $schema_file)) {
print STDERR "** Regenerating the cache DB...\n";
unlink $cache_db;
}

require GDBM_File;
tie my %desktop_file, 'GDBM_File', $cache_db, &GDBM_File::GDBM_WRCREAT, 0640;
tie my %cache_db, 'GDBM_File', $cache_db, &GDBM_File::GDBM_WRCREAT, 0640;

{ # Regenerate the icon db if the GTKRC file has been modified
my $gtkrc_mtime = (stat $CONFIG{'Linux::DesktopFiles'}{gtk_rc_filename})[9];

if (exists $desktop_file{__GTKRC_MTIME__}) {
if ($desktop_file{__GTKRC_MTIME__} != $gtkrc_mtime) {
print STDERR "** Regenerating the icons DB...\n";
unlink $icons_db;
if (exists $cache_db{__GTKRC_MTIME__}) {
if ($cache_db{__GTKRC_MTIME__} != $gtkrc_mtime
or ($with_icons and not exists $cache_db{__WITH_ICONS__})) {
print STDERR "** Regenerating the cache DB...\n";
untie %desktop_file;
untie %cache_db;
unlink $cache_db;
tie %desktop_file, 'GDBM_File', $cache_db, &GDBM_File::GDBM_WRCREAT, 0640;
$desktop_file{__GTKRC_MTIME__} = $gtkrc_mtime;
tie %cache_db, 'GDBM_File', $cache_db, &GDBM_File::GDBM_WRCREAT, 0640;
$cache_db{__GTKRC_MTIME__} = $gtkrc_mtime;
$cache_db{__WITH_ICONS__} = 1 if $with_icons;
}
}
else {
$desktop_file{__GTKRC_MTIME__} = $gtkrc_mtime;
$cache_db{__GTKRC_MTIME__} = $gtkrc_mtime;
$cache_db{__WITH_ICONS__} = 1 if $with_icons;
}
}

Expand All @@ -360,7 +340,8 @@ my $desk_obj = Linux::DesktopFiles->new(

categories => [map $_->{cat}[0], grep exists $_->{cat}, @$SCHEMA],
keys_to_keep => [@{$CONFIG{name_keys}},
'Exec', 'Icon',
'Exec',
($with_icons ? 'Icon' : ()),
(
defined($CONFIG{'Linux::DesktopFiles'}{skip_entry})
&& ref($CONFIG{'Linux::DesktopFiles'}{skip_entry}) eq 'ARRAY'
Expand All @@ -369,13 +350,6 @@ my $desk_obj = Linux::DesktopFiles->new(
),
],

$icons
? (
abs_icon_paths => 1,
icon_db_filename => $icons_db,
)
: (),

terminal => $CONFIG{terminal},
case_insensitive_cats => 1,
);
Expand Down Expand Up @@ -405,7 +379,7 @@ if ($pipe or $static) {
$exec_name = "$^X $exec_name";
}

$icons && ($exec_name .= q{ -i});
$with_icons && ($exec_name .= q{ -i});

open my $fh, '>', $menufile
or die "Can't open file '${menufile}' for write: $!";
Expand Down Expand Up @@ -437,18 +411,40 @@ my $generated_menu = $static
STATIC_MENU_HEADER
: "<openbox_pipe_menu>\n";

{
my %cache;
sub get_icon_path {
my ($name) = @_;

sub check_icon {
$_[0] // return '';
$cache{$_[0]} //= ((chr ord($_[0]) eq '/') ? $_[0] : $desk_obj->get_icon_path($_[0])
|| $desk_obj->get_icon_path($CONFIG{missing_icon}));
}
state $gtk = do {
require Gtk2;
'Gtk2'->init;
'Gtk2';
};

state $theme = do {
"${gtk}::IconTheme"->get_default;
};

state $flags = "${gtk}::IconLookupFlags"->new(
[($CONFIG{force_icon_size} ? 'force-size' : ()),
($CONFIG{skip_svg_icons} ? 'no-svg' : ()),
($CONFIG{force_svg_icons} ? 'force-svg' : ()),
]
);

my $icon_info = $theme->lookup_icon($name, $CONFIG{icon_size}, $flags);

defined($icon_info)
? $icon_info->get_filename
: eval { $theme->lookup_icon($CONFIG{missing_icon}, $CONFIG{icon_size}, $flags)->get_filename };
}

sub check_icon {
substr(($_[0] // return ''), 0, 1) eq '/' && return $_[0];
$cache_db{$_[0]} //= get_icon_path($_[0]);
}

sub begin_category {
$icons
$with_icons
? <<"MENU_WITH_ICON"
<menu id="$_[0]" icon="${\check_icon($_[1])}" label="$_[0]">
MENU_WITH_ICON
Expand All @@ -461,8 +457,8 @@ my %categories;
foreach my $file ($desk_obj->get_desktop_files) {

my %info;
if (exists $desktop_file{$file}) {
%info = split("\0\1\0", $desktop_file{$file}, -1);
if (exists $cache_db{$file}) {
%info = split("\0\1\0", $cache_db{$file}, -1);
}

next if exists $info{__IGNORE__};
Expand All @@ -476,8 +472,12 @@ foreach my $file ($desk_obj->get_desktop_files) {
my $fields = %cats ? (values %cats)[0][0] : {__IGNORE__ => 1};
%info = (%{$fields}, __MTIME__ => $mtime);

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

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

next if exists $info{__IGNORE__};
}
Expand All @@ -496,7 +496,7 @@ my %entities = (
foreach my $schema (@$SCHEMA) {
if (exists $schema->{cat}) {
exists($categories{my $category = lc($schema->{cat}[0]) =~ tr/_a-z0-9/_/cr}) || next;
$generated_menu .= begin_category($schema->{cat}[1], ($icons ? $schema->{cat}[2] : ())) . join(
$generated_menu .= begin_category($schema->{cat}[1], ($with_icons ? $schema->{cat}[2] : ())) . join(
q{},
(
map $_->[1],
Expand All @@ -518,9 +518,9 @@ foreach my $schema (@$SCHEMA) {
$name =~ tr/"&_//
&& $name =~ s/([&"_])/$entities{$1}/g;

$icons
$with_icons
? <<"ITEM_WITH_ICON"
<item label="$name" icon="${\check_icon($_->{Icon})}"><action name="Execute"><command><![CDATA[$exec]]></command></action></item>
<item label="$name" icon="$_->{Icon}"><action name="Execute"><command><![CDATA[$exec]]></command></action></item>
ITEM_WITH_ICON
: <<"ITEM";
<item label="$name"><action name="Execute"><command><![CDATA[$exec]]></command></action></item>
Expand All @@ -533,7 +533,7 @@ ITEM
}
elsif (exists $schema->{item}) {
my ($command, $label, $icon) = @{$schema->{item}};
$generated_menu .= $icons
$generated_menu .= $with_icons
? <<"ITEM_WITH_ICON"
<item label="$label" icon="${\check_icon($icon)}"><action name="Execute"><command><![CDATA[$command]]></command></action></item>
ITEM_WITH_ICON
Expand All @@ -555,7 +555,7 @@ ITEM
}
elsif (exists $schema->{exit}) {
my ($label, $icon) = @{$schema->{exit}};
$generated_menu .= $icons
$generated_menu .= $with_icons
? <<"EXIT_WITH_ICON"
<item label="$label" icon="${\check_icon($icon)}"><action name="Exit"/></item>
EXIT_WITH_ICON
Expand All @@ -572,7 +572,7 @@ EXIT
}
elsif (exists $schema->{pipe}) {
my ($command, $label, $icon) = @{$schema->{pipe}};
$generated_menu .= $icons
$generated_menu .= $with_icons
? <<"PIPE_WITH_ICON"
<menu id="$label" label="$label" execute="$command" icon="${\check_icon($icon)}"/>
PIPE_WITH_ICON
Expand All @@ -582,13 +582,13 @@ PIPE
}
elsif (exists $schema->{obgenmenu}) {
my ($name, $icon) = ref($schema->{obgenmenu}) eq 'ARRAY' ? @{$schema->{obgenmenu}} : $schema->{obgenmenu};
$generated_menu .= ($icons ? <<"MENU_WITH_ICON" : <<"MENU");
$generated_menu .= ($with_icons ? <<"MENU_WITH_ICON" : <<"MENU");
<menu id="$name" label="$name" icon="${\check_icon($icon)}">
MENU_WITH_ICON
<menu id="$name" label="$name">
MENU

$generated_menu .= ($icons ? <<"ITEMS_WITH_ICONS" : <<"ITEMS");
$generated_menu .= ($with_icons ? <<"ITEMS_WITH_ICONS" : <<"ITEMS");
<item label="Menu Schema" icon="${\check_icon('text-x-generic')}"><action name="Execute"><command><![CDATA[$CONFIG{editor} $schema_file]]></command></action></item>
<item label="Menu Config" icon="${\check_icon('text-x-generic')}"><action name="Execute"><command><![CDATA[$CONFIG{editor} $config_file]]></command></action></item>
<separator/>
Expand Down

0 comments on commit 71b1333

Please sign in to comment.