~ubuntu-branches/ubuntu/karmic/photoprint/karmic

« back to all changes in this revision

Viewing changes to stp_support/printerqueues_unix.c

  • Committer: Bazaar Package Importer
  • Author(s): Milan Zamazal
  • Date: 2007-05-01 16:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20070501163213-k0gaendx7grjlmk5
Tags: upstream-0.3.5
ImportĀ upstreamĀ versionĀ 0.3.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
#include <fcntl.h>
5
5
#include <unistd.h>
6
6
#include <signal.h>
 
7
#include <sys/wait.h>
7
8
 
8
9
#ifdef HAVE_CONFIG_H
9
10
#include "config.h"
51
52
};
52
53
 
53
54
 
 
55
enum pqoutputmode
 
56
{
 
57
        PQMODE_COMMAND,
 
58
        PQMODE_CUSTOMCOMMAND,
 
59
        PQMODE_FILE     
 
60
};
 
61
 
 
62
 
54
63
struct pqprivate
55
64
{
56
65
        void (*Dispose)(struct pqprivate *pp);
59
68
        struct printernode *first;
60
69
        int cancelled;
61
70
        char *printcommand;
 
71
 
 
72
        enum pqoutputmode mode;
 
73
        char *customcommand;
 
74
        char *(*getfilecallback)(void *userdata);
 
75
        void *userdata;
 
76
 
62
77
        int pipefd[2];
63
 
        int childpid;
 
78
        int outputfd;
 
79
        int childpid;   
64
80
};
65
81
 
66
82
 
111
127
                free(pp->currentqueue);
112
128
        if(pp->printcommand)
113
129
                free(pp->printcommand);
 
130
        if(pp->customcommand)
 
131
                free(pp->customcommand);
114
132
        free(pp);
115
133
}
116
134
 
136
154
 
137
155
static void pqp_buildqueuelist(struct pqprivate *pp)
138
156
{
 
157
/* FIXME - should use libcups here, if available... */
 
158
 
139
159
        FILE *pfile;
140
 
        char buf[256];
 
160
        char buf[256];
 
161
 
 
162
        const char *old_lang=getenv("LC_ALL");
 
163
        setenv("LC_ALL","C",1);
141
164
 
142
165
        pqp_identifyprintsystem(pp);
143
166
        if ((pfile = popen(printsystems[pp->printsystem].scan_command, "r")))
155
178
                        printernode_create(pp,buf);
156
179
                }
157
180
                pclose(pfile);
158
 
        }
 
181
        }
 
182
        printernode_create(pp,PRINTERQUEUE_CUSTOMCOMMAND);
 
183
        printernode_create(pp,PRINTERQUEUE_SAVETOFILE);
 
184
 
 
185
        if(old_lang)
 
186
                setenv("LC_ALL",old_lang,1);
 
187
        else
 
188
                unsetenv("LC_ALL");
159
189
}
160
190
 
161
191
 
168
198
                pp->first=NULL;
169
199
                pp->currentqueue=NULL;
170
200
                pp->printcommand=NULL;
 
201
                pp->customcommand=NULL;
 
202
                pp->getfilecallback=NULL;
171
203
                pqp_buildqueuelist(pp);
172
204
        }
173
205
        return(pp);
222
254
                        free(pq->priv->currentqueue);
223
255
                pq->priv->currentqueue=strdup(queue);
224
256
        }
 
257
        if(strcmp(queue,PRINTERQUEUE_CUSTOMCOMMAND)==0)
 
258
                pq->priv->mode=PQMODE_CUSTOMCOMMAND;
 
259
        else if(strcmp(queue,PRINTERQUEUE_SAVETOFILE)==0)
 
260
                pq->priv->mode=PQMODE_FILE;
 
261
        else
 
262
                pq->priv->mode=PQMODE_COMMAND;
 
263
}
 
264
 
 
265
 
 
266
static const char *getprinterqueue(struct pqinfo *pq)
 
