Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / chromeos / dbus / ibus / ibus_property.cc
blob56c0d7662b42d1089892c74a342ef768ace5bdcd
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/dbus/ibus/ibus_property.h"
7 #include <string>
8 #include "base/logging.h"
9 #include "chromeos/dbus/ibus/ibus_constants.h"
10 #include "chromeos/dbus/ibus/ibus_object.h"
11 #include "chromeos/dbus/ibus/ibus_text.h"
12 #include "dbus/message.h"
14 namespace chromeos {
16 bool CHROMEOS_EXPORT PopIBusProperty(dbus::MessageReader* reader,
17 IBusProperty* property) {
18 IBusObjectReader ibus_property_reader("IBusProperty", reader);
19 if (!ibus_property_reader.Init())
20 return false;
22 std::string key;
23 if (!ibus_property_reader.PopString(&key)) {
24 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
25 << "1st argument should be string.";
26 return false;
28 property->set_key(key);
30 uint32 type = 0;
31 if (!ibus_property_reader.PopUint32(&type)) {
32 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
33 << "2nd argument should be unsigned 32bit integer.";
34 return false;
36 if (type > 4UL) {
37 LOG(ERROR) << "Invalid value for IBusProperty type";
38 return false;
40 property->set_type(static_cast<IBusProperty::IBusPropertyType>(type));
42 std::string label;
43 if (!ibus_property_reader.PopStringFromIBusText(&label)) {
44 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
45 << "3rd argument should be IBusText.";
46 return false;
48 property->set_label(label);
50 // The 4th string argument represents icon path, but not supported in
51 // Chrome OS.
52 std::string icon;
53 if (!ibus_property_reader.PopString(&icon)) {
54 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
55 << "4th argument should be string.";
58 std::string tooltip;
59 if (!ibus_property_reader.PopStringFromIBusText(&tooltip)) {
60 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
61 << "5th argument should be IBusText.";
62 return false;
64 property->set_tooltip(tooltip);
66 // The 6th bool argument represents whether the property is event sensitive or
67 // not, but not supported in Chrome OS.
68 bool sensitive = true;
69 if (!ibus_property_reader.PopBool(&sensitive)) {
70 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
71 << "6th argument should be boolean.";
74 bool visible = true;
75 if (!ibus_property_reader.PopBool(&visible)) {
76 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
77 << "7th argument should be boolean.";
78 return false;
80 property->set_visible(visible);
82 uint32 state = 0;
83 if (!ibus_property_reader.PopUint32(&state)) {
84 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
85 << "8th argument should be unsigned 32bit integer.";
86 return false;
89 if (type > 3UL) {
90 LOG(ERROR) << "Invalid value for IBusProperty.";
91 return false;
94 DCHECK_LE(state, 3UL);
95 if (state == ibus::IBUS_PROPERTY_STATE_INCONSISTENT) {
96 LOG(ERROR) << "PROP_STATE_INCONSISTENT is not supported in Chrome OS.";
97 } else {
98 property->set_checked(state == ibus::IBUS_PROPERTY_STATE_CHECKED);
101 if (!ibus_property_reader.PopIBusPropertyList(
102 property->mutable_sub_properties())) {
103 LOG(ERROR) << "Invalid variant structure[IBusProperty]: "
104 << "9th argument should be IBusPropList.";
105 return false;
108 return true;
111 bool CHROMEOS_EXPORT PopIBusPropertyList(dbus::MessageReader* reader,
112 IBusPropertyList* property_list) {
113 IBusObjectReader ibus_property_reader("IBusPropList", reader);
114 if (!ibus_property_reader.Init())
115 return false;
117 dbus::MessageReader property_array_reader(NULL);
118 if (!ibus_property_reader.PopArray(&property_array_reader)) {
119 LOG(ERROR) << "Invalid variant structure[IBusPropList]: "
120 << "1st argument should be array.";
121 return false;
123 property_list->clear();
124 while (property_array_reader.HasMoreData()) {
125 IBusProperty* property = new IBusProperty;
126 if (!PopIBusProperty(&property_array_reader, property)) {
127 LOG(ERROR) << "Invalid variant structure[IBusPropList]: "
128 << "1st argument should be array of IBusProperty.";
129 return false;
131 property_list->push_back(property);
134 return true;
137 void CHROMEOS_EXPORT AppendIBusProperty(const IBusProperty& property,
138 dbus::MessageWriter* writer) {
139 IBusObjectWriter ibus_property_writer("IBusProperty", "suvsvbbuv", writer);
140 ibus_property_writer.CloseHeader();
142 ibus_property_writer.AppendString(property.key());
143 ibus_property_writer.AppendUint32(static_cast<uint32>(property.type()));
144 ibus_property_writer.AppendStringAsIBusText(property.label());
145 ibus_property_writer.AppendString(""); // The icon path is not supported.
146 ibus_property_writer.AppendStringAsIBusText(property.tooltip());
147 // The event sensitive field is not supported.
148 ibus_property_writer.AppendBool(false);
149 ibus_property_writer.AppendBool(property.visible());
150 ibus_property_writer.AppendUint32(static_cast<uint32>(property.checked()));
151 ibus_property_writer.AppendIBusPropertyList(property.sub_properties());
152 ibus_property_writer.CloseAll();
155 void CHROMEOS_EXPORT AppendIBusPropertyList(
156 const IBusPropertyList& property_list,
157 dbus::MessageWriter* writer) {
158 IBusObjectWriter ibus_property_list_writer("IBusPropList", "av", writer);
159 ibus_property_list_writer.CloseHeader();
160 dbus::MessageWriter property_list_writer(NULL);
161 ibus_property_list_writer.OpenArray("v", &property_list_writer);
162 for (size_t i = 0; i < property_list.size(); ++i) {
163 AppendIBusProperty(*(property_list[i]), &property_list_writer);
165 ibus_property_list_writer.CloseContainer(&property_list_writer);
166 ibus_property_list_writer.CloseAll();
170 ///////////////////////////////////////////////////////////////////////////////
171 // IBusProperty
172 IBusProperty::IBusProperty()
173 : type_(IBUS_PROPERTY_TYPE_NORMAL),
174 visible_(false),
175 checked_(false) {
178 IBusProperty::~IBusProperty() {
181 } // namespace chromeos