File: fma-export-ask.c

package info (click to toggle)
filemanager-actions 3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,428 kB
  • sloc: ansic: 48,857; xml: 17,767; sh: 4,662; makefile: 1,375; lisp: 9
file content (485 lines) | stat: -rw-r--r-- 14,764 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * FileManager-Actions
 * A file-manager extension which offers configurable context menu actions.
 *
 * Copyright (C) 2005 The GNOME Foundation
 * Copyright (C) 2006-2008 Frederic Ruaudel and others (see AUTHORS)
 * Copyright (C) 2009-2015 Pierre Wieser and others (see AUTHORS)
 *
 * FileManager-Actions is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * FileManager-Actions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FileManager-Actions; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Frederic Ruaudel <grumz@grumz.net>
 *   Rodrigo Moya <rodrigo@gnome-db.org>
 *   Pierre Wieser <pwieser@trychlos.org>
 *   ... and many others (see AUTHORS)
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <glib/gi18n.h>

#include "api/fma-object-api.h"

#include "core/fma-exporter.h"
#include "core/fma-export-format.h"
#include "core/fma-gtk-utils.h"
#include "core/fma-ioptions-list.h"

#include "base-gtk-utils.h"
#include "fma-application.h"
#include "fma-export-ask.h"
#include "fma-main-window.h"

/* private instance data
 */
struct _FMAExportAskPrivate {
	gboolean       dispose_has_run;
	gboolean       preferences_locked;
	FMAObjectItem *item;
	gchar         *format;
	gboolean       format_mandatory;
	gboolean       keep_last_choice;
	gboolean       keep_last_choice_mandatory;
};

static const gchar     *st_xmlui_filename = PKGUIDIR "/fma-export-ask.ui";
static const gchar     *st_toplevel_name  = "ExportAskDialog";
static const gchar     *st_wsp_name       = IPREFS_EXPORT_ASK_USER_WSP;

static BaseDialogClass *st_parent_class   = NULL;

static GType    register_type( void );
static void     class_init( FMAExportAskClass *klass );
static void     ioptions_list_iface_init( FMAIOptionsListInterface *iface, void *user_data );
static GList   *ioptions_list_get_formats( const FMAIOptionsList *instance, GtkWidget *container );
static void     ioptions_list_free_formats( const FMAIOptionsList *instance, GtkWidget *container, GList *formats );
static void     instance_init( GTypeInstance *instance, gpointer klass );
static void     instance_constructed( GObject *dialog );
static void     instance_dispose( GObject *dialog );
static void     instance_finalize( GObject *dialog );

static void     on_base_initialize_gtk( FMAExportAsk *editor, GtkDialog *toplevel, gpointer user_data );
static void     on_base_initialize_window( FMAExportAsk *editor, gpointer user_data );
static void     keep_choice_on_toggled( GtkToggleButton *button, FMAExportAsk *editor );
static void     on_cancel_clicked( GtkButton *button, FMAExportAsk *editor );
static void     on_ok_clicked( GtkButton *button, FMAExportAsk *editor );
static gchar   *get_export_format( FMAExportAsk *editor );

GType
fma_export_ask_get_type( void )
{
	static GType dialog_type = 0;

	if( !dialog_type ){
		dialog_type = register_type();
	}

	return( dialog_type );
}

static GType
register_type( void )
{
	static const gchar *thisfn = "fma_export_ask_register_type";
	GType type;

	static GTypeInfo info = {
		sizeof( FMAExportAskClass ),
		( GBaseInitFunc ) NULL,
		( GBaseFinalizeFunc ) NULL,
		( GClassInitFunc ) class_init,
		NULL,
		NULL,
		sizeof( FMAExportAsk ),
		0,
		( GInstanceInitFunc ) instance_init
	};

	static const GInterfaceInfo ioptions_list_iface_info = {
		( GInterfaceInitFunc ) ioptions_list_iface_init,
		NULL,
		NULL
	};

	g_debug( "%s", thisfn );

	type = g_type_register_static( BASE_TYPE_DIALOG, "FMAExportAsk", &info, 0 );

	g_type_add_interface_static( type, FMA_TYPE_IOPTIONS_LIST, &ioptions_list_iface_info );

	return( type );
}

