File-DesktopEntry

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.07 2013-10-03
 - Fixed dependency on Win32 module.

0.06 2013-10-03
 - Switched to EU::MM
 - POD fix - RT 89116 - GWOLF

0.05 2013-06-05 
 - Fixed tests on Windows - RT 45669
 - Set perl 5.8.6 as minimum version - RT 42770 - ANDK
 - Fixed link to freedesktop.org - RT 37320 - GWOLF

0.04 2007-11-04
Hot fix release - POSIX does not export LC_MESSAGES on all platforms
  - Removed POSIX dependency

0.03 2007-11-04
 - Added support for writing Desktop Entry files
 - Updated to version 1.0 of the specification
 - Added much more sanity checks while handling data
 - Extended unit tests to almost full coverage

MANIFEST  view on Meta::CPAN

Makefile.PL
MANIFEST			This list of files
MANIFEST.SKIP
README.md
t/01_data.t
t/02_DesktopEntry.t
t/03_run.t
t/04_pod_ok.t
t/05_pod_cover.t
t/06_changes.t
t/applications/foo.desktop
t/applications/rt65394.desktop
t/uri_escape.t
META.yml                                 Module YAML meta-data (added by MakeMaker)
META.json                                Module JSON meta-data (added by MakeMaker)

META.json  view on Meta::CPAN

