#!/usr/bin/perl -w use strict; use warnings; use Getopt::Std; getopts('pieh', \my %opts); # help requested? usage() && exit if $opts{h}; # get the current desktop & background image (if any) my $desktop = getCurrentDesktop(); my $currentImage = getCurrentImage($desktop); # set up the location of the artwork directory and index file my $backgroundDir = "/home/dave/BackgroundArt"; my $indexfile = "$backgroundDir/.index.txt"; # Setup complete, now take an action based on the flags passed in # info request for current image? info($currentImage) && exit if $opts{i}; # edit request for current image? edit($currentImage) && exit if $opts{e}; # otherwise we must be changing to a new image -- assume going forward unless -p specified my $direction = $opts{p} ? -1 : +1; # figure out the next image from the index file my $nextImage = getNextImage($currentImage, $direction); # change the wallpaper for the current desktop `dcop kdesktop KBackgroundIface setWallpaper $desktop $nextImage 4`; # thats it... exit 0; ########################################### sub info { ########################################### my ( $currentImage ) = @_; my $infofile = getInfoFile($currentImage); my @data = readFile($infofile); if ( @data ) { `kdialog --passivepopup "@data" 60`; } return 1; } ########################################### sub edit { ########################################### my ( $currentImage ) = @_; my $infofile = getInfoFile($currentImage); my @data = readFile($infofile); my $rv = `kdialog --title "Add/Edit" --textinputbox "Add/Edit details for $currentImage" "@data"`; # Now save the resulting text back to the info file writeFile($infofile, $rv); return 1; } ########################################### sub getInfoFile { ########################################### my ( $infofile ) = @_; $infofile =~ s#^(.*)/(.*)#$1/.info/$2.txt#; return $infofile; } ########################################### sub readFile { ########################################### my ($filename ) = @_; my @data; if ( -e $filename ) { open (FILE, "< $filename") or die "Error: couldn't open file $filename: $!\n"; @data = ; close FILE; } return @data; } ########################################### sub writeFile { ########################################### my ( $filename, $data ) = @_; open ( FILE, "> $filename"); print FILE $data; close FILE; } ########################################### sub getCurrentDesktop { ########################################### my $rv = `dcop kwin KWinInterface currentDesktop`; chomp $rv; return $rv; } ########################################### sub getCurrentImage { ########################################### my ( $desktop ) = @_; my $wallpaper = `dcop kdesktop KBackgroundIface currentWallpaper $desktop`; chomp $wallpaper; # return a dummy value if no image currently set $wallpaper = "_noimage_" if !$wallpaper; return $wallpaper; } ########################################### sub getNextImage { ########################################### my ( $currentImage, $direction ) = @_; # first get the index of this image in the indexfile my $index = `grep -n $currentImage $indexfile | cut -d':' -f1`; chomp $index; # set to 0 if not found, since there may be either no image or one we haven't got listed $index = 0 if !$index; # we'll need to know the number of lines so we can wrap around if required my $length = `wc -l $indexfile | cut -d' ' -f1`; chomp $length; # increment/decrement index, wrapping around if required $index += $direction; $index = 1 if $index > $length; $index = $length if $index <= 0; # get the filename corresponding to this new index my $newBackground = `head -$index $indexfile | tail -1`; chomp $newBackground; return $newBackground; } ########################################### sub usage { ########################################### print <<"END"; usage: wallpaper