Adapt migration for files
[pidgin-git.git] / libpurple / plugins / dbus-example.c
blobe1d95ec717737ee28f58f9191d0d9c7f02f8977b
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:
5 * prompt$ purple-send DbusExampleGetHelloObject
7 * returns, say: int32 74
9 * prompt$ purple-send DbusExampleGetText int32:74
11 * returns: string "Hello."
13 * prompt$ purple-send DbusExampleSetText int32:74 string:Bye!
15 * prompt$ purple-send DbusExampleGetText int32:74
17 * returns: string "Bye!"
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.
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.
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.
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
38 /* When writing a third-party plugin, do not include libpurple's internal.h
39 * included below. This file is for internal libpurple use only. We're including
40 * it here for our own convenience. */
41 #include "internal.h"
43 /* This file defines PURPLE_PLUGINS and includes all the libpurple headers */
44 #include <purple.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
50 #define DBUS_API_SUBJECT_TO_CHANGE
51 #include "dbus-maybe.h"
52 #include "dbus-bindings.h"
54 typedef struct {
55 char *text;
56 } PurpleText;
58 /* This makes the structure PurpleText visible to the purple-dbus type
59 system. It defines PurpleText as a type with no parent. From now
60 on, we will be able to register pointers to structures of this
61 type. You to dbus-define types you want to be directly accessible
62 by external applications. */
63 PURPLE_DBUS_DEFINE_TYPE(PurpleText)
65 /* Here we make four functions accessible to other applications by
66 DBus. These functions can access types defined in purple proper
67 (PurpleBuddy) as well as the types defined in the plugin (PurpleText). */
68 DBUS_EXPORT PurpleText* dbus_example_get_hello_object(void);
69 DBUS_EXPORT void dbus_example_set_text(PurpleText *obj, const char *text);
70 DBUS_EXPORT const char *dbus_example_get_text(PurpleText *obj);
71 DBUS_EXPORT const char *dbus_example_get_buddy_name(PurpleBuddy *buddy);
73 /* This file has been generated by the #dbus-analize-functions.py
74 script. It contains dbus wrappers for the four functions declared
75 above. */
76 #include "dbus-example-bindings.c"
78 /* This is the PurpleText object we want to make publicly visible. */
79 static PurpleText hello;
81 /* Here come the definitions of the four exported functions. */
82 DBUS_EXPORT PurpleText* dbus_example_get_hello_object(void)
84 return &hello;
87 DBUS_EXPORT void dbus_example_set_text(PurpleText *obj, const char *text)
89 if (obj != NULL) {
90 g_free(obj->text);
91 obj->text = g_strdup(text);
95 DBUS_EXPORT const char *dbus_example_get_text(PurpleText *obj)
97 if (obj != NULL)
98 return obj->text;
99 else
100 return NULL;
103 DBUS_EXPORT const char *dbus_example_get_buddy_name(PurpleBuddy *buddy)
105 return purple_buddy_get_name(buddy);
108 /* And now standard plugin stuff */
110 static PurplePluginInfo *
111 plugin_query(GError **error)
113 const gchar * const authors[] = {
114 "Piotr Zielinski (http://cl.cam.ac.uk/~pz215)",
115 NULL
118 return purple_plugin_info_new(
119 "id", "dbus-example",
120 "name", N_("DBus Example"),
121 "version", DISPLAY_VERSION,
122 "category", N_("Example"),
123 "summary", N_("DBus Plugin Example"),
124 "description", N_("DBus Plugin Example"),
125 "authors", authors,
126 "website", PURPLE_WEBSITE,
127 "abi-version", PURPLE_ABI_VERSION,
128 NULL
132 static gboolean
133 plugin_load(PurplePlugin *plugin, GError **error)
135 PURPLE_DBUS_RETURN_FALSE_IF_DISABLED(plugin);
137 /* First, we have to register our four exported functions with the
138 main purple dbus loop. Without this statement, the purple dbus
139 code wouldn't know about our functions. */
140 PURPLE_DBUS_REGISTER_BINDINGS(plugin);
142 /* Then, we register the hello object of type PurpleText. Note that
143 pointer registrations / unregistrations are completely dynamic;
144 they don't have to be made when the plugin is loaded /
145 unloaded. Without this statement the dbus purple code wouldn't
146 know about the hello object. */
147 PURPLE_DBUS_REGISTER_POINTER(&hello, PurpleText);
149 hello.text = g_strdup("Hello.");
151 return TRUE;
155 static gboolean
156 plugin_unload(PurplePlugin *plugin, GError **error)
158 g_free(hello.text);
160 /* It is necessary to unregister all pointers registered by the module. */
161 PURPLE_DBUS_UNREGISTER_POINTER(&hello);
163 return TRUE;
166 PURPLE_PLUGIN_INIT(dbus_example, plugin_query, plugin_load, plugin_unload);