#!/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, "; 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 () { 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 () { # Check it's a launcher icon open(IN, "<$desktop") or die "Unable to open $desktop: $!\n"; my $skip = 0; my $noskip = 0; while () { $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 < Main /usr/share/applications/hildon /opt/catorise/share all EOH my $menus = ''; foreach my $s (sort keys %sections) { print OUT " $s\n" unless $s eq 'other'; $menus .= " \n $s\n". " $s.directory\n \n"; foreach my $v (@{ $sections{$s} }) { $menus .= " $v.desktop\n"; } $menus .= " \n \n"; } print OUT <other $menus all all.directory EOF close(OUT) or die "Unable to close menu: $!"; # -- Restart hildon-desktop if necessary... # system("killall", "hildon-desktop") if $needRestart; exit;