267
{
 
268
        return(pq->priv->currentqueue);
 
269
}
 
270
 
 
271
 
 
272
static void setcustomcommand(struct pqinfo *pq,const char *command)
 
273
{
 
274
        if(pq->priv->customcommand)
 
275
                free(pq->priv->customcommand);
 
276
        if(command)
 
277
                pq->priv->customcommand=strdup(command);
 
278
        else
 
279
                pq->priv->customcommand=NULL;
 
280
}
 
281
 
 
282
 
 
283
static const char *getcustomcommand(struct pqinfo *pq)
 
284
{
 
285
        return(pq->priv->customcommand);
 
286
}
 
287
 
 
288
 
 
289
static void setgetfilenamecallback(struct pqinfo *pq,char *(*func)(void *userdata),void *userdata)
 
290
{
 
291
        pq->priv->getfilecallback=func;
 
292
        pq->priv->userdata=userdata;
225
293
}
226
294
 
227
295
 
228
296
static char *getdriver(struct pqinfo *pq)
229
297
{
 
298
        if(strcmp(pq->priv->currentqueue,PRINTERQUEUE_SAVETOFILE)==0)
 
299
                return(NULL);
 
300
        if(strcmp(pq->priv->currentqueue,PRINTERQUEUE_CUSTOMCOMMAND)==0)
 
301
                return(NULL);
230
302
#ifdef HAVE_LIBCUPS
 
303
        fprintf(stderr,"Querying cups for printer driver...\n");
231
304
        char *result=NULL;
232
305
        const char *ppdname=cupsGetPPD(pq->priv->currentqueue);
233
306
        if(ppdname)
258
331
                        pclose(pfile);
259
332
                }
260
333
                free(cmd2);
 
334
                if(result)
 
335
                        fprintf(stderr,"Got driver: %s\n",result);
261
336
        }
262
337
        if(!result)
263
338
                result=strdup("ps2");
271
346
static char *getppd(struct pqinfo *pq)
272
347
{
273
348
        char *result=NULL;
274
 
#ifdef HAVE_LIBCUPS
 
349
#ifdef HAVE_LIBCUPS
 
350
        fprintf(stderr,"Current queue: %s\n",pq->priv->currentqueue);
275
351
        const char *ppdname=cupsGetPPD(pq->priv->currentqueue);
276
 
        result=strdup(ppdname);
 
352
        if(ppdname)
 
353
                result=strdup(ppdname);
277
354
#endif
278
355
        return(result);
279
356
}
299
376
 
