Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    ra130fc5 recd4edf  
    55
    66use base qw(Exporter);
    7 our @EXPORT_OK = qw(command getcurmsg getnumcols getidletime
     7our @EXPORT_OK = qw(command getcurmsg getnumcols getnumlines getidletime
     8                    register_idle_watcher unregister_idle_watcher
    89                    zephyr_getsender zephyr_getrealm zephyr_zwrite
    910                    zephyr_stylestrip zephyr_smartstrip_user zephyr_getsubs
    1011                    queue_message admin_message
     12                    start_edit
    1113                    start_question start_password start_edit_win
    1214                    get_data_dir get_config_dir popless_text popless_ztext
    1315                    error debug
    1416                    create_style getnumcolors wordwrap
     17                    message_matches_filter
    1518                    add_dispatch remove_dispatch
    1619                    add_io_dispatch remove_io_dispatch
    1720                    new_command
    1821                    new_variable_int new_variable_bool new_variable_string
     22                    new_variable_enum
    1923                    quote redisplay);
    2024our %EXPORT_TAGS = (all => \@EXPORT_OK);
     
    4347
    4448use List::Util qw(max);
     49use Tie::RefHash;
    4550
    4651=head1 NAME
     
    9297command line, and C<MESSAGE> is the zephyr body to send.
    9398
     99=cut
     100
     101sub zephyr_zwrite {
     102    my ($command, $message) = @_;
     103    my $ret = BarnOwl::Internal::zephyr_zwrite($command, $message);
     104    die "Error sending zephyr" unless $ret == 0;
     105}
     106
    94107=head2 ztext_stylestrip STRING
    95108
     
    105118Enqueue a message in the BarnOwl message list, logging it and
    106119processing it appropriately. C<MESSAGE> should be an instance of
    107 BarnOwl::Message or a subclass.  Returns the queued message.  This
    108 is useful for, e.g., deleting a message from the message list.
     120BarnOwl::Message or a subclass.
    109121
    110122=head2 admin_message HEADER BODY
     
    112124Display a BarnOwl B<Admin> message, with the given header and body.
    113125
     126=head2 start_edit %ARGS
     127
     128Displays a prompt on the screen and lets the user enter text,
     129and calls a callback when the editwin is closed.
     130
     131C<%ARGS> must contain the following keys:
     132
     133=over 4
     134
     135=item prompt
     136
     137The line to display on the screen
     138
     139=item type
     140
     141One of:
     142
     143=over 4
     144
     145=item edit_win
     146
     147Displays the prompt on a line of its own and opens the edit_win.
     148
     149=item question
     150
     151Displays prompt on the screen and lets the user enter a line of
     152text.
     153
     154=item password
     155
     156Like question, but echoes the user's input as C<*>s when they
     157input.
     158
     159=back
     160
     161=item callback
     162
     163A Perl subroutine that is called when the user closes the edit_win.
     164C<CALLBACK> gets called with two parameters: the text the user entered,
     165and a C<SUCCESS> boolean parameter which is false if the user canceled
     166the edit_win and true otherwise.
     167
     168=back
     169
    114170=head2 start_question PROMPT CALLBACK
    115171
    116 Displays C<PROMPT> on the screen and lets the user enter a line of
    117 text, and calls C<CALLBACK>, which must be a perl subroutine
    118 reference, with the text the user entered
    119 
    120172=head2 start_password PROMPT CALLBACK
    121173
    122 Like C<start_question>, but echoes the user's input as C<*>s when they
    123 input.
    124 
    125174=head2 start_edit_win PROMPT CALLBACK
    126175
    127 Like C<start_question>, but displays C<PROMPT> on a line of its own
    128 and opens the editwin. If the user cancels the edit win, C<CALLBACK>
    129 is not invoked.
     176Roughly equivalent to C<start_edit> called with the appropriate parameters.
     177C<CALLBACK> is only called on success, for compatibility.
     178
     179These are deprecated wrappers around L<BarnOwl::start_edit>, and should not
     180be uesd in new code.
     181
     182=cut
     183
     184sub start_edit {
     185    my %args = (@_);
     186    BarnOwl::Internal::start_edit($args{type}, $args{prompt}, $args{callback});
     187}
     188
     189sub start_question {
     190    my ($prompt, $callback) = @_;
     191    BarnOwl::start_edit(type => 'question', prompt => $prompt, callback => sub {
     192            my ($text, $success) = @_;
     193            $callback->($text) if $success;
     194        });
     195}
     196
     197sub start_password {
     198    my ($prompt, $callback) = @_;
     199    BarnOwl::start_edit(type => 'password', prompt => $prompt, callback => sub {
     200            my ($text, $success) = @_;
     201            $callback->($text) if $success;
     202        });
     203}
     204
     205sub start_edit_win {
     206    my ($prompt, $callback) = @_;
     207    BarnOwl::start_edit(type => 'edit_win', prompt => $prompt, callback => sub {
     208            my ($text, $success) = @_;
     209            $callback->($text) if $success;
     210        });
     211}
    130212
    131213=head2 get_data_dir
     
    160242
    161243Returns the number of colors this BarnOwl is capable of displaying
     244
     245=head2 message_matches_filter MESSAGE FILTER_NAME [QUIET = 0]
     246
     247Returns 1 if C<FILTER_NAME> is the name of a valid filter, and
     248C<MESSAGE> matches that filter.  Returns 0 otherwise.  If
     249C<QUIET> is false, this method displays an error message if
     250if C<FILTER_NAME> does not name a valid filter.
    162251
    163252=head2 add_dispatch FD CALLBACK
     
    259348our @all_commands;
    260349
    261 if(!$configfile && -f $ENV{HOME} . "/.barnowlconf") {
    262     $configfile = $ENV{HOME} . "/.barnowlconf";
    263 }
    264 $configfile ||= $ENV{HOME}."/.owlconf";
     350if(!$configfile) {
     351    if (-f get_config_dir() . "/init.pl") {
     352        $configfile = get_config_dir() . "/init.pl";
     353    } elsif (-f $ENV{HOME} . "/.barnowlconf") {
     354        $configfile = $ENV{HOME} . "/.barnowlconf";
     355    } else {
     356        $configfile = $ENV{HOME}."/.owlconf";
     357    }
     358}
    265359
    266360# populate global variable space for legacy owlconf files
     
    319413=head2 new_variable_string NAME [{ARGS}]
    320414
    321 Add a new owl variable, either an int, a bool, or a string, with the
     415=head2 new_variable_enum NAME [{ARGS}]
     416
     417Add a new owl variable, either an int, a bool, a string, or an enum with the
    322418specified name.
    323419
    324 ARGS can optionally contain the following keys:
     420For new_variable_enum, ARGS is required to contain a validsettings key pointing
     421to an array reference. For all four, it can optionally contain the following
     422keys:
    325423
    326424=over 4
     
    340438=back
    341439
     440In addition, new_variable_string optionally accepts a string validsettings
     441parameter, in case people want to set it to "<path>".
     442
    342443=cut
    343444
    344445sub new_variable_int {
    345     unshift @_, \&BarnOwl::Internal::new_variable_int, 0;
    346     goto \&_new_variable;
     446    my ($name, $args) = @_;
     447    my $storage = defined($args->{default}) ? $args->{default} : 0;
     448    BarnOwl::new_variable_full($name, {
     449            %{$args},
     450            get_tostring => sub { "$storage" },
     451            set_fromstring => sub {
     452                die "Expected integer" unless $_[0] =~ /^-?[0-9]+$/;
     453                $storage = 0 + $_[0];
     454            },
     455            validsettings => "<int>",
     456            takes_on_off => 0,
     457        });
    347458}
    348459
    349460sub new_variable_bool {
    350     unshift @_, \&BarnOwl::Internal::new_variable_bool, 0;
    351     goto \&_new_variable;
     461    my ($name, $args) = @_;
     462    my $storage = defined($args->{default}) ? $args->{default} : 0;
     463    BarnOwl::new_variable_full($name, {
     464            %{$args},
     465            get_tostring => sub { $storage ? "on" : "off" },
     466            set_fromstring => sub {
     467                die "Valid settings are on/off" unless $_[0] eq "on" || $_[0] eq "off";
     468                $storage = $_[0] eq "on";
     469            },
     470            validsettings => "on,off",
     471            takes_on_off => 1,
     472        });
    352473}
    353474
    354475sub new_variable_string {
    355     unshift @_, \&BarnOwl::Internal::new_variable_string, "";
    356     goto \&_new_variable;
    357 }
    358 
    359 sub _new_variable {
    360     my $func = shift;
    361     my $default_default = shift;
     476    my ($name, $args) = @_;
     477    my $storage = defined($args->{default}) ? $args->{default} : "";
     478    BarnOwl::new_variable_full($name, {
     479            # Allow people to override this one if they /reaaally/ want to for
     480            # some reason. Though we still reserve the right to interpret this
     481            # value in interesting ways for tab-completion purposes.
     482            validsettings => "<string>",
     483            %{$args},
     484            get_tostring => sub { $storage },
     485            set_fromstring => sub { $storage = $_[0]; },
     486            takes_on_off => 0,
     487        });
     488}
     489
     490sub new_variable_enum {
     491    my ($name, $args) = @_;
     492
     493    # Gather the valid settings.
     494    die "validsettings is required" unless defined($args->{validsettings});
     495    my %valid;
     496    map { $valid{$_} = 1 } @{$args->{validsettings}};
     497
     498    my $storage = (defined($args->{default}) ?
     499                   $args->{default} :
     500                   $args->{validsettings}->[0]);
     501    BarnOwl::new_variable_full($name, {
     502            %{$args},
     503            get_tostring => sub { $storage },
     504            set_fromstring => sub {
     505                die "Invalid input" unless $valid{$_[0]};
     506                $storage = $_[0];
     507            },
     508            validsettings => join(",", @{$args->{validsettings}})
     509        });
     510}
     511
     512=head2 new_variable_full NAME {ARGS}
     513
     514Create a variable, in full generality. The keyword arguments have types below:
     515
     516 get_tostring : ()  -> string
     517 set_fromstring : string -> int
     518 -- optional --
     519 summary : string
     520 description : string
     521 validsettings : string
     522 takes_on_off : int
     523
     524The get/set functions are required. Note that the caller manages storage for the
     525variable. get_tostring/set_fromstring both convert AND store the value.
     526set_fromstring dies on failure.
     527
     528If the variable takes parameters 'on' and 'off' (i.e. is boolean-looking), set
     529takes_on_off to 1. This makes :set VAR and :unset VAR work. set_fromstring will
     530be called with those arguments.
     531
     532=cut
     533
     534sub new_variable_full {
    362535    my $name = shift;
    363536    my $args = shift || {};
    364537    my %args = (
    365         summary     => "",
     538        summary => "",
    366539        description => "",
    367         default     => $default_default,
     540        takes_on_off => 0,
     541        validsettings => "<string>",
    368542        %{$args});
    369     $func->($name, $args{default}, $args{summary}, $args{description});
     543
     544    die "get_tostring required" unless $args{get_tostring};
     545    die "set_fromstring required" unless $args{set_fromstring};
     546
     547    # Strip off the bogus dummy argument. Aargh perl-Glib.
     548    my $get_tostring_fn = sub { $args{get_tostring}->() };
     549    my $set_fromstring_fn = sub {
     550      my ($dummy, $val) = @_;
     551      # Translate from user-supplied die-on-failure callback to expected
     552      # non-zero on error. Less of a nuisance than interacting with ERRSV.
     553      eval { $args{set_fromstring}->($val) };
     554      # TODO: Consider changing B::I::new_variable to expect string|NULL with
     555      # string as the error message. That can then be translated to a GError in
     556      # owl_variable_set_fromstring. For now the string is ignored.
     557      return ($@ ? -1 : 0);
     558    };
     559
     560    BarnOwl::Internal::new_variable($name, $args{summary}, $args{description}, $args{validsettings},
     561                                    $args{takes_on_off}, $get_tostring_fn, $set_fromstring_fn, undef);
    370562}
    371563
     
    603795}
    604796
     797=head3 register_idle_watcher %ARGS
     798
     799Call a callback whenever the amount of time the user becomes idle or comes
     800back from being idle.
     801
     802You must include the following parameters:
     803
     804=over 4
     805
     806=item name
     807
     808The name given to the idle watcher
     809
     810=item after
     811
     812How long the user must be idle, in seconds, before the callback is called.
     813If the value is too small, you may have spurious or inaccurate calls.
     814(The current lower limit is about 1 second.)
     815
     816=item callback
     817
     818The Perl subroutine that gets called when the user has been idle for C<AFTER>
     819seconds, or comes back from being idle.  The subroutine is passed one parameter,
     820which is true if the user is currently idle, and false otherwise.
     821
     822=back
     823
     824This method returns a unique identifier which may be passed to
     825L<BarnOwl::unregister_idle_watcher>.
     826
     827=cut
     828
     829=head3 unregister_idle_watcher UNIQUE_ID [...]
     830
     831Removed and returns the idle watcher specified by C<UNIQUE_ID>.
     832You may specify multiple unique ids.
     833
     834=cut
     835
     836my %idle_watchers;
     837tie %idle_watchers, 'Tie::RefHash';
     838
     839$BarnOwl::Hooks::wakeup->add(sub {
     840        foreach my $idle_watcher (values %idle_watchers) {
     841            _wakeup_idle_watcher($idle_watcher);
     842        }
     843    });
     844
     845sub _wakeup_idle_watcher {
     846    my ($idle_watcher, $offset) = @_;
     847    $offset = 0 unless defined $offset;
     848    # go unidle
     849    $idle_watcher->{idle_timer}->stop if $idle_watcher->{idle_timer};
     850    undef $idle_watcher->{idle_timer};
     851    $idle_watcher->{callback}->(0) if $idle_watcher->{is_idle};
     852    $idle_watcher->{is_idle} = 0;
     853
     854    # queue going idle
     855    $idle_watcher->{idle_timer} = BarnOwl::Timer->new({
     856        name  => $idle_watcher->{name},
     857        after => $idle_watcher->{after} - $offset,
     858        cb    => sub {
     859            $idle_watcher->{is_idle} = 1;
     860            $idle_watcher->{callback}->(1);
     861        }
     862    });
     863}
     864
     865sub register_idle_watcher {
     866    my %args = (@_);
     867    $idle_watchers{\%args} = \%args;
     868    _wakeup_idle_watcher(\%args, BarnOwl::getidletime); # make sure to queue up the idle/unidle events from this idle watcher
     869    return \%args;
     870}
     871
     872sub unregister_idle_watcher {
     873    my ($id) = @_;
     874    $idle_watchers{$id}->{idle_timer}->stop if $idle_watchers{$id}->{idle_timer};
     875    return delete $idle_watchers{$id};
     876}
     877
    605878# Stub for owl::startup / BarnOwl::startup, so it isn't bound to the
    606879# startup command. This may be redefined in a user's configfile.
Note: See TracChangeset for help on using the changeset viewer.