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
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
26 #include "purple-gio.h"
27 #include "tls-certificate.h"
32 GOutputStream
*output
;
36 graceful_close_cb(gpointer user_data
)
38 GracefulCloseData
*data
= user_data
;
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.
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",
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",
68 g_clear_error(&error
);
71 g_clear_object(&data
->output
);
74 if (!g_io_stream_close(data
->stream
, NULL
, &error
)) {
75 purple_debug_warning("gio",
76 "Error closing stream: %s",
78 g_clear_error(&error
);
81 g_clear_object(&data
->stream
);
85 return G_SOURCE_REMOVE
;
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
);
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
) {
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
);