1 /*
2  * telepathy-example-inspect-cm - inspect a connection manager
3  *
4  * Usage:
5  *
6  * telepathy-example-inspect-cm gabble
7  *    Inspect the Gabble connection manager, by reading the installed
8  *    .manager file if available, and introspecting the running CM if not
9  *
10  * telepathy-example-inspect-cm gabble data/gabble.manager
11  *    As above, but assume the given filename is correct
12  *
13  * telepathy-example-inspect-cm gabble ""
14  *    Don't read any .manager file, just introspect the running CM
15  *
16  * Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
17  * Copyright (C) 2007 Nokia Corporation
18  * Copyright (C) 2013 Intel Corporation
19  *
20  * Copying and distribution of this file, with or without modification,
21  * are permitted in any medium without royalty provided the copyright
22  * notice and this notice are preserved.
23  */
24 
25 #include "config.h"
26 
27 #include <stdio.h>
28 
29 #include <telepathy-glib/telepathy-glib.h>
30 
31 static const gchar *
nonnull(const gchar * s)32 nonnull (const gchar *s)
33 {
34   if (s == NULL)
35     return "(null)";
36 
37   return s;
38 }
39 
40 static void
show_cm(TpConnectionManager * cm)41 show_cm (TpConnectionManager *cm)
42 {
43   GList *protocols;
44 
45   g_message ("Connection manager name: %s",
46       tp_connection_manager_get_name (cm));
47   g_message ("Is running: %s",
48       tp_connection_manager_is_running (cm) ? "yes" : "no");
49   g_message ("Source of information: %s",
50       tp_connection_manager_get_info_source (cm) == TP_CM_INFO_SOURCE_LIVE
51         ? "D-Bus" : ".manager file");
52 
53   protocols = tp_connection_manager_dup_protocols (cm);
54   while (protocols)
55     {
56       TpProtocol *protocol = protocols->data;
57       GList *params;
58       const gchar *const *strv;
59       const gchar *const *strv_iter;
60       TpAvatarRequirements *avatar_reqs;
61 
62       g_message ("Protocol: %s", tp_protocol_get_name (protocol));
63 
64       g_message ("\tEnglish name: %s", tp_protocol_get_english_name (protocol));
65       g_message ("\tIcon name: %s", tp_protocol_get_icon_name (protocol));
66       g_message ("\tvCard field: %s",
67           nonnull (tp_protocol_get_vcard_field (protocol)));
68       g_message ("\tCan register accounts via Telepathy: %s",
69           tp_protocol_can_register (protocol) ? "yes" : "no");
70 
71       strv = tp_protocol_get_authentication_types (protocol);
72 
73       for (strv_iter = strv;
74           strv_iter != NULL && *strv_iter != NULL;
75           strv_iter++)
76         g_message ("\tAuthentication type: %s", *strv_iter);
77 
78       avatar_reqs = tp_protocol_get_avatar_requirements (protocol);
79 
80       if (avatar_reqs == NULL)
81         {
82           g_message ("\tNo known avatar requirements, or no avatar support");
83         }
84       else
85         {
86           gboolean first = TRUE;
87 
88           g_message ("\tAvatar requirements:");
89 
90           for (strv_iter =
91                   (const gchar * const *) avatar_reqs->supported_mime_types;
92               strv_iter != NULL && *strv_iter != NULL;
93               strv_iter++)
94             {
95               g_message ("\t\t%s MIME type: %s",
96                   (first ? "Recommended" : "Supported"),
97                   *strv_iter);
98               first = FALSE;
99             }
100 
101           g_message ("\t\tMinimum: %ux%u px",
102               avatar_reqs->minimum_width,
103               avatar_reqs->minimum_height);
104           g_message ("\t\tRecommended: %ux%u px",
105               avatar_reqs->recommended_width,
106               avatar_reqs->recommended_height);
107           g_message ("\t\tMaximum: %ux%u px, %u bytes",
108               avatar_reqs->maximum_width,
109               avatar_reqs->maximum_height,
110               avatar_reqs->maximum_bytes);
111         }
112 
113       params = tp_protocol_dup_params (protocol);
114       while (params)
115         {
116           TpConnectionManagerParam *param = params->data;
117           GValue value = { 0 };
118 
119           g_message ("\tParameter: %s",
120               tp_connection_manager_param_get_name (param));
121           g_message ("\t\tD-Bus signature: %s",
122               tp_connection_manager_param_get_dbus_signature (param));
123           g_message ("\t\tIs required: %s",
124               tp_connection_manager_param_is_required (param) ?
125                 "yes" : "no");
126 
127           if (tp_protocol_can_register (protocol))
128             {
129               g_message ("\t\tIs required for registration: %s",
130                 tp_connection_manager_param_is_required_for_registration (
131                     param) ?  "yes" : "no");
132             }
133 
134           g_message ("\t\tIs secret (password etc.): %s",
135               tp_connection_manager_param_is_secret (param) ?
136                 "yes" : "no");
137           g_message ("\t\tIs a D-Bus property: %s",
138               tp_connection_manager_param_is_dbus_property (param) ?
139                 "yes" : "no");
140 
141           if (tp_connection_manager_param_get_default (param, &value))
142             {
143               gchar *s = g_strdup_value_contents (&value);
144 
145               g_message ("\t\tDefault value: %s", s);
146               g_free (s);
147               g_value_unset (&value);
148             }
149           else
150             {
151               g_message ("\t\tNo default value");
152             }
153 
154           tp_connection_manager_param_free (param);
155           params = g_list_delete_link (params, params);
156         }
157 
158       g_object_unref (protocol);
159       protocols = g_list_delete_link (protocols, protocols);
160     }
161 }
162 
163 static void
list_cb(GObject * source G_GNUC_UNUSED,GAsyncResult * result,gpointer user_data)164 list_cb (GObject *source G_GNUC_UNUSED,
165     GAsyncResult *result,
166     gpointer user_data)
167 {
168   GMainLoop *mainloop = user_data;
169   GError *error = NULL;
170   GList *cms = tp_list_connection_managers_finish (result, &error);
171 
172   if (error != NULL)
173     {
174       g_warning ("Error getting list of CMs: %s", error->message);
175       g_error_free (error);
176     }
177   else if (cms == NULL)
178     {
179       g_message ("No Telepathy connection managers found");
180     }
181   else
182     {
183       while (cms != NULL)
184         {
185           show_cm (cms->data);
186           g_object_unref (cms->data);
187           cms = g_list_delete_link (cms, cms);
188 
189           if (cms != NULL)
190             g_message ("----------------------------------------");
191         }
192     }
193 
194   g_main_loop_quit (mainloop);
195 }
196 
197 static void
ready(GObject * source,GAsyncResult * result,gpointer user_data)198 ready (GObject *source,
199     GAsyncResult *result,
200     gpointer user_data)
201 {
202   TpConnectionManager *cm = TP_CONNECTION_MANAGER (source);
203   GMainLoop *mainloop = user_data;
204   GError *error = NULL;
205 
206   if (!tp_proxy_prepare_finish (cm, result, &error))
207     {
208       g_assert (!tp_proxy_is_prepared (cm,
209             TP_CONNECTION_MANAGER_FEATURE_CORE));
210 
211       g_warning ("Error getting CM info: %s", error->message);
212       g_error_free (error);
213     }
214   else
215     {
216       g_assert (tp_proxy_is_prepared (cm,
217             TP_CONNECTION_MANAGER_FEATURE_CORE));
218 
219       show_cm (cm);
220     }
221 
222   g_main_loop_quit (mainloop);
223 }
224 
225 int
main(int argc,char ** argv)226 main (int argc,
227       char **argv)
228 {
229   const gchar *cm_name, *manager_file;
230   TpConnectionManager *cm = NULL;
231   GMainLoop *mainloop = NULL;
232   GError *error = NULL;
233   TpDBusDaemon *dbus = NULL;
234   int ret = 1;
235 
236   tp_debug_set_flags (g_getenv ("EXAMPLE_DEBUG"));
237 
238   if (g_getenv ("EXAMPLE_TIMING") != NULL)
239     g_log_set_default_handler (tp_debug_timestamped_log_handler, NULL);
240 
241   dbus = tp_dbus_daemon_dup (&error);
242 
243   if (dbus == NULL)
244     {
245       g_warning ("%s", error->message);
246       g_error_free (error);
247       goto out;
248     }
249 
250   mainloop = g_main_loop_new (NULL, FALSE);
251 
252   if (argc >= 2)
253     {
254       cm_name = argv[1];
255       manager_file = argv[2];   /* possibly NULL */
256 
257       cm = tp_connection_manager_new (dbus, cm_name, manager_file, &error);
258 
259       if (cm == NULL)
260         {
261           g_warning ("%s", error->message);
262           g_error_free (error);
263           goto out;
264         }
265 
266       tp_proxy_prepare_async (cm, NULL, ready, mainloop);
267     }
268   else
269     {
270       tp_list_connection_managers_async (dbus, list_cb, mainloop);
271     }
272 
273   g_main_loop_run (mainloop);
274   ret = 0;
275 
276 out:
277   if (cm != NULL)
278     g_object_unref (cm);
279 
280   if (mainloop != NULL)
281     g_main_loop_unref (mainloop);
282 
283   if (dbus != NULL)
284     g_object_unref (dbus);
285 
286   return ret;
287 }
288