4 * Copyright 2012 Samsung Electronics Co., Ltd
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://floralicense.org/license/
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <cups/cups.h>
23 #include <pt_optionmapping.h>
35 static int REAL_PAPERSIZE_MAX;
37 static counter X[] = {
38 { .id = PT_OPTION_ID_QUALITY, .desc = "QL", .current = PT_QUALITY_DRAFT,.min = PT_QUALITY_DRAFT, .max = PT_QUALITY_ANY },
39 { .id = PT_OPTION_ID_PAPERSIZE, .desc = "PS" , .current = PT_PAPER_A4, .min = PT_PAPER_A4, .max = PT_PAPER_SIZE_MAX },
40 { .id = PT_OPTION_ID_GRAYSCALE,.desc = "GL", .current = PT_GRAYSCALE_GRAYSCALE, .min = PT_GRAYSCALE_GRAYSCALE, .max = PT_GRAYSCALE_ANY },
41 { .id = PT_OPTION_ID_PAPER, .desc = "PP", .current = PT_PAPER_NORMAL, .min = PT_PAPER_NORMAL, .max = PT_PAPER_ANY },
42 {NULL, NULL, NULL, NULL, NULL}
45 const static int N = (sizeof(X) / sizeof(counter)) - 1; // Remove {NULL} value
47 bool reliable_set_choice(int id, int choice)
49 int old_choice = -1, new_choice = -1, result = -1;
50 old_choice = pt_get_selected(id);
51 result = pt_set_choice(id, choice);
52 new_choice = pt_get_selected(id);
54 if ((old_choice == -1) ||
57 PT_DEBUG("OptionMapping:");
58 PT_DEBUG("Internal Error");
62 if ((result == choice) &&
63 (result == new_choice)) { // choice is enabled
65 } else if ((old_choice == result) &&
66 (result == new_choice)) { //choice is disabled
68 } else { //OM internal error
69 PT_DEBUG("OptionMapping:");
70 PT_DEBUG("[Error] pt_set_choice doesn't work correctly");
75 void check_state(counter *array, int count)
81 for (i = array[count].min; i < array[count].max; ++i) {
82 //int old_choice = -1, new_choice = -1, result = -1;
83 bool set_choice = false;
84 array[count].current = i;
85 state st = save_state();
86 print_groups_before(count);
88 set_choice = reliable_set_choice(array[count].id, i);
90 printf(YE "%s: " RS, array[count].desc);
91 printf(GR "%d " RS, array[count].current);
92 print_groups_after(count);
93 check_state(array, ++count);
95 printf(YE "%s: " RS, array[count].desc);
96 printf(RD "%d " RS, i);
97 print_groups_after(count);
100 restore_state(st, &array[--count]);
105 void swap(int a,int b)
119 //const char *string = BOLD YE "%s %s %s %s\n" RS RS_BOLD;
120 printf("GROUP %d [DEFAULT VALUES]\n", cnt);
121 st = get_current_state();
122 // FIXME It's hack! As order of GROUPS can be changed in future
123 printf(YE "%d %d %d %d\n" RS, st.quality,st.papersize,st.grayscale,st.paper);
128 for (j=k; j<N; j++) {
136 void init_groups(counter *array)
139 while (array[cnt].desc) {
140 if (!strcmp(array[cnt].desc, "QL")) {
141 array[cnt].current = pt_get_selected(PT_OPTION_ID_QUALITY);
142 } else if (!strcmp(array[cnt].desc, "PS")) {
143 array[cnt].max = pt_get_print_option_papersize_num();
144 REAL_PAPERSIZE_MAX = array[cnt].max;
145 if (REAL_PAPERSIZE_MAX < PT_PAPER_A4) {
146 PT_DEBUG("[papersize] max value [%d] is out of the correct values: [%d-%d]",\
147 REAL_PAPERSIZE_MAX, PT_PAPER_A4, PT_PAPER_SIZE_MAX);
150 array[cnt].current = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
151 } else if (!strcmp(array[cnt].desc, "GL")) {
152 array[cnt].current = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
153 } else if (!strcmp(array[cnt].desc, "PP")) {
154 array[cnt].current = pt_get_selected(PT_OPTION_ID_PAPER);
156 printf("Unknown description of option!");
165 int main(int argc, char **argv)
168 //option_list *option_list;
170 PT_DEBUG("No any ppd file");
173 ppd = open_ppd_file(argv[1]);
174 pt_parse_options(ppd);
177 // Print current state of OptionMapping
178 // before any manipulation
179 state st = get_current_state();
180 printf(BOLD YE "DEFAULT:\n" RS RS_BOLD);
182 printf(BOLD YE "%s %s %s %s\n" RS RS_BOLD, X[0].desc,X[1].desc,X[2].desc,X[3].desc);
183 // FIXME It's hack! As order of GROUPS can be changed in future
184 printf(YE "%d %d %d %d\n" RS, st.quality,st.papersize,st.grayscale,st.paper);
192 st.quality = pt_get_selected(PT_OPTION_ID_QUALITY);
193 st.paper = pt_get_selected(PT_OPTION_ID_PAPER);
194 st.grayscale = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
195 st.papersize = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
200 if ((st.quality == -1) || (st.paper == -1) ||
201 (st.grayscale == -1) || (st.papersize == -1)) {
202 PT_DEBUG("Returned value not correct:");
203 PT_DEBUG("[GRAYSCALE]: %d", st.grayscale);
204 PT_DEBUG("[QUALITY]: %d", st.quality);
205 PT_DEBUG("[PAPER]: %d", st.paper);
206 PT_DEBUG("[PAPERSIZE]: %d", st.papersize);
212 state get_current_state()
215 st.quality = pt_get_selected(PT_OPTION_ID_QUALITY);
216 st.paper = pt_get_selected(PT_OPTION_ID_PAPER);
217 st.grayscale = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
218 st.papersize = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
220 if ((st.quality < PT_QUALITY_DRAFT) || (st.quality >= PT_QUALITY_ANY)) {
221 PT_DEBUG("[quality] choice [%d] is out of the correct values: [%d-%d]",\
222 st.quality, PT_QUALITY_DRAFT, (PT_QUALITY_ANY-1));
225 if ((st.paper < PT_PAPER_NORMAL) || (st.paper >= PT_PAPER_ANY)) {
226 PT_DEBUG("[paper] choice [%d] is out of the correct values: [%d-%d]",\
227 st.paper, PT_PAPER_NORMAL, (PT_PAPER_ANY-1));
230 if ((st.grayscale < PT_GRAYSCALE_GRAYSCALE) || (st.grayscale >= PT_GRAYSCALE_ANY)) {
231 PT_DEBUG("[grayscale] choice [%d] is out of the correct values: [%d-%d]",\
232 st.grayscale, PT_GRAYSCALE_GRAYSCALE, (PT_GRAYSCALE_ANY-1));
235 if ((st.papersize < PT_PAPER_A4) || (st.grayscale >= REAL_PAPERSIZE_MAX)) {
236 PT_DEBUG("[papersize] choice [%d] is out of the correct values: [%d-%d]",\
237 st.papersize, PT_PAPER_A4, (REAL_PAPERSIZE_MAX-1));
243 void print_cur_state()
246 st.quality = pt_get_selected(PT_OPTION_ID_QUALITY);
247 st.paper = pt_get_selected(PT_OPTION_ID_PAPER);
248 st.grayscale = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
249 st.papersize = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
250 PT_DEBUG("CURRENT STATE");
254 void print_groups_before(int num)
257 while ((val < num) && (X[val].desc != NULL)) {
258 printf(YE "%s: " RS, X[val].desc);
259 printf(GR "%d: " RS, X[val].current);
264 void print_groups_after(int num)
267 while (X[++num].desc) {
268 printf(YE "%s: " RS, X[num].desc);
269 printf("%d: ", X[num].current);
274 void print_state(state st)
276 PT_DEBUG("[GRAYSCALE]: %d", st.grayscale);
277 PT_DEBUG("[QUALITY]: %d", st.quality);
278 PT_DEBUG("[PAPER]: %d", st.paper);
279 PT_DEBUG("[PAPERSIZE]: %d, %s", st.papersize, \
280 pt_get_print_option_papersize(st.papersize));
283 for (i = 0; i < pt_get_print_option_papersize_num(); ++i) {
284 PT_DEBUG("TEST [PAPERSIZE]: %d, %s", i, \
285 pt_get_print_option_papersize(i));
289 void restore_state(state st, counter *group)
291 int set_choice = false;
292 pt_print_option_e option = group->id;
295 case PT_OPTION_ID_QUALITY:
296 set_choice = reliable_set_choice(option, st.quality);
297 group->current = st.quality;
299 case PT_OPTION_ID_PAPER:
300 set_choice = reliable_set_choice(option, st.paper);
301 group->current = st.paper;
303 case PT_OPTION_ID_PAPERSIZE:
304 set_choice = reliable_set_choice(option, st.papersize);
305 group->current = st.papersize;
307 case PT_OPTION_ID_GRAYSCALE:
308 set_choice = reliable_set_choice(option, st.grayscale);
309 group->current = st.grayscale;
312 PT_DEBUG("[Error] Got unknown state");
317 PT_DEBUG("Can't restore choice back");
320 PT_DEBUG("RESTORED:");
326 check_selected_choice();
327 check_selected_choice_is_enabled();
328 check_all_choices_disabled();
331 void check_selected_choice()
333 if (!pt_selected_choice(PT_OPTION_ID_PAPERSIZE, PT_ORIENTATION_PORTRAIT)) {
334 PT_DEBUG("No any selected choice in PT_OPTION_ID_PAPERSIZE option");
337 if (!pt_selected_choice(PT_OPTION_ID_QUALITY, PT_ORIENTATION_PORTRAIT)) {
338 PT_DEBUG("No any selected choice in PT_OPTION_ID_QUALITY option");
341 if (!pt_selected_choice(PT_OPTION_ID_PAPER, PT_ORIENTATION_PORTRAIT)) {
342 PT_DEBUG("No any selected choice in PT_OPTION_ID_PAPER option");
345 if (!pt_selected_choice(PT_OPTION_ID_GRAYSCALE, PT_ORIENTATION_PORTRAIT)) {
346 PT_DEBUG("No any selected choice in PT_OPTION_ID_GRAYSCALE option");
351 void check_selected_choice_is_enabled()
355 choice = pt_get_selected(PT_OPTION_ID_PAPERSIZE);
357 PT_DEBUG("function pt_get_selected returned -1");
360 if (!pt_is_enabled(PT_OPTION_ID_PAPERSIZE, choice)) {
361 PT_DEBUG("We've got selected choice which is not enabled");
367 choice = pt_get_selected(PT_OPTION_ID_QUALITY);
369 PT_DEBUG("function pt_get_selected returned -1");
372 if (!pt_is_enabled(PT_OPTION_ID_QUALITY, choice)) {
373 PT_DEBUG("We've got selected choice which is not enabled");
379 choice = pt_get_selected(PT_OPTION_ID_PAPER);
381 PT_DEBUG("function pt_get_selected returned -1");
384 if (!pt_is_enabled(PT_OPTION_ID_PAPER, choice)) {
385 PT_DEBUG("We've got selected choice which is not enabled");
391 choice = pt_get_selected(PT_OPTION_ID_GRAYSCALE);
393 PT_DEBUG("function pt_get_selected returned -1");
396 if (!pt_is_enabled(PT_OPTION_ID_GRAYSCALE, choice)) {
397 PT_DEBUG("We've got selected choice which is not enabled");
402 bool check_all_choices_disabled()
404 bool papersize = true, quality = true;
405 bool grayscale = true, paper = true;
407 papersize = check_all_choices_disabled_papersize();
408 grayscale = check_all_choices_disabled_grayscale();
409 quality = check_all_choices_disabled_quality();
410 paper = check_all_choices_disabled_paper();
411 if (papersize || quality || grayscale || paper) {
412 PT_DEBUG("TEST [check_all_choices_disabled] failed");
419 int check_all_choices_disabled_papersize()
421 bool enabled = false;
423 for (i = PT_PAPER_A4; i < PT_PAPER_SIZE_MAX; ++i) {
424 if (pt_is_enabled(PT_OPTION_ID_PAPERSIZE, i)) {
429 PT_DEBUG("All choices are DISABLED in PAPERSIZE");
435 int check_all_choices_disabled_grayscale()
437 bool enabled = false;
439 for (i = PT_GRAYSCALE_GRAYSCALE; i < PT_GRAYSCALE_MAX; ++i) {
440 if (pt_is_enabled(PT_OPTION_ID_GRAYSCALE, i)) {
445 PT_DEBUG("All choices are DISABLED in GRAYSCALE");
451 int check_all_choices_disabled_quality()
453 bool enabled = false;
455 for (i = PT_QUALITY_DRAFT; i < PT_QUALITY_MAX; ++i) {
456 if (pt_is_enabled(PT_OPTION_ID_QUALITY, i)) {
461 PT_DEBUG("All choices are DISABLED in QUALITY");
467 int check_all_choices_disabled_paper()
469 bool enabled = false;
471 for (i = PT_PAPER_NORMAL; i < PT_PAPER_MAX; ++i) {
472 if (pt_is_enabled(PT_OPTION_ID_PAPER, i)) {
477 PT_DEBUG("All choices are DISABLED in PAPER");
483 void set_custom_choice(int qual, int paper, int gr_scale, int paper_size)
485 pt_set_choice(PT_OPTION_ID_PAPERSIZE, paper);
487 pt_set_choice(PT_OPTION_ID_QUALITY, qual);
489 pt_set_choice(PT_OPTION_ID_GRAYSCALE, gr_scale);
491 pt_set_choice(PT_OPTION_ID_PAPERSIZE, paper_size);
494 ppd_file_t *open_ppd_file(const char *filename)
496 ppd_file_t *ppd = ppdOpenFile(filename);
498 PT_DEBUG("PPD file can't be loaded");