1. Changed license year
[apps/home/mobileprint.git] / mobileprint / app / preview_content.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
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
9
10 * http://floralicense.org/license/
11
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.
17 *
18 */
19
20 #include <app.h>
21 #include <ui-gadget.h>
22
23 #include <page_preview.h>
24 #include <paper_size.h>
25 #include <previewgen.h>
26 #include <pt_api.h>
27
28 #include "pts_common.h"
29 #include "pts_main_view.h"
30
31 #include "preview_content.h"
32
33 #define  DEFAULT_PPD_FILE       RESDIR "/ppd/default.ppd"
34
35 int is_printer_grayscale(const pt_printer_mgr_t *active_printer)
36 {
37         switch (pt_get_selected(PT_OPTION_ID_GRAYSCALE)) {
38         case PT_GRAYSCALE_COLOUR:
39                 return 0;
40         default:
41                 return 1;
42         }
43 }
44
45 enum page_orientation printer_get_setting_orientation(
46         const pt_printer_mgr_t *active_printer)
47 {
48         /* should be taken from settings */
49         switch (active_printer->landscape) {
50 //      case PT_ORIENTATION_AUTO:
51                 /* TODO: check for page size, n-up */
52 //              return PAGE_ORIENTATION_AUTO;
53         case PT_ORIENTATION_PORTRAIT:
54                 return PAGE_ORIENTATION_PORTRAIT;
55         case PT_ORIENTATION_LANDSCAPE:
56                 return PAGE_ORIENTATION_LANDSCAPE;
57         }
58         return PAGE_ORIENTATION_PORTRAIT;
59 }
60
61 int printer_get_setting_n_up(const pt_printer_mgr_t *active_printer)
62 {
63         /* should be taken from settings */
64         switch (active_printer->scaling) {
65         case PT_SCALING_2_PAGES:
66                 return 2;
67         case PT_SCALING_4_PAGES:
68                 return 4;
69 #if 0
70         case PT_SCALING_8_PAGES:
71                 return 8;
72 #endif
73         default:
74                 break;
75         }
76         return 1;
77 }
78
79 /**
80  * @brief       Get paper size and paper name for CUPS utilities cmd usage
81  * @param[out] paper_size       paper_size should be local variable
82  *                              to keep further store we should provide a copy
83  */
84 int printer_get_paper_size(struct paper_size_pts *paper_size)
85 {
86         PTS_TRACE_BEGIN;
87         /* get paper size of active printer from print-service */
88         pt_pagesize_t current_pagesize;
89         int result = pt_get_selected_paper_size_pts(&current_pagesize);
90         PTS_RETV_IF(result < 0, -1, "pt_get_selected_paper_size_pts returns error");
91
92         /* get really selected papersize name for command line commands */
93         paper_size->name = pt_get_print_option_papersize_cmd(
94                                                    pt_get_selected(PT_OPTION_ID_PAPERSIZE));
95         paper_size->s.x = current_pagesize.x;
96         paper_size->s.y = current_pagesize.y;
97
98         PTS_TRACE_END;
99         return 0;
100 }
101
102 int pts_image_size_t2page_scale(pts_image_size_e pts_size,
103                                                                 struct page_scale *scale)
104 {
105         PTS_RETV_IF(scale == NULL, -1, "scale is NULL");
106
107         scale->w = 0;
108         scale->h = 0;
109
110         switch (pts_size) {
111         case PTS_SIZE_FIT_TO_PAPER:
112                 scale->type = SCALE_FIT_TO_PAPER;
113                 return 0;
114         case PTS_SIZE_5X7:
115                 scale->w = 5;
116                 scale->h = 7;
117                 break;
118         case PTS_SIZE_4X6:
119                 scale->w = 4;
120                 scale->h = 6;
121                 break;
122         case PTS_SIZE_3_5X5:
123                 scale->w = 3.5;
124                 scale->h = 5;
125                 break;
126         case PTS_SIZE_WALLET:
127                 /* 6.4x8.4 cm */
128                 scale->w = 3.30709;
129                 scale->h = 2.51969;
130                 break;
131                 //TODO : CUSTOM SIZE
132         default:
133                 scale->type = SCALE_FIT_TO_PAPER;
134                 return 1;
135         }
136
137         scale->type = SCALE_CUSTOM;
138         return 0;
139 }
140
141
142 /**
143  * @brief This function generates preview images
144  * @param[in] ugd the pointer to the main data structure
145  * @return
146  */
147 int generate_preview_images(pts_appdata_t *ad,
148                                                         app_device_orientation_e dev_orientation)
149 {
150         PTS_TRACE_BEGIN;
151         PTS_RETV_IF(ad == NULL , -1 , "ad is NULL");
152
153         int result = 0;
154
155         struct paper_size_pts paper_size;
156         enum page_orientation orientation;
157         struct page_scale scale = {0, 0, 0};
158         int n_up;
159         int is_grayscale;
160         pt_printer_mgr_t *active_printer = ad->list_info.active_printer;
161         char *ppd;
162
163         /* currently supported: only one PDF or multiple images;
164            mixed images-pdf unsupported */
165
166         PTS_RETV_IF(ad->printing_data.num_of_files <= 0, -1 , "Invalid argument");
167
168         /* should be taken from settings */
169         if (active_printer != NULL) {
170                 ppd = active_printer->ppd;
171                 printer_get_paper_size(&paper_size);
172                 orientation = printer_get_setting_orientation(active_printer);
173                 n_up = printer_get_setting_n_up(active_printer);
174                 pts_image_size_t2page_scale(ad->size_popup_info.image_size, &scale);
175                 if (ad->size_popup_info.image_size == PTS_SIZE_CUSTOM) {
176                         scale.w = ad->size_popup_info.custom_width;
177                         scale.h = ad->size_popup_info.custom_height;
178                         scale.type = SCALE_CUSTOM;
179                 }
180                 is_grayscale = is_printer_grayscale(active_printer);
181         } else {
182                 PTS_DEBUG("No active printer preview");
183
184                 ppd = DEFAULT_PPD_FILE;
185                 get_paper_size_pts("A4", &paper_size);
186                 orientation = PAGE_ORIENTATION_PORTRAIT;
187                 //is_original_zoom = 0;
188                 scale.type = SCALE_FIT_TO_PAPER;
189                 n_up = 1;
190                 is_grayscale = 1;
191         }
192
193         PTS_DEBUG("USING PPD: %s", ppd);
194         PTS_DEBUG("PAPER NAME: %s", paper_size.name);
195         PTS_DEBUG("PAPER SIZE: %f, %f", paper_size.s.x, paper_size.s.y);
196         PTS_DEBUG("ALBUM, N_UP, ZOOM: %d, %d, %d", orientation, n_up, scale.type);
197
198         result = evas_smart_smsc_set_file(ad->main_info.smsc,
199                         ad->printing_data.request_files, ad->printing_data.num_of_files,
200                         ppd, &paper_size, orientation, n_up, &scale,
201                         is_grayscale);
202         PTS_DEBUG("evas_smart_smsc_set_file: %d", result);
203
204         PTS_TRACE_END;
205         return result;
206 }
207