Update to version 2.33.1
[profile/ivi/glib2.git] / docs / reference / gio / html / ch31s03.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <title>Owning bus names</title>
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="index.html" title="GIO Reference Manual">
8 <link rel="up" href="ch31.html" title="Migrating to GDBus">
9 <link rel="prev" href="ch31s02.html" title="API comparison">
10 <link rel="next" href="ch31s04.html" title="Creating proxies for well-known names">
11 <meta name="generator" content="GTK-Doc V1.18 (XML mode)">
12 <link rel="stylesheet" href="style.css" type="text/css">
13 </head>
14 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
15 <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
16 <td><a accesskey="p" href="ch31s02.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
17 <td><a accesskey="u" href="ch31.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
18 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
19 <th width="100%" align="center">GIO Reference Manual</th>
20 <td><a accesskey="n" href="ch31s04.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
21 </tr></table>
22 <div class="section">
23 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
24 <a name="idp76275952"></a>Owning bus names</h2></div></div></div>
25 <p>
26       Using dbus-glib, you typically call RequestName manually
27       to own a name, like in the following excerpt:
28       </p>
29 <div class="informalexample"><pre class="programlisting">
30   error = NULL;
31   res = dbus_g_proxy_call (system_bus_proxy,
32                            "RequestName",
33                            &amp;error,
34                            G_TYPE_STRING, NAME_TO_CLAIM,
35                            G_TYPE_UINT,   DBUS_NAME_FLAG_ALLOW_REPLACEMENT,
36                            G_TYPE_INVALID,
37                            G_TYPE_UINT,   &amp;result,
38                            G_TYPE_INVALID);
39   if (!res)
40     {
41       if (error != NULL)
42         {
43           g_warning ("Failed to acquire %s: %s",
44                      NAME_TO_CLAIM, error-&gt;message);
45           g_error_free (error);
46         }
47       else
48         {
49           g_warning ("Failed to acquire %s", NAME_TO_CLAIM);
50         }
51       goto out;
52     }
53
54   if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
55     {
56       if (error != NULL)
57         {
58           g_warning ("Failed to acquire %s: %s",
59                      NAME_TO_CLAIM, error-&gt;message);
60           g_error_free (error);
61         }
62       else
63         {
64           g_warning ("Failed to acquire %s", NAME_TO_CLAIM);
65         }
66       exit (1);
67     }
68
69   dbus_g_proxy_add_signal (system_bus_proxy, "NameLost",
70                            G_TYPE_STRING, G_TYPE_INVALID);
71   dbus_g_proxy_connect_signal (system_bus_proxy, "NameLost",
72                                G_CALLBACK (on_name_lost), NULL, NULL);
73
74   /* further setup ... */
75
76       </pre></div>
77 <p>
78     </p>
79 <p>
80     While you can do things this way with GDBus too, using
81     <a class="link" href="GDBusProxy.html#g-dbus-proxy-call-sync" title="g_dbus_proxy_call_sync ()"><code class="function">g_dbus_proxy_call_sync()</code></a>, it is much nicer to use the high-level API
82     for this:
83     </p>
84 <div class="informalexample"><pre class="programlisting">
85 static void
86 on_name_acquired (GDBusConnection *connection,
87                   const gchar     *name,
88                   gpointer         user_data)
89 {
90   /* further setup ... */
91 }
92
93 /* ... */
94
95   owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
96                              NAME_TO_CLAIM,
97                              G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
98                              on_bus_acquired,
99                              on_name_acquired,
100                              on_name_lost,
101                              NULL,
102                              NULL);
103
104   g_main_loop_run (loop);
105
106   g_bus_unown_name (owner_id);
107
108     </pre></div>
109 <p>
110     Note that <a class="link" href="gio-Owning-Bus-Names.html#g-bus-own-name" title="g_bus_own_name ()"><code class="function">g_bus_own_name()</code></a> works asynchronously and requires
111     you to enter your mainloop to await the <code class="function">on_name_aquired()</code>
112     callback. Also note that in order to avoid race conditions (e.g.
113     when your service is activated by a method call), you have to export
114     your manager object <span class="emphasis"><em>before</em></span> acquiring the
115     name. The <code class="function">on_bus_acquired()</code> callback is the right place to do
116     such preparations.
117     </p>
118 </div>
119 <div class="footer">
120 <hr>
121           Generated by GTK-Doc V1.18</div>
122 </body>
123 </html>