1. Changed license copyright year
[apps/home/mobileprint.git] / mobileprint / preview_engine / lib / preview_model.c
1 /*
2 *  Mobileprint
3 *
4 * Copyright 2012-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 <previewgen.h>
23 #include <pts_debug.h>
24
25 #include "preview_model.h"
26
27
28 void destroy_preview_conf(struct preview_conf *conf)
29 {
30         int i;
31         PTS_RET_IF(NULL == conf, "Argument error");
32
33         if (conf->initial_fnames != NULL)
34                 for (i = 0; i < conf->files_count; ++i)
35                         PTS_IF_FREE_MEM(conf->initial_fnames[i]);
36         PTS_IF_FREE_MEM(conf->initial_fnames);
37         conf->files_count = 0;
38
39         PTS_IF_FREE_MEM(conf->fname);
40         PTS_IF_FREE_MEM(conf->ppd);
41         PTS_IF_FREE_MEM(conf->paper_size.name);
42         conf->pages_count = 0;
43 }
44
45
46 int copy_preview_conf(struct preview_conf *dest, const struct preview_conf *src)
47 {
48         int i;
49         PTS_RETV_IF(NULL == dest || NULL == src, -1, "Argument error");
50         memset(dest, 0, sizeof(struct preview_conf));
51         *dest = *src;
52
53         dest->initial_fnames = calloc(src->files_count + 1, sizeof(char*));
54         if (NULL == dest->initial_fnames)
55                 goto ERR_CASE;
56         for (i = 0; i < src->files_count; ++i) {
57                 dest->initial_fnames[i] = strdup(src->initial_fnames[i]);
58                 if (NULL == dest->initial_fnames[i])
59                         goto ERR_CASE;
60         }
61
62         dest->fname = strdup(src->fname);
63         dest->ppd = strdup(src->ppd);
64         dest->paper_size.name = strdup(src->paper_size.name);
65         if (NULL == dest->fname || NULL == dest->ppd
66                         || NULL == dest->paper_size.name)
67                 goto ERR_CASE;
68
69         return 0;
70
71 ERR_CASE:
72         destroy_preview_conf(dest);
73         PTS_RETV_IF(1, -1, "Insufficient memory");
74 }
75
76
77 int init_preview_model(struct preview_model *model,
78                                            const struct preview_conf *conf,
79                                            struct preview_engine *engine)
80 {
81         PTS_TRACE_BEGIN;
82         PTS_RETV_IF(model == NULL || conf == NULL || engine == NULL,
83                         -1, "Invalid argument");
84
85         //model->conf = *conf;
86         model->pages_state = malloc(sizeof(enum preview_page_state)
87                                                                 * (conf->pages_count + 1));
88         PTS_RETV_IF(model->pages_state == NULL, -1, "Insufficient memory");
89
90         if (copy_preview_conf(&(model->conf), conf) < 0) {
91                 PTS_IF_FREE_MEM(model->pages_state);
92                 PTS_RETV_IF(1, -1, "Error in configuration copy");
93         }
94
95         int i;
96         for (i = 1; i <= conf->pages_count; ++i) {
97                 model->pages_state[i] = PREVIEW_PAGE_STATE_NA;
98         }
99
100         model->engine = engine;
101         model->empty_page_state = PREVIEW_PAGE_STATE_NA;
102
103         PTS_TRACE_END;
104         return 0;
105 }
106
107 void destroy_preview_model(struct preview_model *model)
108 {
109         PTS_TRACE_BEGIN;
110         PTS_RET_IF(model == NULL, "Invalid argument");
111
112         PTS_IF_FREE_MEM(model->pages_state);
113         destroy_preview_conf(&(model->conf));
114
115         PTS_TRACE_END;
116 }
117
118 int preview_model_request_empty_page(struct preview_model *model)
119 {
120         PTS_TRACE_BEGIN;
121         PTS_RETV_IF(model == NULL, -1, "model is NULL");
122         PTS_RETV_IF(model->pages_state == NULL, -1, "model is not initialized");
123
124         if (model->empty_page_state != PREVIEW_PAGE_STATE_READY)
125                 preview_engine_send_page_message(model->engine,
126                                 PREVIEW_ENGINE_EVENT_EMPTY_PAGE_PROCESSING, 0);
127
128         if (PREVIEW_PAGE_STATE_NA == model->empty_page_state) {
129                 add_preview_task_empty_page(
130                                 &(model->engine->task_model), model);
131                 model->empty_page_state = PREVIEW_PAGE_STATE_READY;
132         } else {
133                 preview_engine_send_page_message(model->engine,
134                                 PREVIEW_ENGINE_EVENT_EMPTY_PAGE_READY, 0);
135         }
136
137         PTS_TRACE_END;
138         return 0;
139 }
140
141 int preview_model_request_page(struct preview_model *model, int page)
142 {
143         PTS_TRACE_BEGIN;
144         PTS_RETV_IF(model == NULL, -1, "model is NULL");
145         PTS_RETV_IF(model->pages_state == NULL, -1, "model is not initialized");
146         PTS_RETV_IF(model->conf.pages_count - 1 < page - 1, -1 , "page is incorrect");
147
148         /* send message for empty page */
149         if (model->pages_state[page] != PREVIEW_PAGE_STATE_READY) {
150                 preview_engine_send_page_message(model->engine,
151                                                                                  PREVIEW_ENGINE_EVENT_PAGE_PROCESSING, page);
152         }
153
154         if (PREVIEW_PAGE_STATE_NA == model->pages_state[page]) {
155                 add_preview_task(&(model->engine->task_model), page, model);
156                 model->pages_state[page] = PREVIEW_PAGE_STATE_PROCESSING;
157         } else if (PREVIEW_PAGE_STATE_READY == model->pages_state[page]) {
158                 preview_engine_send_page_message(model->engine,
159                                                                                  PREVIEW_ENGINE_EVENT_PAGE_READY, page);
160         }
161
162         PTS_TRACE_END;
163         return 0;
164 }
165
166 int preview_model_page_available(struct preview_model *model,
167                                                                  enum preview_engine_event_status status, int page)
168 {
169         PTS_TRACE_BEGIN;
170         PTS_RETV_IF(model == NULL || model->pages_state == NULL, -1, "model is not initialized");
171
172         if (PREVIEW_ENGINE_EVENT_PAGE_READY == status)
173                 model->pages_state[page] = PREVIEW_PAGE_STATE_READY;
174         else if (PREVIEW_ENGINE_EVENT_EMPTY_PAGE_READY == status)
175                 model->empty_page_state = PREVIEW_PAGE_STATE_READY;
176
177         preview_engine_send_page_message(model->engine,
178                         status, page);
179
180         PTS_TRACE_END;
181         return 0;
182 }
183
184
185 int preview_model_debug_print_pages_state(struct preview_model *model)
186 {
187         int i;
188         int pages_count;
189         char *outstr;
190         char s;
191
192         PTS_TRACE_BEGIN;
193         PTS_RETV_IF(NULL == model, -1, "Argument error");
194         pages_count = model->conf.pages_count;
195         if (pages_count <= 0 || pages_count > PDF_PAGE_MAX_VAL)
196                 return -1;
197
198         outstr = malloc(sizeof(char) * (pages_count + 1));
199         for (i = 1; i <= pages_count; ++i) {
200                 s = '?';
201                 switch(model->pages_state[i]) {
202                 case PREVIEW_PAGE_STATE_NA:
203                         s = 'N';
204                         break;
205                 case PREVIEW_PAGE_STATE_PROCESSING:
206                         s = 'P';
207                         break;
208                 case PREVIEW_PAGE_STATE_READY:
209                         s = 'R';
210                         break;
211                 case PREVIEW_PAGE_STATE_ERROR:
212                         s = 'E';
213                         break;
214                 }
215                 outstr[i - 1] = s;
216         }
217         outstr[pages_count - 1] = '\0';
218         PTS_DEBUG("pages: %d, state: %s", pages_count, outstr);
219         free(outstr);
220
221         PTS_TRACE_END;
222         return 0;
223 }
224