#!/usr/bin/perl -w # # Creates a Mandrake menu file from a KDE desktop file # # Usage: kdedesktop2mdkmenu.pl package section input_file output_file # # Copyright: MandrakeSoft, licensed under the GPL v2. # Author: David Faure <david@mandrakesoft.com> # # modified by Guillaume Cottenceau <gc@mandrakesoft.com> Fri Aug 25 18:49:45 2000 # modified by Christopher Molnar <molnarc@mandrakesoft.com> 9-6-2000 to remove icon path # modified by David Faure <david@mandrakesoft.com> Sep 29/30 2000 to keep other groups untouched # and to support .directory files # modified by Frederic Crozat <fcrozat@mandrakesoft.com> Aug 07 2001 use new mimetype keyword for Mdk 8.1 # modified by Laurent Montel <lmontel@mandrakesoft.com> Fri Aug 10 2001 init icon variable some desktop # file doesn't have icon # modified by Laurent Montel <lmontel@mandrakesoft.com> 2002-11-05 DOS file formatting of .desktop files. patch from Andreas Pour <pour@mieterra.com> # 2004-01-13 remove Categorie entry to allow to use menu "menu" type # Type can be a Service # 2004-02-16 fix remove Categori option (($#ARGV == 3) or ($#ARGV == 4) or ($#ARGV == 5)) or die "E: You gave me $#ARGV parameters. This script must be invoked with parameters: <package> <section> <file_in> <file_out> [requires] [title]\n"; my $name; my $comment; my $icon = ""; my $indesktopgroup=0; my $kde_opt = ""; my $kde_filename = $ARGV[2]; my $package_name = $ARGV[0]; my $section = $ARGV[1]; my $requires = (defined $ARGV[4]) ? $ARGV[4] : "x11"; my $mdk_version = ""; ($requires eq "text") or ($requires eq "x11") or ($requires eq "kde") or print "W: requires is $requires (e.g. not text nor x11 nor kde)\n"; my $debug = 1; open (MDKVERSION,"/etc/mandrake-release") or die "Can't open /etc/mandrake-release"; while (<MDKVERSION>) { chomp; if (/\S+\s+\S+\s+\S+\s+(\S+)/) { $mdk_version = $1; } } close (MDKVERSION); open (INPUT, $ARGV[2]) or die "Can't open $ARGV[2]!\n"; open (OUTPUT, ">$ARGV[3]") or die "Can't open $ARGV[3]!\n"; while (<INPUT>) { next if (/^\s*#/); # Ignore comments next if (/^\s*$/); # Ignore void lines chomp; # Remove trailing slash if (m/^([^=]+)\s*=(.*)/) # Line of the form Key = Value ? { $key = $1; $value = $2; chomp($value); if ($indesktopgroup) { if ($key eq "Name") { $name = $value; $debug and print "I: found name: $value\n"; } elsif ($key eq "Comment") { $comment = $value; $debug and print "I: found comment: $value\n"; } elsif ($key eq "Icon") { $icon = "$value"; $debug and print "I: found icon: $value\n";} ## This had better exist..... elsif ($key eq "Exec") { $command = $value; $debug and print "I: found command: $value\n"; } elsif ($key eq "Type") { die "E: Not an application!" unless ($value eq "Application" || $value eq "Service" ); } elsif ($key eq "MimeType") { $mimetype = $value; $debug and print "I: found mimetype: $value\n"; } elsif ($key eq "Categories") {;} #### ignore this entry elsif ($key eq "OnlyShowIn") {;} #### ignore this entry else { # Everything else next if ($key =~ /Name\[.+\]/); next if ($key =~ /Comment\[.+\]/); # What about other translated fields ? There is Keywords, for instance... $kde_opt .= "\\\\n$_"; } } else { # No in Desktop Entry group -> copy everything untouched $kde_opt .= "\\\\n$_"; } } elsif (m/^\[([^\]]+)\]/) { if ($1 eq "Desktop Entry" || $1 eq "KDE Desktop Entry") { $indesktopgroup=1; } else { $indesktopgroup=0; $kde_opt .= "\\\\n$_"; } } else { print "W: This should never happen: Ignoring $_\n"; } } close(INPUT); # prevent from menu system ($name =~ /\"/) and print "W: Name contains some \" characters\n"; ($section =~ /\"/) and die "E: Section should not contain some \" characters\n"; defined $command and $command =~ s/\"/\\\"/g; defined $comment and $comment =~ s/\"/\\\"/g; defined $mimetype and $mimetype =~ s/\"/\\\"/g; $kde_opt =~ s/\"/\\\"/g; $kde_filename=~s|.*/||; #keep filename my $is_dot_directory=($kde_filename eq ".directory"); $kde_filename=~s|\..*||; #remove extension $icon=~s|\..*||; #Remove icon extension as some files have it and some dont re-add below to all (!defined $command && !$is_dot_directory) and die "E: Command field not defined, cannot proceed.\n"; (!defined $icon) and die "E: Icon field not defined, cannot proceed.\n"; (!defined $kde_opt) and print "W: no kde special options found\n"; # set the name to title if defined $name = defined ($ARGV[5]) ? $ARGV[5] : $name; # handle specific kde commands if ( defined $command ) { my $kdecommand = $command; if ($mdk_version >= 8.1) { $_ = $command; if ( $requires ne "kde") { if (/.*%u.*/) { $accept_url = "true"; $multiple_files = "false"; $command =~ s/%u//g; } elsif (/.*%U.*/) { $accept_url = "true"; $multiple_files= "true"; $command =~ s/%U//g; } elsif (/.*%F.*/) { $accept_url = "false"; $multiple_files= "true"; $command =~ s/%F//g; } elsif (/.*%f.*/) { $accept_url = "false"; $multiple_files= "false"; $command =~ s/%f//g; } # mimetype must be seperated by comma defined $mimetype and $mimetype =~ s/;/,/g; defined $mimetype and $mimetype =~ s/,$//g; chomp $kdecommand; } } $command =~ s/%i//g; $command =~ s/%m//g; $command =~ s/%U//g; $command =~ s:\\"%c\\"::g; $command =~ s/%c//g; $command =~ s/-caption//g; $command =~ s/\s+$//; if (!($kdecommand eq $command)) { $command = "$command\" kde_command=\"$kdecommand"; } } # Ok, we've got all the info, write the menu file. print OUTPUT "?package($package_name): needs=\"$requires\" kde_filename=\"$kde_filename\" section=\"$section\"". " title=\"$name\" icon=\"$icon.png\""; defined $command and print OUTPUT " command=\"$command\""; defined $comment and print OUTPUT " longtitle=\"$comment\""; if ($mdk_version >= 8.1) { defined $mimetype and print OUTPUT " mimetypes=\"$mimetype\""; defined $multiple_files and print OUTPUT " multiple_files=\"$multiple_files\""; defined $accept_url and print OUTPUT " accept_url=\"$accept_url\""; } else { defined $mimetype and print OUTPUT " kde_mimetype=\"$mimetype\""; } print OUTPUT " kde_opt=\"$kde_opt\""; close(OUTPUT);