Added API for enabling tethering
[profile/ivi/settings-daemon.git] / lib / response_callback.cpp
1 /**
2  * @file response_callback.cpp
3  *
4  * @brief Settings plugin response_callback implementation.
5  *
6  * @author Ossama Othman @<ossama.othman@@intel.com@>
7  *
8  * @copyright @par
9  * Copyright 2013 Intel Corporation All Rights Reserved.
10  * @par
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License.
15  * @par
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  * @par
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA  02110-1301  USA
25  */
26
27 #include <settingsd/response_callback.hpp>
28 #include <settingsd/json_glib_traits.hpp>
29 #include <settingsd/unique_ptr.hpp>
30
31
32 ivi::settings::response_callback::response_callback(
33   libwebsocket * wsi,
34   std::string type,
35   std::string transaction_id)
36   : writer_(wsi)
37   , type_(type)
38   , transaction_id_(transaction_id)
39 {
40 }
41
42 ivi::settings::response_callback::response_callback(
43   response_callback const & other)
44   : writer_(other.writer_)
45   , type_(other.type_)
46   , transaction_id_(other.transaction_id_)
47 {
48 }
49
50
51 bool
52 ivi::settings::response_callback::send_response(
53   std::function<void(JsonBuilder *)> response_builder) const
54 {
55   unique_ptr<JsonBuilder> const builder = begin_response("succeeded");
56
57   // Append successful Settings app results to the JSON formatted
58   // response.
59   response_builder(builder.get());
60
61   end_response(builder);
62
63   bool const success = writer_.send_payload("response", builder);
64
65   if (!success)
66     g_critical("Unable to send response for:\n"
67                "    type:          %s\n"
68                "    transactionid: %s\n",
69                type_.c_str(),
70                transaction_id_.c_str());
71
72   return success;
73 }
74
75 bool
76 ivi::settings::response_callback::send_error(
77   std::string error_message) const
78 {
79   unique_ptr<JsonBuilder> builder = begin_response("failed");
80
81   json_builder_set_member_name(builder.get(), "reason");;
82   json_builder_add_string_value(builder.get(), error_message.c_str());
83
84   end_response(builder);
85
86   bool const success = writer_.send_payload("error", builder);
87
88   if (!success)
89     g_critical("Unable to send error for:\n"
90                "    type:          %s\n"
91                "    transactionid: %s\n",
92                type_.c_str(),
93                transaction_id_.c_str());
94
95   return success;
96 }
97
98 void
99 ivi::settings::response_callback::add_string_value(
100   JsonBuilder * builder,
101   std::string const & name,
102   std::string const & value) const
103 {
104   json_builder_set_member_name(builder, name.c_str());
105   if (value.empty())
106     json_builder_add_null_value(builder);
107   else
108     json_builder_add_string_value(builder, value.c_str());
109 }
110
111 ivi::settings::unique_ptr<JsonBuilder>
112 ivi::settings::response_callback::begin_response(
113   char const * result) const
114 {
115   // Construct JSON response.
116   unique_ptr<JsonBuilder> safe_builder(json_builder_new());
117   JsonBuilder * const builder = safe_builder.get();
118
119   json_builder_begin_object(builder);
120
121   add_string_value(builder, "type",          type_);
122   add_string_value(builder, "transactionid", transaction_id_);
123
124   json_builder_set_member_name(builder, "result");
125   json_builder_add_string_value(builder, result);
126
127   return safe_builder;
128 }
129
130 void
131 ivi::settings::response_callback::end_response(
132   ivi::settings::unique_ptr<JsonBuilder> const & builder) const
133 {
134   json_builder_end_object(builder.get());
135 }
136
137
138 // Local Variables:
139 // mode:c++
140 // c-basic-offset:2
141 // indent-tabs-mode: nil
142 // End: