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

#titleObject

#url_pathObject (readonly)

Instance Method Details

#cancelBoolean

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

Returns:

  • (Boolean)


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'ext/cups.c', line 213

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)


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
359
360
361
362
# File 'ext/cups.c', line 327

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

  num_jobs = cupsGetJobs(&jobs, printer_arg, 1, -1); // Get jobs

  // find our job
  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)


271
272
273
274
275
276
277
278
279
280
281
# File 'ext/cups.c', line 271

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)


253
254
255
256
257
258
259
260
261
262
263
# File 'ext/cups.c', line 253

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)


236
237
238
239
240
241
242
243
244
245
# File 'ext/cups.c', line 236

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
# File 'ext/cups.c', line 108

static VALUE cups_print(VALUE self)
{
  int job_id;
  VALUE file = rb_iv_get(self, "@filename");
  VALUE rname = rb_iv_get(self, "@title");
  VALUE printer = rb_iv_get(self, "@printer");
  VALUE url_path = rb_iv_get(self, "@url_path");

  char *fname = RSTRING_PTR(file); // Filename
  char *title = T_STRING == TYPE(rname) ? RSTRING_PTR(rname) : "rCups";
  char *target = RSTRING_PTR(printer); // Target printer string
  const 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 = httpConnect2(url, port, NULL, AF_UNSPEC, (http_encryption_t) encryption, 1, 30000, NULL);

  job_id = cupsPrintFile2(http, target, fname, title, 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;
}

#stateString

Get human-readable state of current job.

Returns:

  • (String)


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
316
317
318
319
# File 'ext/cups.c', line 289

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