Package: gbackground
Version: 1.2-1
Severity: wishlist
Tags: patch

gbackground would be much more usable if it saved a configuration file
- The patch I am attaching implements it, using YAML.

Greetings,

-- System Information:
Debian Release: squeeze/sid
  APT prefers unstable
  APT policy: (500, 'unstable')
Architecture: amd64 (x86_64)

Kernel: Linux 2.6.32-5-openvz-amd64 (SMP w/2 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages gbackground depends on:
ii  libglib-perl                  2:1.223-1  interface to the GLib and GObject 
ii  libgtk2-gladexml-perl         1.007-1    Perl interface to use user interfa
ii  libgtk2-perl                  2:1.222-1  Perl interface to the 2.x series o
ii  libproc-pid-file-perl         1.27-1     Perl module for managing process i
ii  libui-dialog-perl             1.08-1.1   UI::Dialog a wrapper for various d
ii  perl                          5.10.1-14  Larry Wall's Practical Extraction 

gbackground recommends no packages.

gbackground suggests no packages.

-- no debconf information
--- /usr/bin/gbackground        2009-08-25 18:35:06.000000000 -0500
+++ /tmp/gbackground-1.2/gbackground    2010-08-31 11:38:36.000000000 -0500
@@ -5,17 +5,20 @@
 use UI::Dialog;
 use UI::Dialog::Backend::Zenity;
 use Switch;
+use YAML;
 
 ## Variables Declaration
 
-my($program, $main_window, $background_path, $exit_button, $path_images, $pid, 
$label_number_of_seconds, $number_of_seconds, $label_status, $about_window, 
$about, $preferences_window, $filechooserbutton, $file_choose);
+my($config, $program, $main_window, $exit_button, $label_number_of_seconds, 
$label_status, $about_window, $about, $preferences_window, $filechooserbutton, 
$file_choose);
 
-my(@status, $home_directory, $path, $path_pid, $last_background);
+my(@status, $path, $last_background);
+my $path = "$ENV{HOME}/.gbackground";
+my $configfile = "$path/gbackground.yml";
 
 ## From Preferences window
-my($return_value_widget, $type_of_change_widget, $type_of_change, 
$last_change);
-my $return_value = 1;
+my($return_value_widget, $type_of_change_widget);
 
+load_config();
 &trash;
 
 ## Loading from UI::Dialog
@@ -46,12 +49,46 @@
 
 Gtk2->main;
 
+END {
+    save_config();
+}
+
 ## Subroutines ##
+sub config_defaults {
+    return { seconds => 5,
+            bg_dir => '/usr/share/backgrounds',
+            keep_last_bg => 1,
+            # bg_options:
+            # 0 => scaled, 1 => stretched, 2 => wallpaper, 3 => centered
+            # (hard-coded in gbackground.glade)
+            bg_options => 1, 
+            pid => "$path/gbackground.pid"
+    };
+}
+
+sub load_config {
+    my ($defaults);
+    mkdir($path, 0755) unless -d $path;
+    $defaults = &config_defaults;
+    if (-e $configfile) {
+       $config = YAML::LoadFile($configfile);
+       # Use default values for whatever has not been specified
+       map( { $config->{$_} = $defaults->{$_} unless defined $config->{$_} }
+            keys(%$defaults) );
+    } else {
+       $config = $defaults;
+    }
+}
+
+sub save_config {
+    my $file;
+    return undef unless $config;
+    mkdir($path, 0755) unless -d $path;
+    YAML::DumpFile($configfile, $config);
+}
+
 sub trash {
        @status = ("<span foreground=\"blue\">Running</span>", "<span 
foreground=\"red\">Stopped</span>");
-       $home_directory = $ENV{HOME};
-       $path_pid = $home_directory . "/.gbackground" . "/gbackground.pid";
-       $last_change = 'stretched';
 }
 sub load_widgets {
        $main_window = $program->get_widget('main_window');
@@ -63,11 +100,26 @@
        $return_value_widget = 
$program->get_widget('keep_last_bground_combobox');
        $type_of_change_widget = 
$program->get_widget('background_options_select');
        $filechooserbutton = $program->get_widget('filechooserbutton');
-       
$filechooserbutton->set_current_folder_uri("/usr/share/images/desktop-base/");
+       # Set values from configuration
+       $label_number_of_seconds->set_text($config->{seconds});
+       $filechooserbutton->set_current_folder($config->{bg_dir});
+       $return_value_widget->set_active($config->{keep_last_bg});
+       $type_of_change_widget->set_active($config->{bg_options});
+}
+
+# Returns the PID if it is running, undef otherwise
+sub is_running {
+    my ($fh, $pid);
+    return undef if ! -e $config->{pid};
+    open($fh, '<', $config->{pid});
+    $pid = $fh->getline;
+    chomp $pid;
+    # We assume to have a working /proc
+    return (-d "/proc/$pid") ? $pid : undef;
 }
 
 sub check_status {
-       if(-e $path_pid){
+       if(is_running()){
                $label_status->set_markup("@status[0]");
        }else{
                $label_status->set_markup("@status[1]");
@@ -75,44 +127,41 @@
 }
 
 sub on_start_button_event {
-       $path_images = $filechooserbutton->get_filename;
+       $config->{bg_dir} = $filechooserbutton->get_filename;
        $last_background = `gconftool-2 --get 
/desktop/gnome/background/picture_filename`;
        $last_background =~ s/\s/\\ /g;
-       if(-e "$path_pid"){
+       if(is_running()){
                $window_dialogs->error(title => 'Error!!', text => 'Gbackground 
<b>is already</b> working');
                }
        else{
-               if(-e $path_images){
-                       $number_of_seconds = 
$label_number_of_seconds->get_text();
-                       $path_images =~ s/\s/\\ /g;
+               if(-e $config->{bg_dir}){
+                       $config->{seconds} = 
$label_number_of_seconds->get_text();
+                       $config->{bg_dir} =~ s/\s/\\ /g;
                        if(-e './gbackgroundD'){
-                               system("perl ./gbackgroundD \"$path_images\" 
$number_of_seconds $last_change \&");
+                               system("perl ./gbackgroundD 
\"$config->{bg_dir}\" $config->{seconds} $config->{bg_options} \&");
                        } else {
-                               system("perl 
/usr/share/gbackground/gbackgroundD \"$path_images\" $number_of_seconds 
$last_change \&");
+                               system("perl 
/usr/share/gbackground/gbackgroundD \"$config->{bg_dir}\" $config->{seconds} 
$config->{bg_options} \&");
                        }
                        
                        sleep 1; # Esto es por que muchas veces tarda en crear 
el gbackground.pid
-                       $label_status->set_markup("@status[0]") if(-e 
"$path_pid");
+                       $label_status->set_markup("@status[0]") 
if(is_running());
                        $window_dialogs->msgbox(title => "Daemon Gbackground", 
text => 'Gbackground has started <b>successfully</b>');  
                }
                else{
                        $window_dialogs->error(title => "Error, dir doesn't 
exist", text => 'The directory that you introduced <b>is not valid</b>, please 
verify it and try again');   
                }
        }
+       save_config();
 }
 sub on_stop_button_event {
-       if(-e "$path_pid"){
-               open(ARCHIVO_PID, "$path_pid");
-               while(<ARCHIVO_PID>){
-                       $pid = $_;
-               }
+       if(my $pid = is_running()){
                system("kill -9 $pid");
-               system("rm -f $path_pid");
-               if($return_value == 1){
+               unlink($config->{pid});
+               if($config->{keep_last_bg} == 1){
                                system("gconftool-2 -t str -s 
/desktop/gnome/background/picture_filename $last_background");
                }
                $window_dialogs->msgbox(title => "Daemon Gbackground", text => 
'Gbackground has been <b>stopped</b> successfully'); 
-               $window_dialogs->error(title => 'Error!!', text => 'Error, it 
was not possible to stop Gbackground. Probably because it <b>is not</b> 
running') if(-e "$path_pid");
+               $window_dialogs->error(title => 'Error!!', text => 'Error, it 
was not possible to stop Gbackground. Probably because it <b>is not</b> 
running') if(is_running());
        }else{
                $window_dialogs->error(title => 'Error!!', text => 'Gbackground 
<b>is not</b> running');        
        }
@@ -120,8 +169,8 @@
 }
 
 sub on_preferences_accept_button_event {
-       $return_value = $return_value_widget->get_active;
-       $last_change = $type_of_change_widget->get_active_text();
+       $config->{keep_last_bg} = $return_value_widget->get_active;
+       $config->{bg_options} = $type_of_change_widget->get_active_text();
        $preferences_window->hide;
 }
 
@@ -144,7 +193,7 @@
 sub on_quit_button_event {Gtk2->main_quit;}
 sub on_about_menu_event {$about->present;}
 sub on_preferences_event {
-       my $value = $type_of_change_widget->set_active("1");
+       my $value = $type_of_change_widget->set_active($config->{bg_options});
        $preferences_window->show_all;
 }
 sub on_preferences_cancel_button_event {$preferences_window->hide;}

Reply via email to