Fix some functions descriptions
[pidgin-git.git] / libpurple / purple-gio.c
blob02e4e9a3a7c425b1593893cba3f91b7caefbc3a7
1 /*
3 * purple
5 * Purple is the legal property of its developers, whose names are too numerous
6 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * source distribution.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include "internal.h"
25 #include "proxy.h"
26 #include "purple-gio.h"
27 #include "tls-certificate.h"
29 typedef struct {
30 GIOStream *stream;
31 GInputStream *input;
32 GOutputStream *output;
33 } GracefulCloseData;
35 static gboolean
36 graceful_close_cb(gpointer user_data)
38 GracefulCloseData *data = user_data;
39 GError *error = NULL;
41 if (g_input_stream_has_pending(data->input) ||
42 g_output_stream_has_pending(data->output)) {
43 /* Has pending operations. Not ready to close yet.
44 * Try again later.
46 return G_SOURCE_CONTINUE;
49 /* Finally can gracefully close */
51 /* Close wrapper input stream, if any */
52 if (data->input != NULL &&
53 !g_input_stream_close(data->input, NULL, &error)) {
54 purple_debug_warning("gio",
55 "Error closing input stream: %s",
56 error->message);
57 g_clear_error(&error);
60 g_clear_object(&data->input);
62 /* Close wrapper output stream, if any */
63 if (data->output != NULL &&
64 !g_output_stream_close(data->output, NULL, &error)) {
65 purple_debug_warning("gio",
66 "Error closing output stream: %s",
67 error->message);
68 g_clear_error(&error);
71 g_clear_object(&data->output);
73 /* Close io stream */
74 if (!g_io_stream_close(data->stream, NULL, &error)) {
75 purple_debug_warning("gio",
76 "Error closing stream: %s",
77 error->message);
78 g_clear_error(&error);
81 g_clear_object(&data->stream);
83 /* Clean up */
84 g_free(data);
85 return G_SOURCE_REMOVE;
88 void
89 purple_gio_graceful_close(GIOStream *stream,
90 GInputStream *input, GOutputStream *output)
92 GracefulCloseData *data;
94 g_return_if_fail(G_IS_IO_STREAM(stream));
95 g_return_if_fail(input == NULL || G_IS_INPUT_STREAM(input));
96 g_return_if_fail(output == NULL || G_IS_OUTPUT_STREAM(output));
98 data = g_new(GracefulCloseData, 1);
99 data->stream = g_object_ref(stream);
100 data->input = g_object_ref(input);
101 data->output = g_object_ref(output);
103 /* Try gracefully closing the stream synchronously */
104 if (graceful_close_cb(data) == G_SOURCE_CONTINUE) {
105 /* Has pending operations. Do so asynchronously */
106 g_idle_add(graceful_close_cb, data);
110 GSocketClient *
111 purple_gio_socket_client_new(PurpleAccount *account, GError **error)
113 GProxyResolver *resolver;
114 GSocketClient *client;
116 resolver = purple_proxy_get_proxy_resolver(account, error);
118 if (resolver == NULL) {
119 return NULL;
122 client = g_socket_client_new();
123 g_socket_client_set_proxy_resolver(client, resolver);
124 g_object_unref(resolver);
126 /* Attach purple's tls certificate handler in case tls is used */
127 purple_tls_certificate_attach_to_socket_client(client);
129 return client;