Chromium Code Reviews| Index: chromeos/dbus/ibus/ibus_lookup_table.cc |
| diff --git a/chromeos/dbus/ibus/ibus_lookup_table.cc b/chromeos/dbus/ibus/ibus_lookup_table.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..39965ac99826fd0afaa50dbaca3ac4ea1957978f |
| --- /dev/null |
| +++ b/chromeos/dbus/ibus/ibus_lookup_table.cc |
| @@ -0,0 +1,167 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromeos/dbus/ibus/ibus_lookup_table.h" |
| + |
| +#include <string> |
| +#include "base/logging.h" |
| +#include "dbus/message.h" |
| +#include "chromeos/dbus/ibus/ibus_text.h" |
| +#include "chromeos/dbus/ibus/ibus_object.h" |
| + |
| +namespace chromeos { |
| + |
| +// TODO(nona): Remove after complete libibus removal. |
| +using chromeos::ibus::IBusText; |
| +using chromeos::ibus::AppendStringAsIBusText; |
| +using chromeos::ibus::PopStringFromIBusText; |
|
satorux1
2012/05/18 18:27:22
I think we should use 'ibus' namespace here too.
Seigo Nonaka
2012/05/18 19:53:16
Done.
|
| + |
| +void AppendIBusLookupTable(const IBusLookupTable& table, |
| + dbus::MessageWriter* writer) { |
| + IBusObjectWriter ibus_lookup_table_writer("IBusLookupTable", |
| + "uubbiavav", |
| + writer); |
| + ibus_lookup_table_writer.AppendUint32(table.page_size()); |
| + ibus_lookup_table_writer.AppendUint32(table.cursor_position()); |
| + ibus_lookup_table_writer.AppendBool(table.is_cursor_visible()); |
| + ibus_lookup_table_writer.AppendBool(false); // Not used in Chrome. |
| + ibus_lookup_table_writer.AppendInt32(static_cast<int32>(table.orientation())); |
| + |
| + const std::vector<IBusLookupTable::Entry>& candidates = table.candidates(); |
| + dbus::MessageWriter text_writer(NULL); |
| + ibus_lookup_table_writer.OpenArray("v", &text_writer); |
| + bool write_label = false; |
|
satorux1
2012/05/18 18:27:22
write_label -> have_labels
Seigo Nonaka
2012/05/18 19:53:16
Done.
|
| + for (size_t i = 0; i < candidates.size(); ++i) { |
| + // Write candidate string as IBusText. |
| + AppendStringAsIBusText(candidates[i].value, &text_writer); |
| + if (!candidates[i].label.empty()) |
| + write_label = true; |
| + } |
| + ibus_lookup_table_writer.CloseContainer(&text_writer); |
| + |
| + dbus::MessageWriter label_writer(NULL); |
| + ibus_lookup_table_writer.OpenArray("v", &label_writer); |
| + |
| + // If there are not any labels, do not write empty string. |
|
satorux1
2012/05/18 18:27:22
If there are no labels, don't append labels.
Seigo Nonaka
2012/05/18 19:53:16
Done.
|
| + if (write_label) { |
| + for (size_t i = 0; i < candidates.size(); ++i) { |
| + // Write label string as IBusText. |
| + AppendStringAsIBusText(candidates[i].label, &label_writer); |
| + } |
| + } |
| + ibus_lookup_table_writer.CloseContainer(&label_writer); |
| + |
| + ibus_lookup_table_writer.CloseAll(); |
| +} |
| + |
| +bool PopIBusLookupTable(dbus::MessageReader* reader, IBusLookupTable* table) { |
| + IBusObjectReader ibus_object_reader("IBusLookupTable", reader); |
| + if (!ibus_object_reader.Init()) |
| + return false; |
| + |
| + uint32 page_size = 0; |
| + if (!ibus_object_reader.PopUint32(&page_size)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "1st argument should be uint32."; |
| + return false; |
| + } |
| + table->set_page_size(page_size); |
| + |
| + uint32 cursor_position = 0; |
| + if (!ibus_object_reader.PopUint32(&cursor_position)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "2nd argument should be uint32."; |
| + return false; |
| + } |
| + table->set_cursor_position(cursor_position); |
| + |
| + bool cursor_visible = true; |
| + if (!ibus_object_reader.PopBool(&cursor_visible)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "3rd arugment should be boolean."; |
| + return false; |
| + } |
| + table->set_is_cursor_visible(cursor_visible); |
| + |
| + bool unused_round_value = true; |
| + if (!ibus_object_reader.PopBool(&unused_round_value)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "4th argument should be boolean."; |
| + return false; |
| + } |
| + |
| + int32 orientation = 0; |
| + if (!ibus_object_reader.PopInt32(&orientation)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "5th arguemnt should be int32."; |
| + return false; |
| + } |
| + table->set_orientation( |
| + static_cast<IBusLookupTable::Orientation>(orientation)); |
| + |
| + dbus::MessageReader text_array_reader(NULL); |
| + if (!ibus_object_reader.PopArray(&text_array_reader)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "6th argument should be array."; |
| + return false; |
| + } |
| + |
| + std::vector<IBusLookupTable::Entry>* candidates = table->mutable_candidates(); |
| + while (text_array_reader.HasMoreData()) { |
| + std::string candidate_text; |
| + // The attributes in IBusText are not used in Chrome. |
| + if (!PopStringFromIBusText(&text_array_reader, &candidate_text)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "6th argument should be array of IBusText."; |
| + return false; |
| + } |
| + IBusLookupTable::Entry entry; |
| + entry.value = candidate_text; |
| + candidates->push_back(entry); |
| + } |
| + |
| + dbus::MessageReader label_array_reader(NULL); |
| + if (!ibus_object_reader.PopArray(&label_array_reader)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "7th argument should be array."; |
| + return false; |
| + } |
| + |
| + if (!label_array_reader.HasMoreData()) { |
| + return true; |
| + } |
| + |
| + for (size_t i = 0; i < candidates->size(); ++i) { |
| + if (!label_array_reader.HasMoreData()) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "The number of label entry does not match with candidate " |
| + << "text. Same length or no label entry can be accepted."; |
| + return false; |
| + } |
| + |
| + std::string label_text; |
| + // The attributes in IBusText are not used in Chrome. |
| + if (!PopStringFromIBusText(&label_array_reader, &label_text)) { |
| + LOG(ERROR) << "Invalid variant structure[IBusLookupTable]: " |
| + << "7th argument should be array of IBusText."; |
| + return false; |
| + } |
| + (*candidates)[i].label = label_text; |
| + } |
| + return true; |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| +// IBusLookupTable |
| +IBusLookupTable::IBusLookupTable() |
| + : page_size_(0), |
| + cursor_position_(0), |
| + is_cursor_visible_(true), |
| + orientation_(IBUS_LOOKUP_TABLE_ORIENTATION_HORIZONTAL) { |
| +} |
| + |
| +IBusLookupTable::~IBusLookupTable() { |
| +} |
| + |
| +} // namespace chromeos |