Remove the WAC code
[apps/home/wrt-setting.git] / webapp-detail / permview.cpp
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 #include <Elementary.h>
18
19 #include <dpl/wrt-dao-rw/widget_dao.h>
20 #include <dpl/foreach.h>
21 #include <dpl/assert.h>
22
23 #include "permview.h"
24 #include "wac_feature.h"
25 #include "tizen_feature.h"
26 #include "util.h"
27
28 namespace WebAppDetailSetting {
29
30 void PermView::onFeatureClicked(void */*data*/,
31                                 Evas_Object */*obj*/,
32                                 void *event_info)
33 {
34     Elm_Object_Item *it;
35
36     it = static_cast<Elm_Object_Item*>(event_info);
37     Assert(it);
38     elm_genlist_item_selected_set(it, EINA_FALSE);
39     elm_genlist_item_expanded_set(it, !elm_genlist_item_expanded_get(it));
40 }
41
42 void PermView::onPermClicked(void *data,
43                              Evas_Object */*obj*/,
44                              void *event_info)
45 {
46     PermData *permData;
47     FeatureData *featureData;
48     Elm_Object_Item *it;
49     Elm_Object_Item *parent_it;
50
51     it = static_cast<Elm_Object_Item*>(event_info);
52     Assert(it);
53     permData = static_cast<PermData *>(data);
54     Assert(permData);
55     featureData = permData->m_featureData;
56     Assert(featureData);
57     ace_set_widget_resource_preference(
58             featureData->m_appID,
59             const_cast<char *>(featureData->m_feature->getUri().c_str()),
60             permData->m_type);
61     ace_preference_t perm;
62     ace_get_widget_resource_preference(
63         featureData->m_appID,
64         const_cast<char *>(featureData->m_feature->getUri().c_str()),
65         &perm);
66     featureData->m_feature->setPerm(perm);
67     elm_genlist_item_selected_set(it, EINA_FALSE);
68     elm_genlist_item_update(it);
69     parent_it = elm_genlist_item_parent_get(it);
70     if (parent_it)
71         elm_genlist_item_update(parent_it);
72 }
73
74 void PermView::onExpanded(void */*data*/, Evas_Object *obj, void *event_info)
75 {
76     static Elm_Genlist_Item_Class itc;
77     Elm_Object_Item *it;
78     Elm_Object_Item *newIt;
79     PermData *permData;
80     FeatureData *featureData;
81
82     Assert(obj && event_info);
83     it = static_cast<Elm_Object_Item *>(event_info);
84     featureData = static_cast<FeatureData *>(elm_object_item_data_get(it));
85     Assert(featureData);
86
87     itc.item_style = "dialogue/1text.1icon.2/expandable2";
88     itc.func.text_get = getPermStr;
89     itc.func.content_get = getPermRadio;
90     itc.func.state_get = NULL;
91     itc.func.del = delPermData;
92
93     try {
94         permData = new PermData(featureData, ACE_PREFERENCE_DENY);
95         newIt = elm_genlist_item_append(obj,
96                                         &itc,
97                                         permData,
98                                         it,
99                                         ELM_GENLIST_ITEM_NONE,
100                                         onPermClicked,
101                                         permData);
102         if (!newIt)
103             delete permData;
104
105         permData = new PermData(featureData, ACE_PREFERENCE_DENY);
106         newIt = elm_genlist_item_append(obj,
107                                         &itc,
108                                         permData,
109                                         it,
110                                         ELM_GENLIST_ITEM_NONE,
111                                         onPermClicked,
112                                         permData);
113         if (!newIt)
114             delete permData;
115
116     } catch (const std::bad_alloc &) {
117     }
118
119 }
120
121 void PermView::onContracted(void */*data*/,
122                             Evas_Object */*obj*/,
123                             void *event_info)
124 {
125     Elm_Object_Item *it;
126
127     Assert(event_info);
128     it = static_cast<Elm_Object_Item *>(event_info);
129     elm_genlist_item_subitems_clear(it);
130
131 }
132 char *PermView::getPermStr(void *data,
133                            Evas_Object */*obj*/,
134                            const char */*part*/)
135 {
136     PermData *permData;
137
138     Assert(data);
139     permData = static_cast<PermData *>(data);
140     switch (permData->m_type) {
141         case ACE_PREFERENCE_DENY:
142             /* FIXME: i18n */
143             return strdup("Deny");
144         case ACE_PREFERENCE_DEFAULT:
145         default:
146             /* FIXME: i18n */
147             return strdup("Default");
148     }
149
150     return NULL;
151 }
152
153 Evas_Object *PermView::getPermRadio(void *data,
154                                     Evas_Object */*obj*/,
155                                     const char */*part*/)
156 {
157     PermData *permData;
158     FeatureData *featureData;
159     Evas_Object *radio;
160     Evas_Object *radio_grp;
161     int type;
162
163     Assert(data);
164     permData = static_cast<PermData *>(data);
165     featureData = permData->m_featureData;
166     Assert(featureData);
167
168     radio_grp = featureData->m_rg;
169     Assert(radio_grp);
170     radio = elm_radio_add(radio_grp);
171     if (!radio)
172         return NULL;
173
174     type = static_cast<int>(permData->m_type);
175     elm_radio_state_value_set(radio, type);
176     elm_radio_group_add(radio, radio_grp);
177     if (permData->m_type == featureData->m_feature->getPerm())
178         elm_radio_value_set(radio, type);
179
180     return radio;
181 }
182
183 void PermView::delPermData(void *data, Evas_Object */*obj*/)
184 {
185     PermData *permData;
186
187     permData = static_cast<PermData *>(data);
188     delete permData;
189 }
190
191 char *PermView::getFeatureTitle(void */*data*/,
192                                 Evas_Object */*obj*/,
193                                 const char */*part*/)
194 {
195     const char *title;
196
197     /* FIXME: i18n */
198     title = "Features";
199
200     return strdup(title);
201 }
202
203 char *PermView::getFeatureStr(void *data,
204                               Evas_Object */*obj*/,
205                               const char */*part*/)
206 {
207     const char *str;
208     FeatureData *featureData;
209
210     featureData = static_cast<FeatureData *>(data);
211     if (!featureData)
212         return NULL;
213
214     str = featureData->m_feature->getName().c_str();
215     if (!str)
216         return NULL;
217
218     return strdup(str);
219 }
220
221 char *PermView::getFeatureStrWithPerm(void *data,
222                                       Evas_Object */*obj*/,
223                                       const char *part)
224 {
225     const char *str;
226     FeatureData *featureData;
227
228     featureData = static_cast<FeatureData *>(data);
229     if (!featureData)
230         return NULL;
231
232     str = NULL;
233     if (!strcmp(part, "elm.text.1")) {
234         str = featureData->m_feature->getName().c_str();
235     } else if (!strcmp(part, "elm.text.2")) {
236
237         switch (featureData->m_feature->getPerm()) {
238         case ACE_PREFERENCE_DENY:
239             /* FIXME: i18n */
240             str = "Deny";
241             break;
242         case ACE_PREFERENCE_DEFAULT:
243         default:
244             /* FIXME: i18n */
245             str = "Default";
246             break;
247         }
248     }
249
250     if (!str)
251         return NULL;
252
253     return strdup(str);
254 }
255
256 void PermView::delFeatureData(void *data, Evas_Object */*obj*/)
257 {
258     FeatureData *featureData;
259
260     featureData = static_cast<FeatureData *>(data);
261     if (featureData) {
262         if (featureData->m_rg)
263             evas_object_del(featureData->m_rg);
264         delete featureData;
265     }
266 }
267
268 char *PermView::getUriTitle(void */*data*/,
269                             Evas_Object */*obj*/,
270                             const char */*part*/)
271 {
272     const char *title;
273
274     /* FIXME: i18n */
275     title = "URI";
276
277     return strdup(title);
278 }
279
280 char *PermView::getUriStr(void *data,
281                           Evas_Object */*obj*/,
282                           const char */*part*/)
283 {
284     const char *str;
285     UriData *uriData;
286
287     uriData = static_cast<UriData *>(data);
288     if (!uriData)
289         return NULL;
290
291     str = uriData->m_uri.c_str();
292     if (!str)
293         return NULL;
294
295     return strdup(str);
296 }
297
298 void PermView::delUriData(void *data, Evas_Object */*obj*/)
299 {
300     UriData *uriData;
301
302     uriData = static_cast<UriData*>(data);
303     delete uriData;
304 }
305
306 void PermView::addFeatureTitle(Evas_Object *gl)
307 {
308     static Elm_Genlist_Item_Class itc;
309
310     Assert(gl);
311     itc.item_style = "dialogue/title";
312     itc.func.text_get = getFeatureTitle;
313     itc.func.content_get = NULL;
314     itc.func.state_get = NULL;
315     itc.func.del = NULL;
316     elm_genlist_item_append(gl,
317                             &itc,
318                             NULL,
319                             NULL,
320                             ELM_GENLIST_ITEM_NONE,
321                             NULL,
322                             NULL);
323 }
324
325 void PermView::addFeatureWithPerm(Evas_Object *gl, int idx)
326 {
327     static Elm_Genlist_Item_Class itc;
328     FeatureData *featureData;
329     Elm_Object_Item *it;
330     Evas_Object *rg;
331
332     Assert(gl);
333     itc.item_style = "dialogue/2text.3/expandable";
334     itc.func.text_get = getFeatureStrWithPerm;
335     itc.func.content_get = NULL;
336     itc.func.state_get = NULL;
337     itc.func.del = delFeatureData;
338
339     rg = elm_radio_add(gl);
340     if (!rg)
341         return;
342
343     try {
344         featureData = new FeatureData(&m_featureMap[idx], m_appID, rg);
345         it = elm_genlist_item_append(gl,
346                                      &itc,
347                                      static_cast<void *>(featureData),
348                                      NULL,
349                                      ELM_GENLIST_ITEM_TREE,
350                                      onFeatureClicked,
351                                      NULL);
352         if (!it) {
353             delete featureData;
354             return;
355         }
356     } catch (const std::bad_alloc &) {
357         evas_object_del(rg);
358     }
359 }
360
361 void PermView::addFeature(Evas_Object *gl, int idx)
362 {
363     static Elm_Genlist_Item_Class itc;
364     FeatureData *featureData;
365     Elm_Object_Item *it;
366
367     Assert(gl);
368     itc.item_style = "dialogue/1text";
369     itc.func.text_get = getFeatureStr;
370     itc.func.content_get = NULL;
371     itc.func.state_get = NULL;
372     itc.func.del = delFeatureData;
373
374     try {
375         featureData = new FeatureData(&m_featureMap[idx], m_appID);
376         it = elm_genlist_item_append(gl,
377                                      &itc,
378                                      static_cast<void *>(featureData),
379                                      NULL,
380                                      ELM_GENLIST_ITEM_NONE,
381                                      NULL,
382                                      NULL);
383         if (!it) {
384             delete featureData;
385             return;
386         }
387         elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
388     } catch (const std::bad_alloc &) {
389     }
390 }
391
392 void PermView::addFeatureList(Evas_Object *gl)
393 {
394     std::map<int, Feature>::iterator it;
395
396     if (m_featureMap.empty())
397         return;
398
399     addFeatureTitle(gl);
400     switch (m_appType) {
401     case WrtDB::APP_TYPE_TIZENWEBAPP:
402         for (it = m_featureMap.begin(); it != m_featureMap.end(); it++)
403             addFeature(gl, it->first);
404         break;
405     default:
406         break;
407     }
408
409     evas_object_smart_callback_add(gl, "expanded", onExpanded, NULL);
410     evas_object_smart_callback_add(gl, "contracted", onContracted, NULL);
411 }
412
413 void PermView::addUriTitle(Evas_Object *gl)
414 {
415     static Elm_Genlist_Item_Class itc;
416
417     Assert(gl);
418     itc.item_style = "dialogue/title";
419     itc.func.text_get = getUriTitle;
420     itc.func.content_get = NULL;
421     itc.func.state_get = NULL;
422     itc.func.del = NULL;
423     elm_genlist_item_append(gl,
424                             &itc,
425                             NULL,
426                             NULL,
427                             ELM_GENLIST_ITEM_NONE,
428                             NULL,
429                             NULL);
430 }
431
432 void PermView::addUri(Evas_Object *gl, DPL::String &uri)
433 {
434     UriData *uriData;
435     static Elm_Genlist_Item_Class itc;
436     Elm_Object_Item *it;
437
438     Assert(gl);
439     itc.item_style = "dialogue/1text";
440     itc.func.text_get = getUriStr;
441     itc.func.content_get = NULL;
442     itc.func.state_get = NULL;
443     itc.func.del = delUriData;
444
445     try {
446         uriData = new UriData(DPL::ToUTF8String(uri));
447         it = elm_genlist_item_append(gl,
448                                      &itc,
449                                      static_cast<void *>(uriData),
450                                      NULL,
451                                      ELM_GENLIST_ITEM_NONE,
452                                      NULL,
453                                      NULL);
454         if (!it) {
455             delete uriData;
456             return;
457         }
458         elm_genlist_item_select_mode_set(it, ELM_OBJECT_SELECT_MODE_DISPLAY_ONLY);
459     } catch (const std::bad_alloc &) {
460     }
461 }
462
463 void PermView::addAccessInfo(Evas_Object *gl)
464 {
465     if (m_accessInfo.empty())
466         return;
467
468     addUriTitle(gl);
469     FOREACH(accessInfo, m_accessInfo)
470         addUri(gl, accessInfo->strIRI);
471 }
472
473 Evas_Object *PermView::createContent(Evas_Object *parent)
474 {
475     Evas_Object *eo;
476
477     Assert(parent);
478     if (m_featureMap.empty() && m_accessInfo.empty()) {
479         eo = elm_layout_add(parent);
480         if (!eo)
481             return NULL;
482         elm_layout_theme_set(eo, "layout", "nocontents", "full");
483         elm_object_part_text_set(eo, "elm.text", D_("IDS_COM_BODY_NO_CONTENTS"));
484         return eo;
485     }
486
487     eo = elm_genlist_add(parent);
488     if (!eo)
489         return NULL;
490
491     addFeatureList(eo);
492     addAccessInfo(eo);
493
494     return eo;
495 }
496
497 Evas_Object *PermView::loadView(void)
498 {
499     Evas_Object *ctnt;
500     WrtDB::DbWidgetFeatureSet features;
501
502     resetBase();
503
504     try {
505         m_dao.Reset(new WrtDB::WidgetDAO(m_appID));
506         features = m_dao->getFeaturesList();
507         m_appType = m_dao->getWidgetType().appType;
508         switch (m_appType) {
509         case WrtDB::APP_TYPE_TIZENWEBAPP:
510             m_featureMap = TizenFeature::getFeatureMap(features);
511             break;
512         default:
513             break;
514         }
515         m_dao->getWidgetAccessInfo(m_accessInfo);
516
517         ctnt = createContent(m_parent);
518         if (!ctnt)
519             return NULL;
520         resetBase(ctnt);
521
522         return ctnt;
523
524     } catch (const std::bad_alloc &) {
525         return NULL;
526     }
527 }
528
529 PermView::PermView(Evas_Object *parent, int appID) :
530     m_parent(parent),
531     m_appType(WrtDB::APP_TYPE_UNKNOWN),
532     m_appID(appID)
533 {
534     m_dao.Reset();
535     m_featureMap.clear();
536     m_accessInfo.clear();
537 }
538
539 PermView::~PermView(void)
540 {
541 }
542
543 } /* WebAppDetailSetting */