Convert NASettings to a private singleton
[nautilus-actions.git] / src / nact / nact-export-ask.c
blobf450e21c3d71185c4bf11cd559a28ddd9247a946
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/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"
47 /* private class data
49 struct _NactExportAskClassPrivate {
50 void *empty; /* so that gcc -pedantic is happy */
53 /* private instance data
55 struct _NactExportAskPrivate {
56 gboolean dispose_has_run;
57 BaseWindow *parent;
58 gboolean preferences_locked;
59 NAObjectItem *item;
60 GQuark format;
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 );
85 GType
86 nact_export_ask_get_type( void )
88 static GType dialog_type = 0;
90 if( !dialog_type ){
91 dialog_type = register_type();
94 return( dialog_type );
97 static GType
98 register_type( void )
100 static const gchar *thisfn = "nact_export_ask_register_type";
101 GType type;
103 static GTypeInfo info = {
104 sizeof( NactExportAskClass ),
105 ( GBaseInitFunc ) NULL,
106 ( GBaseFinalizeFunc ) NULL,
107 ( GClassInitFunc ) class_init,
108 NULL,
109 NULL,
110 sizeof( NactExportAsk ),
112 ( GInstanceInitFunc ) instance_init
115 g_debug( "%s", thisfn );
117 type = g_type_register_static( BASE_DIALOG_TYPE, "NactExportAsk", &info, 0 );
119 return( type );
122 static void
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 );
139 static void
140 instance_init( GTypeInstance *instance, gpointer klass )
142 static const gchar *thisfn = "nact_export_ask_instance_init";
143 NactExportAsk *self;
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;
163 static void
164 instance_dispose( GObject *dialog )
166 static const gchar *thisfn = "nact_export_ask_instance_dispose";
167 NactExportAsk *self;
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 );
185 static void
186 instance_finalize( GObject *dialog )
188 static const gchar *thisfn = "nact_export_ask_instance_finalize";
189 NactExportAsk *self;
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
220 * the export mode.
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.
226 GQuark
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;
234 GQuark format = g_quark_from_static_string( NA_IPREFS_DEFAULT_EXPORT_FORMAT );
236 g_return_val_if_fail( BASE_IS_WINDOW( parent ), format );
238 g_debug( "%s: parent=%p, item=%p (%s), first=%s",
239 thisfn, ( void * ) parent, ( void * ) item, G_OBJECT_TYPE_NAME( item ), first ? "True":"False" );
241 format = na_iprefs_get_export_format( NA_IPREFS_EXPORT_ASK_USER_LAST_FORMAT, &mandatory );
242 keep = na_settings_get_boolean( NA_IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE, NULL, &keep_mandatory );
244 if( first || !keep ){
246 editor = g_object_new( NACT_EXPORT_ASK_TYPE,
247 BASE_PROP_PARENT, parent,
248 BASE_PROP_XMLUI_FILENAME, st_xmlui_filename,
249 BASE_PROP_TOPLEVEL_NAME, st_toplevel_name,
250 BASE_PROP_WSP_NAME, st_wsp_name,
251 NULL );
253 editor->private->format = format;
254 editor->private->format_mandatory = mandatory;
255 editor->private->keep_last_choice = keep;
256 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 if( base_window_run( BASE_WINDOW( editor )) == GTK_RESPONSE_OK ){
265 format = get_export_format( editor );
268 g_object_unref( editor );
271 return( format );
274 static void
275 on_base_initialize_gtk_toplevel( NactExportAsk *editor, GtkDialog *toplevel )
277 static const gchar *thisfn = "nact_export_ask_on_base_initialize_gtk_toplevel";
278 NactApplication *application;
279 NAUpdater *updater;
280 GtkWidget *container;
282 g_return_if_fail( NACT_IS_EXPORT_ASK( editor ));
284 if( !editor->private->dispose_has_run ){
285 g_debug( "%s: editor=%p, toplevel=%p", thisfn, ( void * ) editor, ( void * ) toplevel );
287 application = NACT_APPLICATION( base_window_get_application( BASE_WINDOW( editor )));
288 updater = nact_application_get_updater( application );
289 container = base_window_get_widget( BASE_WINDOW( editor ), "ExportFormatAskVBox" );
291 nact_export_format_init_display( container,
292 NA_PIVOT( updater ), EXPORT_FORMAT_DISPLAY_ASK, !editor->private->preferences_locked );
296 static void
297 on_base_initialize_base_window( NactExportAsk *editor )
299 static const gchar *thisfn = "nact_export_ask_on_base_initialize_base_window";
300 GtkWidget *container;
301 gchar *item_label, *label;
302 GtkWidget *widget;
304 g_return_if_fail( NACT_IS_EXPORT_ASK( editor ));
306 if( !editor->private->dispose_has_run ){
307 g_debug( "%s: editor=%p", thisfn, ( void * ) editor );
309 item_label = na_object_get_label( editor->private->item );
311 if( NA_IS_OBJECT_ACTION( editor->private->item )){
312 /* i18n: The action <label> is about to be exported */
313 label = g_strdup_printf( _( "The action \"%s\" is about to be exported." ), item_label );
314 } else {
315 /* i18n: The menu <label> is about to be exported */
316 label = g_strdup_printf( _( "The menu \"%s\" is about to be exported." ), item_label );
319 widget = base_window_get_widget( BASE_WINDOW( editor ), "ExportAskLabel1" );
320 gtk_label_set_text( GTK_LABEL( widget ), label );
321 g_free( label );
322 g_free( item_label );
324 container = base_window_get_widget( BASE_WINDOW( editor ), "ExportFormatAskVBox" );
325 nact_export_format_select( container, !editor->private->format_mandatory, editor->private->format );
327 base_gtk_utils_toggle_set_initial_state( BASE_WINDOW( editor ),
328 "AskKeepChoiceButton", G_CALLBACK( keep_choice_on_toggled ),
329 editor->private->keep_last_choice,
330 !editor->private->keep_last_choice_mandatory, !editor->private->preferences_locked );
332 base_window_signal_connect_by_name( BASE_WINDOW( editor ),
333 "CancelButton", "clicked", G_CALLBACK( on_cancel_clicked ));
335 base_window_signal_connect_by_name( BASE_WINDOW( editor ),
336 "OKButton", "clicked", G_CALLBACK( on_ok_clicked ));
340 static void
341 keep_choice_on_toggled( GtkToggleButton *button, NactExportAsk *editor )
343 gboolean editable;
345 editable = ( gboolean ) GPOINTER_TO_UINT( g_object_get_data( G_OBJECT( button ), NACT_PROP_TOGGLE_EDITABLE ));
347 if( editable ){
348 editor->private->keep_last_choice = gtk_toggle_button_get_active( button );
350 } else {
351 base_gtk_utils_toggle_reset_initial_state( button );
355 static void
356 on_cancel_clicked( GtkButton *button, NactExportAsk *editor )
358 GtkWindow *toplevel = base_window_get_gtk_toplevel( BASE_WINDOW( editor ));
359 gtk_dialog_response( GTK_DIALOG( toplevel ), GTK_RESPONSE_CLOSE );
362 static void
363 on_ok_clicked( GtkButton *button, NactExportAsk *editor )
365 GtkWindow *toplevel = base_window_get_gtk_toplevel( BASE_WINDOW( editor ));
366 gtk_dialog_response( GTK_DIALOG( toplevel ), GTK_RESPONSE_OK );
370 * we have come here because preferred_export_format was 'Ask'
371 * in all cases, the chosen format is kept in 'ask_user_last_chosen_format'
372 * and so this will be the default format which will be proposed the next
373 * time we come here
374 * if the 'remember_my_choice' is cheecked, then we do not even ask the user
375 * but returns as soon as we enter
377 * not overrinding the preferred export format (i.e. letting it to 'Ask')
378 * let the user go back in ask dialog box the next time he will have some
379 * files to export
381 static GQuark
382 get_export_format( NactExportAsk *editor )
384 GtkWidget *container;
385 NAExportFormat *format;
386 GQuark format_quark;
388 container = base_window_get_widget( BASE_WINDOW( editor ), "ExportFormatAskVBox" );
389 format = nact_export_format_get_selected( container );
390 format_quark = na_export_format_get_quark( format );
392 if( !editor->private->keep_last_choice_mandatory ){
393 na_settings_set_boolean( NA_IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE, editor->private->keep_last_choice );
396 na_iprefs_set_export_format( NA_IPREFS_EXPORT_ASK_USER_LAST_FORMAT, format_quark );
398 return( format_quark );