e97229e3cc0d6ee73728ccf694cc058b71aec597
[framework/security/cert-svc-ui.git] / cert-selection-ug / src / cert-selection.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        cert-selection.c
18  * @author      Janusz Kozerski (j.kozerski@samsung.com)
19  * @version     1.0
20  */
21
22 #include <Elementary.h>
23 #include "dlog.h"
24
25 #include <stdio.h>
26 #include <dirent.h>
27
28 #include <cert-svc/ccert.h>
29 #include <cert-svc/cinstance.h>
30 #include <cert-svc/ccrl.h>
31 #include <cert-svc/cocsp.h>
32 #include <cert-svc/cpkcs12.h>
33 #include <cert-svc/cstring.h>
34
35 #include "cert-selection-uigadget.h"
36
37 #define CERT_MAX_DATA_SIZE  256
38
39 static CertSvcInstance instance;
40 static CertSvcStringList stringList;
41 static Elm_Genlist_Item_Class itc;
42 static int state_index = 0; //selected radio index
43 static char *selected_name = NULL;
44 static Eina_Bool selected = EINA_FALSE;
45 static Evas_Object *radio_main = NULL;
46
47 static void _open(void *data, Evas_Object *obj, void *event_info) {
48
49     LOGD("selected index = %d", state_index);
50     (void)obj;
51     (void)event_info;
52
53     struct ug_data *ad = (struct ug_data*) data;
54
55     if (selected) {
56         service_h service;
57         service_create(&service);
58         service_add_extra_data(service, "selected-cert", selected_name);
59         ug_send_result(ad->ug, service);
60         LOGD("result send");
61     }
62
63     /* bg must delete before starting on_destroy */
64     LOGD("Closing UG");
65     free(selected_name);
66     evas_object_del(ad->bg);
67     ad->bg = NULL;
68     ug_destroy_me(ad->ug);
69 }
70
71 static void _cancel(void *data, Evas_Object *obj, void *event_info) {
72
73     (void)obj;
74     (void)event_info;
75
76     struct ug_data *ad = (struct ug_data*) data;
77
78     /* bg must delete before starting on_destroy */
79     LOGD("Closing UG");
80     free(selected_name);
81     evas_object_del(ad->bg);
82     ad->bg = NULL;
83     ug_destroy_me(ad->ug);
84 }
85
86 static char *_gl_text_get(void *data, Evas_Object *obj, const char *part) {
87
88     (void)obj;
89     int index = (int) data;
90
91     CertSvcString buffer;
92     if (certsvc_string_list_get_one(stringList, index, &buffer) != CERTSVC_SUCCESS) {
93         return strdup("ERROR WHILE LOADING STRING");
94     }
95     char *char_buffer;
96     char_buffer = strndup(buffer.privateHandler, buffer.privateLength);
97     certsvc_string_free(buffer);
98
99     if (!strcmp(part, "elm.text.1") || !strcmp(part, "elm.text")) {
100         return char_buffer;
101     } else if (!strcmp(part, "elm.text.2")) {
102         return strdup("example@address.net");
103     }
104
105     return NULL;
106 }
107
108 static Evas_Object *_gl_content_get(void *data, Evas_Object *obj, const char *part) {
109
110     int index = (int) data;
111     Evas_Object *radio;
112
113     if (!strcmp(part, "elm.icon") || !strcmp(part, "elm.swallow.icon")) {
114         radio = elm_radio_add(obj);
115         elm_radio_state_value_set(radio, index);
116         elm_radio_group_add(radio, radio_main);
117         if (index == state_index)
118             elm_radio_value_set(radio, state_index);
119         evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
120         evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
121
122         return radio;
123     }
124     return NULL;
125 }
126
127 static Eina_Bool _gl_state_get(void *data, Evas_Object *obj, const char *part) {
128
129     (void)data;
130     (void)obj;
131     (void)part;
132
133     return EINA_FALSE;
134 }
135
136 static void _gl_del(void *data, Evas_Object *obj) {
137
138     (void)data;
139     (void)obj;
140
141     return;
142 }
143
144 static void _gl_sel(void *data, Evas_Object *obj, void *event_info) {
145
146     (void)obj;
147
148     int pkcs_index = (int) data;
149     int index;
150     Elm_Object_Item *item = (Elm_Object_Item *) event_info;
151
152     if (item) {
153         index = (int) elm_object_item_data_get(item);
154         state_index = index;
155         elm_genlist_item_update(item);
156     }
157     selected = EINA_TRUE;
158
159     CertSvcString buffer;
160     if (certsvc_string_list_get_one(stringList, pkcs_index, &buffer) != CERTSVC_SUCCESS) {
161         selected = EINA_FALSE;
162         return;
163     }
164     if(selected_name)
165         free(selected_name);
166     selected_name = malloc((buffer.privateLength+1) * sizeof(char));
167     strncpy(selected_name, buffer.privateHandler, buffer.privateLength);
168     selected_name[buffer.privateLength] = 0;
169     LOGD("SELECTED NAME = %s", selected_name);
170     certsvc_string_free(buffer);
171 }
172
173 void cert_selection_cb(void *data, Evas_Object *obj, void *event_info) {
174     LOGD("cert_selection");
175
176     (void)data;
177     (void)obj;
178     (void)event_info;
179
180     struct ug_data *ad = (struct ug_data *) data;
181     Evas_Object *genlist = NULL;
182     Evas_Object *layout = NULL;
183     Evas_Object *toolbar = NULL;
184
185     layout = elm_layout_add(ad->win_main);
186     if (!layout)
187         return;
188
189     toolbar = elm_toolbar_add(layout);
190     if (!toolbar)
191         return;
192     elm_toolbar_shrink_mode_set(toolbar, ELM_TOOLBAR_SHRINK_EXPAND);
193
194     elm_toolbar_item_append(toolbar, NULL, "Open", _open, ad);
195     elm_toolbar_item_append(toolbar, NULL, "Cancel", _cancel, ad);
196
197     // Create genlist;
198     genlist = elm_genlist_add(layout);
199     if (!radio_main) {
200         radio_main = elm_radio_add(genlist);
201         elm_radio_state_value_set(radio_main, 0);
202         elm_radio_value_set(radio_main, 0);
203     }
204
205     // Set genlist item class
206     itc.item_style = "2text.1icon.2";
207     itc.func.text_get = _gl_text_get;
208     itc.func.content_get = _gl_content_get;
209     itc.func.state_get = _gl_state_get;
210     itc.func.del = _gl_del;
211
212     if (certsvc_instance_new(&instance) == CERTSVC_FAIL) {
213         LOGD("CERTSVC_FAIL");
214         return;
215     }
216     certsvc_pkcs12_get_id_list(instance, &stringList);
217
218     int i;
219     int list_length;
220     certsvc_string_list_get_length(stringList, &list_length);
221     for (i = 0; i < list_length; i++) {
222         elm_genlist_item_append(genlist, &itc, (void*) i, NULL, ELM_GENLIST_ITEM_NONE, _gl_sel, (void*) i);
223
224     }
225
226     Elm_Object_Item *itm = elm_naviframe_item_push(ad->navi_bar, "Pick Certificate", NULL, NULL, genlist, NULL);
227     elm_object_item_part_content_set(itm, "controlbar", toolbar);
228
229     LOGD("end of cert_selection");
230 }