static void
class_init( FMAExportAskClass *klass )
{
	static const gchar *thisfn = "fma_export_ask_class_init";
	GObjectClass *object_class;

	g_debug( "%s: klass=%p", thisfn, ( void * ) klass );

	st_parent_class = g_type_class_peek_parent( klass );

	object_class = G_OBJECT_CLASS( klass );
	object_class->constructed = instance_constructed;
	object_class->dispose = instance_dispose;
	object_class->finalize = instance_finalize;
}

static void
ioptions_list_iface_init( FMAIOptionsListInterface *iface, void *user_data )
{
	static const gchar *thisfn = "fma_assistant_export_ioptions_list_iface_init";

	g_debug( "%s: iface=%p, user_data=%p", thisfn, ( void * ) iface, ( void * ) user_data );

	iface->get_options = ioptions_list_get_formats;
	iface->free_options = ioptions_list_free_formats;
}

static GList *
ioptions_list_get_formats( const FMAIOptionsList *instance, GtkWidget *container )
{
	FMAExportAsk *window;
	FMAApplication *application;
	FMAUpdater *updater;
	GList *formats;

	g_return_val_if_fail( FMA_IS_EXPORT_ASK( instance ), NULL );
	window = FMA_EXPORT_ASK( instance );

	application = FMA_APPLICATION( base_window_get_application( BASE_WINDOW( window )));
	updater = fma_application_get_updater( application );
	formats = fma_exporter_get_formats( FMA_PIVOT( updater ));

	return( formats );
}

static void
ioptions_list_free_formats( const FMAIOptionsList *instance, GtkWidget *container, GList *formats )
{
	fma_exporter_free_formats( formats );
}

static void
instance_init( GTypeInstance *instance, gpointer klass )
{
	static const gchar *thisfn = "fma_export_ask_instance_init";
	FMAExportAsk *self;

	g_return_if_fail( FMA_IS_EXPORT_ASK( instance ));

	g_debug( "%s: instance=%p (%s), klass=%p",
			thisfn, ( void * ) instance, G_OBJECT_TYPE_NAME( instance ), ( void * ) klass );

	self = FMA_EXPORT_ASK( instance );

	self->private = g_new0( FMAExportAskPrivate, 1 );

	self->private->dispose_has_run = FALSE;
}

static void
instance_constructed( GObject *dialog )
{
	static const gchar *thisfn = "fma_export_ask_instance_constructed";
	FMAExportAskPrivate *priv;

	g_return_if_fail( FMA_IS_EXPORT_ASK( dialog ));

	priv = FMA_EXPORT_ASK( dialog )->private;

	if( !priv->dispose_has_run ){

		/* chain up to the parent class */
		if( G_OBJECT_CLASS( st_parent_class )->constructed ){
			G_OBJECT_CLASS( st_parent_class )->constructed( dialog );
		}

		g_debug( "%s: dialog=%p (%s)", thisfn, ( void * ) dialog, G_OBJECT_TYPE_NAME( dialog ));

		base_window_signal_connect(
				BASE_WINDOW( dialog ),
				G_OBJECT( dialog ),
				BASE_SIGNAL_INITIALIZE_GTK,
				G_CALLBACK( on_base_initialize_gtk ));

		base_window_signal_connect(
				BASE_WINDOW( dialog ),
				G_OBJECT( dialog ),
				BASE_SIGNAL_INITIALIZE_WINDOW,
				G_CALLBACK( on_base_initialize_window ));
	}
}

static void
instance_dispose( GObject *dialog )
{
	static const gchar *thisfn = "fma_export_ask_instance_dispose";
	FMAExportAsk *self;

	g_return_if_fail( FMA_IS_EXPORT_ASK( dialog ));

	self = FMA_EXPORT_ASK( dialog );

	if( !self->private->dispose_has_run ){
		g_debug( "%s: dialog=%p (%s)", thisfn, ( void * ) dialog, G_OBJECT_TYPE_NAME( dialog ));

		self->private->dispose_has_run = TRUE;

		/* chain up to the parent class */
		if( G_OBJECT_CLASS( st_parent_class )->dispose ){
			G_OBJECT_CLASS( st_parent_class )->dispose( dialog );
		}
	}
}

