~jbicha/hud/build-depend-on-valac-not-gir

« back to all changes in this revision

Viewing changes to tools/hud-cli-param.c

  • Committer: Tarmac
  • Author(s): Ted Gould, Pete Woods, Antti Kaijanmäki, Ted Gould, Albert Astals, Ryan Lortie, Łukasz 'sil2100' Zemczak, Albert Astals Cid, Mathieu Trudel-Lapierre, Kaleo, Tarmac, Ricardo Salveti de Araujo, Michael Terry, Automatic PS uploader
  • Date: 2013-04-10 16:04:51 UTC
  • mfrom: (227.3.148 phablet)
  • Revision ID: tarmac-20130410160451-o3owpv3zaxulm5of
HUD 2.0 Merge.

Approved by PS Jenkins bot, Mathieu Trudel-Lapierre.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Small utility to excersise the HUD from the command line
 
3
 
 
4
Copyright 2011 Canonical Ltd.
 
5
 
 
6
Authors:
 
7
    Ted Gould <ted@canonical.com>
 
8
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
*/
 
21
 
 
22
#include <glib.h>
 
23
#include <gio/gunixinputstream.h>
 
24
#include <gtk/gtk.h>
 
25
#include <unistd.h>
 
26
#include <stdio.h>
 
27
#include <stdlib.h>
 
28
 
 
29
#include <hud-client.h>
 
30
 
 
31
 
 
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;
 
35
 
 
36
int
 
37
main (int argc, char *argv[])
 
38
{
 
39
#ifndef GLIB_VERSION_2_36
 
40
        g_type_init ();
 
41
#endif
 
42
 
 
43
        const gchar * search = "";
 
44
 
 
45
        if (argc == 2) {
 
46
                search = argv[1];
 
47
        }
 
48
 
 
49
        printf("\nsearch token: %s\n", search);
 
50
        print_suggestions(search);
 
51
 
 
52
        g_clear_object(&client_query);
 
53
 
 
54
        return 0;
 
55
}
 
56
 
 
57
/* Notification from Dee that we can stop waiting */
 
58
static void
 
59
wait_for_sync_notify (GObject * object, GParamSpec * pspec, gpointer user_data)
 
60
{
 
61
        GMainLoop * loop = (GMainLoop *)user_data;
 
62
        g_main_loop_quit(loop);
 
63
        return;
 
64
}
 
65
 
 
66
/* Waits for the DeeModel of results to get synchoronized from the
 
67
   HUD service */
 
68
static gboolean
 
69
wait_for_sync (DeeModel * model)
 
70
{
 
71
        if (dee_shared_model_is_synchronized(DEE_SHARED_MODEL(model))) {
 
72
                return TRUE;
 
73
        }
 
74
 
 
75
        GMainLoop * loop = g_main_loop_new(NULL, FALSE);
 
76
 
 
77
        glong sig = g_signal_connect(G_OBJECT(model), "notify::synchronized", G_CALLBACK(wait_for_sync_notify), loop);
 
78
 
 
79
        g_main_loop_run(loop);
 
80
        g_main_loop_unref(loop);
 
81
 
 
82
        g_signal_handler_disconnect(G_OBJECT(model), sig);
 
83
 
 
84
        return dee_shared_model_is_synchronized(DEE_SHARED_MODEL(model));
 
85
}
 
86
 
 
87
/* Prints out the label for each item in the model.  Makes it easier
 
88
   to see what we've got */
 
89
static void
 
90
print_model (GMenuModel * model)
 
91
{
 
92
        int i;
 
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);
 
95
 
 
96
                if (vlabel == NULL) {
 
97
                        g_print("\t\t(null)\n");
 
98
                        continue;
 
99
                }
 
100
 
 
101
                g_print("\t\t%s\n", g_variant_get_string(vlabel, NULL));
 
102
                g_variant_unref(vlabel);
 
103
        }
 
