summaryrefslogtreecommitdiffstats
path: root/dcopc/glibtest.c
blob: b2473e663e5a1ef9bad7285aa267c57494526d4e (plain)
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
/*
Copyright (c) 2000 Simon Hausmann <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <dcopc/dcopc.h>
#include <dcopc/dcopobject.h>
#include <stdio.h>
#include <glib.h>

#define C( call ) \
    if ( !call ) \
    { \
      fprintf( stderr, "dcop error: %s\n", dcop_client_error_message() ); \
      return 1; \
    }

gboolean dcop_socket_notify( GIOChannel *chan, GIOCondition condition, gpointer data )
{
    DcopClient *client = (DcopClient *)data;
    fprintf( stderr, "dcop_socket_notify\n" );
    dcop_client_process_socket_data( client );
}

gboolean (*old_process_func) ( DcopObject *obj, const gchar *fun, dcop_data *data,
			      gchar **reply_type, dcop_data **reply_data );

gboolean my_obj_process( DcopObject *obj, const gchar *fun, dcop_data *data,
			 gchar **reply_type, dcop_data **reply_data )
{
    if ( strcmp( fun, "doExit()" ) == 0 )
    {
	gtk_main_quit();
	return TRUE;
    }
    if ( strcmp( fun, "mySlot()" ) == 0 )
	{
	    g_message( "mySlot called!" );
	    return TRUE;
	}

    return old_process_func( obj, fun, data, reply_type, reply_data );
}

GList *(*old_functions_func)( DcopObject *obj ) = 0;

GList *my_obj_functions( DcopObject *obj )
{
    GList *res = old_functions_func( obj );
    res = g_list_append( res, g_strdup( "doExit()" ) );
    res = g_list_append( res, g_strdup( "mySlot()" ) );
    return res;
}

GList *(*old_interfaces_func)( DcopObject *obj ) = 0;

GList *my_obj_interfaces( DcopObject *obj )
{
    GList *res = old_interfaces_func( obj );
    res = g_list_append( res, g_strdup( "MyObj" ) );
    return res;
}

int main( int argc, char **argv )
{
  DcopClient *c;
  dcop_data *d;
  dcop_data *reply;
  char *reply_type;
  GIOChannel *chan;
  DcopObject *obj;

  gtk_init( &argc, &argv );

  c = dcop_client_new();

  C( dcop_client_register_as( c, "dcoptest", False ) );

  chan = g_io_channel_unix_new( dcop_client_socket( c ) );
  g_io_channel_ref( chan );

  g_io_add_watch( chan, G_IO_IN, dcop_socket_notify, c );

  obj = dcop_object_new();
  dcop_object_set_id( obj, "myServer" );

  old_process_func = DCOP_OBJECT_CLASS( GTK_OBJECT( obj )->klass )->process;
  old_functions_func = DCOP_OBJECT_CLASS( GTK_OBJECT( obj )->klass )->functions;
  old_interfaces_func = DCOP_OBJECT_CLASS( GTK_OBJECT( obj )->klass )->interfaces;

  DCOP_OBJECT_CLASS( GTK_OBJECT( obj )->klass )->process = my_obj_process;
  DCOP_OBJECT_CLASS( GTK_OBJECT( obj )->klass )->functions = my_obj_functions;
  DCOP_OBJECT_CLASS( GTK_OBJECT( obj )->klass )->interfaces = my_obj_interfaces;

  if ( 	dcop_client_connect_dcop_signal( c, dcop_client_app_id( c ), "myServer",
					 "mySignal()",
					 "myServer", "mySlot()", FALSE ) == FALSE )
      g_message( "error connecting dcop signal :-(" );

  d = dcop_data_ref( dcop_data_new() );

  dcop_client_emit_dcop_signal( c, "myServer", "mySignal()", d );

  dcop_data_deref( d );

  dcop_client_set_daemon_mode( c, TRUE );  

  gtk_main();

  g_io_channel_unref( chan );

  C( dcop_client_detach( c ) );

  gtk_object_unref( GTK_OBJECT( c ) );

  return 0;
}