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-object-api.h>
39 #include <core/na-exporter.h>
40 #include <core/na-iprefs.h>
42 #include "nact-application.h"
43 #include "nact-export-format.h"
44 #include "nact-export-ask.h"
45 #include "base-gtk-utils.h"
49 struct _NactExportAskClassPrivate
{
50 void *empty
; /* so that gcc -pedantic is happy */
53 /* private instance data
55 struct _NactExportAskPrivate
{
56 gboolean dispose_has_run
;
58 gboolean preferences_locked
;
61 gboolean format_mandatory
;
62 gboolean keep_last_choice
;
63 gboolean keep_last_choice_mandatory
;
66 static const gchar
*st_xmlui_filename
= PKGDATADIR
"/nact-assistant-export.ui";
67 static const gchar
*st_toplevel_name
= "ExportAskDialog";
68 static const gchar
*st_wsp_name
= NA_IPREFS_EXPORT_ASK_USER_WSP
;
70 static BaseDialogClass
*st_parent_class
= NULL
;
72 static GType
register_type( void );
73 static void class_init( NactExportAskClass
*klass
);
74 static void instance_init( GTypeInstance
*instance
, gpointer klass
);
75 static void instance_dispose( GObject
*dialog
);
76 static void instance_finalize( GObject
*dialog
);
78 static void on_base_initialize_gtk_toplevel( NactExportAsk
*editor
, GtkDialog
*toplevel
);
79 static void on_base_initialize_base_window( NactExportAsk
*editor
);
80 static void keep_choice_on_toggled( GtkToggleButton
*button
, NactExportAsk
*editor
);
81 static void on_cancel_clicked( GtkButton
*button
, NactExportAsk
*editor
);
82 static void on_ok_clicked( GtkButton
*button
, NactExportAsk
*editor
);
83 static GQuark
get_export_format( NactExportAsk
*editor
);
86 nact_export_ask_get_type( void )
88 static GType dialog_type
= 0;
91 dialog_type
= register_type();
94 return( dialog_type
);
100 static const gchar
*thisfn
= "nact_export_ask_register_type";
103 static GTypeInfo info
= {
104 sizeof( NactExportAskClass
),
105 ( GBaseInitFunc
) NULL
,
106 ( GBaseFinalizeFunc
) NULL
,
107 ( GClassInitFunc
) class_init
,
110 sizeof( NactExportAsk
),
112 ( GInstanceInitFunc
) instance_init
115 g_debug( "%s", thisfn
);
117 type
= g_type_register_static( BASE_DIALOG_TYPE
, "NactExportAsk", &info
, 0 );
123 class_init( NactExportAskClass
*klass
)
125 static const gchar
*thisfn
= "nact_export_ask_class_init";
126 GObjectClass
*object_class
;
128 g_debug( "%s: klass=%p", thisfn
, ( void * ) klass
);
130 st_parent_class
= g_type_class_peek_parent( klass
);
132 object_class
= G_OBJECT_CLASS( klass
);
133 object_class
->dispose
= instance_dispose
;
134 object_class
->finalize
= instance_finalize
;
136 klass
->private = g_new0( NactExportAskClassPrivate
, 1 );
140 instance_init( GTypeInstance
*instance
, gpointer klass
)
142 static const gchar
*thisfn
= "nact_export_ask_instance_init";
145 g_return_if_fail( NACT_IS_EXPORT_ASK( instance
));
147 g_debug( "%s: instance=%p (%s), klass=%p",
148 thisfn
, ( void * ) instance
, G_OBJECT_TYPE_NAME( instance
), ( void * ) klass
);
150 self
= NACT_EXPORT_ASK( instance
);
152 self
->private = g_new0( NactExportAskPrivate
, 1 );
154 base_window_signal_connect( BASE_WINDOW( instance
),
155 G_OBJECT( instance
), BASE_SIGNAL_INITIALIZE_GTK
, G_CALLBACK( on_base_initialize_gtk_toplevel
));
157 base_window_signal_connect( BASE_WINDOW( instance
),
158 G_OBJECT( instance
), BASE_SIGNAL_INITIALIZE_WINDOW
, G_CALLBACK( on_base_initialize_base_window
));
160 self
->private->dispose_has_run
= FALSE
;
164 instance_dispose( GObject
*dialog
)
166 static const gchar
*thisfn
= "nact_export_ask_instance_dispose";
169 g_return_if_fail( NACT_IS_EXPORT_ASK( dialog
));
171 self
= NACT_EXPORT_ASK( dialog
);
173 if( !self
->private->dispose_has_run
){
174 g_debug( "%s: dialog=%p (%s)", thisfn
, ( void * ) dialog
, G_OBJECT_TYPE_NAME( dialog
));
176 self
->private->dispose_has_run
= TRUE
;
178 /* chain up to the parent class */
179 if( G_OBJECT_CLASS( st_parent_class
)->dispose
){
180 G_OBJECT_CLASS( st_parent_class
)->dispose( dialog
);
186 instance_finalize( GObject
*dialog
)
188 static const gchar
*thisfn
= "nact_export_ask_instance_finalize";
191 g_return_if_fail( NACT_IS_EXPORT_ASK( dialog
));
193 g_debug( "%s: dialog=%p (%s)", thisfn
, ( void * ) dialog
, G_OBJECT_TYPE_NAME( dialog
));
195 self
= NACT_EXPORT_ASK( dialog
);
197 g_free( self
->private );
199 /* chain call to parent class */
200 if( G_OBJECT_CLASS( st_parent_class
)->finalize
){
201 G_OBJECT_CLASS( st_parent_class
)->finalize( dialog
);
206 * nact_export_ask_user:
207 * @parent: the NactAssistant parent of this dialog.
208 * @item: the NAObjectItem to be exported.
209 * @first: whether this is the first call of a serie.
210 * On a first call, the user is really asked for his choice.
211 * The next times, the 'keep-last-choice' flag will be considered.
213 * Initializes and runs the dialog.
215 * This is a small dialog which is to be ran during export operations,
216 * when the set export format is 'Ask me'. Each exported file runs this
217 * dialog, unless the user selects the 'keep same choice' box.
219 * Returns: the mode chosen by the user as a #GQuark which identifies
221 * The function defaults to returning NA_IPREFS_DEFAULT_EXPORT_FORMAT.
223 * When the user selects 'Keep same choice without asking me', this choice
224 * becomes his new preferred export format.
227 nact_export_ask_user( BaseWindow
*parent
, NAObjectItem
*item
, gboolean first
)
229 static const gchar
*thisfn
= "nact_export_ask_user";
230 NactExportAsk
*editor
;
231 gboolean are_locked
, mandatory
;
232 gboolean keep
, keep_mandatory
;
235 GQuark format
= g_quark_from_static_string( NA_IPREFS_DEFAULT_EXPORT_FORMAT
);
237 g_return_val_if_fail( BASE_IS_WINDOW( parent
), format
);
239 g_debug( "%s: parent=%p, item=%p (%s), first=%s",
240 thisfn
, ( void * ) parent
, ( void * ) item
, G_OBJECT_TYPE_NAME( item
), first
? "True":"False" );
242 format
= na_iprefs_get_export_format( NA_IPREFS_EXPORT_ASK_USER_LAST_FORMAT
, &mandatory
);
243 keep
= na_settings_get_boolean( NA_IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE
, NULL
, &keep_mandatory
);
245 if( first
|| !keep
){
247 editor
= g_object_new( NACT_EXPORT_ASK_TYPE
,
248 BASE_PROP_PARENT
, parent
,
249 BASE_PROP_XMLUI_FILENAME
, st_xmlui_filename
,
250 BASE_PROP_TOPLEVEL_NAME
, st_toplevel_name
,
251 BASE_PROP_WSP_NAME
, st_wsp_name
,
254 editor
->private->format
= format
;
255 editor
->private->format_mandatory
= mandatory
;
256 editor
->private->keep_last_choice
= keep
;
257 editor
->private->keep_last_choice_mandatory
= keep_mandatory
;
258 editor
->private->parent
= parent
;
259 editor
->private->item
= item
;
261 are_locked
= na_settings_get_boolean( NA_IPREFS_ADMIN_PREFERENCES_LOCKED
, NULL
, &mandatory
);
262 editor
->private->preferences_locked
= are_locked
&& mandatory
;
264 code
= base_window_run( BASE_WINDOW( editor
));
266 case GTK_RESPONSE_OK
:
267 format
= get_export_format( editor
);
270 case GTK_RESPONSE_CANCEL
:
271 /* base_dialog::do_run only returns OK or CANCEL */
273 format
= IPREFS_EXPORT_NO_EXPORT
;
277 g_object_unref( editor
);
284 on_base_initialize_gtk_toplevel( NactExportAsk
*editor
, GtkDialog
*toplevel
)
286 static const gchar
*thisfn
= "nact_export_ask_on_base_initialize_gtk_toplevel";
287 NactApplication
*application
;
289 GtkWidget
*container
;
291 g_return_if_fail( NACT_IS_EXPORT_ASK( editor
));
293 if( !editor
->private->dispose_has_run
){
294 g_debug( "%s: editor=%p, toplevel=%p", thisfn
, ( void * ) editor
, ( void * ) toplevel
);
296 application
= NACT_APPLICATION( base_window_get_application( BASE_WINDOW( editor
)));
297 updater
= nact_application_get_updater( application
);
298 container
= base_window_get_widget( BASE_WINDOW( editor
), "ExportFormatAskVBox" );
300 nact_export_format_init_display( container
,
301 NA_PIVOT( updater
), EXPORT_FORMAT_DISPLAY_ASK
, !editor
->private->preferences_locked
);
306 on_base_initialize_base_window( NactExportAsk
*editor
)
308 static const gchar
*thisfn
= "nact_export_ask_on_base_initialize_base_window";
309 GtkWidget
*container
;
310 gchar
*item_label
, *label
;
313 g_return_if_fail( NACT_IS_EXPORT_ASK( editor
));
315 if( !editor
->private->dispose_has_run
){
316 g_debug( "%s: editor=%p", thisfn
, ( void * ) editor
);
318 item_label
= na_object_get_label( editor
->private->item
);
320 if( NA_IS_OBJECT_ACTION( editor
->private->item
)){
321 /* i18n: The action <label> is about to be exported */
322 label
= g_strdup_printf( _( "The action \"%s\" is about to be exported." ), item_label
);
324 /* i18n: The menu <label> is about to be exported */
325 label
= g_strdup_printf( _( "The menu \"%s\" is about to be exported." ), item_label
);
328 widget
= base_window_get_widget( BASE_WINDOW( editor
), "ExportAskLabel1" );
329 gtk_label_set_text( GTK_LABEL( widget
), label
);
331 g_free( item_label
);
333 container
= base_window_get_widget( BASE_WINDOW( editor
), "ExportFormatAskVBox" );
334 nact_export_format_select( container
, !editor
->private->format_mandatory
, editor
->private->format
);
336 base_gtk_utils_toggle_set_initial_state( BASE_WINDOW( editor
),
337 "AskKeepChoiceButton", G_CALLBACK( keep_choice_on_toggled
),
338 editor
->private->keep_last_choice
,
339 !editor
->private->keep_last_choice_mandatory
, !editor
->private->preferences_locked
);
341 base_window_signal_connect_by_name( BASE_WINDOW( editor
),
342 "CancelButton", "clicked", G_CALLBACK( on_cancel_clicked
));
344 base_window_signal_connect_by_name( BASE_WINDOW( editor
),
345 "OKButton", "clicked", G_CALLBACK( on_ok_clicked
));
350 keep_choice_on_toggled( GtkToggleButton
*button
, NactExportAsk
*editor
)
354 editable
= ( gboolean
) GPOINTER_TO_UINT( g_object_get_data( G_OBJECT( button
), NACT_PROP_TOGGLE_EDITABLE
));
357 editor
->private->keep_last_choice
= gtk_toggle_button_get_active( button
);
360 base_gtk_utils_toggle_reset_initial_state( button
);
365 on_cancel_clicked( GtkButton
*button
, NactExportAsk
*editor
)
367 GtkWindow
*toplevel
= base_window_get_gtk_toplevel( BASE_WINDOW( editor
));
368 gtk_dialog_response( GTK_DIALOG( toplevel
), GTK_RESPONSE_CLOSE
);
372 on_ok_clicked( GtkButton
*button
, NactExportAsk
*editor
)
374 GtkWindow
*toplevel
= base_window_get_gtk_toplevel( BASE_WINDOW( editor
));
375 gtk_dialog_response( GTK_DIALOG( toplevel
), GTK_RESPONSE_OK
);
379 * we have come here because preferred_export_format was 'Ask'
380 * in all cases, the chosen format is kept in 'ask_user_last_chosen_format'
381 * and so this will be the default format which will be proposed the next
383 * if the 'remember_my_choice' is cheecked, then we do not even ask the user
384 * but returns as soon as we enter
386 * not overrinding the preferred export format (i.e. letting it to 'Ask')
387 * let the user go back in ask dialog box the next time he will have some
391 get_export_format( NactExportAsk
*editor
)
393 GtkWidget
*container
;
394 NAExportFormat
*format
;
397 container
= base_window_get_widget( BASE_WINDOW( editor
), "ExportFormatAskVBox" );
398 format
= nact_export_format_get_selected( container
);
399 format_quark
= na_export_format_get_quark( format
);
401 if( !editor
->private->keep_last_choice_mandatory
){
402 na_settings_set_boolean( NA_IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE
, editor
->private->keep_last_choice
);
405 na_iprefs_set_export_format( NA_IPREFS_EXPORT_ASK_USER_LAST_FORMAT
, format_quark
);
407 return( format_quark
);