NAIDuplicable: rewrite modification check stack
[nautilus-actions.git] / src / core / na-object-id.c
blobcbae95457cd8469ca6035ed5980b8d13e86316b4
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->is_valid = object_is_valid;
120 klass->private = g_new0( NAObjectIdClassPrivate, 1 );
123 static void
124 instance_init( GTypeInstance *instance, gpointer klass )
126 NAObjectId *self;
128 g_return_if_fail( NA_IS_OBJECT_ID( instance ));
130 self = NA_OBJECT_ID( instance );
132 self->private = g_new0( NAObjectIdPrivate, 1 );
136 * note that when the tree store is cleared, Gtk begins with the deepest
137 * levels, so that children are disposed before their parent
138 * as we try to dispose all children when disposing a parent, we have to
139 * remove a disposing child from its parent
141 static void
142 instance_dispose( GObject *object )
144 static const gchar *thisfn = "na_object_id_instance_dispose";
145 NAObjectId *self;
146 NAObjectItem *parent;
148 g_return_if_fail( NA_IS_OBJECT_ID( object ));
150 self = NA_OBJECT_ID( object );
152 if( !self->private->dispose_has_run ){
154 self->private->dispose_has_run = TRUE;
156 parent = na_object_get_parent( object );
157 g_debug( "%s: parent=%p (%s)",
158 thisfn, ( void * ) parent, parent ? G_OBJECT_TYPE_NAME( parent ) : "n/a" );
159 if( parent ){
160 na_object_remove_item( parent, object );
161 na_object_set_parent( object, NULL );
164 self->private->dispose_has_run = TRUE;
166 /* chain up to the parent class */
167 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
168 G_OBJECT_CLASS( st_parent_class )->dispose( object );
173 static void
174 instance_finalize( GObject *object )
176 NAObjectId *self;
178 g_return_if_fail( NA_IS_OBJECT_ID( object ));
180 self = NA_OBJECT_ID( object );
182 g_free( self->private );
184 /* chain call to parent class */
185 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
186 G_OBJECT_CLASS( st_parent_class )->finalize( object );
191 * a NAObjectId is valid if it has a non-null id
193 static gboolean
194 object_is_valid( const NAObject *object )
196 gboolean is_valid;
197 gchar *id;
199 is_valid = TRUE;
201 if( is_valid ){
202 id = na_object_get_id( object );
203 is_valid = ( id != NULL && strlen( id ) > 0 );
204 g_free( id );
207 return( is_valid );
211 * na_object_id_sort_alpha_asc:
212 * @a: first #NAObjectId.
213 * @b: second #NAObjectId.
215 * Sort the objects in alphabetical ascending order of their label.
217 * Returns:
219 * <itemizedlist>
220 * <listitem>
221 * <para>-1 if @a must be sorted before @b,</para>
222 * </listitem>
223 * <listitem>
224 * <para>0 if @a and @b are equal from the local point of view,</para>
225 * </listitem>
226 * <listitem>
227 * <para>1 if @a must be sorted after @b.</para>
228 * </listitem>
229 * </itemizedlist>
231 * Since: 2.30
233 gint
234 na_object_id_sort_alpha_asc( const NAObjectId *a, const NAObjectId *b )
236 gchar *label_a, *label_b;
237 gint compare;
239 label_a = na_object_get_label( a );
240 label_b = na_object_get_label( b );
242 compare = na_core_utils_str_collate( label_a, label_b );
244 g_free( label_b );
245 g_free( label_a );
247 return( compare );
251 * na_object_id_sort_alpha_desc:
252 * @a: first #NAObjectId.
253 * @b: second #NAObjectId.
255 * Sort the objects in alphabetical descending order of their label.
257 * Returns:
259 * <itemizedlist>
260 * <listitem>
261 * <para>-1 if @a must be sorted before @b,</para>
262 * </listitem>
263 * <listitem>
264 * <para>0 if @a and @b are equal from the local point of view,</para>
265 * </listitem>
266 * <listitem>
267 * <para>1 if @a must be sorted after @b.</para>
268 * </listitem>
269 * </itemizedlist>
271 * Since: 2.30
273 gint
274 na_object_id_sort_alpha_desc( const NAObjectId *a, const NAObjectId *b )
276 return( -1 * na_object_id_sort_alpha_asc( a, b ));
280 * na_object_id_prepare_for_paste:
281 * @object: the #NAObjectId object to be pasted.
282 * @relabel: whether this object should be relabeled when pasted.
283 * @renumber: whether this item should be renumbered ?
284 * @parent: the parent of @object, or %NULL.
286 * Prepares @object to be pasted.
288 * If a #NAObjectProfile, then @object is attached to the specified
289 * #NAObjectAction @action. The identifier is always renumbered to be
290 * suitable with the already existing profiles.
292 * If a #NAObjectAction or a #NAObjectMenu, a new identifier is allocated
293 * if and only if @relabel is %TRUE.
295 * Actual relabeling takes place if @relabel is %TRUE, depending of the
296 * user preferences.
298 * Since: 2.30
300 void
301 na_object_id_prepare_for_paste( NAObjectId *object, gboolean relabel, gboolean renumber, NAObjectId *parent )
303 static const gchar *thisfn = "na_object_id_prepare_for_paste";
304 GList *subitems, *it;
306 g_return_if_fail( NA_IS_OBJECT_ID( object ));
307 g_return_if_fail( !parent || NA_IS_OBJECT_ITEM( parent ));
309 if( !object->private->dispose_has_run ){
311 g_debug( "%s: object=%p, relabel=%s, renumber=%s, parent=%p",
312 thisfn, ( void * ) object, relabel ? "True":"False", renumber ? "True":"False", ( void * ) parent );
314 if( NA_IS_OBJECT_PROFILE( object )){
315 na_object_set_parent( object, parent );
316 na_object_set_new_id( object, parent );
317 if( renumber && relabel ){
318 na_object_set_copy_of_label( object );
321 } else {
322 if( renumber ){
323 na_object_set_new_id( object, NULL );
324 if( relabel ){
325 na_object_set_copy_of_label( object );
327 na_object_set_provider( object, NULL );
328 na_object_set_provider_data( object, NULL );
329 na_object_set_readonly( object, FALSE );
331 if( NA_IS_OBJECT_MENU( object )){
332 subitems = na_object_get_items( object );
333 for( it = subitems ; it ; it = it->next ){
334 na_object_prepare_for_paste( it->data, relabel, renumber, NULL );
342 * na_object_id_set_copy_of_label:
343 * @object: the #NAObjectId object whose label is to be changed.
345 * Sets the 'Copy of' label.
347 * Since: 2.30
349 void
350 na_object_id_set_copy_of_label( NAObjectId *object )
352 gchar *label, *new_label;
354 g_return_if_fail( NA_IS_OBJECT_ID( object ));
356 if( !object->private->dispose_has_run ){
358 label = na_object_get_label( object );
360 /* i18n: copied items have a label as 'Copy of original label' */
361 new_label = g_strdup_printf( _( "Copy of %s" ), label );
363 na_object_set_label( object, new_label );
365 g_free( new_label );
366 g_free( label );
371 * na_object_id_set_new_id:
372 * @object: the #NAObjectId object whose internal identifier is to be
373 * set.
374 * @new_parent: if @object is a #NAObjectProfile, then @new_parent
375 * should be set to the #NAObjectAction new parent. Else, it would not
376 * be possible to allocate a new profile id compatible with already
377 * existing ones.
379 * Request a new id to the derived class, and set it.
381 * Since: 2.30
383 void
384 na_object_id_set_new_id( NAObjectId *object, const NAObjectId *new_parent )
386 static const gchar *thisfn = "na_object_id_set_new_id";
387 gchar *id;
389 g_return_if_fail( NA_IS_OBJECT_ID( object ));
390 g_return_if_fail( !new_parent || NA_IS_OBJECT_ITEM( new_parent ));
392 if( !object->private->dispose_has_run ){
394 g_debug( "%s: object=%p (%s), new_parent=%p (%s)",
395 thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ),
396 ( void * ) new_parent, new_parent ? G_OBJECT_TYPE_NAME( new_parent ) : "n/a" );
398 id = v_new_id( object, new_parent );
400 if( id ){
401 na_object_set_id( object, id );
402 g_free( id );
407 static gchar *
408 v_new_id( const NAObjectId *object, const NAObjectId *new_parent )
410 gchar *new_id;
411 GList *hierarchy, *ih;
412 gboolean found;
414 found = FALSE;
415 new_id = NULL;
416 hierarchy = g_list_reverse( na_object_get_hierarchy( NA_OBJECT( object )));
417 /*g_debug( "na_object_id_most_derived_id: object=%p (%s)",
418 ( void * ) object, G_OBJECT_TYPE_NAME( object ));*/
420 for( ih = hierarchy ; ih && !found ; ih = ih->next ){
421 if( NA_OBJECT_ID_CLASS( ih->data )->new_id ){
422 new_id = NA_OBJECT_ID_CLASS( ih->data )->new_id( object, new_parent );
423 found = TRUE;
425 if( G_OBJECT_CLASS_TYPE( ih->data ) == NA_OBJECT_ID_TYPE ){
426 break;
430 na_object_free_hierarchy( hierarchy );
432 return( new_id );