source: client/bin/gbrm @ 6b7441a

debianmacno-cups
Last change on this file since 6b7441a was 6b7441a, checked in by Jessica B. Hamrick <jhamrick@…>, 14 years ago

Add comments to the client scripts.

  • Property mode set to 100755
File size: 2.7 KB
RevLine 
[2a2f76c]1#!/usr/bin/perl
2
[6b7441a]3# This script was largely written by Jessica Hamrick (jhamrick), with
4# help from Kyle Brogle (broglek)
[2a2f76c]5
6use strict;
7use warnings;
8
[a81397c]9use Net::CUPS;
10use Net::CUPS::Destination;
[7cdd65d]11use Getopt::Long;
[a81397c]12
[7cdd65d]13my $usage = "Usage: gbq [-q QUEUE] ID\n";
14my $q = "";
15GetOptions ('q|queue=s' => \$q);
[2a2f76c]16
[7cdd65d]17my @ids = @ARGV[0 .. $#ARGV];
[2a2f76c]18
[6b7441a]19# if the -q option is not specified, then assume we're using the
20# default queue
[7cdd65d]21if (!$q){
22    $q = "DEFAULT";
23}
[6b7441a]24
25# if there are no ids specified to remove, then print the usage
[7cdd65d]26if (!@ids) {
[2a2f76c]27    print $usage;
28    exit 1
29}
30
[6b7441a]31# set configuration path, and complain if it doesn't exist
[2a2f76c]32my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
[85a1ac1]33if (! -e $configpath) {
[0d02eca]34    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
[85a1ac1]35    exit 1;
36}
37
[6b7441a]38# initialize the host and queue variables: host holds the address for
39# the machine on which the remote queue runs, and queue holds the name
40# of the printer
[2a2f76c]41my ($host, $queue);
42
[6b7441a]43# load the configuration file (this will set $host and $queue)
[2a2f76c]44if (-r $configpath) {
45    local $/;
46    my $fh;
47    open $fh, $configpath;
48    eval <$fh>;
49}
50
[6b7441a]51# initialize a new CUPS session
[b58aada]52my $cups = Net::CUPS->new();
[6b7441a]53# set the server to the one specified in the config file
[b58aada]54$cups->setServer("$host");
[6b7441a]55# set the printer name to the one specified in the config file
[b58aada]56my $printer = $cups->getDestination("$queue");
[6b7441a]57
58# if $printer is not defined, then throw an error
[9fdf4a1]59unless( $printer){
60    print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n";
61    exit 1;
62}
[6b7441a]63
64# get the list of jobs from the printer
[b58aada]65my @jobs = $printer->getJobs(0, 0);
[6b7441a]66
67# for each id that we want to remove
[335786f]68foreach my $id(@ids){
[6b7441a]69
70    # if the id is 'all', then we remove all jobs
71    if ($id eq "all") {
72        foreach $id(@jobs) {
73            cancel_job($id, $printer);
74        }
75    }
76
77    # if the id is 'current', then we remove just the currently
78    # playing job
79    elsif ($id eq "current") {
80        $id = $jobs[0];
[d768767]81        cancel_job($id, $printer);
[b58aada]82    }
[6b7441a]83
84    # if the id is 'last', then we remove just the last job
85    elsif ($id eq "last") {
86        $id = $jobs[-1];
87        cancel_job($id, $printer);
88    }
89
90    # otherwise, remove the job based on its actual (numeric) id
91    else {
92        foreach my $item(@jobs) {
93            if($item =~ /$id/){
94                cancel_job($item, $printer);
95            }
[335786f]96        }
97    }
98}
[d768767]99
[6b7441a]100# helper function to remove a job
[d768767]101sub cancel_job {
[6b7441a]102    # get the id and printer from the arguments
[d768767]103    my ($id, $printer) = @_;
[6b7441a]104    # get the reference to the job
[d768767]105    my $job_ref = $printer->getJob($id);
[6b7441a]106    # find the job title (so we can print it out for the user)
[d768767]107    my $title = $job_ref->{'title'};
[6b7441a]108    # cancel the job
[b58aada]109    $printer->cancelJob($id);
[d768767]110
[6b7441a]111    # print out that we canceled the job
[d768767]112    print "Canceled job '$title' (id $id)\n";
[2a2f76c]113}
Note: See TracBrowser for help on using the repository browser.