Update copyright message
[nautilus-actions.git] / src / nact / nact-window.c
blob3e4cf102db2f9355c0c7bba9c1d4477a35d79dd2
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.h>
36 #include <glib/gi18n.h>
38 #include <api/na-core-utils.h>
39 #include <api/na-iio-provider.h>
40 #include <api/na-object-api.h>
42 #include <core/na-io-provider.h>
43 #include <core/na-iprefs.h>
45 #include "nact-application.h"
46 #include "nact-window.h"
48 /* private class data
50 struct NactWindowClassPrivate {
51 void *empty; /* so that gcc -pedantic is happy */
54 /* private instance data
56 struct NactWindowPrivate {
57 gboolean dispose_has_run;
60 static BaseWindowClass *st_parent_class = NULL;
62 static GType register_type( void );
63 static void class_init( NactWindowClass *klass );
64 static void instance_init( GTypeInstance *instance, gpointer klass );
65 static void instance_dispose( GObject *application );
66 static void instance_finalize( GObject *application );
68 GType
69 nact_window_get_type( void )
71 static GType window_type = 0;
73 if( !window_type ){
74 window_type = register_type();
77 return( window_type );
80 static GType
81 register_type( void )
83 static const gchar *thisfn = "nact_window_register_type";
84 GType type;
86 static GTypeInfo info = {
87 sizeof( NactWindowClass ),
88 ( GBaseInitFunc ) NULL,
89 ( GBaseFinalizeFunc ) NULL,
90 ( GClassInitFunc ) class_init,
91 NULL,
92 NULL,
93 sizeof( NactWindow ),
95 ( GInstanceInitFunc ) instance_init
98 g_debug( "%s", thisfn );
100 type = g_type_register_static( BASE_WINDOW_TYPE, "NactWindow", &info, 0 );
102 return( type );
105 static void
106 class_init( NactWindowClass *klass )
108 static const gchar *thisfn = "nact_window_class_init";
109 GObjectClass *object_class;
111 g_debug( "%s: klass=%p", thisfn, ( void * ) klass );
113 st_parent_class = g_type_class_peek_parent( klass );
115 object_class = G_OBJECT_CLASS( klass );
116 object_class->dispose = instance_dispose;
117 object_class->finalize = instance_finalize;
119 klass->private = g_new0( NactWindowClassPrivate, 1 );
122 static void
123 instance_init( GTypeInstance *instance, gpointer klass )
125 static const gchar *thisfn = "nact_window_instance_init";
126 NactWindow *self;
128 g_debug( "%s: instance=%p (%s), klass=%p",
129 thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );
130 g_return_if_fail( NACT_IS_WINDOW( instance ));
131 self = NACT_WINDOW( instance );
133 self->private = g_new0( NactWindowPrivate, 1 );
135 self->private->dispose_has_run = FALSE;
138 static void
139 instance_dispose( GObject *window )
141 static const gchar *thisfn = "nact_window_instance_dispose";
142 NactWindow *self;
144 g_debug( "%s: window=%p (%s)", thisfn, ( void * ) window, G_OBJECT_TYPE_NAME( window ));
145 g_return_if_fail( NACT_IS_WINDOW( window ));
146 self = NACT_WINDOW( window );
148 if( !self->private->dispose_has_run ){
150 self->private->dispose_has_run = TRUE;
152 /* chain up to the parent class */
153 if( G_OBJECT_CLASS( st_parent_class )->dispose ){
154 G_OBJECT_CLASS( st_parent_class )->dispose( window );
159 static void
160 instance_finalize( GObject *window )
162 static const gchar *thisfn = "nact_window_instance_finalize";
163 NactWindow *self;
165 g_debug( "%s: window=%p", thisfn, ( void * ) window );
166 g_return_if_fail( NACT_IS_WINDOW( window ));
167 self = NACT_WINDOW( window );
169 g_free( self->private );
171 /* chain call to parent class */
172 if( G_OBJECT_CLASS( st_parent_class )->finalize ){
173 G_OBJECT_CLASS( st_parent_class )->finalize( window );
178 * nact_window_has_writable_providers:
179 * @window: this #NactWindow instance.
181 * Returns: %TRUE if at least one I/O provider is writable, %FALSE else.
183 gboolean
184 nact_window_has_writable_providers( NactWindow *window )
186 gboolean has_writables;
187 NactApplication *application;
188 NAUpdater *updater;
189 NAIOProvider *provider;
191 has_writables = FALSE;
193 g_return_val_if_fail( NACT_IS_WINDOW( window ), has_writables );
195 if( !window->private->dispose_has_run ){
197 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
198 updater = nact_application_get_updater( application );
199 provider = na_io_provider_get_writable_provider( NA_PIVOT( updater ));
201 if( provider ){
202 has_writables = TRUE;
206 return( has_writables );
210 * nact_window_is_item_writable:
211 * @window: this #NactWindow object.
212 * @item: the #NAObjectItem to be evaluated.
213 * @reason: the reason for what @item may not be writable.
215 * Returns: %TRUE: if @item is actually writable, given the current
216 * status of its provider, %FALSE else.
218 * For an item be actually writable:
219 * - the item must not be itself in a read-only store, which has been
220 * checked when first reading it
221 * - the provider must be willing (resp. able) to write
222 * - the provider must not has been locked by the admin
223 * - the writability of the provider must not have been removed by the user
224 * - the whole configuration must not have been locked by the admin.
226 gboolean
227 nact_window_is_item_writable( const NactWindow *window, const NAObjectItem *item, guint *reason )
229 gboolean writable;
230 NactApplication *application;
231 NAUpdater *updater;
232 NAIOProvider *provider;
234 g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), FALSE );
236 writable = FALSE;
237 if( reason ){
238 *reason = NA_IIO_PROVIDER_STATUS_UNDETERMINED;
241 if( !window->private->dispose_has_run ){
243 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
244 updater = nact_application_get_updater( application );
246 writable = TRUE;
247 if( reason ){
248 *reason = NA_IIO_PROVIDER_STATUS_WRITABLE;
251 if( writable ){
252 if( na_object_is_readonly( item )){
253 writable = FALSE;
254 if( reason ){
255 *reason = NA_IIO_PROVIDER_STATUS_ITEM_READONLY;
260 if( writable ){
261 provider = na_object_get_provider( item );
262 if( provider ){
263 if( !na_io_provider_is_willing_to_write( provider )){
264 writable = FALSE;
265 if( reason ){
266 *reason = NA_IIO_PROVIDER_STATUS_PROVIDER_NOT_WILLING_TO;
268 } else if( na_io_provider_is_locked_by_admin( provider, NA_IPREFS( updater ))){
269 writable = FALSE;
270 if( reason ){
271 *reason = NA_IIO_PROVIDER_STATUS_PROVIDER_LOCKED_BY_ADMIN;
273 } else if( !na_io_provider_is_user_writable( provider, NA_IPREFS( updater ))){
274 writable = FALSE;
275 if( reason ){
276 *reason = NA_IIO_PROVIDER_STATUS_PROVIDER_LOCKED_BY_USER;
278 } else if( na_pivot_is_configuration_locked_by_admin( NA_PIVOT( updater ))){
279 writable = FALSE;
280 if( reason ){
281 *reason = NA_IIO_PROVIDER_STATUS_CONFIGURATION_LOCKED_BY_ADMIN;
283 } else if( !na_io_provider_has_write_api( provider )){
284 writable = FALSE;
285 if( reason ){
286 *reason = NA_IIO_PROVIDER_STATUS_NO_API;
290 /* the get_writable_provider() api already takes above checks
292 } else {
293 provider = na_io_provider_get_writable_provider( NA_PIVOT( updater ));
294 if( !provider ){
295 writable = FALSE;
296 if( reason ){
297 *reason = NA_IIO_PROVIDER_STATUS_NO_PROVIDER_FOUND;
304 return( writable );
308 * nact_window_save_item:
309 * @window: this #NactWindow instance.
310 * @item: the #NAObjectItem to be saved.
311 * @msg: a pointer to a location where wi may allocate a new error message.
313 * Saves a modified item (action or menu) to the I/O storage subsystem.
315 * An action is always written at once, with all its profiles.
317 * Writing a menu only involves writing its NAObjectItem properties,
318 * along with the list and the order of its subitems, but not the
319 * subitems themselves (because they may be unmodified).
321 * 2010-12-13 pwi v 3.0.3
322 * As we are displaying a summary of errors from the calling function,
323 * we no more care here of displaying each individual error message.
325 gboolean
326 nact_window_save_item( NactWindow *window, NAObjectItem *item, gchar **msg )
328 static const gchar *thisfn = "nact_window_save_item";
329 gboolean save_ok = FALSE;
330 NactApplication *application;
331 NAUpdater *updater;
332 GSList *messages = NULL;
333 guint ret;
335 g_debug( "%s: window=%p, item=%p (%s)", thisfn,
336 ( void * ) window, ( void * ) item, G_OBJECT_TYPE_NAME( item ));
337 g_return_val_if_fail( NACT_IS_WINDOW( window ), FALSE );
338 g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), FALSE );
340 if( !window->private->dispose_has_run ){
342 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
343 updater = nact_application_get_updater( application );
345 ret = na_updater_write_item( updater, item, &messages );
346 g_debug( "nact_window_save_item: ret=%d", ret );
348 if( messages ){
349 *msg = na_core_utils_slist_join_at_end( messages, "\n" );
350 na_core_utils_slist_free( messages );
352 } else if( ret != NA_IIO_PROVIDER_CODE_OK ){
353 *msg = na_io_provider_get_return_code_label( ret );
356 save_ok = ( ret == NA_IIO_PROVIDER_CODE_OK );
359 return( save_ok );
363 * nact_window_delete_item:
364 * @window: this #NactWindow object.
365 * @item: the item (action or menu) to delete.
367 * Deleted an item from the I/O storage subsystem.
369 * 2010-12-13 pwi v 3.0.3
370 * As we are displaying a summary of errors from the calling function,
371 * we no more care here of displaying each individual error message.
373 gboolean
374 nact_window_delete_item( NactWindow *window, const NAObjectItem *item, gchar **msg )
376 static const gchar *thisfn = "nact_window_delete_item";
377 gboolean delete_ok = FALSE;
378 NactApplication *application;
379 NAUpdater *updater;
380 GSList *messages = NULL;
381 guint ret;
383 g_debug( "%s: window=%p, item=%p (%s)", thisfn,
384 ( void * ) window, ( void * ) item, G_OBJECT_TYPE_NAME( item ));
385 g_return_val_if_fail( NACT_IS_WINDOW( window ), FALSE );
386 g_return_val_if_fail( NA_IS_OBJECT_ITEM( item ), FALSE );
388 if( !window->private->dispose_has_run ){
390 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
391 updater = nact_application_get_updater( application );
393 na_object_dump_norec( item );
395 ret = na_updater_delete_item( updater, item, &messages );
397 if( messages ){
398 *msg = na_core_utils_slist_join_at_end( messages, "\n" );
399 na_core_utils_slist_free( messages );
401 } else if( ret != NA_IIO_PROVIDER_CODE_OK ){
402 *msg = na_io_provider_get_return_code_label( ret );
405 delete_ok = ( ret == NA_IIO_PROVIDER_CODE_OK );
408 return( delete_ok );
412 * nact_window_count_level_zero_items:
414 void
415 nact_window_count_level_zero_items( GList *items, guint *actions, guint *profiles, guint *menus )
417 GList *it;
419 g_return_if_fail( actions );
420 g_return_if_fail( profiles );
421 g_return_if_fail( menus );
423 *actions = 0;
424 *profiles = 0;
425 *menus = 0;
427 for( it = items ; it ; it = it->next ){
428 if( NA_IS_OBJECT_ACTION( it->data )){
429 *actions += 1;
430 } else if( NA_IS_OBJECT_PROFILE( it->data )){
431 *profiles += 1;
432 } else if( NA_IS_OBJECT_MENU( it->data )){
433 *menus += 1;
439 * nact_window_warn_modified:
440 * @window: this #NactWindow instance.
442 * Emits a warning if the action has been modified.
444 * Returns: %TRUE if the user confirms he wants to quit.
446 gboolean
447 nact_window_warn_modified( NactWindow *window )
449 gboolean confirm = FALSE;
450 gchar *first;
451 gchar *second;
453 g_return_val_if_fail( NACT_IS_WINDOW( window ), FALSE );
455 if( !window->private->dispose_has_run ){
457 first = g_strdup_printf( _( "Some items have been modified." ));
458 second = g_strdup( _( "Are you sure you want to quit without saving them ?" ));
460 confirm = base_window_yesno_dlg( BASE_WINDOW( window ), GTK_MESSAGE_QUESTION, first, second );
462 g_free( second );
463 g_free( first );
466 return( confirm );