Tizen 2.1 base
[framework/security/security-server.git] / ace_popup_validation / src / ace_api_popup_validation.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  * @file        ace_api_popup_validation.cpp
18  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version     1.0
20  * @brief       This file contains implementation of ACE popup validation API.
21  */
22
23 #include <string>
24 #include <vector>
25 #include <dpl/log/log.h>
26 #include "SecurityCommunicationClient.h"
27 #include "popup_response_server_api.h"
28 #include "security_daemon_dbus_config.h"
29 #include "ace_api_popup_validation.h"
30
31 namespace {
32 static WrtSecurity::Communication::Client *communicationClient = NULL;
33 static const int VALIDITY_ONCE_VALUE = 0;
34 static const int VALIDITY_SESSION_VALUE = 1;
35 static const int VALIDITY_ALWAYS_VALUE = 1;
36 } // anonymous
37
38 ace_return_t ace_popup_validation_initialize(void)
39 {
40     if (NULL != communicationClient) {
41         LogError("ace_api_popup_validation already initialized");
42         return ACE_INTERNAL_ERROR;
43     }
44     Try {
45         communicationClient = new WrtSecurity::Communication::Client(
46                    WrtSecurity::PopupServerApi::INTERFACE_NAME());
47     } Catch (WrtSecurity::Communication::Client::Exception::SecurityCommunicationClientException) {
48         LogError("Can't connect to daemon");
49         return ACE_INTERNAL_ERROR;
50     }
51
52     return ACE_OK; 
53 }
54
55 ace_return_t ace_popup_validation_shutdown(void)
56 {
57     if (NULL == communicationClient) {
58         LogError("ace_api_popup_validation not initialized");
59         return ACE_INTERNAL_ERROR;
60     }
61     delete communicationClient;
62     communicationClient = NULL;
63
64     return ACE_OK;
65 }
66
67 ace_return_t ace_validate_answer(ace_bool_t answer,
68                                  ace_validity_t validity,
69                                  const ace_resource_t resource_name,
70                                  const ace_session_id_t session_id,
71                                  const ace_param_list_t* param_list,
72                                  ace_widget_handle_t handle,
73                                  ace_bool_t* validation_result)
74 {
75     if (NULL == resource_name ||
76         NULL == session_id ||
77         NULL == param_list ||
78         NULL == validation_result)
79     {
80         LogError("NULL argument(s) passed");
81         return ACE_INVALID_ARGUMENTS;
82     }
83
84     bool dbusAnswer = answer == ACE_TRUE;
85     int dbusValidity = 0;
86
87     switch (validity) {
88     case ACE_ONCE: {
89         dbusValidity = VALIDITY_ONCE_VALUE;
90         //static_cast<int>(Prompt::Validity::ONCE);
91         break; }
92     case ACE_SESSION: {
93         dbusValidity = VALIDITY_SESSION_VALUE;
94         //static_cast<int>(Prompt::Validity::SESSION);
95         break; }
96     case ACE_ALWAYS: {
97         dbusValidity = VALIDITY_ALWAYS_VALUE;
98         //static_cast<int>(Prompt::Validity::ALWAYS);
99         break; }
100     default: {
101         LogError("Invalid validity passed");
102         return ACE_INVALID_ARGUMENTS; }
103     }
104
105     std::string subjectId;
106     std::string resourceId(resource_name);
107     std::string sessionId(session_id);
108     std::vector<std::string> keys, values;
109     unsigned int i;
110     for (i = 0; i < param_list->count; ++i) {
111         keys.push_back(std::string(param_list->items[i].name));
112         values.push_back(std::string(param_list->items[i].value));
113     }
114
115     bool response = false;
116     Try{
117         communicationClient->call(WrtSecurity::PopupServerApi::VALIDATION_METHOD(),
118                          dbusAnswer,
119                          dbusValidity,
120                          handle,
121                          subjectId,
122                          resourceId,
123                          keys,
124                          values,
125                          sessionId,
126                          &response);
127     } Catch (WrtSecurity::Communication::Client::Exception::SecurityCommunicationClientException) {
128         LogError("Can't call daemon");
129         return ACE_INTERNAL_ERROR;
130     }
131
132     *validation_result = response ? ACE_TRUE : ACE_FALSE;
133
134     return ACE_OK;
135 }