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.
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)
35 #include <glib/gi18n.h>
37 #include <api/na-core-utils.h>
38 #include <api/na-object-api.h>
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
);
65 na_object_id_get_type( void )
67 static GType item_type
= 0;
70 item_type
= register_type();
79 static const gchar
*thisfn
= "na_object_id_register_type";
82 static GTypeInfo info
= {
83 sizeof( NAObjectIdClass
),
86 ( GClassInitFunc
) class_init
,
91 ( GInstanceInitFunc
) instance_init
94 g_debug( "%s", thisfn
);
96 type
= g_type_register_static( NA_OBJECT_TYPE
, "NAObjectId", &info
, 0 );
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 );
126 instance_init( GTypeInstance
*instance
, gpointer klass
)
128 static const gchar
*thisfn
= "na_object_id_instance_init";
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
148 instance_dispose( GObject
*object
)
150 static const gchar
*thisfn
= "na_object_id_instance_dispose";
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" );
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
);
182 instance_finalize( GObject
*object
)
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
202 object_is_valid( const NAObject
*object
)
210 id
= na_object_get_id( object
);
211 is_valid
= ( id
!= NULL
&& strlen( id
) > 0 );
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.
229 * <para>-1 if @a must be sorted before @b,</para>
232 * <para>0 if @a and @b are equal from the local point of view,</para>
235 * <para>1 if @a must be sorted after @b.</para>
242 na_object_id_sort_alpha_asc( const NAObjectId
*a
, const NAObjectId
*b
)
244 gchar
*label_a
, *label_b
;
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
);
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.
269 * <para>-1 if @a must be sorted before @b,</para>
272 * <para>0 if @a and @b are equal from the local point of view,</para>
275 * <para>1 if @a must be sorted after @b.</para>
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
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
);
331 na_object_set_new_id( object
, NULL
);
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.
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
);
379 * na_object_id_set_new_id:
380 * @object: the #NAObjectId object whose internal identifiant is to be
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
387 * Request a new id to the derived class, and set it.
392 na_object_id_set_new_id( NAObjectId
*object
, const NAObjectId
*new_parent
)
394 static const gchar
*thisfn
= "na_object_id_set_new_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
);
409 na_object_set_id( object
, id
);
416 v_new_id( const NAObjectId
*object
, const NAObjectId
*new_parent
)
419 GList
*hierarchy
, *ih
;
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
);
433 if( G_OBJECT_CLASS_TYPE( ih
->data
) == NA_OBJECT_ID_TYPE
){
438 na_object_free_hierarchy( hierarchy
);