Subject: | [PATCH] support for more xdg specs |
I added some modules that build on top of File::BaseDir and realise
some more functionality from freedesktop.org.
Patch applies cleanly against 0.03 with the command
git apply 0001*.patch
Subject: | 0001-support-for-more-xdg-specs.patch |
From 4d83a63e03c3b36dcd35a7da64b056782e074e74 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20D=C9=AA=E1=B4=87=E1=B4=84=E1=B4=8B=E1=B4=8F=E1=B4=A1=20=E8=BF=AA=E6=8B=89=E6=96=AF?= <daxim@cpan.org>
Date: Thu, 8 Oct 2009 21:00:49 +0200
Subject: [PATCH] support for more xdg specs
---
lib/File/IconTheme.pm | 66 ++++++++++++++++++++++
lib/File/UserDirs.pm | 145 +++++++++++++++++++++++++++++++++++++++++++++++++
t/03_icontheme.t | 23 ++++++++
t/03_userdirs.t | 49 +++++++++++++++++
4 files changed, 283 insertions(+), 0 deletions(-)
create mode 100644 lib/File/IconTheme.pm
create mode 100644 lib/File/UserDirs.pm
create mode 100644 t/03_icontheme.t
create mode 100644 t/03_userdirs.t
diff --git a/lib/File/IconTheme.pm b/lib/File/IconTheme.pm
new file mode 100644
index 0000000..2573cd5
--- /dev/null
+++ b/lib/File/IconTheme.pm
@@ -0,0 +1,66 @@
+package File::IconTheme;
+use strict;
+use warnings FATAL => 'all';
+use File::BaseDir qw(data_dirs);
+require File::Spec;
+use parent qw(Exporter);
+
+our $VERSION = '0.04';
+our @EXPORT_OK = qw(xdg_icon_theme_search_dirs);
+
+sub xdg_icon_theme_search_dirs {
+ return grep {-d $_ && -r $_}
+ File::Spec->catfile($ENV{HOME}, '.icons'),
+ data_dirs('icons'),
+ '/usr/share/pixmaps';
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+File::IconTheme - find icon directories
+
+=head1 VERSION
+
+This document describes File::IconTheme version C<0.04>.
+
+=head1 SYNOPSIS
+
+ use File::IconTheme qw(xdg_icon_theme_search_dirs);
+ print join "\n", xdg_icon_theme_search_dirs;
+
+=head1 DESCRIPTION
+
+This module can be used to find directories as specified
+by the Freedesktop.org Icon Theme Specification. Currently only a tiny
+(but most useful) part of the specification is implemented.
+
+In case you want to B<store> an icon theme, use the directory returned by:
+
+ use File::BaseDir qw(data_dirs);
+ print scalar data_dirs('icons');
+
+=head1 INTERFACE
+
+=over
+
+=item C<xdg_icon_theme_search_dirs>
+
+Returns a list of the base directories of icon themes.
+
+=back
+
+=head1 EXPORTS
+
+None by default, but the method can be exported on demand.
+
+=head1 CONFIGURATION AND ENVIRONMENT
+
+C<$XDG_DATA_HOME>, C<$XDG_DATA_DIRS>
+
+=head1 SEE ALSO
+
+L<http://standards.freedesktop.org/icon-theme-spec/>
diff --git a/lib/File/UserDirs.pm b/lib/File/UserDirs.pm
new file mode 100644
index 0000000..ccaba4a
--- /dev/null
+++ b/lib/File/UserDirs.pm
@@ -0,0 +1,145 @@
+package File::UserDirs;
+use strict;
+use warnings FATAL => 'all';
+use IPC::System::Simple qw(capturex);
+use parent qw(Exporter);
+
+our $VERSION = '0.04';
+our %EXPORT_TAGS = (
+ all => [
+ qw(xdg_desktop_dir xdg_documents_dir xdg_download_dir xdg_music_dir
+ xdg_pictures_dir xdg_publicshare_dir xdg_templates_dir xdg_videos_dir)
+ ]);
+our @EXPORT_OK = @{$EXPORT_TAGS{all}};
+
+
+sub _xdg_user_dir {
+ my ($purpose) = @_;
+ my $dir = capturex 'xdg-user-dir', $purpose;
+ chomp $dir;
+ return $dir;
+}
+
+sub xdg_desktop_dir {return _xdg_user_dir 'DESKTOP';}
+sub xdg_documents_dir {return _xdg_user_dir 'DOCUMENTS';}
+sub xdg_download_dir {return _xdg_user_dir 'DOWNLOAD';}
+sub xdg_music_dir {return _xdg_user_dir 'MUSIC';}
+sub xdg_pictures_dir {return _xdg_user_dir 'PICTURES';}
+sub xdg_publicshare_dir {return _xdg_user_dir 'PUBLICSHARE';}
+sub xdg_templates_dir {return _xdg_user_dir 'TEMPLATES';}
+sub xdg_videos_dir {return _xdg_user_dir 'VIDEOS';}
+
+1;
+
+__END__
+
+=head1 NAME
+
+File::UserDirs - find extra media and documents directories
+
+=head1 VERSION
+
+This document describes File::UserDirs version C<0.04>.
+
+=head1 SYNOPSIS
+
+ use File::UserDirs qw(:all);
+ print xdg_desktop_dir; # e.g. /home/user/Desktop
+
+=head1 DESCRIPTION
+
+This module can be used to find directories as informally specified
+by the Freedesktop.org xdg-user-dirs software. This
+gives a mechanism to locate extra directories for media and documents files.
+
+=head1 INTERFACE
+
+=over
+
+=item C<xdg_desktop_dir>
+
+Returns the desktop directory. Unless changed by the user,
+this is the directory F<Desktop> in the home directory.
+
+=item C<xdg_documents_dir>
+
+Returns the documents directory. Unless changed by the user,
+this is the home directory.
+
+=item C<xdg_download_dir>
+
+Returns the download directory. Unless changed by the user,
+this is the home directory.
+
+=item C<xdg_music_dir>
+
+Returns the music directory. Unless changed by the user,
+this is the home directory.
+
+=item C<xdg_pictures_dir>
+
+Returns the pictures directory. Unless changed by the user,
+this is the home directory.
+
+=item C<xdg_publicshare_dir>
+
+Returns the public share directory. Unless changed by the user,
+this is the home directory.
+
+=item C<xdg_templates_dir>
+
+Returns the templates directory. Unless changed by the user,
+this is the home directory.
+
+=item C<xdg_videos_dir>
+
+Returns the videos directory. Unless changed by the user,
+this is the home directory.
+
+=back
+
+=head1 EXPORTS
+
+None by default, but any method can be exported on demand.
+Also the group C<:all> is defined which exports all methods.
+
+=head1 DIAGNOSTICS
+
+=over
+
+=item C<"xdg-user-dir" failed to start: %s>
+
+The executable C<xdg-user-dir> could not be run, most likely because it was not
+installed. See L</"DEPENDENCIES">.
+
+=back
+
+=head1 CONFIGURATION AND ENVIRONMENT
+
+The location of the directories can be specified by the user in the file
+F<$XDG_CONFIG_HOME/user-dirs.dirs>. It is a shell file setting a number of
+environment variables. To find the exact pathname from Perl, run:
+
+ use File::BaseDir qw(config_home);
+ print config_home('user-dirs.dirs');
+
+=head2 Example customised F<user-dirs.dirs>
+
+ XDG_DESKTOP_DIR="$HOME/Workspace"
+ XDG_DOCUMENTS_DIR="$HOME/Files"
+ XDG_DOWNLOAD_DIR="$HOME/Files/Downloads"
+ XDG_MUSIC_DIR="$HOME/Files/Audio"
+ XDG_PICTURES_DIR="$HOME/Files/Images"
+ XDG_PUBLICSHARE_DIR="$HOME/public_html"
+ XDG_TEMPLATES_DIR="$HOME/Files/Document templates"
+ XDG_VIDEOS_DIR="$HOME/Files/Video"
+
+=head1 DEPENDENCIES
+
+This module requires the executable F<xdg-user-dir> from the package
+C<xdg-user-dirs>. Source code is available from
+L<http://cgit.freedesktop.org/xdg/xdg-user-dirs/>.
+
+=head1 SEE ALSO
+
+L<http://www.freedesktop.org/wiki/Software/xdg-user-dirs>
diff --git a/t/03_icontheme.t b/t/03_icontheme.t
new file mode 100644
index 0000000..15439ef
--- /dev/null
+++ b/t/03_icontheme.t
@@ -0,0 +1,23 @@
+#!perl
+use strict;
+use warnings FATAL => 'all';
+use Test::More tests => 1;
+use Config;
+use File::IconTheme qw(xdg_icon_theme_search_dirs);
+require File::Spec;
+use File::Temp qw();
+
+my @dirs = map {File::Temp->newdir} 0 .. 2;
+my @icondirs = map {File::Spec->catfile($_, 'icons')} @dirs;
+mkdir $_ for @icondirs;
+
+$ENV{XDG_DATA_HOME} = $dirs[0];
+$ENV{XDG_DATA_DIRS} = $dirs[1] . $Config{path_sep} . $dirs[2];
+
+is_deeply
+ [xdg_icon_theme_search_dirs],
+ [grep {-d $_ && -r $_}
+ File::Spec->catfile($ENV{HOME}, '.icons'),
+ @icondirs,
+ '/usr/share/pixmaps'
+ ];
diff --git a/t/03_userdirs.t b/t/03_userdirs.t
new file mode 100644
index 0000000..0734f8f
--- /dev/null
+++ b/t/03_userdirs.t
@@ -0,0 +1,49 @@
+#!perl
+use strict;
+use warnings FATAL => 'all';
+use Test::More;
+use File::UserDirs qw(:all);
+use File::BaseDir qw(config_home);
+use File::Spec::Functions qw(catfile);
+use File::Which qw(which);
+
+if (which 'xdg-user-dir') {
+ plan tests => 8;
+} else {
+ plan skip_all => '"xdg-user-dir" executable not found. Install package "xdg-user-dirs".';
+}
+
+my $udd = config_home('user-dirs.dirs');
+if (-e $udd) {
+ rename $udd, "$udd~" or die "could not make backup of $udd: $!";
+}
+
+open my $fh, '>', $udd or die "could not open $udd for writing: $!";
+print $fh <<'UDD';
+XDG_DESKTOP_DIR="$HOME/Workspace"
+XDG_DOCUMENTS_DIR="$HOME/Files"
+XDG_DOWNLOAD_DIR="$HOME/Files/Downloads"
+XDG_MUSIC_DIR="$HOME/Files/Audio"
+XDG_PICTURES_DIR="$HOME/Files/Images"
+XDG_PUBLICSHARE_DIR="$HOME/public_html"
+XDG_TEMPLATES_DIR="$HOME/Files/Document templates"
+XDG_VIDEOS_DIR="$HOME/Files/Video"
+UDD
+close $fh;
+
+is xdg_desktop_dir, catfile($ENV{HOME}, 'Workspace');
+is xdg_documents_dir, catfile($ENV{HOME}, 'Files');
+is xdg_download_dir, catfile($ENV{HOME}, 'Files/Downloads');
+is xdg_music_dir, catfile($ENV{HOME}, 'Files/Audio');
+is xdg_pictures_dir, catfile($ENV{HOME}, 'Files/Images');
+is xdg_publicshare_dir, catfile($ENV{HOME}, 'public_html');
+is xdg_templates_dir, catfile($ENV{HOME}, 'Files/Document templates');
+is xdg_videos_dir, catfile($ENV{HOME}, 'Files/Video');
+
+END {
+ if (-e "$udd~") {
+ rename "$udd~", $udd or die "could not restore backup of $udd: $!";
+ } else {
+ unlink $udd or die "could not delete $udd: $!";
+ }
+}
--
1.6.4