aboutsummaryrefslogtreecommitdiff
path: root/libdbusmenu-gtk/serializablemenuitem.c
blob: 06dfa598fc4e7d6ee39f3eba71507c87be13fb46 (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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
An object to act as a base class for easy GTK widgets that can be
transfered over dbusmenu.

Copyright 2011 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

This program is free software: you can redistribute it and/or modify it 
under the terms of either or both of the following licenses:

1) the GNU Lesser General Public License version 3, as published by the 
Free Software Foundation; and/or
2) the GNU Lesser General Public License version 2.1, as published by 
the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 
PURPOSE.  See the applicable version of the GNU Lesser General Public 
License for more details.

You should have received a copy of both the GNU Lesser General Public 
License version 3 and version 2.1 along with this program.  If not, see 
<http://www.gnu.org/licenses/>
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "client.h"
#include "serializablemenuitem.h"

/**
	DbusmenuGtkSerializableMenuItemPrivate:
	@mi: Menuitem to watch the property changes from
*/
struct _DbusmenuGtkSerializableMenuItemPrivate {
	DbusmenuMenuitem * mi;
};

/* Properties */
enum {
	PROP_0,
	PROP_MENUITEM
};

/* Private macro, only used in object init */
#define DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), DBUSMENU_TYPE_GTK_SERIALIZABLE_MENU_ITEM, DbusmenuGtkSerializableMenuItemPrivate))

/* Function prototypes */
static void dbusmenu_gtk_serializable_menu_item_class_init (DbusmenuGtkSerializableMenuItemClass *klass);
static void dbusmenu_gtk_serializable_menu_item_init       (DbusmenuGtkSerializableMenuItem *self);
static void dbusmenu_gtk_serializable_menu_item_dispose    (GObject *object);
static void dbusmenu_gtk_serializable_menu_item_finalize   (GObject *object);

/* GObject boiler plate */
G_DEFINE_TYPE (DbusmenuGtkSerializableMenuItem, dbusmenu_gtk_serializable_menu_item, GTK_TYPE_MENU_ITEM);