104
        return;
 
105
}
 
106
 
 
107
/* Quit the mainloop if we get anything */
 
108
static void
 
109
population_update (GMenuModel * model, gint position, gint removed, gint added, GMainLoop * loop)
 
110
{
 
111
        g_main_loop_quit(loop);
 
112
        return;
 
113
}
 
114
 
 
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. */
 
118
static void
 
119
populated_model (GMenuModel * model)
 
120
{
 
121
        if (g_menu_model_get_n_items(model) == 0) {
 
122
                GMainLoop * loop = g_main_loop_new(NULL, FALSE);
 
123
 
 
124
                gulong sig = g_signal_connect(model, "items-changed", G_CALLBACK(population_update), loop);
 
125
 
 
126
                g_main_loop_run(loop);
 
127
                g_main_loop_unref(loop);
 
128
 
 
129
                g_signal_handler_disconnect(model, sig);
 
130
        }
 
131
 
 
132
        return print_model(model);
 
133
}
 
134
 
 
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. */
 
138
static void
 
139
model_ready (HudClientParam * param, GMainLoop * loop)
 
140
{
 
141
        GMenuModel * model = hud_client_param_get_model(param);
 
142
        if (model != NULL) {
 
143
                populated_model(model);
 
144
        } else {
 
145
                g_warning("Unable to get model even after it was 'ready'");
 
146
        }
 
147
 
 
148
        g_main_loop_quit(loop);
 
149
        return;
 
150
}
 
151
 
 
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 */
 
155
static void 
 
156
print_suggestions (const char *query)
 
157
{
 
158
        if (client_query == NULL) {
 
159
                client_query = hud_client_query_new(query);
 
160
                
 
161
                GMainLoop * loop = g_main_loop_new(NULL, FALSE);
 
162
 
 
163
                guint sig = g_signal_connect(G_OBJECT(client_query), HUD_CLIENT_QUERY_SIGNAL_MODELS_CHANGED, G_CALLBACK(models_ready), loop);
 
164
 
 
165
                g_main_loop_run(loop);
 
166
                g_main_loop_unref(loop);
 
167
 
 
168
                g_signal_handler_disconnect(G_OBJECT(client_query), sig);
 
169
        } else {
 
170
                hud_client_query_set_query(client_query, query);
 
171
                models_ready(client_query, NULL);
 
172
        }
 
173
 
 
174
        return;
 
175
}
 
176
 
 
177
static void
 
178
models_ready (HudClientQuery * client_query, gpointer user_data)
 
179
{
 
180
        DeeModel * model = hud_client_query_get_results_model(client_query);
 
181
        g_return_if_fail(wait_for_sync(model));
 
182
 
 
183
        DeeModelIter * iter = NULL;
 
184
        int i = 0;
 
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 */
 
188
                        continue;
 
189
                }
 
190
 
 
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);
 
195
                free(clean_line);
 
196
 
 
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);
 
199
 
 
200
                GMenuModel * model = hud_client_param_get_model(param);
 
201
                if (model == NULL) {
 
202
                        GMainLoop * loop = g_main_loop_new(NULL, FALSE);
 
203
 
 
204
                        gulong sig = g_signal_connect(param, "model-ready", G_CALLBACK(model_ready), loop);
 
205
 
 
206
                        g_main_loop_run(loop);
 
207
                        g_main_loop_unref(loop);
 
208
 
 
209
                        g_signal_handler_disconnect(param, sig);
 
210
                } else {
 
211
                        populated_model(model);
 
212
                }
 
213
 
 
214
                hud_client_param_send_cancel(param);
 
215
                g_object_unref(param);
 
216
        }
 
217
 
 
218
        if (user_data != NULL) {
 
219
                GMainLoop * loop = (GMainLoop *)user_data;
 
220
                g_main_loop_quit(loop);
 
221
        }
 
222
 
 
223
        return;
 
224
}
 
225