File: gprintersettings.cpp

package info (click to toggle)
photoprint 0.4.1-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,568 kB
  • ctags: 4,517
  • sloc: cpp: 46,643; sh: 10,360; ansic: 4,755; makefile: 543; sed: 16
file content (537 lines) | stat: -rw-r--r-- 14,898 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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
262
263
264
265
266
267
268
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
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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 * gprintersettings.cpp - Bridges GutenPrint's stp_vars_t and the configuration file.
 * A subclass of ConfigSectionHandler.
 *
 * Copyright (c) 2004, 2008 by Alastair M. Robinson
 * Distributed under the terms of the GNU General Public License -
 * see the file named "COPYING" for more details.
 *
 * TODO: Add support for multi-line parameters (heredoc syntax?)
 */

#include <iostream>

#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>

#include "stp_support/stputil.h"
#include "gprintersettings.h"

#include "../support/debug.h"
#include "../support/util.h"

#include "../miscwidgets/generaldialogs.h"

#include "../config.h"
#include "../gettext.h"

#define _(x) gettext(x)
#define N_(x) gettext_noop(x)


#define DEFAULT_PPD_STRING "<Default Queue PPD>"

using namespace std;


static void dumpstrings(const stp_parameter_t *desc)
{
	int j,strcount;
	stp_string_list_t *strlist=desc->bounds.str;
	if(strlist)
	{
		strcount=stp_string_list_count(strlist);
		for(j=0;j<strcount;++j)
		{
			stp_param_string_t *p=stp_string_list_param(strlist,j);
			fprintf(stderr,"   %s (%s)\n",p->text,p->name);
		}
	}
	else
		fprintf(stderr,"  No string list!\n");
}


static void dumpvars(stp_vars_t *v)
{
	stp_parameter_list_t params = stp_get_parameter_list(v);
	
	int count = stp_parameter_list_count(params);
	
	for (int i = 0; i < count; i++)
	{
		const stp_parameter_t *p = stp_parameter_list_param(params, i);
//		if((p->p_level<=STP_PARAMETER_LEVEL_ADVANCED4)
//			&& ((p->p_class==STP_PARAMETER_CLASS_FEATURE) || (p->p_class==STP_PARAMETER_CLASS_OUTPUT)))
		{
			stp_parameter_t desc;
			stp_describe_parameter(v,p->name,&desc);
			const char *str;
			int ival;
			double fval;

			fprintf(stderr,"%s, %d, %d, %d, %d ",desc.name,desc.p_type,p->p_type,desc.p_class,desc.p_level);
			
			if(desc.is_active)
			{
				fprintf(stderr,", Active");
			}
			else
			{
				fprintf(stderr,", Inactive");
			}
				switch(desc.p_type)
				{
					case STP_PARAMETER_TYPE_STRING_LIST:
						str=stp_get_string_parameter(v,desc.name);
						fprintf(stderr,", StringList\n");
						dumpstrings(&desc);
						if(str)
							fprintf(stderr,"  Currently set to: %s\n",str);
						break;

					case STP_PARAMETER_TYPE_INT:
						ival=stp_get_int_parameter(v,desc.name);
						fprintf(stderr,", Int\n");
						fprintf(stderr,"  Currently set to: %d\n",ival);
						break;

					case STP_PARAMETER_TYPE_BOOLEAN:
						ival=stp_get_boolean_parameter(v,desc.name);
						fprintf(stderr,", Boolean\n");
						fprintf(stderr,"  Currently set to: %d\n",ival);
						break;

					case STP_PARAMETER_TYPE_DOUBLE:
						fval=stp_get_float_parameter(v,desc.name);
						fprintf(stderr,", Double\n");
						fprintf(stderr,"  Currently set to: %f\n",fval);
						break;

					default:
						fprintf(stderr,", Other\n");
						break;					
			}
		}
	}
	stp_parameter_list_destroy(params);
}


void GPrinterSettings::Dump()
{
	dumpvars(stpvars);
}


GPrinterSettings::GPrinterSettings(PrintOutput &output,ConfigFile *inf,const char *section)
	: ConfigSectionHandler(inf,section), PageExtent(), stpvars(NULL), output(output),
	initialised(false), ppdsizes_workaround_done(false)
{
	stpvars=stp_vars_create();

	// Set the driver to that of the default queue in case no preset is loaded
	const char *driver=output.FindString("Driver");
	if(!SetDriver(driver))
		output.SetString("Driver",DEFAULT_PRINTER_DRIVER);
}