/* Initialize the stuff in the class structure */
static void
dbusmenu_gtk_serializable_menu_item_class_init (DbusmenuGtkSerializableMenuItemClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (DbusmenuGtkSerializableMenuItemPrivate));

	object_class->dispose = dbusmenu_gtk_serializable_menu_item_dispose;
	object_class->finalize = dbusmenu_gtk_serializable_menu_item_finalize;

	g_object_class_install_property (object_class, PROP_MENUITEM,
	                                 g_param_spec_object(DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM_PROP_MENUITEM, "DBusmenu Menuitem attached to item",
	                                              "A menuitem who's properties are being watched and where changes should be watched for updates.  It is the responsibility of subclasses to set up the signal handlers for those property changes.",
	                                              DBUSMENU_TYPE_MENUITEM,
	                                              G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	return;
}

/* Initialize the object structures and private structure */
static void
dbusmenu_gtk_serializable_menu_item_init (DbusmenuGtkSerializableMenuItem *self)
{
	self->priv = DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM_GET_PRIVATE(self);

	self->priv->mi = NULL;

	return;
}

/* Free all references to objects */
static void
dbusmenu_gtk_serializable_menu_item_dispose (GObject *object)
{
	DbusmenuGtkSerializableMenuItem * smi = DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM(object);
	g_return_if_fail(smi != NULL);

	if (smi->priv->mi != NULL) {
		g_object_unref(G_OBJECT(smi->priv->mi));
		smi->priv->mi = NULL;
	}


	G_OBJECT_CLASS (dbusmenu_gtk_serializable_menu_item_parent_class)->dispose (object);
	return;
}

/* Free memory */
static void
dbusmenu_gtk_serializable_menu_item_finalize (GObject *object)
{



	G_OBJECT_CLASS (dbusmenu_gtk_serializable_menu_item_parent_class)->finalize (object);
	return;
}

/**
	dbusmenu_gtk_serializable_menu_item_build_dbusmenu_menuitem:
	@smi: #DbusmenuGtkSerializableMenuItem to build a #DbusmenuMenuitem mirroring

	This function is for menu items that are instanciated from
	GTK and have their properites set using GTK functions.  This
	builds a #DbusmenuMenuitem that then has the properties that
	should be sent over the bus to create a new item of this
	type on the other side.

	Return value: A #DbusmenuMenuitem who's values will be set by
		this object.
*/
DbusmenuMenuitem *
dbusmenu_gtk_serializable_menu_item_build_dbusmenu_menuitem (DbusmenuGtkSerializableMenuItem * smi)
{
	g_return_val_if_fail(DBUSMENU_IS_GTK_SERIALIZABLE_MENU_ITEM(smi), NULL);

	DbusmenuGtkSerializableMenuItemClass * klass = DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM_GET_CLASS(smi);
	if (klass->build_dbusmenu_menuitem != NULL) {
		return klass->build_dbusmenu_menuitem(smi);
	}

	return NULL;
}

/* Callback to the generic type handler */
typedef struct _type_handler_t type_handler_t;
struct _type_handler_t {
	DbusmenuGtkSerializableMenuItemClass * class;
	GType type;
};

/* Handle the type with this item. */
static gboolean
type_handler (DbusmenuMenuitem * newitem, DbusmenuMenuitem * parent, DbusmenuClient * client, gpointer user_data)
{
	type_handler_t * th = (type_handler_t *)user_data;

	DbusmenuGtkSerializableMenuItem * smi = DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM(g_object_new(th->type, NULL));
	g_return_val_if_fail(smi != NULL, FALSE);

	dbusmenu_gtk_serializable_menu_item_set_dbusmenu_menuitem(smi, newitem);
	dbusmenu_gtkclient_newitem_base(DBUSMENU_GTKCLIENT(client), newitem, GTK_MENU_ITEM(smi), parent);

	return TRUE;
}

/* Destruction is inevitable */
static void
type_destroy_handler (DbusmenuClient * client, const gchar * type, gpointer user_data)
{
	g_return_if_fail(user_data != NULL);
	type_handler_t * th = (type_handler_t *)user_data;
	g_type_class_unref(th->class);
	g_free(user_data);
	return;
}

/**
	dbusmenu_gtk_serializable_menu_item_register_to_client:
	@client: #DbusmenuClient that we should register a type at.
	@item_type: The #GType of a class that is a subclass of #DbusmenuGtkSerializableMenuItem

	Registers a generic handler for dealing with all subclasses of
	#DbusmenuGtkSerializableMenuItem.  This handler responds to the callback,
	creates a new object and attaches it to the appropriate #DbusmenuMenuitem
	object.
*/
void
dbusmenu_gtk_serializable_menu_item_register_to_client (DbusmenuClient * client, GType item_type)
{
	g_return_if_fail(g_type_is_a(item_type, DBUSMENU_TYPE_GTK_SERIALIZABLE_MENU_ITEM));

	gpointer type_class = g_type_class_ref(item_type);
	g_return_if_fail(type_class != NULL);

	DbusmenuGtkSerializableMenuItemClass * class = DBUSMENU_GTK_SERIALIZABLE_MENU_ITEM_CLASS(type_class);

	if (class->get_type_string == NULL) {
		g_type_class_unref(type_class);
		g_error("No 'get_type_string' in subclass of DbusmenuGtkSerializableMenuItem");
		return;
	}

	/* Register type */
	type_handler_t * th = g_new0(type_handler_t, 1);
	th->class = class;
	th->type = item_type;
	if (!dbusmenu_client_add_type_handler_full(client, class->get_type_string(), type_handler, th, type_destroy_handler)) {
		type_destroy_handler(client, class->get_type_string(), th);
	}

	/* Register defaults */
	/* TODO: Need API on another branch */

	return;
}

/**
	dbusmenu_gtk_serializable_menu_item_set_dbusmenu_menuitem:
	@smi: #DbusmenuGtkSerializableMenuItem to set the @DbusmenuGtkSerializableMenuItem::dbusmenu-menuitem of
	@mi: Menuitem to get the properties from

	This function is used on the server side to signal to the object
	that it should get its' property change events from @mi instead
	of expecting calls to its' API.  A call to this function sets the
	property and subclasses should listen to the notify signal to
	pick up this property being set.
*/
void
dbusmenu_gtk_serializable_menu_item_set_dbusmenu_menuitem (DbusmenuGtkSerializableMenuItem * smi, DbusmenuMenuitem * mi)
{

	return;
}