{
   "abstract" : "Module to handle .desktop files",
   "author" : [
      "Jaap Karssenberg <pardus@cpan.org>"
   ],
   "dynamic_config" : 1,
   "generated_by" : "ExtUtils::MakeMaker version 7.1, CPAN::Meta::Converter version 2.150005",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.yml  view on Meta::CPAN

---
abstract: 'Module to handle .desktop files'
author:
  - 'Jaap Karssenberg <pardus@cpan.org>'
build_requires:
  ExtUtils::MakeMaker: '0'
configure_requires:
  ExtUtils::MakeMaker: '6.30'
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 7.1, CPAN::Meta::Converter version 2.150005'
license: perl
meta-spec:

Makefile.PL  view on Meta::CPAN

use strict;
use warnings;

use 5.008_001;

use ExtUtils::MakeMaker;

WriteMakefile
(
  'NAME'               => 'File::DesktopEntry',
  'ABSTRACT'           => 'Module to handle .desktop files',
  'AUTHOR'             => 'Jaap Karssenberg <pardus@cpan.org>',
  'DISTNAME'           => "File-DesktopEntry",
  'VERSION_FROM'       => 'lib/File/DesktopEntry.pm',
  'LICENSE'            => 'perl',
  'MIN_PERL_VERSION'   => '5.8.6',
  'PREREQ_PM' => {
    'Carp'           => 0,
    'Encode'         => 0,
    'File::Spec'     => 0,
    'File::Path'     => 0,

README.md  view on Meta::CPAN

# File-DesktopEntry

You can use this module to work with `.desktop` files as specified
by the Freedesktop.org specification.

## INSTALLATION

If you are on Linux, the most convenient way to install this module is
via your package manager. On Debian or Ubuntu:

    sudo apt-get install libfile-desktopentry-perl

on Fedora, RHEL or CentOS:

    sudo yum install perl-File-DesktopEntry

note: on RHEL or CentOS you'd need to first install [EPEL](https://fedoraproject.org/wiki/EPEL):

    sudo yum install epel-release

If you can't install from a package manager, the best solution is installation using

lib/File/DesktopEntry.pm  view on Meta::CPAN

our $VERSION = '0.22';
our $VERBOSE = 0;

if ($^O eq 'MSWin32') {
    eval q/use Win32::Process/;
    die $@ if $@;
}

=head1 NAME

File::DesktopEntry - Object to handle .desktop files

=head1 SYNOPSIS

    use File::DesktopEntry;

    my $entry = File::DesktopEntry->new('firefox');

    print "Using ".$entry->Name." to open http://perl.org\n";
    $entry->run('http://perl.org');

=head1 DESCRIPTION

This module is designed to work with F<.desktop> files. The format of these files
is specified by the freedesktop "Desktop Entry" specification. This module can
parse these files but also knows how to run the applications defined by these
files.

For this module version 1.0 of the specification was used.

This module was written to support L<File::MimeInfo::Applications>.

Please remember: case is significant for the names of Desktop Entry keys.

=head1 VARIABLES

lib/File/DesktopEntry.pm  view on Meta::CPAN

sub AUTOLOAD {
    $AUTOLOAD =~ s/.*:://;
    return if $AUTOLOAD eq 'DESTROY';
    croak "No such method: File::DesktopEntry::$AUTOLOAD"
        unless $AUTOLOAD =~ /^[A-Z][A-Za-z0-9-]+$/;
    return $_[0]->get($AUTOLOAD);
}

=item C<lookup(NAME)>

Returns a filename for a desktop entry with desktop file id NAME.

=cut

sub lookup {
    my (undef, $name) = @_;
    $name .= '.desktop';
    my $file = data_files('applications', $name);
    if (! $file and $name =~ /-/) {
        # name contains "-" and was not found
        my @name = split /-/, $name;
        $file = data_files('applications', @name);
    }
    return $file;
}

sub _parse_lang {

lib/File/DesktopEntry.pm  view on Meta::CPAN

    my @locale = (
        $l,
        ($m         ? "$l\@$m"     : ()),
        ($c         ? "$l\_$c"     : ()),
        (($m && $c) ? "$l\_$c\@$m" : ())  );
    return join '|', reverse @locale;
}

=item C<wants_uris( )>

Returns true if the Exec string for this desktop entry specifies that the
application uses URIs instead of paths. This can be used to determine
whether an application uses a VFS library.

=item C<wants_list( )>

Returns true if the Exec string for this desktop entry specifies that the
application can handle multiple arguments at once.

=cut


sub wants_uris {
    my $self = shift;
    my $exec = $self->get('Exec');
    croak "No Exec string defined for desktop entry" unless length $exec;
    $exec =~ s/\%\%//g;
    return $exec =~ /\%U/i;
}

sub wants_list {
    my $self = shift;
    my $exec = $self->get('Exec');
    croak "No Exec string defined for desktop entry" unless length $exec;
    $exec =~ s/\%\%//g;
    return $exec !~ /\%[fud]/; # we default to %F if no /\%[FUD]/i is found
}

=item C<run(@FILES)>

Forks and runs the application specified in this Desktop Entry
with arguments FILES as a background process. Returns the pid.

The child process fails when this is not a Desktop Entry of type Application
or if the Exec key is missing or invalid.

If the desktop entry specifies that the program needs to be executed in a
terminal the $TERMINAL environment variable is used. If this variable is not
set C<xterm -e> is used as default.

(On Windows this method returns a L<Win32::Process> object.)

=item C<system(@FILES)>

Like C<run()> but using the C<system()> system call.
It only return after the application has ended.

lib/File/DesktopEntry.pm  view on Meta::CPAN

    warn "Error: $!\n" if $VERBOSE and $?;

    if (defined $cwd) {
        chdir $cwd or croak "Could not change back to dir: $cwd";
        $ENV{PWD} = $cwd;
    }
}

=item C<parse_Exec(@FILES)>

Expands the Exec format in this desktop entry with. Returns a properly quoted
string in scalar context or a list of words in list context. Dies when the
Exec key is invalid.

It supports the following fields:

    %f    single file
    %F    multiple files
    %u    single url
    %U    multiple urls
    %i    Icon field prefixed by --icon
    %c    Name field, possibly translated
    %k    location of this .desktop file
    %%    literal '%'

If necessary this method tries to convert between paths and URLs but this
is not perfect.

Fields that are deprecated, but (still) supported by this module:

    %d    single directory
    %D    multiple directories

lib/File/DesktopEntry.pm  view on Meta::CPAN

sub write {
    my $self = shift;
    my $file = shift || $$self{file};
    unless ($$self{groups}) {
        if ($$self{file}) { $self->read() }
        else { croak "Can not write empty Desktop Entry file" }
    }

    # Check keys
    for (qw/Name Type/) {
        croak "Can not write a desktop file without a $_ field"
            unless defined $self->get($_);
    }
    $self->set(Version => '1.0', Encoding => 'UTF-8');

    # Check file writable
    $file = $self->_data_home_file
        if (! $file or ! -w $file) and defined $$self{name};
    croak "No file given for writing Desktop Entry" unless length $file;

    # Write file

lib/File/DesktopEntry.pm  view on Meta::CPAN

    open OUT, ">$file" or die "Could not write file: $file\n";
    binmode OUT, ':utf8' unless $] < 5.008;
    print OUT join "\n", @{$$self{groups}};
    close OUT;
}

sub _data_home_file {
    # create new file name in XDG_DATA_HOME from name
    my $self = shift;
    my @parts = split /-/, $$self{name};
    $parts[-1] .= '.desktop';
    my $dir = data_home('applications', @parts[0 .. $#parts-1]);
    unless (-d $dir) { # create dir if it doesn't exist
        require File::Path;
        File::Path::mkpath($dir);
    }
    return data_home('applications', @parts);
}

=back

lib/File/DesktopEntry.pm  view on Meta::CPAN


Copyright (c) 2005, 2007 Jaap G Karssenberg. All rights reserved.

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html>

L<File::BaseDir> and L<File::MimeInfo::Applications>

L<X11::FreeDesktop::DesktopEntry>

=cut

t/01_data.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More tests => 25;

use_ok('File::DesktopEntry');
$File::DesktopEntry::_locale = ''; # reset locale for testing

my $file = File::Spec->catfile(qw/t applications foo.desktop/);
my $entry = File::DesktopEntry->new($file);
is($$entry{file}, $file, 'new(FILE) works');

$entry = File::DesktopEntry->new_from_file($file);
is($$entry{file}, $file, 'new_from_file(FILE) works');

ok(! $$entry{groups}, 'no premature hashing');

is( $entry->get('Comment'),
       	'The best viewer for Foo objects available!',

t/01_data.t  view on Meta::CPAN

	is( File::DesktopEntry::_parse_lang($$_[0]), $$_[1],
		"language parsing $i");
}
$entry->set('Name[nl]' => 'dus ja');
is($entry->get('Name[nl_BE]'), 'dus ja', 'language parsing in get()');

$entry->set('Name[C]' => 'Something new');
is($entry->get('Name[POSIX]'), 'Something new', 'Aliases for default locale');

$ENV{XDG_DATA_HOME} = 't';
$file = File::Spec->catfile(qw/t applications bar baz.desktop/);
$$entry{name} = 'bar-baz';
is($entry->_data_home_file, $file, 'correct file name generated');
rmdir( File::Spec->catdir(qw/t applications bar/) ); # clean up


$file = File::Spec->catfile('t', 'applications', 'bar.desktop');
$entry = File::DesktopEntry->new;
$entry->set(Type => 'Application', Name => 'Bar');
$entry->set('Some Action', Run => 'bar');
$entry->write($file);

$entry = File::DesktopEntry->new($file);
is($entry->text,
"[Desktop Entry]
Version=1.0
Encoding=UTF-8

t/02_DesktopEntry.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;

use File::DesktopEntry;
$File::DesktopEntry::_locale = ''; # reset locale for testing

my $file = File::Spec->catfile(qw/t applications foo.desktop/);

$ENV{XDG_DATA_HOME} = 't';
is(File::DesktopEntry->lookup('foo'), $file, 'lookup works 1');

# Constructor 1
{
	my $entry = File::DesktopEntry->new('foo');
	is($entry->get('Name'), 'Foo Viewer', 'new(NAME) works');
}

t/02_DesktopEntry.t  view on Meta::CPAN

	eval {$entry->parse_Exec()};
	print "# message: $@\n";
	ok($@, 'parse_Exec dies on unsupported field');

	$entry->set(Exec => q#fooview %f#);
	eval {$entry->parse_Exec(qw/foo bar baz/)};
	print "# message: $@\n";
	ok($@, 'parse_Exec dies when multiple args not supported');
}

$file = File::Spec->catfile(qw/t applications rt65394.desktop/);
my $entry = File::DesktopEntry->new($file);
is($entry->get('Name'), 'caja', 'new(FILE) works');
is($entry->Name, 'caja', 'AUTOLOAD works');
is($entry->Path, '', 'Path is empty string');

done_testing;

t/03_run.t  view on Meta::CPAN


$entry->set(
	Exec => qq#"$perl" -e 'print ""'#,
	Path => 't/applications'
);
$entry->system();
print( (-f 'MANIFEST' ? 'ok' : 'nok'),
	" 3 - directory reset properly when using Path\n" );

$entry->set(
	Exec => qq#"$perl" -e 'print( (-f "foo.desktop" ? "ok" : "nok"), " 4 - exec() works using Path\n")'#
);

if ($^O eq 'MSWin32') {
	print "ok 4 # skip fork() not supported\n";
}
else {
	# not sure why, but gives ugly result on Win32
	# probably due to fork() emulation
	$pid = fork;
	unless ($pid) {



( run in 0.743 second using v1.01-cache-2.11-cpan-299005ec8e3 )