Class: Cups::PrintJob

Inherits:
Object
  • Object
show all
Defined in:
lib/cups/print_job/transient.rb,
ext/cups.c

Direct Known Subclasses

Transient

Defined Under Namespace

Classes: Transient

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'ext/cups.c', line 54

static VALUE job_init(int argc, VALUE* argv, VALUE self)
{
  VALUE filename, printer, job_options;
  
  rb_scan_args(argc, argv, "12", &filename, &printer, &job_options);
  
  rb_iv_set(self, "@filename", filename);
  
  if (NIL_P(job_options)) {
    rb_iv_set(self, "@job_options", rb_hash_new());
  } else {
    rb_iv_set(self, "@job_options", job_options);
  }

  if (NIL_P(printer)) {

    // Fall back to default printer
    VALUE def_p = rb_funcall(rubyCups, rb_intern("default_printer"), 0);

    if (def_p == Qfalse) {
			rb_raise(rb_eRuntimeError, "There is no default printer!");
		} else {
			rb_iv_set(self, "@printer", def_p);
		}
    
  } else {
    if (printer_exists(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)

#job_optionsObject (readonly)

#printerObject (readonly)

Cups::PrintJob Attributes

Instance Method Details

#cancelBoolean

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

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'ext/cups.c', line 208

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)


323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'ext/cups.c', line 323

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)


266
267
268
269
270
271
272
273
274
275
276
# File 'ext/cups.c', line 266

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)


248
249
250
251
252
253
254
255
256
257
258
# File 'ext/cups.c', line 248

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)


231
232
233
234
235
236
237
238
239
240
# File 'ext/cups.c', line 231

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)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/cups.c', line 108

static VALUE cups_print(VALUE self)
{
  int job_id;
  VALUE file = rb_iv_get(self, "@filename");
  VALUE 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);

    VALUE job_options = rb_iv_get(self, "@job_options");

    // Create an array of the keys from the job_options hash
    VALUE job_options_keys = rb_ary_new();
    rb_hash_foreach(job_options, cups_keys_i, job_options_keys);
  
    VALUE iter;
    int num_options = 0;
    cups_option_t *options = NULL;

    // foreach option in the job options array
    while (!  NIL_P(iter = rb_ary_pop(job_options_keys))) {

      VALUE value = rb_hash_aref(job_options, iter);

      // assert the key and value are strings
      if (NIL_P(rb_check_string_type(iter)) || NIL_P(rb_check_string_type(value))) {
        cupsFreeOptions(num_options, options);
        rb_raise(rb_eTypeError, "job options is not string => string hash");
        return Qfalse;
      }

      // convert to char pointers and add to cups optoins
      char * iter_str  = rb_string_value_ptr(&iter);
      char * value_str = rb_string_value_ptr(&value);
      cupsAddOption(iter_str, value_str, num_options++, &options);
    }

    job_id = cupsPrintFile(target, fname, "rCUPS", num_options, options); // Do it. "rCups" should be the filename/path

    cupsFreeOptions(num_options, options);

    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 Qfalse;
  }
}

#stateString

Get human-readable state of current job.

Returns:

  • (String)


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'ext/cups.c', line 284

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);
 
    jstate = ipp_state_to_symbol(job_state);

    return jstate;
  }
}