94e1222cfeaa31b2fc4d53edcf052680f698534d
[framework/web/wrt-commons.git] / modules / widget_dao / dao / common_dao_types.cpp
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  *
18  * @file    common_dao_types.h
19  * @author  Michal Ciepielski (m.ciepielski@samsung.com)
20  * @version 1.0
21  * @brief   This file contains the implementation of common data types for wrtdb
22  */
23
24 #include <dpl/wrt-dao-ro/common_dao_types.h>
25
26 #include <dpl/log/log.h>
27
28 namespace WrtDB {
29 namespace Powder {
30
31 Description::LevelEntry::LevelEntry(LevelEnum level) :
32     level(level)
33 {
34 }
35
36 bool Description::LevelEntry::isContextValid(LevelEnum level,
37         const DPL::OptionalString& aContext) const
38 {
39     if (!aContext) {
40         return level < level;
41     } else {
42         Context::const_iterator iter = context.find(*aContext);
43         if (iter != context.end()) {
44             return level < level;
45         } else {
46             return true;
47         }
48     }
49 }
50
51 bool Description::CategoryEntry::isCategoryValid(LevelEntry& aReason,
52         LevelEnum aLevel,
53         const DPL::OptionalString& aContext) const
54 {
55     for (LevelsContainer::const_iterator iter = levels.begin();
56          iter != levels.end(); ++iter) {
57         if (!iter->isContextValid(aLevel, aContext)) {
58             aReason = *iter;
59             return false;
60         }
61     }
62     return true;
63 }
64 }
65
66 namespace ChildProtection
67 {
68
69 PowderRules::CategoryRule::CategoryRule(const DPL::String& aCategory,
70         Powder::Description::LevelEnum aLevel,
71         const DPL::OptionalString& aContext) :
72     category(aCategory),
73     level(aLevel),
74     context(aContext)
75 {
76 }
77
78 PowderRules::PowderResult::PowderResult(InvalidReason aReason,
79         const Powder::Description::LevelEntry& anInvalidDescription,
80         const CategoryRule& anInvalidRule) :
81     invalidDescription(anInvalidDescription),
82     invalidRule(anInvalidRule),
83     reason(aReason)
84 {
85 }
86
87 //! Function checks if rule is fulfilled by description
88 //! \param[in] rule checked rule
89 //! \param[in] description
90 //! \retval true rule is valid
91 //! \retval false rule is invalid
92 PowderRules::ResultPair PowderRules::isRuleValidForDescription(
93         const CategoryRule& aRule,
94         const Powder::Description& aDescription) const
95 {
96     Powder::Description::CategoryEntries::const_iterator
97         iter = aDescription.categories.find(aRule.category);
98     if (iter != aDescription.categories.end()) {
99         Powder::Description::LevelEntry invalidDescription;
100         if (!iter->second.isCategoryValid(invalidDescription, aRule.level,
101                                           aRule.context)) {
102             LogWarning("Widget forbidden for children detected");
103             return std::make_pair(false,
104                                   PowderResult(PowderResult::InvalidRule,
105                                                invalidDescription,
106                                                aRule));
107         } else {
108             return std::make_pair(true, PowderResult());
109         }
110     } else {
111         return std::make_pair(true, PowderResult());
112     }
113 }
114
115 //! Function checks if age limit is fulfilled by description
116 //! \param[in] description
117 //! \retval true age is valid
118 //! \retval false age is invalid
119 PowderRules::ResultPair PowderRules::isAgeValidForDescription(
120         const Powder::Description& aDescription) const
121 {
122     if (!ageLimit) {
123         return std::make_pair(true, PowderResult());
124     } else {
125         if (!!aDescription.ageRating) {
126             if (*aDescription.ageRating <= *ageLimit) {
127                 return std::make_pair(true, PowderResult());
128             } else {
129                 return std::make_pair(false,
130                                       PowderResult(PowderResult::InvalidAge));
131             }
132         } else {
133             if (!isAgeRatingRequired) {
134                 return std::make_pair(true, PowderResult());
135             } else {
136                 return std::make_pair(
137                            false,
138                            PowderResult(PowderResult::AgeRatingNotSet));
139             }
140         }
141     }
142 }
143
144 //! Function check if Widget description is valid for ChildProtection
145 //! configuration
146 //! \param description widget description
147 //! \retval true widget is valid
148 //! \retval false widget is invalid
149 PowderRules::ResultPair PowderRules::isDescriptionValid(
150         const Powder::Description& aDescription) const
151 {
152     ResultPair powderResult;
153     for (RulesContainer::const_iterator iter = rules.begin();
154          iter != rules.end(); ++iter) {
155         powderResult = isRuleValidForDescription(*iter, aDescription);
156         if (!powderResult.first) {
157             return powderResult;
158         }
159     }
160     return isAgeValidForDescription(aDescription);
161 }
162
163 }
164 } // namespace WrtDB