static void
instance_finalize( GObject *dialog )
{
	static const gchar *thisfn = "fma_export_ask_instance_finalize";
	FMAExportAsk *self;

	g_return_if_fail( FMA_IS_EXPORT_ASK( dialog ));

	g_debug( "%s: dialog=%p (%s)", thisfn, ( void * ) dialog, G_OBJECT_TYPE_NAME( dialog ));

	self = FMA_EXPORT_ASK( dialog );

	g_free( self->private->format );

	g_free( self->private );

	/* chain call to parent class */
	if( G_OBJECT_CLASS( st_parent_class )->finalize ){
		G_OBJECT_CLASS( st_parent_class )->finalize( dialog );
	}
}

/**
 * fma_export_ask_user:
 * @item: the FMAObjectItem to be exported.
 * @first: whether this is the first call of a series.
 *  On a first call, the user is really asked for his choice.
 *  The next times, the 'keep-last-choice' flag will be considered.
 *
 * Initializes and runs the dialog.
 *
 * This is a small dialog which is to be ran during export operations,
 * when the set export format is 'Ask me'. Each exported file runs this
 * dialog, unless the user selects the 'keep same choice' box.
 *
 * Returns: the export format chosen by the user as a newly allocated
 * string which should be g_free() by the caller.
 * The function defaults to returning IPREFS_DEFAULT_EXPORT_FORMAT.
 *
 * When the user selects 'Keep same choice without asking me', this choice
 * becomes his new preferred export format.
 */
gchar *
fma_export_ask_user( FMAObjectItem *item, gboolean first )
{
	static const gchar *thisfn = "fma_export_ask_user";
	FMAExportAsk *editor;
	gboolean are_locked, mandatory;
	gboolean keep, keep_mandatory;
	int code;
	gchar *format;

	g_debug( "%s: item=%p (%s), first=%s",
			thisfn,
			( void * ) item, G_OBJECT_TYPE_NAME( item ),
			first ? "True":"False" );

	format = fma_settings_get_string( IPREFS_EXPORT_ASK_USER_LAST_FORMAT, NULL, &mandatory );
	keep = fma_settings_get_boolean( IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE, NULL, &keep_mandatory );

	if( first || !keep ){
		editor = g_object_new( FMA_TYPE_EXPORT_ASK,
				BASE_PROP_XMLUI_FILENAME, st_xmlui_filename,
				BASE_PROP_TOPLEVEL_NAME,  st_toplevel_name,
				BASE_PROP_WSP_NAME,       st_wsp_name,
				NULL );

		editor->private->format = g_strdup( format );
		editor->private->format_mandatory = mandatory;
		editor->private->keep_last_choice = keep;
		editor->private->keep_last_choice_mandatory = keep_mandatory;
		editor->private->item = item;

		are_locked = fma_settings_get_boolean( IPREFS_ADMIN_PREFERENCES_LOCKED, NULL, &mandatory );
		editor->private->preferences_locked = are_locked && mandatory;
		code = base_window_run( BASE_WINDOW( editor ));

		switch( code ){
			case GTK_RESPONSE_OK:
				g_free( format );
				format = get_export_format( editor );
				break;

			case GTK_RESPONSE_CANCEL:
			/* base_dialog::do_run only returns OK or CANCEL */
			default:
				g_free( format );
				format = g_strdup( EXPORTER_FORMAT_NOEXPORT );
				break;
		}

		g_object_unref( editor );
	}

	return( format );
}

static void
on_base_initialize_gtk( FMAExportAsk *editor, GtkDialog *toplevel, gpointer user_data )
{
	static const gchar *thisfn = "fma_export_ask_on_base_initialize_gtk";
	GtkWidget *container;

	g_return_if_fail( FMA_IS_EXPORT_ASK( editor ));

	if( !editor->private->dispose_has_run ){

		g_debug( "%s: dialog=%p, toplevel=%p, user_data=%p",
				thisfn, ( void * ) editor, ( void * ) toplevel, ( void * ) user_data );

		container = base_window_get_widget( BASE_WINDOW( editor ), "export-format-ask" );
		fma_ioptions_list_gtk_init( FMA_IOPTIONS_LIST( editor ), container, FALSE );
	}
}