300
377
static char *buildprintcommand(struct pqinfo *pq)
301
378
{
302
 
        /* FIXME - need to deduce whether or not to use the RAW flag */
303
 
        
304
379
        int rawmode=1;
305
380
        int len=strlen(printsystems[pq->priv->printsystem].print_command);
306
381
        len+=strlen(printsystems[pq->priv->printsystem].queue_select);
327
402
 
328
403
static int initialisejob(struct pqinfo *pq)
329
404
{
 
405
        if(pq->priv->mode==PQMODE_FILE)
 
406
        {
 
407
                char *fn=NULL;
 
408
                aborted=0;
 
409
                pq->priv->cancelled=0;
 
410
 
 
411
                if(pq->priv->getfilecallback)
 
412
                        fn=pq->priv->getfilecallback(pq->priv->userdata);
 
413
 
 
414
                if(!fn)
 
415
                        return(0);
 
416
 
 
417
                pq->priv->outputfd=-1;
 
418
                if(fn)
 
419
                        pq->priv->outputfd=open(fn,O_CREAT|O_RDWR,0644);
 
420
                free(fn);
 
421
 
 
422
                if(pq->priv->outputfd<0)
 
423
                        return(0);
 
424
                return(1);
 
425
        }
 
426
 
 
427
        // FIXME - bail out if the customcommand isn't set.
 
428
        if(pq->priv->mode==PQMODE_CUSTOMCOMMAND)
 
429
        {
 
430
                if(pq->priv->customcommand)
 
431
                        pq->priv->printcommand=strdup(pq->priv->customcommand);
 
432
                else
 
433
                        return(0);
 
434
        }
 
435
        else
 
436
                pq->priv->printcommand=buildprintcommand(pq);
 
437
        
330
438
        fprintf(stderr,"In initialisejob()\n");
331
439
        signal(SIGPIPE,&sighandler);
332
440
        aborted=0;
336
444
        // Workaroud for now - clear aborted and cancelled flags in the InitialisePage function.
337
445
        // Race condition, though - not a permanent fix. 
338
446
 
339
 
        pq->priv->printcommand=buildprintcommand(pq);
340
447
        fprintf(stderr,"Print command: %s\n",pq->priv->printcommand);
341
448
 
342
449
        if(pipe(pq->priv->pipefd))
343
450
                return(0);
 
451
        pq->priv->outputfd=pq->priv->pipefd[1];
344
452
 
345
453
        pq->priv->childpid=fork();
346
454
        if(pq->priv->childpid==-1)
360
468
        return(1);
361
469
}
362
470
 
 
471
 
363
472
static void initialisepage(struct pqinfo *pq)
364
473
{
365
474
        fprintf(stderr,"In initialisepage()\n");
368
477
        pq->priv->cancelled=0;
369
478
}
370
479
 
 
480
 
371
481
static void endpage(struct pqinfo *pq)
372
482
{
373
483
        fprintf(stderr,"In endpage()\n");
374
484
}
375
485
 
 
486
 
376
487
static void endjob(struct pqinfo *pq)
377
488
{
378
 
        fprintf(stderr,"In endjob()\n");
379
 
        if(pq->priv->cancelled)
380
 
        {
381
 
                kill(pq->priv->childpid,SIGTERM);
382
 
        }
383
 
        close(pq->priv->pipefd[0]);
384
 
        close(pq->priv->pipefd[1]);
385
 
        free(pq->priv->printcommand);
386
 
        pq->priv->printcommand=NULL;
387
 
        wait();
 
489
        if(pq->priv->mode==PQMODE_FILE)
 
490
        {
 
491
                close(pq->priv->outputfd);
 
492
        }
 
493
        else
 
494
        {
 
495
                fprintf(stderr,"In endjob()\n");
 
496
                if(pq->priv->cancelled)
 
497
                {
 
498
                        kill(pq->priv->childpid,SIGTERM);
 
499
                }
 
500
                close(pq->priv->pipefd[0]);
 
501
                close(pq->priv->pipefd[1]);
 
502
                free(pq->priv->printcommand);
 
503
                pq->priv->printcommand=NULL;
 
504
                int st;
 
505
                wait(&st);
 
506
        }
388
507
}
389
508
 
390
509
 
396
515
 
397
516
static int writedata(struct pqinfo *pq,const char *data,int bytecount)
398
517
{
399
 
        write(pq->priv->pipefd[1],data,bytecount);
 
518
        write(pq->priv->outputfd,data,bytecount);
400
519
        return(1-aborted);
401
520
}
402
521
 
410
529
                pq->GetPrinterCount=getprintercount;
411
530
                pq->GetPrinterName=getprintername;
412
531
                pq->SetPrinterQueue=setprinterqueue;
 
532
                pq->GetPrinterQueue=getprinterqueue;
413
533
                pq->GetDriver=getdriver;
414
534
                pq->GetPPD=getppd;
415
535
                pq->InitialiseJob=initialisejob;
418
538
                pq->EndJob=endjob;
419
539
                pq->CancelJob=canceljob;
420
540
                pq->WriteData=writedata;
 
541
                pq->SetCustomCommand=setcustomcommand;
 
542
                pq->GetCustomCommand=getcustomcommand;
 
543
                pq->SetGetFilenameCallback=setgetfilenamecallback;
421
544
 
422
545
                if(!(pq->priv=pqprivate_create()))
423
546
                {