Oops, fix a broken part of the patch
[beagle.git] / libbeagle / beagle / beagle-client.c
blob88c8e806b00249d1401548046029c6dbaaef9ed1
1 /*
2 * beagle-client.c
4 * Copyright (C) 2005 Novell, Inc.
6 */
8 /*
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
34 #include "beagle-private.h"
35 #include "beagle-client.h"
36 #include "beagle-util.h"
38 typedef struct {
39 gchar *socket_path;
40 } BeagleClientPrivate;
42 #define BEAGLE_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BEAGLE_TYPE_CLIENT, BeagleClientPrivate))
44 static GObjectClass *parent_class = NULL;
46 G_DEFINE_TYPE (BeagleClient, beagle_client, G_TYPE_OBJECT)
48 static void
49 beagle_client_finalize (GObject *obj)
51 BeagleClientPrivate *priv = BEAGLE_CLIENT_GET_PRIVATE (obj);
53 g_free (priv->socket_path);
55 if (G_OBJECT_CLASS (parent_class)->finalize)
56 G_OBJECT_CLASS (parent_class)->finalize (obj);
59 static void
60 beagle_client_class_init (BeagleClientClass *klass)
62 GObjectClass *obj_class = G_OBJECT_CLASS (klass);
64 parent_class = g_type_class_peek_parent (klass);
66 obj_class->finalize = beagle_client_finalize;
68 g_type_class_add_private (klass, sizeof (BeagleClientPrivate));
71 static void
72 beagle_client_init (BeagleClient *client)
76 /**
77 * beagle_client_new:
78 * @client_name: a string
80 * Creates a new #BeagleClient. If @client_name is %NULL it will default to "socket".
82 * Return value: a newly created #BeagleClient, or NULL if the client cannot be created.
83 **/
84 BeagleClient *
85 beagle_client_new (const char *client_name)
87 BeagleClient *client;
88 BeagleClientPrivate *priv;
89 const gchar *beagle_home;
90 gchar *socket_dir;
91 gchar *socket_path;
92 struct stat buf;
94 if (!client_name)
95 client_name = "socket";
97 beagle_home = g_getenv ("BEAGLE_HOME");
98 if (beagle_home == NULL)
99 beagle_home = g_get_home_dir ();
101 if (! beagle_util_is_path_on_block_device (beagle_home) ||
102 getenv ("BEAGLE_SYNCHRONIZE_LOCALLY") != NULL) {
103 gchar *remote_storage_dir = g_build_filename (beagle_home, ".beagle", "remote_storage_dir", NULL);
104 gchar *tmp;
106 if (! g_file_test (remote_storage_dir, G_FILE_TEST_EXISTS)) {
107 g_free (remote_storage_dir);
108 return NULL;
111 if (! g_file_get_contents (remote_storage_dir, &socket_dir, NULL, NULL)) {
112 g_free (remote_storage_dir);
113 return NULL;
116 g_free (remote_storage_dir);
118 /* There's a newline at the end that we want to strip off */
119 tmp = strrchr (socket_dir, '\n');
120 if (tmp != NULL)
121 *tmp = '\0';
123 if (! g_file_test (socket_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
124 g_free (socket_dir);
125 return NULL;
127 } else {
128 socket_dir = g_build_filename (beagle_home, ".beagle", NULL);
131 socket_path = g_build_filename (socket_dir, client_name, NULL);
132 g_free (socket_dir);
133 if (stat (socket_path, &buf) == -1 || !S_ISSOCK (buf.st_mode)) {
134 g_free (socket_path);
135 return NULL;
138 client = g_object_new (BEAGLE_TYPE_CLIENT, 0);
139 priv = BEAGLE_CLIENT_GET_PRIVATE (client);
140 priv->socket_path = socket_path;
142 return client;
146 * beagle_client_new_from_socket_path:
147 * @socket_path: a string of the path to the daemon socket
149 * Creates a new #BeagleClient, connecting to the path with @socket_path. NULL
150 * is not allowed.
152 * Return value: a newly created #BeagleClient, or NULL if the client cannot be created.
154 BeagleClient *
155 beagle_client_new_from_socket_path (const char *socket_path)
157 BeagleClient *client;
158 BeagleClientPrivate *priv;
159 struct stat buf;
161 if (stat (socket_path, &buf) == -1 || !S_ISSOCK (buf.st_mode))
162 return NULL;
164 client = g_object_new (BEAGLE_TYPE_CLIENT, 0);
165 priv = BEAGLE_CLIENT_GET_PRIVATE (client);
166 priv->socket_path = g_strdup (socket_path);
168 return client;
172 * beagle_client_send_request:
173 * @client: a #BeagleClient
174 * @request: a #BeagleRequest
175 * @err: a location to return an error #GError of type #GIOChannelError.
177 * Synchronously send a #BeagleRequest using the given #BeagleClient.
179 * Return value: a #BeagleResponse.
181 BeagleResponse *
182 beagle_client_send_request (BeagleClient *client,
183 BeagleRequest *request,
184 GError **err)
186 BeagleClientPrivate *priv;
188 g_return_val_if_fail (BEAGLE_IS_CLIENT (client), NULL);
189 g_return_val_if_fail (BEAGLE_IS_REQUEST (request), NULL);
191 priv = BEAGLE_CLIENT_GET_PRIVATE (client);
193 return _beagle_request_send (request, priv->socket_path, err);
197 * beagle_client_send_request_async:
198 * @client: a #BeagleClient
199 * @request: a #BeagleRequest
200 * @err: a location to store a #GError of type #GIOChannelError
202 * Asynchronously send a #BeagleRequest using the given #BeagleClient.
204 * Return value: %TRUE on success and otherwise %FALSE.
206 gboolean
207 beagle_client_send_request_async (BeagleClient *client,
208 BeagleRequest *request,
209 GError **err)
211 BeagleClientPrivate *priv;
213 g_return_val_if_fail (BEAGLE_IS_CLIENT (client), FALSE);
214 g_return_val_if_fail (BEAGLE_IS_REQUEST (request), FALSE);
216 priv = BEAGLE_CLIENT_GET_PRIVATE (client);
218 return _beagle_request_send_async (request, priv->socket_path, err);