2
Small utility to excersise the HUD from the command line
4
Copyright 2011 Canonical Ltd.
7
Ted Gould <ted@canonical.com>
9
This program is free software: you can redistribute it and/or modify it
10
under the terms of the GNU General Public License version 3, as published
11
by the Free Software Foundation.
13
This program is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranties of
15
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
16
PURPOSE. See the GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License along
19
with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <gio/gunixinputstream.h>
29
#include <hud-client.h>
32
static void print_suggestions(const char * query);
33
static void models_ready (HudClientQuery * client_query, gpointer user_data);
34
static HudClientQuery * client_query = NULL;
37
main (int argc, char *argv[])
39
#ifndef GLIB_VERSION_2_36
43
const gchar * search = "";
49
printf("\nsearch token: %s\n", search);
50
print_suggestions(search);
52
g_clear_object(&client_query);
57
/* Notification from Dee that we can stop waiting */
59
wait_for_sync_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
61
GMainLoop * loop = (GMainLoop *)user_data;
62
g_main_loop_quit(loop);
66
/* Waits for the DeeModel of results to get synchoronized from the
69
wait_for_sync (DeeModel * model)
71
if (dee_shared_model_is_synchronized(DEE_SHARED_MODEL(model))) {
75
GMainLoop * loop = g_main_loop_new(NULL, FALSE);
77
glong sig = g_signal_connect(G_OBJECT(model), "notify::synchronized", G_CALLBACK(wait_for_sync_notify), loop);
79
g_main_loop_run(loop);
80
g_main_loop_unref(loop);
82
g_signal_handler_disconnect(G_OBJECT(model), sig);
84
return dee_shared_model_is_synchronized(DEE_SHARED_MODEL(model));
87
/* Prints out the label for each item in the model. Makes it easier
88
to see what we've got */
90
print_model (GMenuModel * model)
93
for (i = 0; i < g_menu_model_get_n_items(model); i++) {
94
GVariant * vlabel = g_menu_model_get_item_attribute_value(model, i, G_MENU_ATTRIBUTE_LABEL, G_VARIANT_TYPE_STRING);
97
g_print("\t\t(null)\n");
101
g_print("\t\t%s\n", g_variant_get_string(vlabel, NULL));
102
g_variant_unref(vlabel);
107
/* Quit the mainloop if we get anything */
109
population_update (GMenuModel * model, gint position, gint removed, gint added, GMainLoop * loop)
111
g_main_loop_quit(loop);
115
/* Waiting here for the model to get populated with something
116
before printing. In a real world scenario you'd probably
117
be responsive to the items being added. We're a CLI tool. */
119
populated_model (GMenuModel * model)
121
if (g_menu_model_get_n_items(model) == 0) {
122
GMainLoop * loop = g_main_loop_new(NULL, FALSE);
124
gulong sig = g_signal_connect(model, "items-changed", G_CALLBACK(population_update), loop);
126
g_main_loop_run(loop);
127
g_main_loop_unref(loop);
129
g_signal_handler_disconnect(model, sig);
132
return print_model(model);
135
/* Signal from the Client Param that it now has a model ready
136
for us to look at. This doesn't mean that the model has
137
any data in it, but that the object is available. */
139
model_ready (HudClientParam * param, GMainLoop * loop)
141
GMenuModel * model = hud_client_param_get_model(param);
143
populated_model(model);
145
g_warning("Unable to get model even after it was 'ready'");
148
g_main_loop_quit(loop);
152
/* Prints out the entries from the search, but only the parameterized
153
ones. Then it looks at each parameterized one prints out the items
154
that are available as parameters */
156
print_suggestions (const char *query)
158
if (client_query == NULL) {
159
client_query = hud_client_query_new(query);
161
GMainLoop * loop = g_main_loop_new(NULL, FALSE);
163
guint sig = g_signal_connect(G_OBJECT(client_query), HUD_CLIENT_QUERY_SIGNAL_MODELS_CHANGED, G_CALLBACK(models_ready), loop);
165
g_main_loop_run(loop);
166
g_main_loop_unref(loop);
168
g_signal_handler_disconnect(G_OBJECT(client_query), sig);
170
hud_client_query_set_query(client_query, query);
171
models_ready(client_query, NULL);
178
models_ready (HudClientQuery * client_query, gpointer user_data)
180
DeeModel * model = hud_client_query_get_results_model(client_query);
181
g_return_if_fail(wait_for_sync(model));
183
DeeModelIter * iter = NULL;
185
for (iter = dee_model_get_first_iter(model); !dee_model_is_last(model, iter); iter = dee_model_next(model, iter), i++) {
186
if (!hud_client_query_results_is_parameterized(client_query, iter)) {
187
/* Only want parameterized */
191
const gchar * suggestion = hud_client_query_results_get_command_name(client_query, iter);
192
gchar * clean_line = NULL;
193
pango_parse_markup(suggestion, -1, 0, NULL, &clean_line, NULL, NULL);
194
printf("\t%s\n", clean_line);
197
HudClientParam * param = hud_client_query_execute_param_command(client_query, hud_client_query_results_get_command_id(client_query, iter), 0);
198
g_return_if_fail(param != NULL);
200
GMenuModel * model = hud_client_param_get_model(param);
202
GMainLoop * loop = g_main_loop_new(NULL, FALSE);
204
gulong sig = g_signal_connect(param, "model-ready", G_CALLBACK(model_ready), loop);
206
g_main_loop_run(loop);
207
g_main_loop_unref(loop);
209
g_signal_handler_disconnect(param, sig);
211
populated_model(model);
214
hud_client_param_send_cancel(param);
215
g_object_unref(param);
218
if (user_data != NULL) {
219
GMainLoop * loop = (GMainLoop *)user_data;
220
g_main_loop_quit(loop);