GPrinterSettings::~GPrinterSettings()
{
	if(stpvars)
		stp_vars_destroy(stpvars);
}


void GPrinterSettings::SelectSection()
{
	/* Slight hack here: The printer driver is stored in the [Output] section
	   which is prior to the [Print] section.  Clearing this forces the driver
	   to be set before the printer options are set */

	initialised=false;
}


void GPrinterSettings::ParseString(const char *string)
{
	if(!initialised)
	{
		const char *driver=output.FindString("Driver");
		if(!SetDriver(driver))
			output.SetString("Driver",DEFAULT_PRINTER_DRIVER);
		initialised=true;
	}

	while(*string==' ')
		++string;
	
	char *value;
	char *token;
	value=token=strdup(string);
	int l=strlen(token)-1;
	while((l>0) && ((token[l]=='\n') || (token[l]=='\r')))
		--l;
	token[l+1]=0;

	while(1)
	{
		switch (*value)
		{
			case '\0':
				free(token);
				return;
				break;
			case '=':
				*value='\0';
				++value;
				if(*value)
				{
					if(strcmp("CustomWidth",token)==0)
					{
						Debug[TRACE] << "Setting custom width to: " << value << endl;
						stp_set_page_width(stpvars,pagewidth=atoi(value));
					}
					else if(strcmp("CustomHeight",token)==0)
					{
						Debug[TRACE] << "Setting custom height to: " << value << endl;
						stp_set_page_height(stpvars,pageheight=atoi(value));
					}					
					else
					{
						stp_parameter_t param;
						stp_describe_parameter(stpvars,token,&param);
						switch(param.p_type)
						{
							case STP_PARAMETER_TYPE_STRING_LIST:
								if(strcmp("PageSize",token)==0)
									Debug[TRACE] << "Setting PageSize to: " << value << endl;
								stp_set_string_parameter(stpvars,token,value);
								break;
		
							case STP_PARAMETER_TYPE_FILE:
								// Because the queue PPD filenames returned by CUPS
								// aren't persistent between sessions, we have to
								// save an escape string, and substitute the current
								// filename at preset loading time.
								if(strcmp(value,DEFAULT_PPD_STRING)==0)
								{
									Debug[TRACE] << "*** Fetching default PPD filename" << endl;
									char *defppd=output.GetPPD();
									if(defppd)
									{
										Debug[TRACE] << "Got default PPD filename: " << defppd << endl;
										stp_set_file_parameter(stpvars,token,defppd);
										free(defppd);

										stp_parameter_t desc2;
										stp_describe_parameter(stpvars,"PageSize",&desc2);
										Debug[TRACE] << "After setting PPD Default page size is now: " << desc2.deflt.str << endl;
										stp_set_string_parameter(stpvars,"PageSize",desc2.deflt.str);
										stp_parameter_description_destroy(&desc2);
										ppdsizes_workaround_done=true;
									}
									else
										Debug[TRACE] << "Couldn't get default PPD." << endl;
								}
								else
									stp_set_file_parameter(stpvars,token,value);
								break;

							case STP_PARAMETER_TYPE_INT:
	//							Debug[TRACE] << "Setting " << token << " to " << value << endl;
								stp_set_int_parameter(stpvars,token,atoi(value));
								break;
		
							case STP_PARAMETER_TYPE_BOOLEAN:
	//							Debug[TRACE] << "Setting " << token << " to " << value << endl;
								stp_set_boolean_parameter(stpvars,token,atoi(value));
								break;
		
							case STP_PARAMETER_TYPE_DOUBLE:
	//							Debug[TRACE] << "Setting " << token << " to " << value << endl;
								stp_set_float_parameter(stpvars,token,atof(value));
								break;
		
							default:
								break;
						}
						stp_parameter_description_destroy(&param);
					}
				}
				free(token);
				return;
				break;
		}
		++value;
	}
}


