bac2645072c7f76fdb86e9b86e906ca0ed7f5a4b
[apps/home/mobileprint.git] / mobileprint / app / preview_engine.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (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 <page_preview_thread.h>
24 #include <previewgen.h>
25
26 #include "pts_debug.h"
27 #include "preview_engine.h"
28
29 int init_preview_engine(struct preview_engine *engine)
30 {
31         PTS_TRACE_BEGIN;
32         PTS_RETV_IF(engine == NULL, -1, "engine is NULL");
33
34         /*engine->fname = NULL;
35         DBG("preview_engine: fname = %s", fname);*/
36
37         engine->state = PREVIEW_ENGINE_STATE_INITIAL;
38         engine->initial_thread = NULL;
39         engine->next_preview_data = NULL;
40
41         memset(&(engine->p_model), 0, sizeof(struct preview_model));
42
43         if (init_preview_task_model(&(engine->task_model), engine) < 0) {
44                 PTS_TRACE_BEGIN;
45                 return -1;
46         }
47         /*if (init_preview_model(&(engine->p_model)) < 0)
48                 return -1;*/
49
50         /*engine->preview_data.pages_count = -1;
51         engine->preview_data.fname = strdup(fname);*/
52
53         engine->page_ready_event_type = ecore_event_type_new();
54
55         PTS_TRACE_END;
56         return 0;
57 }
58
59 /* TODO: engine destruction */
60 int preview_engine_init_done(struct preview_engine *engine,
61                                                          const struct preview_conf *conf)
62 {
63         PTS_TRACE_BEGIN;
64         if (conf->pages_count > 0) {
65                 engine->state = PREVIEW_ENGINE_STATE_READY;
66                 if (init_preview_model(&(engine->p_model),
67                                                            conf, engine) < 0) {
68                         PTS_DEBUG("ERROR in preview model initialization");
69                         engine->state = PREVIEW_ENGINE_STATE_ERROR;
70                         preview_engine_send_page_message(engine,
71                                                                                          PREVIEW_ENGINE_EVENT_INITIAL_ERROR, -1);
72                         PTS_TRACE_END;
73                         return -1;
74                 }
75                 preview_engine_send_page_message(engine,
76                                                                                  PREVIEW_ENGINE_EVENT_INITIAL_READY, 0);
77                 PTS_TRACE_END;
78                 return 0;
79         }
80
81         PTS_DEBUG("ERROR: no pages");
82         engine->state = PREVIEW_ENGINE_STATE_ERROR;
83         preview_engine_send_page_message(engine,
84                                                                          PREVIEW_ENGINE_EVENT_INITIAL_ERROR, -1);
85         PTS_TRACE_END;
86         return 0;
87 }
88
89 int process_preview_engine_file(struct preview_engine *engine,
90                                                                 char *fname,
91                                                                 const char *ppd,
92                                                                 const struct paper_size_pts *paper_size,
93                                                                 const struct size_px *max_available_size_px,
94                                                                 enum page_orientation orientation, int n_up,
95                                                                 const struct page_scale *scale, int is_grayscale)
96 {
97         PTS_TRACE_BEGIN;
98         PTS_RETV_IF(NULL == engine ||
99                                 NULL == fname ||
100                                 NULL == ppd ||
101                                 NULL == paper_size ||
102                                 NULL == scale ||
103                                 NULL == max_available_size_px
104                                 , -1 , "Invalid argument");
105
106         struct initial_preview_data *data;
107
108         data = malloc(sizeof(struct initial_preview_data));
109         PTS_RETV_IF(data == NULL, -1, "Insufficient memory");
110
111         data->conf.initial_fname = strdup(fname);
112         data->conf.ppd = strdup(ppd);
113         data->conf.fname = strdup(TEMP_PDFTOPDF_FNAME);
114         data->conf.paper_size = *paper_size;
115         data->conf.paper_size.name = strdup(paper_size->name);
116         data->conf.max_available_size_px = *max_available_size_px;
117         data->conf.orientation = orientation;
118         data->conf.n_up = n_up;
119         data->conf.scale = *scale;
120         data->conf.is_grayscale = is_grayscale;
121         data->engine = engine;
122
123         //careful memory release:
124         if (data->conf.initial_fname == NULL || data->conf.ppd == NULL ||
125                         data->conf.fname == NULL || data->conf.paper_size.name == NULL) {
126                 PTS_IF_FREE_MEM(data->conf.initial_fname);
127                 PTS_IF_FREE_MEM(data->conf.ppd);
128                 PTS_IF_FREE_MEM(data->conf.fname);
129                 PTS_IF_FREE_MEM(data->conf.paper_size.name);
130                 PTS_IF_FREE_MEM(data);
131                 PTS_RETV_IF(1, -1, "Insufficient memory");
132         }
133
134         engine->state = PREVIEW_ENGINE_STATE_PROCESSING;
135         add_preview_task_initial(&(engine->task_model), &(data->conf));
136         PTS_IF_FREE_MEM(data);
137
138         PTS_TRACE_END;
139         return 0;
140 }
141
142 void preview_engine_send_page_message(struct preview_engine *engine,
143                                                                           enum preview_engine_event_status status, int page)
144 {
145         PTS_RET_IF(engine == NULL, "engine is NULL");
146
147         struct preview_engine_event *event_data = malloc(sizeof(*event_data));
148         PTS_TRACE_BEGIN;
149
150         PTS_DEBUG("Send event: [%d, %d]", page, status);
151
152         /* TODO: check for active preview model */
153         event_data->page = page;
154         event_data->status = status;
155         ecore_event_add(engine->page_ready_event_type, event_data, NULL, NULL);
156         PTS_TRACE_END;
157 }