1 /*
2 * This is an example of a purple dbus plugin. After enabling this
3 * plugin, the following commands should work from the command line:
4 *
5 * prompt$ purple-send DbusExampleGetHelloObject
6 *
7 * returns, say: int32 74
8 *
9 * prompt$ purple-send DbusExampleGetText int32:74
10 *
11 * returns: string "Hello."
12 *
13 * prompt$ purple-send DbusExampleSetText int32:74 string:Bye!
14 *
15 * prompt$ purple-send DbusExampleGetText int32:74
16 *
17 * returns: string "Bye!"
18 *
19 * Purple is the legal property of its developers, whose names are too numerous
20 * to list here. Please refer to the COPYRIGHT file distributed with this
21 * source distribution.
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
36 */
37
38 #include "internal.h"
39
40 #include "blist.h"
41 #include "notify.h"
42 #include "plugin.h"
43 #include "version.h"
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #define DBUS_API_SUBJECT_TO_CHANGE
50 #include "dbus-maybe.h"
51 #include "dbus-bindings.h"
52
53 typedef struct {
54 char *text;
55 } PurpleText;
56
57 /* This makes the structure PurpleText visible to the purple-dbus type
58 system. It defines PurpleText as a type with no parent. From now
59 on, we will be able to register pointers to structures of this
60 type. You to dbus-define types you want to be directly accessible
61 by external applications. */
62 PURPLE_DBUS_DEFINE_TYPE(PurpleText)
63
64 /* Here we make four functions accessible to other applications by
65 DBus. These functions can access types defined in purple proper
66 (PurpleBuddy) as well as the types defined in the plugin (PurpleText). */
67 DBUS_EXPORT PurpleText* dbus_example_get_hello_object(void);
68 DBUS_EXPORT void dbus_example_set_text(PurpleText *obj, const char *text);
69 DBUS_EXPORT const char *dbus_example_get_text(PurpleText *obj);
70 DBUS_EXPORT const char *dbus_example_get_buddy_name(PurpleBuddy *buddy);
71
72 /* This file has been generated by the #dbus-analize-functions.py
73 script. It contains dbus wrappers for the four functions declared
74 above. */
75 #include "dbus-example-bindings.c"
76
77 /* This is the PurpleText object we want to make publicly visible. */
78 static PurpleText hello;
79
80 /* Here come the definitions of the four exported functions. */
dbus_example_get_hello_object(void)81 PurpleText* dbus_example_get_hello_object(void)
82 {
83 return &hello;
84 }
85
dbus_example_set_text(PurpleText * obj,const char * text)86 void dbus_example_set_text(PurpleText *obj, const char *text)
87 {
88 if (obj != NULL) {
89 g_free(obj->text);
90 obj->text = g_strdup(text);
91 }
92 }
93
dbus_example_get_text(PurpleText * obj)94 const char *dbus_example_get_text(PurpleText *obj)
95 {
96 if (obj != NULL)
97 return obj->text;
98 else
99 return NULL;
100 }
101
dbus_example_get_buddy_name(PurpleBuddy * buddy)102 const char *dbus_example_get_buddy_name(PurpleBuddy *buddy)
103 {
104 return purple_buddy_get_name(buddy);
105 }
106
107 /* And now standard plugin stuff */
108
109 static gboolean
plugin_load(PurplePlugin * plugin)110 plugin_load(PurplePlugin *plugin)
111 {
112 PURPLE_DBUS_RETURN_FALSE_IF_DISABLED(plugin);
113
114 /* First, we have to register our four exported functions with the
115 main purple dbus loop. Without this statement, the purple dbus
116 code wouldn't know about our functions. */
117 PURPLE_DBUS_REGISTER_BINDINGS(plugin);
118
119 /* Then, we register the hello object of type PurpleText. Note that
120 pointer registrations / unregistrations are completely dynamic;
121 they don't have to be made when the plugin is loaded /
122 unloaded. Without this statement the dbus purple code wouldn't
123 know about the hello object. */
124 PURPLE_DBUS_REGISTER_POINTER(&hello, PurpleText);
125
126 hello.text = g_strdup("Hello.");
127
128 return TRUE;
129 }
130
131
132 static gboolean
plugin_unload(PurplePlugin * plugin)133 plugin_unload(PurplePlugin *plugin)
134 {
135 g_free(hello.text);
136
137 /* It is necessary to unregister all pointers registered by the module. */
138 PURPLE_DBUS_UNREGISTER_POINTER(&hello);
139
140 return TRUE;
141 }
142
143 static PurplePluginInfo info =
144 {
145 PURPLE_PLUGIN_MAGIC,
146 PURPLE_MAJOR_VERSION,
147 PURPLE_MINOR_VERSION,
148 PURPLE_PLUGIN_STANDARD, /**< type */
149 NULL, /**< ui_requirement */
150 0, /**< flags */
151 NULL, /**< dependencies */
152 PURPLE_PRIORITY_DEFAULT, /**< priority */
153
154 "dbus-example", /**< id */
155 N_("DBus Example"), /**< name */
156 DISPLAY_VERSION, /**< version */
157 /** summary */
158 N_("DBus Plugin Example"),
159 /** description */
160 N_("DBus Plugin Example"),
161 "Piotr Zielinski (http://cl.cam.ac.uk/~pz215)", /**< author */
162 PURPLE_WEBSITE, /**< homepage */
163
164 plugin_load, /**< load */
165 plugin_unload, /**< unload */
166 NULL, /**< destroy */
167
168 NULL, /**< ui_info */
169 NULL, /**< extra_info */
170 NULL, /**< prefs_info */
171 NULL,
172
173 /* padding */
174 NULL,
175 NULL,
176 NULL,
177 NULL
178 };
179
init_plugin(PurplePlugin * plugin)180 static void init_plugin(PurplePlugin *plugin)
181 {
182 }
183
184 PURPLE_INIT_PLUGIN(dbus_example, init_plugin, info)
185