void GPrinterSettings::SaveSection(FILE *file)
{
	stp_parameter_list_t params = stp_get_parameter_list(stpvars);
	stp_parameter_t desc;
	desc.is_active=false;

	int count = stp_parameter_list_count(params);
	
	// PPDFile parameter must be saved first, because it must be restored
	// before other parameters upon loading.
	// Because CUPS returns a local temporary filename for a queue's PPD
	// which is not persistent between sessions, we must save an escape
	// string to indicate that the queue's default should be used.

	stp_describe_parameter(stpvars,"PPDFile",&desc);
	if(desc.is_active)
	{
		Debug[TRACE] << "Saving PPDFile parameter..." << endl;
		const char *ppd=stp_get_file_parameter(stpvars,"PPDFile");
		char *defppd=output.GetPPD();
		if(ppd)
			Debug[TRACE] << "Current PPD: " << ppd << endl;
		if(defppd)
			Debug[TRACE] << "Default PPD: " << defppd << endl;
		if(defppd && ppd && CompareFiles(defppd,ppd))
			ppd=DEFAULT_PPD_STRING;
		if(!ppd)
			ppd=DEFAULT_PPD_STRING;
		if(defppd)
			free(defppd);
		fprintf(file,"PPDFile=%s\n",ppd);
	}

	for (int i = 0; i < count; i++)
	{
		const stp_parameter_t *p = stp_parameter_list_param(params, i);
		if((p->p_level<=STP_PARAMETER_LEVEL_ADVANCED4))
		{
			stp_parameter_t desc;
			stp_describe_parameter(stpvars,p->name,&desc);
			if(desc.is_active)
			{
				switch(desc.p_type)
				{
					case STP_PARAMETER_TYPE_STRING_LIST:
						{
							if(stp_check_string_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								const char *str=stp_get_string_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || strcmp(str,desc.deflt.str)!=0)
									fprintf(file,"%s=%s\n",desc.name,str);
							}
						}
						break;

					case STP_PARAMETER_TYPE_INT:
						{
							if(stp_check_int_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								int val=stp_get_int_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || val!=desc.deflt.integer)
									fprintf(file,"%s=%d\n",desc.name,val);
							}
						}
						break;

					case STP_PARAMETER_TYPE_BOOLEAN:
						{
							if(stp_check_boolean_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								int val=stp_get_boolean_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || val!=desc.deflt.boolean)
									fprintf(file,"%s=%d\n",desc.name,val);
							}
						}
						break;

					case STP_PARAMETER_TYPE_DOUBLE:
						{
							if(stp_check_float_parameter(stpvars,desc.name,STP_PARAMETER_DEFAULTED))
							{
								double val=stp_get_float_parameter(stpvars,desc.name);
//								if(!desc.is_mandatory || val!=desc.deflt.dbl)
									fprintf(file,"%s=%f\n",desc.name,val);
							}
						}
						break;

					default:
						break;					
				}     
			}
			stp_parameter_description_destroy(&desc);
		}
	}

	fprintf(file,"CustomWidth=%d\n",pagewidth);
	fprintf(file,"CustomHeight=%d\n",pageheight);

	stp_parameter_list_destroy(params);
}


// SetDriver
// Returns false if the driver isn't recognised, true otherwise.

