Class: Cups::PrintJob

Inherits:
Object
  • Object
show all
Defined in:
ext/cups.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#new(filename, printer = nil) ⇒ Object

Initializes a new PrintJob object. If no target printer/class is specified, the default is chosen. Note the specified file does not have to exist until print is called.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'ext/cups.c', line 16

static VALUE job_init(int argc, VALUE* argv, VALUE self)
{
  VALUE filename, printer;
  
  rb_scan_args(argc, argv, "11", &filename, &printer);
  
  rb_iv_set(self, "@filename", filename);
  
  if (NIL_P(printer)) {

    // Fall back to default printer
    VALUE def_p = rb_funcall(rubyCups, rb_intern("default_printer"), 0);
    rb_iv_set(self, "@printer", def_p);
    
  } else {
    // First call Cups#show_destinations
    VALUE dest_list = rb_funcall(rubyCups, rb_intern("show_destinations"), 0);
    // Then check the printer arg is included in the returned array...
    if (rb_ary_includes(dest_list, printer)) {
      rb_iv_set(self, "@printer", printer);
    } else {
      rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
    }
  }
  return self;
}

Instance Attribute Details

#filenameObject (readonly)

#job_idObject (readonly)

#printerObject (readonly)

Cups::PrintJob Attributes

Instance Method Details

#cancelBoolean

Cancel the current job. Returns true if successful, false otherwise.

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/cups.c', line 115

static VALUE cups_cancel(VALUE self)
{
  VALUE printer, job_id;
  printer = rb_iv_get(self, "@printer");
  job_id = rb_iv_get(self, "@job_id");
  
  if (NIL_P(job_id)) {
    return Qfalse; // If @job_id is nil
  } else { // Otherwise attempt to cancel
    int job = NUM2INT(job_id);
    char *target = RSTRING_PTR(printer); // Target printer string
    int cancellation;
    cancellation = cupsCancelJob(target, job);
    return Qtrue;
  }
}

#completed?Boolean

Has the job completed?

Returns:

  • (Boolean)


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/cups.c', line 254

static VALUE cups_job_completed(VALUE self)
{
  VALUE job_id = rb_iv_get(self, "@job_id");
  VALUE printer = rb_iv_get(self, "@printer");
  VALUE jstate;

  int num_jobs;
  cups_job_t *jobs;
  ipp_jstate_t job_state = IPP_JOB_PENDING;
  int i;
  char *printer_arg = RSTRING_PTR(printer);

  if (NIL_P(job_id)) {
    return Qfalse;
  } else {
    num_jobs = cupsGetJobs(&jobs, printer_arg, 1, -1); // Get jobs
    // job_state = IPP_JOB_COMPLETED;

    for (i = 0; i < num_jobs; i ++) {
      if (jobs[i].id == NUM2INT(job_id)) {
        job_state = jobs[i].state;
        break;
      }
      
      // Free job array
      cupsFreeJobs(num_jobs, jobs);
      
      if (job_state == IPP_JOB_COMPLETED) {
        return Qtrue;
      } else {
        return Qfalse;
      }
      
    }
  }  
}

#error_codeFixnum

Get the last IPP error code.

Returns:

  • (Fixnum)


173
174
175
176
177
178
179
180
181
182
183
# File 'ext/cups.c', line 173

static VALUE cups_get_error_code(VALUE self)
{
  VALUE job_id = rb_iv_get(self, "@job_id");
  
  if (NIL_P(job_id) || !NUM2INT(job_id) == 0) {
    return Qnil;
  } else {
    VALUE ipp_error_code = INT2NUM(cupsLastError());
    return ipp_error_code;
  }
}

#error_reasonString

Get the last human-readable error string

Returns:

  • (String)


155
156
157
158
159
160
161
162
163
164
165
# File 'ext/cups.c', line 155

static VALUE cups_get_error_reason(VALUE self)
{
  VALUE job_id = rb_iv_get(self, "@job_id");
  
  if (NIL_P(job_id) || !NUM2INT(job_id) == 0) {
    return Qnil;
  } else {
    VALUE error_exp = rb_str_new2(cupsLastErrorString());
    return error_exp;
  }
}

#failed?Boolean

Did this job fail?

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
# File 'ext/cups.c', line 138

static VALUE cups_job_failed(VALUE self)
{
  VALUE job_id = rb_iv_get(self, "@job_id");

 if (NIL_P(job_id) || !NUM2INT(job_id) == 0) {
   return Qfalse;
 } else {
   return Qtrue;
 }
}

Submit a print job to the selected printer or class. Returns true on success.

Returns:

  • (Fixnum)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/cups.c', line 49

static VALUE cups_print(VALUE self, VALUE file, VALUE printer)
{
  int job_id;
  file = rb_iv_get(self, "@filename");
  printer = rb_iv_get(self, "@printer");
  
  char *fname = RSTRING_PTR(file); // Filename
  char *target = RSTRING_PTR(printer); // Target printer string
  
  FILE *fp = fopen(fname,"r");
  // Check @filename actually exists...
  if( fp ) {
    fclose(fp);
    job_id = cupsPrintFile(target, fname, "rCUPS", num_options, options); // Do it.
    rb_iv_set(self, "@job_id", INT2NUM(job_id));
    return Qtrue;
  } else {
  // and if it doesn't...
    rb_raise(rb_eRuntimeError, "Couldn't find file");
    return self;
  }
}

#stateString

Get human-readable state of current job.

Returns:

  • (String)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'ext/cups.c', line 191

static VALUE cups_get_job_state(VALUE self)
{
  VALUE job_id = rb_iv_get(self, "@job_id");
  VALUE printer = rb_iv_get(self, "@printer");
  VALUE jstate;
  
  int num_jobs;
  cups_job_t *jobs;
  ipp_jstate_t job_state = IPP_JOB_PENDING;
  int i;
  char *printer_arg = RSTRING_PTR(printer);
 
  if (NIL_P(job_id)) {
    return Qnil;
  } else {
    num_jobs = cupsGetJobs(&jobs, printer_arg, 1, -1); // Get jobs

    for (i = 0; i < num_jobs; i ++) {
      if (jobs[i].id == NUM2INT(job_id)) {
        job_state = jobs[i].state;
        break;
      }
    }

     // Free job array
     cupsFreeJobs(num_jobs, jobs);
 
    switch (job_state) {
      case IPP_JOB_PENDING :
        jstate = rb_str_new2("Pending...");
        break;
      case IPP_JOB_HELD :
        jstate = rb_str_new2("Held");
        break;
      case IPP_JOB_PROCESSING :
        jstate = rb_str_new2("Processing...");
        break;
      case IPP_JOB_STOPPED :
        jstate = rb_str_new2("Stopped");
        break;
      case IPP_JOB_CANCELED :
        jstate = rb_str_new2("Cancelled");
        break;
      case IPP_JOB_ABORTED :
        jstate = rb_str_new2("Aborted");
        break;
      case IPP_JOB_COMPLETED :
		jstate = rb_str_new2("Completed");
        break;
      default:
		jstate = rb_str_new2("Unknown Job Code...");
    }

    return jstate;
  }
}