From 90812cb89ba8ad9047f6f56be5d77f6bcac93edf Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Wed, 27 Aug 2014 11:49:04 +0200 Subject: [PATCH] Initial release. --- GNUmakefile | 6 +++ xfce-xterm-launcher-update | 103 +++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 GNUmakefile create mode 100755 xfce-xterm-launcher-update diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 0000000..c486400 --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,6 @@ +all: + +install: $(HOME)/bin/xfce-xterm-launcher-update + +$(HOME)/bin/%: % + cp $^ $@ diff --git a/xfce-xterm-launcher-update b/xfce-xterm-launcher-update new file mode 100755 index 0000000..cf66588 --- /dev/null +++ b/xfce-xterm-launcher-update @@ -0,0 +1,103 @@ +#!/usr/bin/perl + +use warnings; +use strict; +use File::Slurp; +use Data::Dumper; + +my $launcher = $ARGV[0]; + +my $launcher_dir = "$ENV{HOME}/.config/xfce4/panel/launcher-$launcher"; + +my @desktop_files = glob("$launcher_dir/*.desktop"); + +my %desktop_files; +for (@desktop_files) { + my $content = read_file($_); + my ($name) = $content =~ /^Name=(\N*)/ms; + $desktop_files{$name} = { name => $name, filename => $_, content => $content } +} + +my @xterm_scripts = glob("$ENV{HOME}/bin/hosts/*"); +@xterm_scripts = sort { -A $a <=> -A $b } @xterm_scripts; +@xterm_scripts = @xterm_scripts[0..31]; +for my $xterm_script (@xterm_scripts) { + my ($name) = $xterm_script =~ m{([^/]+)$}; + if ($desktop_files{$name}) { + $desktop_files{$name}{current} = 1; + parse($desktop_files{$name}); + my $icon_file = icon_file($name); + if ($desktop_files{$name}{content_fields}{Icon} ne $icon_file) { + $desktop_files{$name}{content_fields}{Icon} = $icon_file; + replace($desktop_files{$name}); + say STDERR "replaced desktop entry $desktop_files{$name}{filename} for $name"; + } + } else { + + $desktop_files{$name} = { + content_fields => { + Name=> $name, + Type=> 'Application', + Exec=> "xterm -e $name", + Terminal=> 'false', + Icon=> icon_file($name), + Path=> "$ENV{HOME}/tmp", + StartupNotify=> 'false', + }, + current => 1 + }; + replace($desktop_files{$name}); + say STDERR "created new desktop entry $desktop_files{$name}{filename} for $name"; + } +} + +$desktop_files{XTerm}{current} = 1; # we always want that + +for my $name (sort keys %desktop_files) { + unless ($desktop_files{$name}{current}) { + say STDERR "deleting obsolete desktop entry $desktop_files{$name}{filename} for $name"; + unlink($desktop_files{$name}{filename}); + } +} + +sub icon_file { + my ($name) = @_; + for ("$ENV{HOME}/icons/$name.png", + "$ENV{HOME}/icons/$name.jpg") { + return $_ if -f $_; + } + return "xterm-color_48x48"; +} + +sub parse { + my ($desktop_file) = @_; + for (split(/\n/, $desktop_file->{content})) { + if (my ($k, $v) = m/^(\w+)=(.+)/) { + $desktop_file->{content_fields}{$k} = $v; + } + } +} + +sub replace { + my ($desktop_file) = @_; + + if ($desktop_file->{filename}) { + unlink($desktop_file->{filename}); + } + my $filename; + my $base = "$launcher_dir/" . time(); + my $ext = 0; + # XXX - possible race condition, but we don't expect this script + # to run concurrently for the same user. + for(;;) { + $filename = "$base$ext.desktop"; + last unless (-f $filename); + $ext++; + } + open(my $fh, '>', $filename); + print $fh "[Desktop Entry]\n"; + for my $k (sort keys $desktop_file->{content_fields}) { + print $fh "$k=$desktop_file->{content_fields}{$k}\n"; + } + close($fh); +}