tizen 2.3.1 release
[framework/web/wearable/wrt-security.git] / ace / dao / PromptModel.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 /* @file        PromptModel.cpp
17  * @author      Justyna Mejzner (j.kwiatkowsk@samsung.com)
18  * @author      Jaroslaw Osmanski (j.osmanski@samsung.com)
19  * @version     1.0
20  *
21  */
22
23 #include <ace-dao-ro/PromptModel.h>
24
25 #include <algorithm>
26 #include <dpl/log/log.h>
27 #include <dpl/assert.h>
28
29 namespace {
30
31 const char INFO[] = "Widget requires access to:";
32 const char DENY[] = "Deny";
33 const char ALLOW[] = "Permit";
34
35 const char BLANKET_CHECKBOX_LABEL[] = "Keep setting as permanent";
36 const char SESSION_CHECKBOX_LABEL[] = "Remember for one run";
37
38 Prompt::ButtonLabels aceQuestionLabel = {DENY, ALLOW};
39
40 static Prompt::PromptLabels* getModel(
41         Prompt::PromptModel::PromptType promptType,
42         const std::string& resourceId)
43 {
44     std::string strLabel;
45     strLabel = INFO;
46     strLabel += "<br>";
47     strLabel += resourceId;
48
49     return new Prompt::PromptLabels(promptType, aceQuestionLabel, strLabel);
50 }
51
52 Prompt::Validity fromPromptTypeToValidity(int aPromptType, bool checkClicked)
53 {
54     using namespace Prompt;
55     PromptModel::PromptType promptTypeEnum =
56         static_cast<PromptModel::PromptType>(aPromptType);
57     switch (promptTypeEnum) {
58     case PromptModel::PROMPT_ONESHOT:
59         return Validity::ONCE;
60     case PromptModel::PROMPT_SESSION:
61         if (checkClicked)
62         {
63             return Validity::SESSION;
64         }
65         else
66         {
67             return Validity::ONCE;
68         }
69     case PromptModel::PROMPT_BLANKET:
70         if (checkClicked)
71         {
72             return Validity::ALWAYS;
73         }
74         else
75         {
76             return Validity::ONCE;
77         }
78     default:
79         Assert(0);
80         return Validity::ONCE;
81     }
82 }
83 } // namespace anonymous
84
85 namespace Prompt {
86
87
88 PromptLabels::PromptLabels(int promptType,
89                            const Prompt::ButtonLabels& questionLabel,
90                            const std::string& mainLabel) :
91                m_promptType(promptType),
92                m_buttonLabels(questionLabel),
93                m_mainLabel(mainLabel)
94 {
95
96 }
97
98 int PromptLabels::getPromptType() const
99 {
100     return m_promptType;
101 }
102 const ButtonLabels& PromptLabels::getButtonLabels() const
103 {
104     return m_buttonLabels;
105 }
106 const std::string& PromptLabels::getMainLabel() const
107 {
108     return m_mainLabel;
109 }
110
111 DPL::OptionalString PromptLabels::getCheckLabel() const
112 {
113     if (PromptModel::PROMPT_BLANKET == m_promptType)
114     {
115         return DPL::OptionalString(
116                 DPL::FromUTF8String(BLANKET_CHECKBOX_LABEL));
117     }
118     else if (PromptModel::PROMPT_SESSION == m_promptType)
119     {
120         return DPL::OptionalString(
121                 DPL::FromUTF8String(SESSION_CHECKBOX_LABEL));
122     }
123
124     return DPL::OptionalString();
125 }
126
127 bool PromptLabels::isAllowed(const size_t buttonClicked) const
128 {
129     Assert(buttonClicked < aceQuestionLabel.size() &&
130             "Button Clicked number is not in range of questionLabel");
131
132     return aceQuestionLabel[buttonClicked] == ALLOW;
133 }
134
135 PromptAnswer::PromptAnswer(bool isAccessAllowed, Validity validity) :
136         m_isAccessAllowed(isAccessAllowed),
137         m_validity(validity)
138 {
139
140 }
141
142 PromptAnswer::PromptAnswer(
143         int aPromptType, unsigned int buttonAns, bool checkAns)
144 {
145     Assert(buttonAns < aceQuestionLabel.size() &&
146             "Button Clicked number is not in range of questionLabel");
147
148     m_isAccessAllowed = aceQuestionLabel[buttonAns] == ALLOW;
149     m_validity = fromPromptTypeToValidity(aPromptType, checkAns);
150 }
151
152 bool PromptAnswer::isAccessAllowed() const
153 {
154     return m_isAccessAllowed;
155 }
156
157 Validity PromptAnswer::getValidity() const
158 {
159     return m_validity;
160 }
161
162 PromptLabels* PromptModel::getOneShotModel(const std::string& resourceId)
163 {
164     return getModel(PROMPT_ONESHOT, resourceId);
165 }
166
167 PromptLabels* PromptModel::getSessionModel(const std::string& resourceId)
168 {
169     return getModel(PROMPT_SESSION, resourceId);
170 }
171
172 PromptLabels* PromptModel::getBlanketModel(const std::string& resourceId)
173 {
174     return getModel(PROMPT_BLANKET, resourceId);
175 }
176
177
178 } // Prompt