bool GPrinterSettings::SetDriver(const char *driver)
{
	bool result=true;
	bool driverchanged=false;

	// FIXME - Not pretty.  We need to strdup() the result of this because it's
	// not valid after the driver has changed.
	const char *_oldpagesize=stp_get_string_parameter(stpvars,"PageSize");
	char *oldpagesize=NULL;
	if(_oldpagesize)
	{
		oldpagesize=strdup(_oldpagesize);
		Debug[TRACE] << "Old page size is: " << oldpagesize << endl;
	}

	Debug[TRACE] << "Checking stpvars" << endl;
	if(stpvars)
	{
		// We avoid messing with this stuff as much as possible if the driver didn't change -
		// that way you can change from a printer's queue to "Save to file" without
		// messing up the print settings.
		const char *olddriver=stp_get_driver(stpvars);
		Debug[TRACE] << "Checking olddriver" << endl;
		if(!olddriver)
			olddriver="None";
		Debug[TRACE] << "Checking driver" << endl;
		if(!driver)
			driver=DEFAULT_PRINTER_DRIVER;
		Debug[TRACE] << "Comparing drivers:" << olddriver << " against " << driver << endl;
		if(strcmp(driver,olddriver)==0) // If the driver hasn't changed...
		{
			// We ensure we can get the printer.  If we can't, chances are the Gutenprint
			// data files weren't loaded correctly.
			if(!stp_get_printer(stpvars))
				throw _("Can't obtain printer from Gutenprint\nCheck STP_DATA_PATH and Gutenprint version!");
		}
		else
		{
			driverchanged=true;
			Debug[TRACE] << "SetDriver(): Setting driver to " << driver << endl;

			// Work around the non-defaulting of inactive settings...
			const stp_vars_t *defaults=stp_default_settings();
			stp_vars_copy(stpvars,defaults);

			stp_set_driver(stpvars,driver);
			output.SetString("Driver",driver);

			const stp_printer_t *printer=stp_get_printer(stpvars);
			Debug[TRACE] << "Checking printer" << endl;
			if(printer)
			{
				Debug[TRACE] << "Setting defaults" << endl;
				stp_set_printer_defaults(stpvars,printer);
			}
			else
			{
				Debug[TRACE] << "Unable to get printer - reverting to default driver" << endl;
				output.SetString("Driver",DEFAULT_PRINTER_DRIVER);
				stp_set_driver(stpvars,DEFAULT_PRINTER_DRIVER);
				Debug[TRACE] << "Checking printer again" << endl;
				if((printer=stp_get_printer(stpvars)))
					stp_set_printer_defaults(stpvars,printer);
				else
					Debug[TRACE] << "Still can't get printer!" << endl;
				result=false;
			}
		}
		stp_set_page_width(stpvars,pagewidth);
		stp_set_page_height(stpvars,pageheight);


		stp_parameter_t desc;
		desc.is_active=false;
		stp_describe_parameter(stpvars,"PPDFile",&desc);
		if(desc.is_active)
		{
			driverchanged=true;
			Debug[TRACE] << "Getting default PPD..." << endl;
			char *defppd=output.GetPPD();
			Debug[TRACE] << "Checking defppd" << endl;
			if(defppd)
			{
				Debug[TRACE] << "Setting PPDFile to " << defppd << endl;
				stp_set_file_parameter(stpvars,"PPDFile",defppd);
				free(defppd);
				
				Debug[TRACE] << "Checking ppdsizes_workaround_done" << endl;
				if(!ppdsizes_workaround_done)
				{
					stp_parameter_t desc2;
					stp_describe_parameter(stpvars,"PageSize",&desc2);
					Debug[TRACE] << "After setting PPD Default page size is now: " << desc2.deflt.str << endl;
					stp_set_string_parameter(stpvars,"PageSize",desc2.deflt.str);
					stp_parameter_description_destroy(&desc2);
					ppdsizes_workaround_done=true;
				}
			}
		}
		stp_parameter_description_destroy(&desc);

		if(driverchanged)
		{
			// We attempt to set the previously used pagesize if possible.
			// We do this by looping through the known papersizes for the
			// new driver and comparing against the old papersize.
			if(oldpagesize)
			{
				Debug[TRACE] << "Old page size is: " << oldpagesize << endl;
				stp_describe_parameter(stpvars,"PageSize",&desc);
				stp_string_list_t *strlist=desc.bounds.str;
				if(strlist)
				{
					int strcount=stp_string_list_count(strlist);
					for(int j=0;j<strcount;++j)
					{
						stp_param_string_t *p=stp_string_list_param(strlist,j);
						Debug[TRACE] << "Comparing against " << p->text << endl;
						if(strcmp(p->text,oldpagesize)==0)
						{
							stp_set_string_parameter(stpvars,"PageSize",oldpagesize);
							j=strcount;
						}
					}
				}
				stp_parameter_description_destroy(&desc);
				free(oldpagesize);
			}

			Validate();
		}
	}
	else
		Debug[TRACE] << "No stp vars!" << endl;
	return(result);
}


void GPrinterSettings::Validate()
{
	stputil_validate_parameters(stpvars);
}


void GPrinterSettings::Reset()
{
	stp_vars_destroy(stpvars);
	stpvars=stp_vars_create();
	// FIXME - a bit of a hack this - set the driver so we can compare against it.
	// (We set the queue's own driver here in case the app doesn't over-ride it
	// Make sure this doesn't break anything...)
	const char *driver=output.FindString("Driver");
	if(!SetDriver(driver))
		output.SetString("Driver",DEFAULT_PRINTER_DRIVER);
//	stp_set_driver(stpvars,"ps");

	Debug[TRACE] << "Created fresh stp_vars" << endl;
	initialised=false;
}