OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS 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 <assert.h> | 5 #include <assert.h> |
6 #include <glib.h> | 6 #include <glib.h> |
7 #include <ibus.h> | 7 #include <ibus.h> |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
| 11 namespace { |
| 12 |
| 13 const gchar kDummySection[] = "aaa/bbb"; |
| 14 const gchar kDummyConfigName[] = "ccc"; |
| 15 |
| 16 const gboolean kDummyValueBoolean = TRUE; |
| 17 const gint kDummyValueInt = 12345; |
| 18 const gdouble kDummyValueDouble = 12345.54321; |
| 19 const gchar kDummyValueString[] = "dummy value"; |
| 20 |
| 21 // Unsets a dummy value from ibus config service. |
| 22 void UnsetConfigAndPrintResult(IBusConfig* ibus_config) { |
| 23 if (ibus_config_unset(ibus_config, kDummySection, kDummyConfigName)) { |
| 24 printf("OK\n"); |
| 25 } else { |
| 26 printf("FAIL\n"); |
| 27 } |
| 28 } |
| 29 |
| 30 // Sets a dummy value to ibus config service. You can specify a type of the |
| 31 // dummy value by |type_string|. "boolean", "int", "double", or "string" are |
| 32 // allowed. |
| 33 void SetConfigAndPrintResult( |
| 34 IBusConfig* ibus_config, const std::string& type_string) { |
| 35 GValue gvalue = {0}; |
| 36 |
| 37 if (type_string == "boolean") { |
| 38 g_value_init(&gvalue, G_TYPE_BOOLEAN); |
| 39 g_value_set_boolean(&gvalue, kDummyValueBoolean); |
| 40 } else if (type_string == "int") { |
| 41 g_value_init(&gvalue, G_TYPE_INT); |
| 42 g_value_set_int(&gvalue, kDummyValueInt); |
| 43 } else if (type_string == "double") { |
| 44 g_value_init(&gvalue, G_TYPE_DOUBLE); |
| 45 g_value_set_double(&gvalue, kDummyValueDouble); |
| 46 } else if (type_string == "string") { |
| 47 g_value_init(&gvalue, G_TYPE_STRING); |
| 48 g_value_set_string(&gvalue, kDummyValueString); |
| 49 } else { |
| 50 printf("FAIL (unknown type: %s)\n", type_string.c_str()); |
| 51 return; |
| 52 } |
| 53 |
| 54 if (ibus_config_set_value( |
| 55 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { |
| 56 printf("OK\n"); |
| 57 } else { |
| 58 printf("FAIL\n"); |
| 59 } |
| 60 } |
| 61 |
| 62 // Gets a dummy value from ibus config service. This function checks if the |
| 63 // dummy value is |type_string| type. |
| 64 void GetConfigAndPrintResult( |
| 65 IBusConfig* ibus_config, const std::string& type_string) { |
| 66 GValue gvalue = {0}; |
| 67 if (!ibus_config_get_value( |
| 68 ibus_config, kDummySection, kDummyConfigName, &gvalue)) { |
| 69 printf("FAIL (not found)\n"); |
| 70 return; |
| 71 } |
| 72 |
| 73 if (type_string == "boolean") { |
| 74 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_BOOLEAN) || |
| 75 (g_value_get_boolean(&gvalue) != kDummyValueBoolean)) { |
| 76 printf("FAIL (type/value mismatch)\n"); |
| 77 return; |
| 78 } |
| 79 } else if (type_string == "int") { |
| 80 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_INT) || |
| 81 (g_value_get_int(&gvalue) != kDummyValueInt)) { |
| 82 printf("FAIL (type/value mismatch)\n"); |
| 83 return; |
| 84 } |
| 85 } else if (type_string == "double") { |
| 86 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_DOUBLE) || |
| 87 // We allow errors for double values. |
| 88 (g_value_get_double(&gvalue) < kDummyValueDouble - 0.001) || |
| 89 (g_value_get_double(&gvalue) > kDummyValueDouble + 0.001)) { |
| 90 printf("FAIL (type/value mismatch)\n"); |
| 91 return; |
| 92 } |
| 93 } else if (type_string == "string") { |
| 94 if ((G_VALUE_TYPE(&gvalue) != G_TYPE_STRING) || |
| 95 (g_value_get_string(&gvalue) != std::string(kDummyValueString))) { |
| 96 printf("FAIL (type/value mismatch)\n"); |
| 97 return; |
| 98 } |
| 99 } else { |
| 100 printf("FAIL (unknown type: %s)\n", type_string.c_str()); |
| 101 return; |
| 102 } |
| 103 |
| 104 printf("OK\n"); |
| 105 } |
| 106 |
11 // Prints the names of the given engines. Takes the ownership of |engines|. | 107 // Prints the names of the given engines. Takes the ownership of |engines|. |
12 void PrintEngineNames(GList* engines) { | 108 void PrintEngineNames(GList* engines) { |
13 for (GList* cursor = engines; cursor; cursor = g_list_next(cursor)) { | 109 for (GList* cursor = engines; cursor; cursor = g_list_next(cursor)) { |
14 IBusEngineDesc* engine_desc = IBUS_ENGINE_DESC(cursor->data); | 110 IBusEngineDesc* engine_desc = IBUS_ENGINE_DESC(cursor->data); |
15 assert(engine_desc); | 111 assert(engine_desc); |
16 printf("%s\n", engine_desc->name); | 112 printf("%s\n", engine_desc->name); |
17 g_object_unref(IBUS_ENGINE_DESC(cursor->data)); | 113 g_object_unref(IBUS_ENGINE_DESC(cursor->data)); |
18 } | 114 } |
19 g_list_free(engines); | 115 g_list_free(engines); |
20 } | 116 } |
21 | 117 |
| 118 void PrintUsage(const char* argv0) { |
| 119 printf("Usage: %s COMMAND\n", argv0); |
| 120 printf("check_reachable Check if ibus-daemon is reachable\n"); |
| 121 printf("list_engines List engine names (all engines)\n"); |
| 122 printf("list_active_engines List active engine names\n"); |
| 123 // TODO(yusukes): Add tests for array of {bool, int, double, string}. |
| 124 // TODO(yusukes): Add 2 parameters, config_key and config_value, to |
| 125 // set_config and get_config commands. |
| 126 printf("set_config (boolean|int|double|string)\n" |
| 127 " Set a dummy value to ibus config service\n"); |
| 128 printf("get_config (boolean|int|double|string)\n" |
| 129 " Get a dummy value from ibus config service\n"); |
| 130 // TODO(yusukes): Add config_key parameter. |
| 131 printf("unset_config Unset a dummy value from ibus config service\n"); |
| 132 } |
| 133 |
| 134 } // namespace |
| 135 |
22 int main(int argc, char **argv) { | 136 int main(int argc, char **argv) { |
23 if (argc == 1) { | 137 if (argc == 1) { |
24 printf("Usage: %s COMMAND\n", argv[0]); | 138 PrintUsage(argv[0]); |
25 printf("check_reachable Check if ibus-daemon is reachable\n"); | |
26 printf("list_engines List engine names (all engines)\n"); | |
27 printf("list_active_engines List active engine names\n"); | |
28 return 1; | 139 return 1; |
29 } | 140 } |
30 | 141 |
31 ibus_init(); | 142 ibus_init(); |
32 bool connected = false; | 143 bool connected = false; |
33 IBusBus* ibus = ibus_bus_new(); | 144 IBusBus* ibus = ibus_bus_new(); |
34 if (ibus) { | 145 if (ibus) { |
35 connected = ibus_bus_is_connected(ibus); | 146 connected = ibus_bus_is_connected(ibus); |
36 } | 147 } |
37 | 148 |
38 const std::string command = argv[1]; | 149 const std::string command = argv[1]; |
39 if (command == "check_reachable") { | 150 if (command == "check_reachable") { |
40 printf("%s\n", connected ? "YES" : "NO"); | 151 printf("%s\n", connected ? "YES" : "NO"); |
| 152 return 0; |
41 } | 153 } |
42 | 154 |
43 // Other commands need the bus to be connected. | 155 // Other commands need the bus to be connected. |
44 assert(ibus); | 156 assert(ibus); |
45 assert(connected); | 157 assert(connected); |
| 158 IBusConnection* ibus_connection = ibus_bus_get_connection(ibus); |
| 159 assert(ibus_connection); |
| 160 IBusConfig* ibus_config = ibus_config_new(ibus_connection); |
| 161 assert(ibus_config); |
| 162 |
46 if (command == "list_engines") { | 163 if (command == "list_engines") { |
47 PrintEngineNames(ibus_bus_list_engines(ibus)); | 164 PrintEngineNames(ibus_bus_list_engines(ibus)); |
48 } else if (command == "list_active_engines") { | 165 } else if (command == "list_active_engines") { |
49 PrintEngineNames(ibus_bus_list_active_engines(ibus)); | 166 PrintEngineNames(ibus_bus_list_active_engines(ibus)); |
| 167 } else if (command == "set_config") { |
| 168 if (argc != 3) { |
| 169 PrintUsage(argv[0]); |
| 170 return 1; |
| 171 } |
| 172 SetConfigAndPrintResult(ibus_config, argv[2]); |
| 173 } else if (command == "get_config") { |
| 174 if (argc != 3) { |
| 175 PrintUsage(argv[0]); |
| 176 return 1; |
| 177 } |
| 178 GetConfigAndPrintResult(ibus_config, argv[2]); |
| 179 } else if (command == "unset_config") { |
| 180 UnsetConfigAndPrintResult(ibus_config); |
50 } | 181 } |
51 | 182 |
52 return 0; | 183 return 0; |
53 } | 184 } |
OLD | NEW |