Changes in client/bin/gbrm [7cdd65d:fc8707b]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
client/bin/gbrm
r7cdd65d rfc8707b 1 1 #!/usr/bin/perl 2 2 3 # Written by Jessica Hamrick (C) 2010 3 # This script was largely written by Jessica Hamrick (jhamrick), with 4 # help from Kyle Brogle (broglek) 4 5 5 6 use strict; … … 10 11 use Getopt::Long; 11 12 12 my $usage = "Usage: gbq [-q QUEUE] ID\n"; 13 # usage 14 my $usage = <<USAGE; 15 Usage: gbq [options] [-q QUEUE] ID 16 17 -q, --queue Specify a queue other than the default 18 -h, --help Print this message 19 USAGE 20 21 # initialize the variables for the options 13 22 my $q = ""; 14 GetOptions ('q|queue=s' => \$q); 23 my $help = 0; 24 25 # parse the options 26 GetOptions ('q|queue=s' => \$q, 27 'h|help', => \$help); 28 29 # if the -h flag was passed, then print the usage and exit 30 if ($help) { 31 print $usage; 32 exit 0; 33 } 15 34 16 35 my @ids = @ARGV[0 .. $#ARGV]; 17 36 18 if (!$q){ 37 # if the -q option is not specified, then assume we're using the 38 # default queue 39 if (!$q) { 19 40 $q = "DEFAULT"; 20 41 } 42 43 # if there are no ids specified to remove, then print the usage 21 44 if (!@ids) { 22 45 print $usage; … … 24 47 } 25 48 26 49 # set configuration path, and complain if it doesn't exist 27 50 my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; 28 51 if (! -e $configpath) { … … 31 54 } 32 55 56 # initialize the host and queue variables: host holds the address for 57 # the machine on which the remote queue runs, and queue holds the name 58 # of the printer 33 59 my ($host, $queue); 34 60 61 # load the configuration file (this will set $host and $queue) 35 62 if (-r $configpath) { 36 63 local $/; … … 40 67 } 41 68 69 # initialize a new CUPS session 42 70 my $cups = Net::CUPS->new(); 71 # set the server to the one specified in the config file 43 72 $cups->setServer("$host"); 73 # set the printer name to the one specified in the config file 44 74 my $printer = $cups->getDestination("$queue"); 75 76 # if $printer is not defined, then throw an error 45 77 unless( $printer){ 46 78 print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n"; 47 79 exit 1; 48 80 } 81 82 # get the list of jobs from the printer 49 83 my @jobs = $printer->getJobs(0, 0); 84 85 # for each id that we want to remove 50 86 foreach my $id(@ids){ 51 if ($id eq "all") { 52 foreach $id(@jobs) { 87 88 # if the id is 'all', then we remove all jobs 89 if ($id eq "all") { 90 foreach $id(@jobs) { 91 cancel_job($id, $printer); 92 } 93 } 94 95 # if the id is 'current', then we remove just the currently 96 # playing job 97 elsif ($id eq "current") { 98 $id = $jobs[0]; 53 99 cancel_job($id, $printer); 54 100 } 55 } 56 elsif ($id eq "current") { 57 $id = $jobs[0];58 cancel_job($id, $printer);59 } 60 elsif ($id eq "last") { 61 $id = $jobs[-1]; 62 cancel_job($id, $printer);63 } 64 else{65 foreach my $item(@jobs){66 if($item =~ /$id/){67 cancel_job($item, $printer);101 102 # if the id is 'last', then we remove just the last job 103 elsif ($id eq "last") { 104 $id = $jobs[-1]; 105 cancel_job($id, $printer); 106 } 107 108 # otherwise, remove the job based on its actual (numeric) id 109 else { 110 foreach my $item(@jobs) { 111 if($item =~ /$id/){ 112 cancel_job($item, $printer); 113 } 68 114 } 69 115 } 70 116 } 71 }72 117 118 # helper function to remove a job 73 119 sub cancel_job { 120 # get the id and printer from the arguments 74 121 my ($id, $printer) = @_; 122 # get the reference to the job 75 123 my $job_ref = $printer->getJob($id); 124 # find the job title (so we can print it out for the user) 76 125 my $title = $job_ref->{'title'}; 126 # cancel the job 77 127 $printer->cancelJob($id); 78 128 129 # print out that we canceled the job 79 130 print "Canceled job '$title' (id $id)\n"; 80 81 131 }
Note: See TracChangeset
for help on using the changeset viewer.