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.



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

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

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


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

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)


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'ext/cups.c', line 269

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)


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

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)


194
195
196
197
198
199
200
201
202
203
204
# File 'ext/cups.c', line 194

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)


177
178
179
180
181
182
183
184
185
186
# File 'ext/cups.c', line 177

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)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'ext/cups.c', line 88

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)


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/cups.c', line 230

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 = rb_str_new2(ipp_state_to_string(job_state));

    return jstate;
  }
}