| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/dbus/ibus/ibus_text.h" | 5 #include "chromeos/dbus/ibus/ibus_text.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/values.h" |
| 8 #include "chromeos/dbus/ibus/ibus_object.h" | 9 #include "chromeos/dbus/ibus/ibus_object.h" |
| 9 #include "dbus/message.h" | 10 #include "dbus/message.h" |
| 10 | 11 |
| 11 namespace chromeos { | 12 namespace chromeos { |
| 12 // TODO(nona): Remove ibus namespace after complete libibus removal. | 13 // TODO(nona): Remove ibus namespace after complete libibus removal. |
| 13 namespace ibus { | 14 namespace ibus { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 const uint32 kAttributeUnderline = 1; // Indicates underline attribute. | 17 const uint32 kAttributeUnderline = 1; // Indicates underline attribute. |
| 17 const uint32 kAttributeSelection = 2; // Indicates background attribute. | 18 const uint32 kAttributeSelection = 2; // Indicates background attribute. |
| 19 const char kAnnotationKey[] = "annotation"; |
| 20 const char kDescriptionKey[] = "description"; |
| 18 | 21 |
| 19 struct IBusAttribute { | 22 struct IBusAttribute { |
| 20 IBusAttribute() : type(0), value(0), start_index(0), end_index(0) {} | 23 IBusAttribute() : type(0), value(0), start_index(0), end_index(0) {} |
| 21 uint32 type; | 24 uint32 type; |
| 22 uint32 value; | 25 uint32 value; |
| 23 uint32 start_index; | 26 uint32 start_index; |
| 24 uint32 end_index; | 27 uint32 end_index; |
| 25 }; | 28 }; |
| 26 | 29 |
| 27 // Pops a IBusAttribute from |reader|. | 30 // Pops a IBusAttribute from |reader|. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 39 << "IBusAttribute should contain 4 unsigned integers."; | 42 << "IBusAttribute should contain 4 unsigned integers."; |
| 40 return false; | 43 return false; |
| 41 } | 44 } |
| 42 return true; | 45 return true; |
| 43 } | 46 } |
| 44 | 47 |
| 45 // Appends a IBusAttribute into |writer|. | 48 // Appends a IBusAttribute into |writer|. |
| 46 void AppendIBusAttribute(dbus::MessageWriter* writer, | 49 void AppendIBusAttribute(dbus::MessageWriter* writer, |
| 47 const IBusAttribute& attribute) { | 50 const IBusAttribute& attribute) { |
| 48 IBusObjectWriter ibus_attribute_writer("IBusAttribute", "uuuu", writer); | 51 IBusObjectWriter ibus_attribute_writer("IBusAttribute", "uuuu", writer); |
| 52 ibus_attribute_writer.CloseHeader(); |
| 49 ibus_attribute_writer.AppendUint32(attribute.type); | 53 ibus_attribute_writer.AppendUint32(attribute.type); |
| 50 ibus_attribute_writer.AppendUint32(attribute.value); | 54 ibus_attribute_writer.AppendUint32(attribute.value); |
| 51 ibus_attribute_writer.AppendUint32(attribute.start_index); | 55 ibus_attribute_writer.AppendUint32(attribute.start_index); |
| 52 ibus_attribute_writer.AppendUint32(attribute.end_index); | 56 ibus_attribute_writer.AppendUint32(attribute.end_index); |
| 53 ibus_attribute_writer.CloseAll(); | 57 ibus_attribute_writer.CloseAll(); |
| 54 } | 58 } |
| 55 | 59 |
| 56 } // namespace | 60 } // namespace |
| 57 | 61 |
| 58 void AppendIBusText(const IBusText& ibus_text, dbus::MessageWriter* writer) { | 62 void AppendIBusText(const IBusText& ibus_text, dbus::MessageWriter* writer) { |
| 59 IBusObjectWriter ibus_text_writer("IBusText", "sv", writer); | 63 IBusObjectWriter ibus_text_writer("IBusText", "sv", writer); |
| 60 | 64 |
| 65 if (!ibus_text.annotation().empty()) { |
| 66 scoped_ptr<base::Value> annotation( |
| 67 base::Value::CreateStringValue(ibus_text.annotation())); |
| 68 ibus_text_writer.AddAttachment(kAnnotationKey, *annotation.get()); |
| 69 } |
| 70 |
| 71 if (!ibus_text.description().empty()) { |
| 72 scoped_ptr<base::Value> description( |
| 73 base::Value::CreateStringValue(ibus_text.description())); |
| 74 ibus_text_writer.AddAttachment(kDescriptionKey, *description.get()); |
| 75 } |
| 76 ibus_text_writer.CloseHeader(); |
| 77 |
| 61 ibus_text_writer.AppendString(ibus_text.text()); | 78 ibus_text_writer.AppendString(ibus_text.text()); |
| 62 | 79 |
| 63 // Start appending IBusAttrList into IBusText | 80 // Start appending IBusAttrList into IBusText |
| 64 IBusObjectWriter ibus_attr_list_writer("IBusAttrList", "av", NULL); | 81 IBusObjectWriter ibus_attr_list_writer("IBusAttrList", "av", NULL); |
| 65 ibus_text_writer.AppendIBusObject(&ibus_attr_list_writer); | 82 ibus_text_writer.AppendIBusObject(&ibus_attr_list_writer); |
| 83 ibus_attr_list_writer.CloseHeader(); |
| 66 dbus::MessageWriter attribute_array_writer(NULL); | 84 dbus::MessageWriter attribute_array_writer(NULL); |
| 67 ibus_attr_list_writer.OpenArray("v", &attribute_array_writer); | 85 ibus_attr_list_writer.OpenArray("v", &attribute_array_writer); |
| 68 | 86 |
| 69 const std::vector<IBusText::UnderlineAttribute>& underline_attributes = | 87 const std::vector<IBusText::UnderlineAttribute>& underline_attributes = |
| 70 ibus_text.underline_attributes(); | 88 ibus_text.underline_attributes(); |
| 71 for (size_t i = 0; i < underline_attributes.size(); ++i) { | 89 for (size_t i = 0; i < underline_attributes.size(); ++i) { |
| 72 IBusAttribute attribute; | 90 IBusAttribute attribute; |
| 73 attribute.type = kAttributeUnderline; | 91 attribute.type = kAttributeUnderline; |
| 74 attribute.value = static_cast<uint32>(underline_attributes[i].type); | 92 attribute.value = static_cast<uint32>(underline_attributes[i].type); |
| 75 attribute.start_index = underline_attributes[i].start_index; | 93 attribute.start_index = underline_attributes[i].start_index; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 97 void CHROMEOS_EXPORT AppendStringAsIBusText(const std::string& text, | 115 void CHROMEOS_EXPORT AppendStringAsIBusText(const std::string& text, |
| 98 dbus::MessageWriter* writer) { | 116 dbus::MessageWriter* writer) { |
| 99 IBusText ibus_text; | 117 IBusText ibus_text; |
| 100 ibus_text.set_text(text); | 118 ibus_text.set_text(text); |
| 101 AppendIBusText(ibus_text, writer); | 119 AppendIBusText(ibus_text, writer); |
| 102 } | 120 } |
| 103 | 121 |
| 104 bool PopIBusText(dbus::MessageReader* reader, IBusText* ibus_text) { | 122 bool PopIBusText(dbus::MessageReader* reader, IBusText* ibus_text) { |
| 105 IBusObjectReader ibus_text_reader("IBusText", reader); | 123 IBusObjectReader ibus_text_reader("IBusText", reader); |
| 106 | 124 |
| 107 dbus::MessageReader attachment_reader(NULL); | 125 if (!ibus_text_reader.Init()) |
| 108 if (!ibus_text_reader.InitWithAttachmentReader(&attachment_reader)) | |
| 109 return false; | 126 return false; |
| 110 | 127 |
| 111 while (attachment_reader.HasMoreData()) { | 128 base::Value* value; |
| 112 dbus::MessageReader dictionary_reader(NULL); | 129 if (ibus_text_reader.GetAttachment(kAnnotationKey, &value)) { |
| 113 if (!attachment_reader.PopDictEntry(&dictionary_reader)) { | 130 std::string annotation; |
| 114 LOG(ERROR) << "Invalid attachment structure: " | 131 if (value->GetAsString(&annotation)) |
| 115 << "The attachment field is array of dictionary entry."; | 132 ibus_text->set_annotation(annotation); |
| 116 return false; | 133 } |
| 117 } | |
| 118 | 134 |
| 119 std::string key; | 135 if (ibus_text_reader.GetAttachment(kDescriptionKey, &value)) { |
| 120 if (!dictionary_reader.PopString(&key)) { | 136 std::string description; |
| 121 LOG(ERROR) << "Invalid attachement structure: " | 137 if (value->GetAsString(&description)) |
| 122 << "The 1st dictionary entry should be string."; | 138 ibus_text->set_description(description); |
| 123 return false; | |
| 124 } | |
| 125 | |
| 126 dbus::MessageReader variant_reader(NULL); | |
| 127 if (!dictionary_reader.PopVariant(&variant_reader)) { | |
| 128 LOG(ERROR) << "Invalid attachment structure: " | |
| 129 << "The 2nd dictionary entry shuold be variant."; | |
| 130 return false; | |
| 131 } | |
| 132 | |
| 133 dbus::MessageReader sub_variant_reader(NULL); | |
| 134 if (!variant_reader.PopVariant(&sub_variant_reader)) { | |
| 135 LOG(ERROR) << "Invalid attachment structure: " | |
| 136 << "The 2nd variant entry should contain variant."; | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 std::string value; | |
| 141 if (!sub_variant_reader.PopString(&value)) | |
| 142 continue; // Ignore other attachment values. | |
| 143 | |
| 144 if (key == "annotation") | |
| 145 ibus_text->set_annotation(value); | |
| 146 else if (key == "description") | |
| 147 ibus_text->set_description(value); | |
| 148 else | |
| 149 continue; // Ignore other fields. | |
| 150 } | 139 } |
| 151 | 140 |
| 152 std::string text; | 141 std::string text; |
| 153 if (!ibus_text_reader.PopString(&text)) { | 142 if (!ibus_text_reader.PopString(&text)) { |
| 154 LOG(ERROR) << "Invalid variant structure[IBusText]: " | 143 LOG(ERROR) << "Invalid variant structure[IBusText]: " |
| 155 << "1st argument should be string."; | 144 << "1st argument should be string."; |
| 156 return false; | 145 return false; |
| 157 } | 146 } |
| 158 | 147 |
| 159 ibus_text->set_text(text); | 148 ibus_text->set_text(text); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 // IBusText | 206 // IBusText |
| 218 IBusText::IBusText() | 207 IBusText::IBusText() |
| 219 : text_("") { | 208 : text_("") { |
| 220 } | 209 } |
| 221 | 210 |
| 222 IBusText::~IBusText() { | 211 IBusText::~IBusText() { |
| 223 } | 212 } |
| 224 | 213 |
| 225 } // namespace ibus | 214 } // namespace ibus |
| 226 } // namespace chromeos | 215 } // namespace chromeos |
| OLD | NEW |