Add class ProxyInfo
[python-purple] / c_purple.c
1 /*
2  * Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3  *
4  * This file is part of python-purple.
5  *
6  * python-purple is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * python-purple is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include <purple.h>
22
23 #include <glib.h>
24
25 #include <signal.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "c_purple.h"
30
31 #define PURPLE_GLIB_READ_COND  (G_IO_IN | G_IO_HUP | G_IO_ERR)
32 #define PURPLE_GLIB_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
33
34 typedef struct _PurpleGLibIOClosure {
35         PurpleInputFunction function;
36         guint result;
37         gpointer data;
38 } PurpleGLibIOClosure;
39
40 static gboolean purple_glib_io_invoke(GIOChannel *source, GIOCondition condition,
41                 gpointer data)
42 {
43         PurpleGLibIOClosure *closure = data;
44         PurpleInputCondition purple_cond = 0;
45
46         if (condition & PURPLE_GLIB_READ_COND)
47                 purple_cond |= PURPLE_INPUT_READ;
48         if (condition & PURPLE_GLIB_WRITE_COND)
49                 purple_cond |= PURPLE_INPUT_WRITE;
50
51         closure->function(closure->data, g_io_channel_unix_get_fd(source),
52                         purple_cond);
53
54         return TRUE;
55 }
56
57 guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
58                 gpointer data)
59 {
60         PurpleGLibIOClosure *closure = g_new0(PurpleGLibIOClosure, 1);
61         GIOChannel *channel;
62         GIOCondition cond = 0;
63
64         closure->function = function;
65         closure->data = data;
66
67         if (condition & PURPLE_INPUT_READ)
68                 cond |= PURPLE_GLIB_READ_COND;
69         if (condition & PURPLE_INPUT_WRITE)
70                 cond |= PURPLE_GLIB_WRITE_COND;
71
72         channel = g_io_channel_unix_new(fd);
73         closure->result = g_io_add_watch_full(channel, 0, cond,
74                         purple_glib_io_invoke, closure, g_free);
75
76         g_io_channel_unref(channel);
77         return closure->result;
78 }