static void
on_base_initialize_window( FMAExportAsk *editor, gpointer user_data )
{
	static const gchar *thisfn = "fma_export_ask_on_base_initialize_window";
	gchar *item_label, *label;
	GtkWidget *widget;
	FMAExportAskPrivate *priv;

	g_return_if_fail( FMA_IS_EXPORT_ASK( editor ));

	priv = editor->private;

	if( !priv->dispose_has_run ){

		g_debug( "%s: dialog=%p, user_data=%p", thisfn, ( void * ) editor, ( void * ) user_data );

		item_label = fma_object_get_label( priv->item );

		if( FMA_IS_OBJECT_ACTION( priv->item )){
			/* i18n: The action <label> is about to be exported */
			label = g_strdup_printf( _( "The action \"%s\" is about to be exported." ), item_label );
		} else {
			/* i18n: The menu <label> is about to be exported */
			label = g_strdup_printf( _( "The menu \"%s\" is about to be exported." ), item_label );
		}

		widget = base_window_get_widget( BASE_WINDOW( editor ), "ExportAskLabel" );
		gtk_label_set_text( GTK_LABEL( widget ), label );
		g_free( label );
		g_free( item_label );

		widget = base_window_get_widget( BASE_WINDOW( editor ), "export-format-ask" );
		fma_ioptions_list_set_editable(
				FMA_IOPTIONS_LIST( editor ), widget,
				!priv->format_mandatory && !priv->preferences_locked );
		fma_ioptions_list_set_default(
				FMA_IOPTIONS_LIST( editor ), widget,
				priv->format );

		base_gtk_utils_toggle_set_initial_state( BASE_WINDOW( editor ),
				"AskKeepChoiceButton", G_CALLBACK( keep_choice_on_toggled ),
				priv->keep_last_choice,
				!priv->keep_last_choice_mandatory, !priv->preferences_locked );

		base_window_signal_connect_by_name(
				BASE_WINDOW( editor ),
				"CancelButton",
				"clicked",
				G_CALLBACK( on_cancel_clicked ));

		base_window_signal_connect_by_name(
				BASE_WINDOW( editor ),
				"OKButton",
				"clicked",
				G_CALLBACK( on_ok_clicked ));
	}
}

static void
keep_choice_on_toggled( GtkToggleButton *button, FMAExportAsk *editor )
{
	gboolean editable;

	editable = ( gboolean ) GPOINTER_TO_UINT( g_object_get_data( G_OBJECT( button ), FMA_TOGGLE_DATA_EDITABLE ));

	if( editable ){
		editor->private->keep_last_choice = gtk_toggle_button_get_active( button );

	} else {
		base_gtk_utils_toggle_reset_initial_state( button );
	}
}

static void
on_cancel_clicked( GtkButton *button, FMAExportAsk *editor )
{
	GtkWindow *toplevel = base_window_get_gtk_toplevel( BASE_WINDOW( editor ));
	gtk_dialog_response( GTK_DIALOG( toplevel ), GTK_RESPONSE_CLOSE );
}

static void
on_ok_clicked( GtkButton *button, FMAExportAsk *editor )
{
	GtkWindow *toplevel = base_window_get_gtk_toplevel( BASE_WINDOW( editor ));
	gtk_dialog_response( GTK_DIALOG( toplevel ), GTK_RESPONSE_OK );
}

/*
 * we have come here because preferred_export_format was 'Ask'
 * in all cases, the chosen format is kept in 'ask_user_last_chosen_format'
 * and so this will be the default format which will be proposed the next
 * time we come here
 * if the 'remember_my_choice' is cheecked, then we do not even ask the user
 * but returns as soon as we enter
 *
 * not overrinding the preferred export format (i.e. letting it to 'Ask')
 * let the user go back in ask dialog box the next time he will have some
 * files to export
 */
static gchar *
get_export_format( FMAExportAsk *editor )
{
	GtkWidget *widget;
	FMAIOption *format;
	gchar *format_id;

	widget = base_window_get_widget( BASE_WINDOW( editor ), "export-format-ask" );
	format = fma_ioptions_list_get_selected( FMA_IOPTIONS_LIST( editor ), widget );
	g_return_val_if_fail( FMA_IS_EXPORT_FORMAT( format ), 0 );

	if( !editor->private->keep_last_choice_mandatory ){
		fma_settings_set_boolean( IPREFS_EXPORT_ASK_USER_KEEP_LAST_CHOICE, editor->private->keep_last_choice );
	}

	format_id = fma_ioption_get_id( format );
	fma_settings_set_string( IPREFS_EXPORT_ASK_USER_LAST_FORMAT, format_id );

	return( format_id );
}