Correct support for persistent options.
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Task.pm
index 04d5a3c..0cbce82 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: Task.pm,v 1.1 2006-10-06 11:33:07 mike Exp $
+# $Id: Task.pm,v 1.6 2007-05-09 11:30:53 mike Exp $
 
 package ZOOM::IRSpy::Task;
 
@@ -32,11 +32,13 @@ pointer to the next task to be performed after this.
 
 sub new {
     my $class = shift();
-    my($conn, %cb) = @_;
+    my($conn, $udata, $options, %cb) = @_;
 
     return bless {
        irspy => $conn->{irspy},
        conn => $conn,
+       udata => $udata,
+       options => $options,
        cb => \%cb,
        timeRegistered => time(),
     }, $class;
@@ -53,11 +55,48 @@ sub conn {
     return $this->{conn};
 }
 
+sub udata {
+    my $this = shift();
+    return $this->{udata};
+}
+
 sub run {
     my $this = shift();
     die "can't run base-class task $this";
 }
 
+# In general, this sets the Connection's options to what is in the
+# task's option hash and sets the option-hash entry to the
+# Connection's old value of the option: this means that calling this
+# twice restores the state to how it was before.  (That's not quite
+# true as its impossible to unset an option that was previously set:
+# such options are instead set to the empty string.)
+#
+# As a special case, options in the task's option-hash whose names
+# begin with an asterisk are taken to be persistent: they are set into
+# the Connection (with the leading hash removed) and deleted from the
+# task's option-hash so that they will NOT be reset the next time this
+# function is called.
+#
+sub set_options {
+    my $this = shift();
+
+    foreach my $key (sort keys %{ $this->{options} }) {
+       my $value = $this->{options}->{$key};
+       my $persistent = ($key =~ s/^\*//);
+       $value = "" if !defined $value;
+       $this->conn()->log("irspy_debug", "$this setting option '$key' -> ",
+                          defined $value ? "'$value'" : "undefined");
+       my $old = $this->conn()->option($key, $value);
+       if ($persistent) {
+           print "deleting '*$key'<br/>\n";
+           delete $this->{options}->{"*$key"}
+       } else {
+           $this->{options}->{$key} = $old;
+       }
+    }
+}
+
 sub render {
     my $this = shift();
     return "[base-class] " . ref($this);