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.
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
)
130 g_return_if_fail( NA_IS_OBJECT_ID( instance
));
132 self
= NA_OBJECT_ID( instance
);
134 self
->private = g_new0( NAObjectIdPrivate
, 1 );
138 * note that when the tree store is cleared, Gtk begins with the deepest
139 * levels, so that children are disposed before their parent
140 * as we try to dispose all children when disposing a parent, we have to
141 * remove a disposing child from its parent
144 instance_dispose( GObject
*object
)
146 static const gchar
*thisfn
= "na_object_id_instance_dispose";
148 NAObjectItem
*parent
;
150 g_return_if_fail( NA_IS_OBJECT_ID( object
));
152 self
= NA_OBJECT_ID( object
);
154 if( !self
->private->dispose_has_run
){
156 self
->private->dispose_has_run
= TRUE
;
158 parent
= na_object_get_parent( object
);
159 g_debug( "%s: parent=%p (%s)",
160 thisfn
, ( void * ) parent
, parent
? G_OBJECT_TYPE_NAME( parent
) : "n/a" );
162 na_object_remove_item( parent
, object
);
163 na_object_set_parent( object
, NULL
);
166 self
->private->dispose_has_run
= TRUE
;
168 /* chain up to the parent class */
169 if( G_OBJECT_CLASS( st_parent_class
)->dispose
){
170 G_OBJECT_CLASS( st_parent_class
)->dispose( object
);
176 instance_finalize( GObject
*object
)
180 g_return_if_fail( NA_IS_OBJECT_ID( object
));
182 self
= NA_OBJECT_ID( object
);
184 g_free( self
->private );
186 /* chain call to parent class */
187 if( G_OBJECT_CLASS( st_parent_class
)->finalize
){
188 G_OBJECT_CLASS( st_parent_class
)->finalize( object
);
193 * a NAObjectId is valid if it has a non-null id
196 object_is_valid( const NAObject
*object
)
204 id
= na_object_get_id( object
);
205 is_valid
= ( id
!= NULL
&& strlen( id
) > 0 );
213 * na_object_id_sort_alpha_asc:
214 * @a: first #NAObjectId.
215 * @b: second #NAObjectId.
217 * Sort the objects in alphabetical ascending order of their label.
223 * <para>-1 if @a must be sorted before @b,</para>
226 * <para>0 if @a and @b are equal from the local point of view,</para>
229 * <para>1 if @a must be sorted after @b.</para>
236 na_object_id_sort_alpha_asc( const NAObjectId
*a
, const NAObjectId
*b
)
238 gchar
*label_a
, *label_b
;
241 label_a
= na_object_get_label( a
);
242 label_b
= na_object_get_label( b
);
244 compare
= na_core_utils_str_collate( label_a
, label_b
);
253 * na_object_id_sort_alpha_desc:
254 * @a: first #NAObjectId.
255 * @b: second #NAObjectId.
257 * Sort the objects in alphabetical descending order of their label.
263 * <para>-1 if @a must be sorted before @b,</para>
266 * <para>0 if @a and @b are equal from the local point of view,</para>
269 * <para>1 if @a must be sorted after @b.</para>
276 na_object_id_sort_alpha_desc( const NAObjectId
*a
, const NAObjectId
*b
)
278 return( -1 * na_object_id_sort_alpha_asc( a
, b
));
282 * na_object_id_prepare_for_paste:
283 * @object: the #NAObjectId object to be pasted.
284 * @relabel: whether this object should be relabeled when pasted.
285 * @renumber: whether this item should be renumbered ?
286 * @parent: the parent of @object, or %NULL.
288 * Prepares @object to be pasted.
290 * If a #NAObjectProfile, then @object is attached to the specified
291 * #NAObjectAction @action. The identifier is always renumbered to be
292 * suitable with the already existing profiles.
294 * If a #NAObjectAction or a #NAObjectMenu, a new identifier is allocated
295 * if and only if @relabel is %TRUE.
297 * Actual relabeling takes place if @relabel is %TRUE, depending of the
303 na_object_id_prepare_for_paste( NAObjectId
*object
, gboolean relabel
, gboolean renumber
, NAObjectId
*parent
)
305 static const gchar
*thisfn
= "na_object_id_prepare_for_paste";
306 GList
*subitems
, *it
;
308 g_return_if_fail( NA_IS_OBJECT_ID( object
));
309 g_return_if_fail( !parent
|| NA_IS_OBJECT_ITEM( parent
));
311 if( !object
->private->dispose_has_run
){
313 g_debug( "%s: object=%p, relabel=%s, renumber=%s, parent=%p",
314 thisfn
, ( void * ) object
, relabel
? "True":"False", renumber
? "True":"False", ( void * ) parent
);
316 if( NA_IS_OBJECT_PROFILE( object
)){
317 na_object_set_parent( object
, parent
);
318 na_object_set_new_id( object
, parent
);
319 if( renumber
&& relabel
){
320 na_object_set_copy_of_label( object
);
325 na_object_set_new_id( object
, NULL
);
327 na_object_set_copy_of_label( object
);
329 na_object_set_provider( object
, NULL
);
330 na_object_set_provider_data( object
, NULL
);
331 na_object_set_readonly( object
, FALSE
);
333 if( NA_IS_OBJECT_MENU( object
)){
334 subitems
= na_object_get_items( object
);
335 for( it
= subitems
; it
; it
= it
->next
){
336 na_object_prepare_for_paste( it
->data
, relabel
, renumber
, NULL
);
344 * na_object_id_set_copy_of_label:
345 * @object: the #NAObjectId object whose label is to be changed.
347 * Sets the 'Copy of' label.
352 na_object_id_set_copy_of_label( NAObjectId
*object
)
354 gchar
*label
, *new_label
;
356 g_return_if_fail( NA_IS_OBJECT_ID( object
));
358 if( !object
->private->dispose_has_run
){
360 label
= na_object_get_label( object
);
362 /* i18n: copied items have a label as 'Copy of original label' */
363 new_label
= g_strdup_printf( _( "Copy of %s" ), label
);
365 na_object_set_label( object
, new_label
);
373 * na_object_id_set_new_id:
374 * @object: the #NAObjectId object whose internal identifier is to be
376 * @new_parent: if @object is a #NAObjectProfile, then @new_parent
377 * should be set to the #NAObjectAction new parent. Else, it would not
378 * be possible to allocate a new profile id compatible with already
381 * Request a new id to the derived class, and set it.
386 na_object_id_set_new_id( NAObjectId
*object
, const NAObjectId
*new_parent
)
388 static const gchar
*thisfn
= "na_object_id_set_new_id";
391 g_return_if_fail( NA_IS_OBJECT_ID( object
));
392 g_return_if_fail( !new_parent
|| NA_IS_OBJECT_ITEM( new_parent
));
394 if( !object
->private->dispose_has_run
){
396 g_debug( "%s: object=%p (%s), new_parent=%p (%s)",
397 thisfn
, ( void * ) object
, G_OBJECT_TYPE_NAME( object
),
398 ( void * ) new_parent
, new_parent
? G_OBJECT_TYPE_NAME( new_parent
) : "n/a" );
400 id
= v_new_id( object
, new_parent
);
403 na_object_set_id( object
, id
);
410 v_new_id( const NAObjectId
*object
, const NAObjectId
*new_parent
)
413 GList
*hierarchy
, *ih
;
418 hierarchy
= g_list_reverse( na_object_get_hierarchy( NA_OBJECT( object
)));
419 /*g_debug( "na_object_id_most_derived_id: object=%p (%s)",
420 ( void * ) object, G_OBJECT_TYPE_NAME( object ));*/
422 for( ih
= hierarchy
; ih
&& !found
; ih
= ih
->next
){
423 if( NA_OBJECT_ID_CLASS( ih
->data
)->new_id
){
424 new_id
= NA_OBJECT_ID_CLASS( ih
->data
)->new_id( object
, new_parent
);
427 if( G_OBJECT_CLASS_TYPE( ih
->data
) == NA_OBJECT_ID_TYPE
){
432 na_object_free_hierarchy( hierarchy
);