Define new 'pivot-prop-loadable' property
[nautilus-actions.git] / src / core / na-object-id.c
blob44c8eff8e609a80a7ed88bd27a69640ec1be52a0
1 /*
2 * Nautilus-Actions
3 * A Nautilus extension which offers configurable context menu actions.
5 * Copyright (C) 2005 The GNOME Foundation
6 * Copyright (C) 2006, 2007, 2008 Frederic Ruaudel and others (see AUTHORS)
7 * Copyright (C) 2009, 2010, 2011 Pierre Wieser and others (see AUTHORS)
9 * This Program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This Program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this Library; see the file COPYING. If not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307, USA.
24 * Authors:
25 * Frederic Ruaudel <grumz@grumz.net>
26 * Rodrigo Moya <rodrigo@gnome-db.org>
27 * Pierre Wieser <pwieser@trychlos.org>
28 * ... and many others (see AUTHORS)
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <glib/gi18n.h>
37 #include <api/na-core-utils.h>
38 #include <api/na-object-api.h>
40 /* private class data
42 struct _NAObjectIdClassPrivate {
43 void *empty; /* so that gcc -pedantic is happy */
46 /* private instance data
48 struct _NAObjectIdPrivate {
49 gboolean dispose_has_run;
52 static NAObjectClass *st_parent_class = NULL;
54 static GType register_type( void );
55 static void class_init( NAObjectIdClass *klass );
56 static void instance_init( GTypeInstance *instance, gpointer klass );
57 static void instance_dispose( GObject *object );
58 static void instance_finalize( GObject *object );
60 static gboolean object_is_valid( const NAObject *object );
62 static gchar *v_new_id( const NAObjectId *object, const NAObjectId *new_parent );
64 GType
65 na_object_id_get_type( void )
67 static GType item_type = 0;
69 if( item_type == 0 ){
70 item_type = register_type();
73 return( item_type );
76 static GType
77 register_type( void )
79 static const gchar *thisfn = "na_object_id_register_type";
80 GType type;
82 static GTypeInfo info = {
83 sizeof( NAObjectIdClass ),
84 NULL,
85 NULL,
86 ( GClassInitFunc ) class_init,
87 NULL,
88 NULL,
89 sizeof( NAObjectId ),
91 ( GInstanceInitFunc ) instance_init
94 g_debug( "%s", thisfn );
96 type = g_type_register_static( NA_OBJECT_TYPE, "NAObjectId", &info, 0 );
98 return( type );
101 static void
102 class_init( NAObjectIdClass *klass )
104 static const gchar *thisfn = "na_object_id_class_init";
105 GObjectClass *object_class;
106 NAObjectClass *naobject_class;
108 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
110 st_parent_class = g_type_class_peek_parent( klass );
112 object_class = G_OBJECT_CLASS( klass );
113 object_class->dispose = instance_dispose;
114 object_class->finalize = instance_finalize;
116 naobject_class = NA_OBJECT_CLASS( klass );
117 naobject_class->copy = NULL;
118 naobject_class->are_equal = NULL;
119 naobject_class->is_valid = object_is_valid;
121 klass->private = g_new0( NAObjectIdClassPrivate, 1 );
124 static void
125 instance_init( GTypeInstance *instance, gpointer klass )
127 NAObjectId *self;
129 g_return_if_fail( NA_IS_OBJECT_ID( instance ));
131 self = NA_OBJECT_ID( instance );
133 self->private = g_new0( NAObjectIdPrivate, 1 );
137 * note that when the tree store is cleared, Gtk begins with the deepest
138 * levels, so that children are disposed before their parent
139 * as we try to dispose all children when disposing a parent, we have to
140 * remove a disposing child from its parent
142 static void
143 instance_dispose( GObject *object )
145 static const gchar *thisfn = "na_object_id_instance_dispose";
146 NAObjectId *self;
147 NAObjectItem *parent;
149 g_return_if_fail( NA_IS_OBJECT_ID( object ));
151 self = NA_OBJECT_ID( object );
153 if( !self->private->dispose_has_run ){
155 self->private->dispose_has_run = TRUE;
157 parent = na_object_get_parent( object );
158 g_debug( "%s: parent=%p (%s)",
159 thisfn, ( void * ) parent, parent ? G_OBJECT_TYPE_NAME( parent ) : "n/a" );
160 if( parent ){
161 na_object_remove_item( parent, object );
162 na_object_set_parent( object, NULL );
165 self->private->dispose_has_run = TRUE;
167 /* chain up to the parent class */
168 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
169 G_OBJECT_CLASS( st_parent_class )->dispose( object );
174 static void
175 instance_finalize( GObject *object )
177 NAObjectId *self;
179 g_return_if_fail( NA_IS_OBJECT_ID( object ));
181 self = NA_OBJECT_ID( object );
183 g_free( self->private );
185 /* chain call to parent class */
186 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
187 G_OBJECT_CLASS( st_parent_class )->finalize( object );
192 * a NAObjectId is valid if it has a non-null id
194 static gboolean
195 object_is_valid( const NAObject *object )
197 gboolean is_valid;
198 gchar *id;
200 is_valid = TRUE;
202 if( is_valid ){
203 id = na_object_get_id( object );
204 is_valid = ( id != NULL && strlen( id ) > 0 );
205 g_free( id );
208 return( is_valid );
212 * na_object_id_sort_alpha_asc:
213 * @a: first #NAObjectId.
214 * @b: second #NAObjectId.
216 * Sort the objects in alphabetical ascending order of their label.
218 * Returns:
220 * <itemizedlist>
221 * <listitem>
222 * <para>-1 if @a must be sorted before @b,</para>
223 * </listitem>
224 * <listitem>
225 * <para>0 if @a and @b are equal from the local point of view,</para>
226 * </listitem>
227 * <listitem>
228 * <para>1 if @a must be sorted after @b.</para>
229 * </listitem>
230 * </itemizedlist>
232 * Since: 2.30
234 gint
235 na_object_id_sort_alpha_asc( const NAObjectId *a, const NAObjectId *b )
237 gchar *label_a, *label_b;
238 gint compare;
240 label_a = na_object_get_label( a );
241 label_b = na_object_get_label( b );
243 compare = na_core_utils_str_collate( label_a, label_b );
245 g_free( label_b );
246 g_free( label_a );
248 return( compare );
252 * na_object_id_sort_alpha_desc:
253 * @a: first #NAObjectId.
254 * @b: second #NAObjectId.
256 * Sort the objects in alphabetical descending order of their label.
258 * Returns:
260 * <itemizedlist>
261 * <listitem>
262 * <para>-1 if @a must be sorted before @b,</para>
263 * </listitem>
264 * <listitem>
265 * <para>0 if @a and @b are equal from the local point of view,</para>
266 * </listitem>
267 * <listitem>
268 * <para>1 if @a must be sorted after @b.</para>
269 * </listitem>
270 * </itemizedlist>
272 * Since: 2.30
274 gint
275 na_object_id_sort_alpha_desc( const NAObjectId *a, const NAObjectId *b )
277 return( -1 * na_object_id_sort_alpha_asc( a, b ));
281 * na_object_id_prepare_for_paste:
282 * @object: the #NAObjectId object to be pasted.
283 * @relabel: whether this object should be relabeled when pasted.
284 * @renumber: whether this item should be renumbered ?
285 * @parent: the parent of @object, or %NULL.
287 * Prepares @object to be pasted.
289 * If a #NAObjectProfile, then @object is attached to the specified
290 * #NAObjectAction @action. The identifier is always renumbered to be
291 * suitable with the already existing profiles.
293 * If a #NAObjectAction or a #NAObjectMenu, a new identifier is allocated
294 * if and only if @relabel is %TRUE.
296 * Actual relabeling takes place if @relabel is %TRUE, depending of the
297 * user preferences.
299 * Since: 2.30
301 void
302 na_object_id_prepare_for_paste( NAObjectId *object, gboolean relabel, gboolean renumber, NAObjectId *parent )
304 static const gchar *thisfn = "na_object_id_prepare_for_paste";
305 GList *subitems, *it;
307 g_return_if_fail( NA_IS_OBJECT_ID( object ));
308 g_return_if_fail( !parent || NA_IS_OBJECT_ITEM( parent ));
310 if( !object->private->dispose_has_run ){
312 g_debug( "%s: object=%p, relabel=%s, renumber=%s, parent=%p",
313 thisfn, ( void * ) object, relabel ? "True":"False", renumber ? "True":"False", ( void * ) parent );
315 if( NA_IS_OBJECT_PROFILE( object )){
316 na_object_set_parent( object, parent );
317 na_object_set_new_id( object, parent );
318 if( renumber && relabel ){
319 na_object_set_copy_of_label( object );
322 } else {
323 if( renumber ){
324 na_object_set_new_id( object, NULL );
325 if( relabel ){
326 na_object_set_copy_of_label( object );
328 na_object_set_provider( object, NULL );
329 na_object_set_provider_data( object, NULL );
330 na_object_set_readonly( object, FALSE );
332 if( NA_IS_OBJECT_MENU( object )){
333 subitems = na_object_get_items( object );
334 for( it = subitems ; it ; it = it->next ){
335 na_object_prepare_for_paste( it->data, relabel, renumber, NULL );
343 * na_object_id_set_copy_of_label:
344 * @object: the #NAObjectId object whose label is to be changed.
346 * Sets the 'Copy of' label.
348 * Since: 2.30
350 void
351 na_object_id_set_copy_of_label( NAObjectId *object )
353 gchar *label, *new_label;
355 g_return_if_fail( NA_IS_OBJECT_ID( object ));
357 if( !object->private->dispose_has_run ){
359 label = na_object_get_label( object );
361 /* i18n: copied items have a label as 'Copy of original label' */
362 new_label = g_strdup_printf( _( "Copy of %s" ), label );
364 na_object_set_label( object, new_label );
366 g_free( new_label );
367 g_free( label );
372 * na_object_id_set_new_id:
373 * @object: the #NAObjectId object whose internal identifier is to be
374 * set.
375 * @new_parent: if @object is a #NAObjectProfile, then @new_parent
376 * should be set to the #NAObjectAction new parent. Else, it would not
377 * be possible to allocate a new profile id compatible with already
378 * existing ones.
380 * Request a new id to the derived class, and set it.
382 * Since: 2.30
384 void
385 na_object_id_set_new_id( NAObjectId *object, const NAObjectId *new_parent )
387 static const gchar *thisfn = "na_object_id_set_new_id";
388 gchar *id;
390 g_return_if_fail( NA_IS_OBJECT_ID( object ));
391 g_return_if_fail( !new_parent || NA_IS_OBJECT_ITEM( new_parent ));
393 if( !object->private->dispose_has_run ){
395 g_debug( "%s: object=%p (%s), new_parent=%p (%s)",
396 thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ),
397 ( void * ) new_parent, new_parent ? G_OBJECT_TYPE_NAME( new_parent ) : "n/a" );
399 id = v_new_id( object, new_parent );
401 if( id ){
402 na_object_set_id( object, id );
403 g_free( id );
408 static gchar *
409 v_new_id( const NAObjectId *object, const NAObjectId *new_parent )
411 gchar *new_id;
412 GList *hierarchy, *ih;
413 gboolean found;
415 found = FALSE;
416 new_id = NULL;
417 hierarchy = g_list_reverse( na_object_get_hierarchy( NA_OBJECT( object )));
418 /*g_debug( "na_object_id_most_derived_id: object=%p (%s)",
419 ( void * ) object, G_OBJECT_TYPE_NAME( object ));*/
421 for( ih = hierarchy ; ih && !found ; ih = ih->next ){
422 if( NA_OBJECT_ID_CLASS( ih->data )->new_id ){
423 new_id = NA_OBJECT_ID_CLASS( ih->data )->new_id( object, new_parent );
424 found = TRUE;
426 if( G_OBJECT_CLASS_TYPE( ih->data ) == NA_OBJECT_ID_TYPE ){
427 break;
431 na_object_free_hierarchy( hierarchy );
433 return( new_id );