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.



53
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 53

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);
  rb_iv_set(self, "@url_path", rb_str_new2(cupsServer()));
  
  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

#url_pathObject (readonly)

Instance Method Details

#cancelBoolean

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

Returns:

  • (Boolean)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/cups.c', line 224

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)


345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'ext/cups.c', line 345

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(jobs != NULL)
        free(jobs);
        
      if(printer_arg != NULL)
        free(printer_arg);
      
      if (job_state == IPP_JOB_COMPLETED) {
        return Qtrue;
      } else {
        return Qfalse;
      }
      
    }
  }  
}

#error_codeFixnum

Get the last IPP error code.

Returns:

  • (Fixnum)


282
283
284
285
286
287
288
289
290
291
292
# File 'ext/cups.c', line 282

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)


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

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)


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

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
163
164
165
166
167
168
169
170
171
172
173
174
175
# 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");
  VALUE url_path = rb_iv_get(self, "@url_path");

  char *fname = RSTRING_PTR(file); // Filename
  char *target = RSTRING_PTR(printer); // Target printer string
  char *url = RSTRING_PTR(url_path); // Server URL address
  int port = 631; // Default CUPS port
    
  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);
  }

  if(NIL_P(url)) {
    url = cupsServer();
  }
    
  int encryption = (http_encryption_t)cupsEncryption();
  http_t *http = httpConnectEncrypt (url, port, (http_encryption_t) encryption);
  job_id = cupsPrintFile2(http, 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));
  
  
  // 25.03 free memory
  if(fname != NULL)
    free(fname);
    
  if(url != NULL)
    free(url);
    
  if(target != NULL)
    free(target);
    
  if(http != NULL)
    free(http);
    
  return Qtrue;    
}

#stateString

Get human-readable state of current job.

Returns:

  • (String)


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'ext/cups.c', line 300

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);

    if(jobs != NULL)
      free(jobs);
      
    if(printer_arg != NULL)
      free(printer_arg);

    return jstate;
  }
}