OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chromeos/dbus/ibus/ibus_lookup_table.h" |
| 6 |
| 7 #include <string> |
| 8 #include "base/logging.h" |
| 9 #include "dbus/message.h" |
| 10 #include "chromeos/dbus/ibus/ibus_text.h" |
| 11 #include "chromeos/dbus/ibus/ibus_object.h" |
| 12 |
| 13 namespace chromeos { |
| 14 // TODO(nona): Remove ibus namespace after complete libibus removal. |
| 15 namespace ibus { |
| 16 |
| 17 void AppendIBusLookupTable(const IBusLookupTable& table, |
| 18 dbus::MessageWriter* writer) { |
| 19 IBusObjectWriter ibus_lookup_table_writer("IBusLookupTable", |
| 20 "uubbiavav", |
| 21 writer); |
| 22 ibus_lookup_table_writer.AppendUint32(table.page_size()); |
| 23 ibus_lookup_table_writer.AppendUint32(table.cursor_position()); |
| 24 ibus_lookup_table_writer.AppendBool(table.is_cursor_visible()); |
| 25 ibus_lookup_table_writer.AppendBool(false); // Not used in Chrome. |
| 26 ibus_lookup_table_writer.AppendInt32(static_cast<int32>(table.orientation())); |
| 27 |
| 28 const std::vector<IBusLookupTable::Entry>& candidates = table.candidates(); |
| 29 dbus::MessageWriter text_writer(NULL); |
| 30 ibus_lookup_table_writer.OpenArray("v", &text_writer); |
| 31 bool have_labels = false; |
| 32 for (size_t i = 0; i < candidates.size(); ++i) { |
| 33 // Write candidate string as IBusText. |
| 34 AppendStringAsIBusText(candidates[i].value, &text_writer); |
| 35 if (!candidates[i].label.empty()) |
| 36 have_labels = true; |
| 37 } |
| 38 ibus_lookup_table_writer.CloseContainer(&text_writer); |
| 39 |
| 40 dbus::MessageWriter label_writer(NULL); |
| 41 ibus_lookup_table_writer.OpenArray("v", &label_writer); |
| 42 |
| 43 // If there are not any labels, don't append labels. |
| 44 if (have_labels) { |
| 45 for (size_t i = 0; i < candidates.size(); ++i) { |
| 46 // Write label string as IBusText. |
| 47 AppendStringAsIBusText(candidates[i].label, &label_writer); |
| 48 } |
| 49 } |
| 50 ibus_lookup_table_writer.CloseContainer(&label_writer); |
| 51 |
| 52 ibus_lookup_table_writer.CloseAll(); |
| 53 } |
| 54 |
| 55 bool PopIBusLookupTable(dbus::MessageReader* reader, IBusLookupTable* table) { |
| 56 IBusObjectReader ibus_object_reader("IBusLookupTable", reader); |
| 57 if (!ibus_object_reader.Init()) |
| 58 return false; |
| 59 |
| 60 uint32 page_size = 0; |
| 61 if (!ibus_object_reader.PopUint32(&page_size)) { |
| 62 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 63 << "1st argument should be uint32."; |
| 64 return false; |
| 65 } |
| 66 table->set_page_size(page_size); |
| 67 |
| 68 uint32 cursor_position = 0; |
| 69 if (!ibus_object_reader.PopUint32(&cursor_position)) { |
| 70 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 71 << "2nd argument should be uint32."; |
| 72 return false; |
| 73 } |
| 74 table->set_cursor_position(cursor_position); |
| 75 |
| 76 bool cursor_visible = true; |
| 77 if (!ibus_object_reader.PopBool(&cursor_visible)) { |
| 78 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 79 << "3rd arugment should be boolean."; |
| 80 return false; |
| 81 } |
| 82 table->set_is_cursor_visible(cursor_visible); |
| 83 |
| 84 bool unused_round_value = true; |
| 85 if (!ibus_object_reader.PopBool(&unused_round_value)) { |
| 86 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 87 << "4th argument should be boolean."; |
| 88 return false; |
| 89 } |
| 90 |
| 91 int32 orientation = 0; |
| 92 if (!ibus_object_reader.PopInt32(&orientation)) { |
| 93 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 94 << "5th arguemnt should be int32."; |
| 95 return false; |
| 96 } |
| 97 table->set_orientation( |
| 98 static_cast<IBusLookupTable::Orientation>(orientation)); |
| 99 |
| 100 dbus::MessageReader text_array_reader(NULL); |
| 101 if (!ibus_object_reader.PopArray(&text_array_reader)) { |
| 102 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 103 << "6th argument should be array."; |
| 104 return false; |
| 105 } |
| 106 |
| 107 std::vector<IBusLookupTable::Entry>* candidates = table->mutable_candidates(); |
| 108 while (text_array_reader.HasMoreData()) { |
| 109 std::string candidate_text; |
| 110 // The attributes in IBusText are not used in Chrome. |
| 111 if (!PopStringFromIBusText(&text_array_reader, &candidate_text)) { |
| 112 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 113 << "6th argument should be array of IBusText."; |
| 114 return false; |
| 115 } |
| 116 IBusLookupTable::Entry entry; |
| 117 entry.value = candidate_text; |
| 118 candidates->push_back(entry); |
| 119 } |
| 120 |
| 121 dbus::MessageReader label_array_reader(NULL); |
| 122 if (!ibus_object_reader.PopArray(&label_array_reader)) { |
| 123 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 124 << "7th argument should be array."; |
| 125 return false; |
| 126 } |
| 127 |
| 128 if (!label_array_reader.HasMoreData()) { |
| 129 return true; |
| 130 } |
| 131 |
| 132 for (size_t i = 0; i < candidates->size(); ++i) { |
| 133 if (!label_array_reader.HasMoreData()) { |
| 134 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 135 << "The number of label entry does not match with candidate " |
| 136 << "text. Same length or no label entry can be accepted."; |
| 137 return false; |
| 138 } |
| 139 |
| 140 std::string label_text; |
| 141 // The attributes in IBusText are not used in Chrome. |
| 142 if (!PopStringFromIBusText(&label_array_reader, &label_text)) { |
| 143 LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| 144 << "7th argument should be array of IBusText."; |
| 145 return false; |
| 146 } |
| 147 (*candidates)[i].label = label_text; |
| 148 } |
| 149 return true; |
| 150 } |
| 151 |
| 152 /////////////////////////////////////////////////////////////////////////////// |
| 153 // IBusLookupTable |
| 154 IBusLookupTable::IBusLookupTable() |
| 155 : page_size_(0), |
| 156 cursor_position_(0), |
| 157 is_cursor_visible_(true), |
| 158 orientation_(IBUS_LOOKUP_TABLE_ORIENTATION_HORIZONTAL) { |
| 159 } |
| 160 |
| 161 IBusLookupTable::~IBusLookupTable() { |
| 162 } |
| 163 |
| 164 } // namespace ibus |
| 165 } // namespace chromeos |
OLD | NEW |