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
Line 
1#!/usr/bin/perl
2
3# This script was largely written by Jessica Hamrick (jhamrick), with
4# help from Kyle Brogle (broglek)
5
6use strict;
7use warnings;
8
9use Net::CUPS;
10use Net::CUPS::Destination;
11use Getopt::Long;
12
13my $usage = "Usage: gbq [-q QUEUE] ID\n";
14my $q = "";
15GetOptions ('q|queue=s' => \$q);
16
17my @ids = @ARGV[0 .. $#ARGV];
18
19# if the -q option is not specified, then assume we're using the
20# default queue
21if (!$q){
22    $q = "DEFAULT";
23}
24
25# if there are no ids specified to remove, then print the usage
26if (!@ids) {
27    print $usage;
28    exit 1
29}
30
31# set configuration path, and complain if it doesn't exist
32my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
33if (! -e $configpath) {
34    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
35    exit 1;
36}
37
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
41my ($host, $queue);
42
43# load the configuration file (this will set $host and $queue)
44if (-r $configpath) {
45    local $/;
46    my $fh;
47    open $fh, $configpath;
48    eval <$fh>;
49}
50
51# initialize a new CUPS session
52my $cups = Net::CUPS->new();
53# set the server to the one specified in the config file
54$cups->setServer("$host");
55# set the printer name to the one specified in the config file
56my $printer = $cups->getDestination("$queue");
57
58# if $printer is not defined, then throw an error
59unless( $printer){
60    print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n";
61    exit 1;
62}
63
64# get the list of jobs from the printer
65my @jobs = $printer->getJobs(0, 0);
66
67# for each id that we want to remove
68foreach my $id(@ids){
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];
81        cancel_job($id, $printer);
82    }
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            }
96        }
97    }
98}
99
100# helper function to remove a job
101sub cancel_job {
102    # get the id and printer from the arguments
103    my ($id, $printer) = @_;
104    # get the reference to the job
105    my $job_ref = $printer->getJob($id);
106    # find the job title (so we can print it out for the user)
107    my $title = $job_ref->{'title'};
108    # cancel the job
109    $printer->cancelJob($id);
110
111    # print out that we canceled the job
112    print "Canceled job '$title' (id $id)\n";
113}
Note: See TracBrowser for help on using the repository browser.