1. Changed license year
[apps/home/mobileprint.git] / mobileprint / preview_engine / lib / preview_engine.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2013  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 <Ecore.h>
21
22 #include <page_preview.h>
23 #include <previewgen.h>
24
25 #include "pts_debug.h"
26 #include "preview_engine.h"
27
28 int init_preview_engine(struct preview_engine *engine)
29 {
30         int ret;
31         int errsv;
32
33         PTS_TRACE_BEGIN;
34         PTS_RETV_IF(engine == NULL, -1, "engine is NULL");
35
36         /*engine->fname = NULL;
37         DBG("preview_engine: fname = %s", fname);*/
38
39         memset(engine, 0, sizeof(struct preview_engine));
40
41         engine->state = PREVIEW_ENGINE_STATE_INITIAL;
42         engine->next_preview_data = NULL;
43
44         memset(&(engine->p_model), 0, sizeof(struct preview_model));
45
46         ret = mkdir(PREVIEW_TEMP_DIR, 0777);
47         errsv = errno;
48         PTS_RETV_IF(ret < 0 && errsv != EEXIST, -1,
49                         "Failed to mkdir " PREVIEW_TEMP_DIR
50                         ", errno = %d", errsv);
51
52         if (init_preview_task_model(&(engine->task_model), engine) < 0) {
53                 PTS_TRACE_BEGIN;
54                 return -1;
55         }
56         /*if (init_preview_model(&(engine->p_model)) < 0)
57                 return -1;*/
58
59         /*engine->preview_data.pages_count = -1;
60         engine->preview_data.fname = strdup(fname);*/
61
62         engine->page_ready_event_type = ecore_event_type_new();
63
64         PTS_TRACE_END;
65         return 0;
66 }
67
68 /* TODO: engine destruction */
69
70 int preview_engine_init_done(struct preview_engine *engine,
71                                                          const struct preview_conf *conf)
72 {
73         PTS_TRACE_BEGIN;
74         if (conf->pages_count > 0) {
75                 engine->state = PREVIEW_ENGINE_STATE_READY;
76                 if (init_preview_model(&(engine->p_model),
77                                                            conf, engine) < 0) {
78                         PTS_DEBUG("ERROR in preview model initialization");
79                         engine->state = PREVIEW_ENGINE_STATE_ERROR;
80                         preview_engine_send_page_message(engine,
81                                                                                          PREVIEW_ENGINE_EVENT_INITIAL_ERROR, -1);
82                         PTS_TRACE_END;
83                         return -1;
84                 }
85                 preview_engine_send_page_message(engine,
86                                                                                  PREVIEW_ENGINE_EVENT_INITIAL_READY, 0);
87                 PTS_TRACE_END;
88                 return 0;
89         }
90
91         PTS_DEBUG("ERROR: no pages");
92         engine->state = PREVIEW_ENGINE_STATE_ERROR;
93         preview_engine_send_page_message(engine,
94                                                                          PREVIEW_ENGINE_EVENT_INITIAL_ERROR, -1);
95         PTS_TRACE_END;
96         return 0;
97 }
98
99 int process_preview_engine_file(struct preview_engine *engine,
100                                                                 char **const fnames, int files_count,
101                                                                 const char *ppd,
102                                                                 const struct paper_size_pts *paper_size,
103                                                                 const struct size_px *max_available_size_px,
104                                                                 enum page_orientation orientation, int n_up,
105                                                                 const struct page_scale *scale, int is_grayscale)
106 {
107         PTS_TRACE_BEGIN;
108         PTS_RETV_IF(NULL == engine ||
109                                 NULL == fnames ||
110                                 files_count <= 0 ||
111                                 NULL == fnames[0] ||
112                                 NULL == ppd ||
113                                 NULL == paper_size ||
114                                 NULL == scale ||
115                                 NULL == max_available_size_px
116                                 , -1 , "Invalid argument");
117
118         struct initial_preview_data *data;
119         struct size_px shadow_size_px = {10, 10};
120         int i;
121
122         data = malloc(sizeof(struct initial_preview_data));
123         PTS_RETV_IF(data == NULL, -1, "Insufficient memory");
124
125
126         data->conf.initial_fnames = calloc(files_count + 1, sizeof(char*));
127         if (NULL == data->conf.initial_fnames) {
128                 PTS_IF_FREE_MEM(data);
129                 PTS_RETV_IF(1, -1, "Insufficient memory");
130         }
131         for (i = 0; i < files_count; ++i) {
132                 data->conf.initial_fnames[i] = strdup(fnames[i]);
133                 PTS_DEBUG("Fname %d: %s", i, fnames[i]);
134                 if (NULL == data->conf.initial_fnames[i]) {
135                         /* memory error */
136                         destroy_preview_conf(&(data->conf));
137                         PTS_IF_FREE_MEM(data);
138                         PTS_RETV_IF(1, -1, "Insufficient memory");
139                 }
140         }
141         data->conf.files_count = files_count;
142
143         data->conf.ppd = strdup(ppd);
144         data->conf.fname = strdup(TEMP_PDFTOPDF_FNAME);
145         data->conf.paper_size = *paper_size;
146         data->conf.paper_size.name = strdup(paper_size->name);
147         //data->conf.max_available_size_px = *max_available_size_px;
148         data->conf.orientation = orientation;
149         data->conf.n_up = n_up;
150         data->conf.scale = *scale;
151         //data->conf.is_grayscale = is_grayscale;
152
153         data->conf.settings_req.paper_size = paper_size->s;
154         data->conf.settings_req.available_size_px = *max_available_size_px;
155         data->conf.settings_req.shadow_offset = shadow_size_px;
156         data->conf.settings_req.is_rotate90 =
157                         (PAGE_ORIENTATION_LANDSCAPE == orientation);
158         data->conf.settings_req.is_grayscale = is_grayscale;
159
160         data->engine = engine;
161
162
163         //careful memory release:
164         if (data->conf.ppd == NULL || data->conf.fname == NULL
165                         || data->conf.paper_size.name == NULL) {
166                 destroy_preview_conf(&(data->conf));
167                 PTS_IF_FREE_MEM(data);
168                 PTS_RETV_IF(1, -1, "Insufficient memory");
169         }
170
171         engine->state = PREVIEW_ENGINE_STATE_PROCESSING;
172         add_preview_task_initial(&(engine->task_model), &(data->conf));
173         PTS_IF_FREE_MEM(data);
174
175         PTS_TRACE_END;
176         return 0;
177 }
178
179 void preview_engine_send_page_message(struct preview_engine *engine,
180                                                                           enum preview_engine_event_status status, int page)
181 {
182         PTS_RET_IF(engine == NULL, "engine is NULL");
183
184         struct preview_engine_event *event_data = malloc(sizeof(*event_data));
185         PTS_TRACE_BEGIN;
186
187         PTS_DEBUG("Send event: [%d, %d]", page, status);
188
189         /* TODO: check for active preview model */
190         event_data->page = page;
191         event_data->status = status;
192         ecore_event_add(engine->page_ready_event_type, event_data, NULL, NULL);
193         PTS_TRACE_END;
194 }