aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-component-listener.c
blob: 1e7027f33d2c6a82900b87789ea9a472c0c3b98b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Component listener.
 *
 * Author:
 *   Rodrigo Moya <rodrigo@ximian.com>
 *
 * Copyright 2002, Ximian, Inc.
 */

#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-object.h>
#include "e-component-listener.h"
#include <libgnome/gnome-i18n.h>

#define PARENT_TYPE GTK_TYPE_OBJECT

struct _EComponentListenerPrivate {
    Bonobo_Unknown component;
};

static void e_component_listener_class_init (EComponentListenerClass *klass);
static void e_component_listener_init       (EComponentListener *cl, EComponentListenerClass *klass);
static void e_component_listener_finalize   (GObject *object);

static GObjectClass *parent_class = NULL;
static GList *watched_connections = NULL;

enum {
    COMPONENT_DIED,
    LAST_SIGNAL
};

static guint comp_listener_signals[LAST_SIGNAL];

static void
connection_listen_cb (gpointer object, gpointer user_data)
{
    GList *l, *next = NULL;
    EComponentListener *cl;

    for (l = watched_connections; l != NULL; l = next) {
        next = l->next;
        cl = l->data;

        switch (ORBit_small_get_connection_status (cl->priv->component)) {
        case ORBIT_CONNECTION_DISCONNECTED :
            watched_connections = g_list_delete_link (watched_connections, l);

            g_object_ref (cl);
            g_signal_emit (cl, comp_listener_signals[COMPONENT_DIED], 0);
            cl->priv->component = CORBA_OBJECT_NIL;
            g_object_unref (cl);
            break;
        default :
            break;
        }
    }
}

static void
e_component_listener_class_init (EComponentListenerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->finalize = e_component_listener_finalize;
    klass->component_died = NULL;

    comp_listener_signals[COMPONENT_DIED] =
        g_signal_new ("component_died",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EComponentListenerClass, component_died),
                  NULL, NULL,
                  g_cclosure_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
}

static void
e_component_listener_init (EComponentListener *cl, EComponentListenerClass *klass)
{
    /* allocate internal structure */
    cl->priv = g_new (EComponentListenerPrivate, 1);
    cl->priv->component = CORBA_OBJECT_NIL;
}

static void
e_component_listener_finalize (GObject *object)
{
    EComponentListener *cl = (EComponentListener *) object;

    g_return_if_fail (E_IS_COMPONENT_LISTENER (cl));

    watched_connections = g_list_remove (watched_connections, cl);

    if (cl->priv->component != CORBA_OBJECT_NIL)
        cl->priv->component = CORBA_OBJECT_NIL;

    /* free memory */
    g_free (cl->priv);
    cl->priv = NULL;

    if (G_OBJECT_CLASS (parent_class)->finalize)
        (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

GType
e_component_listener_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static GTypeInfo info = {
                        sizeof (EComponentListenerClass),
                        (GBaseInitFunc) NULL,
                        (GBaseFinalizeFunc) NULL,
                        (GClassInitFunc) e_component_listener_class_init,
                        NULL, NULL,
                        sizeof (EComponentListener),
                        0,
                        (GInstanceInitFunc) e_component_listener_init
                };
        type = g_type_register_static (G_TYPE_OBJECT, "EComponentListener", &info, 0);
    }

    return type;
}

/**
 * e_component_listener_new
 * @comp: Component to listen for.
 *
 * Create a new #EComponentListener object, which allows to listen
 * for a given component and get notified when that component dies.
 *
 * Returns: a component listener object.
 */
EComponentListener *
e_component_listener_new (Bonobo_Unknown comp)
{
    EComponentListener *cl;

    g_return_val_if_fail (comp != NULL, NULL);

    cl = g_object_new (E_COMPONENT_LISTENER_TYPE, NULL);
    cl->priv->component = comp;

    /* watch the connection */
    ORBit_small_listen_for_broken (comp, G_CALLBACK (connection_listen_cb), cl);
    watched_connections = g_list_prepend (watched_connections, cl);

    return cl;
}

Bonobo_Unknown
e_component_listener_get_component (EComponentListener *cl)
{
    g_return_val_if_fail (E_IS_COMPONENT_LISTENER (cl), CORBA_OBJECT_NIL);
    return cl->priv->component;
}

void
e_component_listener_set_component (EComponentListener *cl, Bonobo_Unknown comp)
{
    g_return_if_fail (E_IS_COMPONENT_LISTENER (cl));

    cl->priv->component = comp;
    ORBit_small_listen_for_broken (comp, G_CALLBACK (connection_listen_cb), cl);
}