Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > main-src > by-pkgid > 10955d0174b3f0a95b3d8a9617d48f6d > files > 3

kdelibs-2.2.2-48mdk.src.rpm

#!/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

(($#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 $mimetype;
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";

($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 (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
  chop;                           # Remove trailing slash
  if (m/^([^=]+)\s*=(.*)/)        # Line of the form Key = Value ?
  {
    $key = $1;
    $value = $2;
    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"); }
      elsif ($key eq "MimeType") { $mimetype = $value; $debug and print "I: found mimetype: $value\n"; }
      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;
  $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\"";
defined $mimetype and print OUTPUT " kde_mimetype=\"$mimetype\"";
print OUTPUT " kde_opt=\"$kde_opt\"";
close(OUTPUT);