2 * @file response_callback.cpp
4 * @brief Settings plugin response_callback implementation.
6 * @author Ossama Othman @<ossama.othman@@intel.com@>
9 * Copyright 2013 Intel Corporation All Rights Reserved.
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.
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.
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
27 #include <settingsd/response_callback.hpp>
28 #include <settingsd/json_glib_traits.hpp>
29 #include <settingsd/unique_ptr.hpp>
32 ivi::settings::response_callback::response_callback(
35 std::string transaction_id)
38 , transaction_id_(transaction_id)
42 ivi::settings::response_callback::response_callback(
43 response_callback const & other)
44 : writer_(other.writer_)
46 , transaction_id_(other.transaction_id_)
52 ivi::settings::response_callback::send_response(
53 std::function<void(JsonBuilder *)> response_builder) const
55 unique_ptr<JsonBuilder> const builder = begin_response("succeeded");
57 // Append successful Settings app results to the JSON formatted
59 response_builder(builder.get());
61 end_response(builder);
63 bool const success = writer_.send_payload("response", builder);
66 g_critical("Unable to send response for:\n"
68 " transactionid: %s\n",
70 transaction_id_.c_str());
76 ivi::settings::response_callback::send_error(
77 std::string error_message) const
79 unique_ptr<JsonBuilder> builder = begin_response("failed");
81 json_builder_set_member_name(builder.get(), "reason");;
82 json_builder_add_string_value(builder.get(), error_message.c_str());
84 end_response(builder);
86 bool const success = writer_.send_payload("error", builder);
89 g_critical("Unable to send error for:\n"
91 " transactionid: %s\n",
93 transaction_id_.c_str());
99 ivi::settings::response_callback::add_string_value(
100 JsonBuilder * builder,
101 std::string const & name,
102 std::string const & value) const
104 json_builder_set_member_name(builder, name.c_str());
106 json_builder_add_null_value(builder);
108 json_builder_add_string_value(builder, value.c_str());
111 ivi::settings::unique_ptr<JsonBuilder>
112 ivi::settings::response_callback::begin_response(
113 char const * result) const
115 // Construct JSON response.
116 unique_ptr<JsonBuilder> safe_builder(json_builder_new());
117 JsonBuilder * const builder = safe_builder.get();
119 json_builder_begin_object(builder);
121 add_string_value(builder, "type", type_);
122 add_string_value(builder, "transactionid", transaction_id_);
124 json_builder_set_member_name(builder, "result");
125 json_builder_add_string_value(builder, result);
131 ivi::settings::response_callback::end_response(
132 ivi::settings::unique_ptr<JsonBuilder> const & builder) const
134 json_builder_end_object(builder.get());
141 // indent-tabs-mode: nil