All pastes #1749489 Raw Edit

Fixed Catorise script

public text v1 · immutable
#1749489 ·published 2010-01-13 11:22 UTC
rendered paste body
#!/usr/bin/perl
#
# Catorise - create and maintain menus corresponding to
# ~~~~~~~~   package sections.            (c) Andrew Flegg 2009.
#                                         Released under the Artistic Licence.

use strict;
use warnings;


# -- Read pre-stored menu...
#
my %overrides = ();
if (open(IN, "</opt/catorise/menu")) {
  %overrides = map { chomp; split /:\s*/ }  <IN>;
  close(IN);
}


#-- Valid sections...
#
my %validSections = map { $_ => 1} qw(
  desktop development education games graphics other
  multimedia navigation network office science system utilities
);


# -- Find current theme and copy icons...
#
my $theme = undef;
if (open(IN, "<$ENV{HOME}/.osso/current-gtk-theme")) {
  while (<IN>) {
    next unless m!^\s*include\s+"(.+?)/gtk-2.0/gtkrc!;
    $theme = $1;
    last;
  }
  close(IN);
}
$theme ||= '/usr/share/themes/default';
my @icons = keys(%validSections);
push @icons, 'all';
foreach my $s (@icons) {
  my $icon = "app_install_$s.png";
  my $src  = "$theme/backgrounds/$icon";
  $src = "/usr/share/themes/default/backgrounds/$icon" unless -f $src;
  system("cp", $src, "/usr/share/icons/hicolor/64x64/hildon/$icon");
}


# -- Back-up hildon.menu if necessary...
#
my $needRestart = 0;
unless (-f '/etc/xdg/menus/hildon.catorise') {
  system('cp', '/etc/xdg/menus/hildon.menu',
               '/etc/xdg/menus/hildon.catorise');
  $needRestart = 1;
}


# -- Loop over all desktop files...
#
my %sections = ();
open(OUT, ">/opt/catorise/menu") or die "Unable to store new menu: $!";
foreach my $desktop (</usr/share/applications/hildon/*.desktop>) {
  # Check it's a launcher icon
  open(IN, "<$desktop") or die "Unable to open $desktop: $!\n";
  my $skip = 0;
  my $noskip = 0;
  while (<IN>) {
	$noskip ||= /^Type=(.*)$/i && $1 eq "Application";
	if($noskip)
	{
	    $skip ||= /^NoDisplay=True/i;
	}
	else
	{
	    $skip ||= /^Type=(.*)$/i && $1 !~ /^Application$/i;
	    $skip ||= /^NoDisplay=True/i;
	}
  }
  close(IN);
  next if $skip;

  # Check if there's an override...
  my ($name)  = $desktop =~ /([^\/]+)\.desktop$/;
  my $section = $overrides{$name};
  if (!$section) {
    # ...find by package, then
    chomp(my $package = `dpkg -S "$desktop"`);
    if (!$package) {
      print STDERR "Unknown desktop file: $desktop\n";
      next;
    }
    $package =~ s/: .*//;
    $section = (grep { /^Section: / } `apt-cache show "$package"`)[0];
    $section =~ s/.*?: (.+?\/)?//;
    chomp($section);
  }

  $section = 'other' unless $validSections{$section || ''};

  # Now we've got the section, store it...
  push @{ $sections{$section} }, $name;
  print OUT "$name: $section\n";
}
close(OUT) or die "Unable to close new menu: $!";


# -- Output the menu...
#
open(OUT, ">/etc/xdg/menus/hildon.menu") or die "Unable to write to menu: $!";
print OUT <<EOH;
<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"
 "http://www.freedesktop.org/standards/menu-spec/menu-1.0.dtd">

<Menu>

  <Name>Main</Name>

  <AppDir>/usr/share/applications/hildon</AppDir>
  <DirectoryDir>/opt/catorise/share</DirectoryDir>

  <Layout>
    <Menuname>all</Menuname>
EOH
my $menus = '';
foreach my $s (sort keys %sections) {
  print OUT "    <Menuname>$s</Menuname>\n" unless $s eq 'other';

  $menus .= "  <Menu>\n    <Name>$s</Name>\n".
            "    <Directory>$s.directory</Directory>\n    <Include>\n";
  foreach my $v (@{ $sections{$s} }) {
    $menus .= "      <Filename>$v.desktop</Filename>\n";
  }
  $menus .= "    </Include>\n  </Menu>\n";
}
print OUT <<EOF;
    <Menuname>other</Menuname>
  </Layout>
$menus
  <Menu>
    <Name>all</Name>
     <Directory>all.directory</Directory>
     <Include>              
       <All/>               
     </Include>
  </Menu>
</Menu>                    
EOF
close(OUT) or die "Unable to close menu: $!";


# -- Restart hildon-desktop if necessary...
#
system("killall", "hildon-desktop") if $needRestart;
exit;