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