Set data provider to NULL when duplicating a desktop file item
[nautilus-actions.git] / src / core / na-object-id.c
blobf2fcff6bbdb5cdf482cef3d8beb3a5bf678f95ac
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 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->dump = NULL;
118 naobject_class->copy = NULL;
119 naobject_class->are_equal = NULL;
120 naobject_class->is_valid = object_is_valid;
122 klass->private = g_new0( NAObjectIdClassPrivate, 1 );
125 static void
126 instance_init( GTypeInstance *instance, gpointer klass )
128 static const gchar *thisfn = "na_object_id_instance_init";
129 NAObjectId *self;
131 g_return_if_fail( NA_IS_OBJECT_ID( instance ));
133 self = NA_OBJECT_ID( instance );
135 g_debug( "%s: instance=%p (%s), klass=%p",
136 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
138 self->private = g_new0( NAObjectIdPrivate, 1 );
142 * note that when the tree store is cleared, Gtk begins with the deepest
143 * levels, so that children are disposed before their parent
144 * as we try to dispose all children when disposing a parent, we have to
145 * remove a disposing child from its parent
147 static void
148 instance_dispose( GObject *object )
150 static const gchar *thisfn = "na_object_id_instance_dispose";
151 NAObjectId *self;
152 NAObjectItem *parent;
154 g_return_if_fail( NA_IS_OBJECT_ID( object ));
156 self = NA_OBJECT_ID( object );
158 if( !self->private->dispose_has_run ){
160 g_debug( "%s: object=%p (%s)", thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ));
162 self->private->dispose_has_run = TRUE;
164 parent = na_object_get_parent( object );
165 g_debug( "%s: parent=%p (%s)",
166 thisfn, ( void * ) parent, parent ? G_OBJECT_TYPE_NAME( parent ) : "n/a" );
167 if( parent ){
168 na_object_remove_item( parent, object );
169 na_object_set_parent( object, NULL );
172 self->private->dispose_has_run = TRUE;
174 /* chain up to the parent class */
175 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
176 G_OBJECT_CLASS( st_parent_class )->dispose( object );
181 static void
182 instance_finalize( GObject *object )
184 NAObjectId *self;
186 g_return_if_fail( NA_IS_OBJECT_ID( object ));
188 self = NA_OBJECT_ID( object );
190 g_free( self->private );
192 /* chain call to parent class */
193 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
194 G_OBJECT_CLASS( st_parent_class )->finalize( object );
199 * a NAObjectId is valid if it has a non-null id
201 static gboolean
202 object_is_valid( const NAObject *object )
204 gboolean is_valid;
205 gchar *id;
207 is_valid = TRUE;
209 if( is_valid ){
210 id = na_object_get_id( object );
211 is_valid = ( id != NULL && strlen( id ) > 0 );
212 g_free( id );
215 return( is_valid );
219 * na_object_id_sort_alpha_asc:
220 * @a: first #NAObjectId.
221 * @b: second #NAObjectId.
223 * Sort the objects in alphabetical ascending order of their label.
225 * Returns:
227 * <itemizedlist>
228 * <listitem>
229 * <para>-1 if @a must be sorted before @b,</para>
230 * </listitem>
231 * <listitem>
232 * <para>0 if @a and @b are equal from the local point of view,</para>
233 * </listitem>
234 * <listitem>
235 * <para>1 if @a must be sorted after @b.</para>
236 * </listitem>
237 * </itemizedlist>
239 * Since: 2.30
241 gint
242 na_object_id_sort_alpha_asc( const NAObjectId *a, const NAObjectId *b )
244 gchar *label_a, *label_b;
245 gint compare;
247 label_a = na_object_get_label( a );
248 label_b = na_object_get_label( b );
250 compare = na_core_utils_str_collate( label_a, label_b );
252 g_free( label_b );
253 g_free( label_a );
255 return( compare );
259 * na_object_id_sort_alpha_desc:
260 * @a: first #NAObjectId.
261 * @b: second #NAObjectId.
263 * Sort the objects in alphabetical descending order of their label.
265 * Returns:
267 * <itemizedlist>
268 * <listitem>
269 * <para>-1 if @a must be sorted before @b,</para>
270 * </listitem>
271 * <listitem>
272 * <para>0 if @a and @b are equal from the local point of view,</para>
273 * </listitem>
274 * <listitem>
275 * <para>1 if @a must be sorted after @b.</para>
276 * </listitem>
277 * </itemizedlist>
279 * Since: 2.30
281 gint
282 na_object_id_sort_alpha_desc( const NAObjectId *a, const NAObjectId *b )
284 return( -1 * na_object_id_sort_alpha_asc( a, b ));
288 * na_object_id_prepare_for_paste:
289 * @object: the #NAObjectId object to be pasted.
290 * @relabel: whether this object should be relabeled when pasted.
291 * @renumber: whether this item should be renumbered ?
292 * @parent: the parent of @object, or %NULL.
294 * Prepares @object to be pasted.
296 * If a #NAObjectProfile, then @object is attached to the specified
297 * #NAObjectAction @action. The identifier is always renumbered to be
298 * suitable with the already existing profiles.
300 * If a #NAObjectAction or a #NAObjectMenu, a new UUID is allocated if
301 * and only if @relabel is %TRUE.
303 * Actual relabeling takes place if @relabel is %TRUE, depending of the
304 * user preferences.
306 * Since: 2.30
308 void
309 na_object_id_prepare_for_paste( NAObjectId *object, gboolean relabel, gboolean renumber, NAObjectId *parent )
311 static const gchar *thisfn = "na_object_id_prepare_for_paste";
312 GList *subitems, *it;
314 g_return_if_fail( NA_IS_OBJECT_ID( object ));
315 g_return_if_fail( !parent || NA_IS_OBJECT_ITEM( parent ));
317 if( !object->private->dispose_has_run ){
319 g_debug( "%s: object=%p, relabel=%s, renumber=%s, parent=%p",
320 thisfn, ( void * ) object, relabel ? "True":"False", renumber ? "True":"False", ( void * ) parent );
322 if( NA_IS_OBJECT_PROFILE( object )){
323 na_object_set_parent( object, parent );
324 na_object_set_new_id( object, parent );
325 if( renumber && relabel ){
326 na_object_set_copy_of_label( object );
329 } else {
330 if( renumber ){
331 na_object_set_new_id( object, NULL );
332 if( relabel ){
333 na_object_set_copy_of_label( object );
335 na_object_set_provider( object, NULL );
336 na_object_set_provider_data( object, NULL );
337 na_object_set_readonly( object, FALSE );
339 if( NA_IS_OBJECT_MENU( object )){
340 subitems = na_object_get_items( object );
341 for( it = subitems ; it ; it = it->next ){
342 na_object_prepare_for_paste( it->data, relabel, renumber, NULL );
350 * na_object_id_set_copy_of_label:
351 * @object: the #NAObjectId object whose label is to be changed.
353 * Sets the 'Copy of' label.
355 * Since: 2.30
357 void
358 na_object_id_set_copy_of_label( NAObjectId *object )
360 gchar *label, *new_label;
362 g_return_if_fail( NA_IS_OBJECT_ID( object ));
364 if( !object->private->dispose_has_run ){
366 label = na_object_get_label( object );
368 /* i18n: copied items have a label as 'Copy of original label' */
369 new_label = g_strdup_printf( _( "Copy of %s" ), label );
371 na_object_set_label( object, new_label );
373 g_free( new_label );
374 g_free( label );
379 * na_object_id_set_new_id:
380 * @object: the #NAObjectId object whose internal identifiant is to be
381 * set.
382 * @new_parent: if @object is a #NAObjectProfile, then @new_parent
383 * should be set to the #NAObjectAction new parent. Else, it would not
384 * be possible to allocate a new profile id compatible with already
385 * existing ones.
387 * Request a new id to the derived class, and set it.
389 * Since: 2.30
391 void
392 na_object_id_set_new_id( NAObjectId *object, const NAObjectId *new_parent )
394 static const gchar *thisfn = "na_object_id_set_new_id";
395 gchar *id;
397 g_return_if_fail( NA_IS_OBJECT_ID( object ));
398 g_return_if_fail( !new_parent || NA_IS_OBJECT_ITEM( new_parent ));
400 if( !object->private->dispose_has_run ){
402 g_debug( "%s: object=%p (%s), new_parent=%p (%s)",
403 thisfn, ( void * ) object, G_OBJECT_TYPE_NAME( object ),
404 ( void * ) new_parent, new_parent ? G_OBJECT_TYPE_NAME( new_parent ) : "n/a" );
406 id = v_new_id( object, new_parent );
408 if( id ){
409 na_object_set_id( object, id );
410 g_free( id );
415 static gchar *
416 v_new_id( const NAObjectId *object, const NAObjectId *new_parent )
418 gchar *new_id;
419 GList *hierarchy, *ih;
420 gboolean found;
422 found = FALSE;
423 new_id = NULL;
424 hierarchy = g_list_reverse( na_object_get_hierarchy( NA_OBJECT( object )));
425 /*g_debug( "na_object_id_most_derived_id: object=%p (%s)",
426 ( void * ) object, G_OBJECT_TYPE_NAME( object ));*/
428 for( ih = hierarchy ; ih && !found ; ih = ih->next ){
429 if( NA_OBJECT_ID_CLASS( ih->data )->new_id ){
430 new_id = NA_OBJECT_ID_CLASS( ih->data )->new_id( object, new_parent );
431 found = TRUE;
433 if( G_OBJECT_CLASS_TYPE( ih->data ) == NA_OBJECT_ID_TYPE ){
434 break;
438 na_object_free_hierarchy( hierarchy );
440 return( new_id );