~ubuntu-branches/ubuntu/quantal/libbonobo/quantal-201207170711

« back to all changes in this revision

Viewing changes to tests/test-uniqapp.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-02-18 14:40:51 UTC
  • mto: (3.1.1 etch) (1.1.25 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050218144051-fo4h9qh2gim8x3wt
Tags: upstream-2.8.1
ImportĀ upstreamĀ versionĀ 2.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
#include <config.h>
 
3
#include <libbonobo.h>
 
4
#include <string.h>
 
5
#include "bonobo/bonobo-marshal.h"
 
6
 
 
7
#define TEST_MESSAGE    "test-message"
 
8
#define CLOSURE_MESSAGE "closure-message"
 
9
 
 
10
static gboolean
 
11
quit_after_timeout (gpointer data)
 
12
{
 
13
        bonobo_main_quit ();
 
14
        return FALSE;
 
15
}
 
16
 
 
17
static GValue *
 
18
message_quit_cb (BonoboAppClient *app_client, const gchar *message, GValueArray *args)
 
19
{
 
20
        g_timeout_add (1000, quit_after_timeout, NULL);
 
21
        return NULL;
 
22
}
 
23
 
 
24
static GValue *
 
25
message_cb (BonoboAppClient *app_client, const gchar *message, GValueArray *args)
 
26
{
 
27
        GValue *retval;
 
28
 
 
29
        g_return_val_if_fail (strcmp (message, TEST_MESSAGE) == 0, NULL);
 
30
        g_return_val_if_fail (args->n_values == 2, NULL);
 
31
        g_return_val_if_fail (G_VALUE_HOLDS_DOUBLE (&args->values[0]), NULL);
 
32
        g_return_val_if_fail (G_VALUE_HOLDS_STRING (&args->values[1]), NULL);
 
33
 
 
34
        g_message ("message_cb: %s(%f, \"%s\")", message,
 
35
                   g_value_get_double (&args->values[0]),
 
36
                   g_value_get_string (&args->values[1]));
 
37
 
 
38
        retval = g_new0 (GValue, 1);
 
39
        g_value_init (retval, G_TYPE_DOUBLE);
 
40
        g_value_set_double (retval, 2 * g_value_get_double (&args->values[0]));
 
41
        return retval;
 
42
}
 
43
 
 
44
static void
 
45
test_app_hook (BonoboApplication *app, gpointer data)
 
46
{
 
47
        g_message ("App '%s' created; data == %p", app->name, data);
 
48
}
 
49
 
 
50
static gint
 
51
new_instance_cb (BonoboApplication *app, gint argc, char *argv[])
 
52
{
 
53
        int i;
 
54
 
 
55
        g_message ("new-instance received. argc = %i; argv follows:", argc);
 
56
        for (i = 0; i < argc; ++i)
 
57
                g_message ("argv[%i] = \"%s\"", i, argv[i]);
 
58
        g_message ("new-instance: returning argc (%i)", argc);
 
59
        return argc;
 
60
}
 
61
 
 
62
 
 
63
static gdouble
 
64
closure_message_cb (BonoboApplication *app, gint arg_1, gdouble arg_2, gpointer data2)
 
65
{
 
66
        g_message("closure_message_cb: %p, %i, %f, %p",
 
67
                  app, arg_1, arg_2, data2);
 
68
        return arg_1 * arg_2;
 
69
}
 
70
 
 
71
 
 
72
int
 
73
main (int argc, char *argv [])
 
74
{
 
75
        BonoboApplication         *app;
 
76
        gchar                     *serverinfo;
 
77
        Bonobo_RegistrationResult  reg_res;
 
78
        BonoboAppClient           *client;
 
79
        double                     msg_arg = 3.141592654;
 
80
        GClosure                  *closure;
 
81
        gchar const               *envp[] = { "LANG", NULL };
 
82
 
 
83
        if (bonobo_init (&argc, argv) == FALSE)
 
84
                g_error ("Can not bonobo_init");
 
85
        bonobo_activate ();
 
86
 
 
87
        bonobo_application_add_hook (test_app_hook, (gpointer) 0xdeadbeef);
 
88
 
 
89
        app = bonobo_application_new ("Libbonobo-Test-Uniqapp");
 
90
 
 
91
        closure = g_cclosure_new (G_CALLBACK (closure_message_cb),
 
92
                                  (gpointer) 0xdeadbeef,  NULL);
 
93
        g_closure_set_marshal (closure, bonobo_marshal_DOUBLE__LONG_DOUBLE);
 
94
        bonobo_application_register_message (app, CLOSURE_MESSAGE,
 
95
                                             "This is a test message",
 
96
                                             closure,
 
97
                                             G_TYPE_DOUBLE, G_TYPE_LONG,
 
98
                                             G_TYPE_DOUBLE, G_TYPE_NONE);
 
99
        serverinfo = bonobo_application_create_serverinfo (app, envp);
 
100
        reg_res = bonobo_application_register_unique (app, serverinfo, &client);
 
101
        g_free (serverinfo);
 
102
 
 
103
        switch (reg_res)
 
104
        {
 
105
 
 
106
        case Bonobo_ACTIVATION_REG_ALREADY_ACTIVE: {
 
107
                BonoboAppClientMsgDesc const *msgdescs;
 
108
                GValue                       *retval;
 
109
                int                           i;
 
110
 
 
111
                g_message ("I am an application client.");
 
112
                bonobo_object_unref (BONOBO_OBJECT (app));
 
113
                app = NULL;
 
114
 
 
115
                msgdescs = bonobo_app_client_msg_list (client);
 
116
                g_assert (msgdescs);
 
117
 
 
118
                for (i = 0; msgdescs[i].name; ++i)
 
119
                        g_message ("Application supports message '%s'", msgdescs[i].name);
 
120
 
 
121
                g_message ("Sending message string '%s' with argument %f",
 
122
                           TEST_MESSAGE, msg_arg);
 
123
                retval = bonobo_app_client_msg_send (client, TEST_MESSAGE, NULL,
 
124
                                                     G_TYPE_DOUBLE, msg_arg,
 
125
                                                     G_TYPE_STRING, "this is a string",
 
126
                                                     G_TYPE_NONE);
 
127
                g_message ("Return value: %f", g_value_get_double (retval));
 
128
                if (retval) {
 
129
                        g_value_unset (retval);
 
130
                        g_free (retval);
 
131
                }
 
132
 
 
133
                g_message ("Sending message string '%s' with arguments %i and %f",
 
134
                           CLOSURE_MESSAGE, 10, 3.141592654);
 
135
                retval = bonobo_app_client_msg_send (client, CLOSURE_MESSAGE,
 
136
                                                     NULL,
 
137
                                                     G_TYPE_LONG, 10,
 
138
                                                     G_TYPE_DOUBLE, 3.141592654,
 
139
                                                     G_TYPE_NONE);
 
140
                g_message ("Return value: %f", g_value_get_double (retval));
 
141
                if (retval) {
 
142
                        g_value_unset (retval);
 
143
                        g_free (retval);
 
144
                }
 
145
 
 
146
                g_message ("Sending new-instance, with argc/argv");
 
147
                i = bonobo_app_client_new_instance (client, argc, argv, NULL);
 
148
                g_message ("new-instance returned %i", i);
 
149
 
 
150
                g_message ("Asking the server to quit");
 
151
                retval = bonobo_app_client_msg_send (client, "quit", NULL, G_TYPE_NONE);
 
152
                if (retval) {
 
153
                        g_value_unset (retval);
 
154
                        g_free (retval);
 
155
                }
 
156
 
 
157
                g_object_unref (client);
 
158
                return bonobo_debug_shutdown ();
 
159
        }
 
160
        case Bonobo_ACTIVATION_REG_SUCCESS:
 
161
                g_message ("I am an application server");
 
162
                g_signal_connect (app, "message::test-message",
 
163
                                  G_CALLBACK (message_cb), NULL);
 
164
                g_signal_connect (app, "new-instance",
 
165
                                  G_CALLBACK (new_instance_cb), NULL);
 
166
                bonobo_application_new_instance (app, argc, argv);
 
167
                g_signal_connect (app, "message::quit",
 
168
                                  G_CALLBACK (message_quit_cb), NULL);
 
169
                break;
 
170
 
 
171
        case Bonobo_ACTIVATION_REG_ERROR:
 
172
        default:
 
173
                g_error("bonobo activation error when registering unique application");
 
174
        }
 
175
 
 
176
        bonobo_main ();
 
177
 
 
178
        if (app) bonobo_object_unref (BONOBO_OBJECT (app));
 
179
 
 
180
        return bonobo_debug_shutdown ();
 
181
}