Set Nautilus-Actions as being the actual official product name
[nautilus-actions.git] / src / nact / nact-export-format.c
blob5b0cbc6846e48c243118a5e103c3e6adcfa0b972
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 <core/na-exporter.h>
38 #include <core/na-export-format.h>
40 #include "nact-iprefs.h"
41 #include "nact-export-format.h"
43 #define EXPORT_FORMAT_PROP_OBJECT "nact-export-format-prop-object"
44 #define EXPORT_FORMAT_PROP_BUTTON "nact-export-format-prop-button"
46 #define ASKME_LABEL N_( "_Ask me" )
47 #define ASKME_DESCRIPTION N_( "You will be asked for the format to choose each time an item is about to be exported." )
49 static const NAIExporterFormat st_ask_str = { "Ask", ASKME_LABEL, ASKME_DESCRIPTION };
51 static void draw_in_vbox( const NAExportFormat *format, GtkWidget *vbox, guint mode, gint id );
52 static void format_weak_notify( NAExportFormat *format, GObject *vbox );
53 static void select_default_iter( GtkWidget *widget, void *quark_ptr );
54 static void get_selected_iter( GtkWidget *widget, NAExportFormat **format );
56 /**
57 * nact_export_format_init_display:
58 * @pivot: the #NAPivot instance.
59 * @vbox: the #GtkVBox in which the display must be drawn.
60 * @mode: the type of the display.
62 * Displays the available export formats in the VBox.
63 * Should only be called once per dialog box instance.
65 void
66 nact_export_format_init_display( const NAPivot *pivot, GtkWidget *vbox, guint mode )
68 static const gchar *thisfn = "nact_export_format_init_display";
69 GList *formats, *ifmt;
70 NAExportFormat *format;
72 formats = na_exporter_get_formats( pivot );
74 for( ifmt = formats ; ifmt ; ifmt = ifmt->next ){
75 draw_in_vbox( NA_EXPORT_FORMAT( ifmt->data ), vbox, mode, -1 );
78 na_exporter_free_formats( formats );
80 switch( mode ){
82 case EXPORT_FORMAT_DISPLAY_PREFERENCES:
83 case EXPORT_FORMAT_DISPLAY_ASSISTANT:
84 format = na_export_format_new( &st_ask_str, NULL );
85 draw_in_vbox( format, vbox, mode, IPREFS_EXPORT_FORMAT_ASK );
86 g_object_unref( format );
87 break;
89 /* this is the mode to be used when we are about to export an item
90 * and the user preference is 'Ask me'; obviously, we don't propose
91 * here to ask him another time :)
93 case EXPORT_FORMAT_DISPLAY_ASK:
94 break;
96 default:
97 g_warning( "%s: mode=%d: unknown mode", thisfn, mode );
102 * container
103 * +- vbox
104 * | +- radio button
105 * | +- hbox
106 * | | +- description
108 static void
109 draw_in_vbox( const NAExportFormat *format, GtkWidget *container, guint mode, gint id )
111 static GtkRadioButton *first_button = NULL;
112 GtkVBox *vbox;
113 gchar *description;
114 GtkHBox *hbox;
115 GtkRadioButton *button;
116 guint size, spacing;
117 gchar *markup, *label;
118 GtkLabel *desc_label;
120 vbox = GTK_VBOX( gtk_vbox_new( FALSE, 0 ));
121 gtk_box_pack_start( GTK_BOX( container ), GTK_WIDGET( vbox ), FALSE, TRUE, 0 );
122 description = na_export_format_get_description( format );
123 g_object_set( G_OBJECT( vbox ), "tooltip-text", description, NULL );
124 g_object_set( G_OBJECT( vbox ), "spacing", 6, NULL );
126 button = GTK_RADIO_BUTTON( gtk_radio_button_new( NULL ));
127 if( first_button ){
128 g_object_set( G_OBJECT( button ), "group", first_button, NULL );
129 } else {
130 first_button = button;
132 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, TRUE, 0 );
134 label = NULL;
135 markup = NULL;
136 switch( mode ){
138 case EXPORT_FORMAT_DISPLAY_ASK:
139 case EXPORT_FORMAT_DISPLAY_PREFERENCES:
140 case EXPORT_FORMAT_DISPLAY_ASSISTANT:
141 label = na_export_format_get_label( format );
142 markup = g_markup_printf_escaped( "%s", label );
143 gtk_button_set_label( GTK_BUTTON( button ), label );
144 g_object_set( G_OBJECT( button ), "use_underline", TRUE, NULL );
145 break;
147 /* this work fine, but it appears that this is not consistant with import assistant */
148 /*case EXPORT_FORMAT_DISPLAY_ASSISTANT:
149 radio_label = GTK_LABEL( gtk_label_new( NULL ));
150 label = na_export_format_get_label( format );
151 markup = g_markup_printf_escaped( "<b>%s</b>", label );
152 gtk_label_set_markup( radio_label, markup );
153 gtk_container_add( GTK_CONTAINER( button ), GTK_WIDGET( radio_label ));
154 break;*/
157 desc_label = NULL;
158 switch( mode ){
160 case EXPORT_FORMAT_DISPLAY_ASSISTANT:
161 hbox = GTK_HBOX( gtk_hbox_new( TRUE, 0 ));
162 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( hbox ), FALSE, TRUE, 0 );
164 gtk_widget_style_get( GTK_WIDGET( button ), "indicator-size", &size, NULL );
165 gtk_widget_style_get( GTK_WIDGET( button ), "indicator-spacing", &spacing, NULL );
166 size += 2*spacing;
168 desc_label = GTK_LABEL( gtk_label_new( description ));
169 g_object_set( G_OBJECT( desc_label ), "xpad", size, NULL );
170 g_object_set( G_OBJECT( desc_label ), "xalign", 0, NULL );
171 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( desc_label ), TRUE, TRUE, 4 );
172 break;
175 g_object_set_data( G_OBJECT( vbox ), EXPORT_FORMAT_PROP_BUTTON, button );
176 g_object_set_data( G_OBJECT( vbox ), EXPORT_FORMAT_PROP_OBJECT, g_object_ref(( gpointer ) format ));
177 g_object_weak_ref( G_OBJECT( vbox ), ( GWeakNotify ) format_weak_notify, ( gpointer ) format );
179 g_free( markup );
180 g_free( label );
181 g_free( description );
184 static void
185 format_weak_notify( NAExportFormat *format, GObject *vbox )
187 static const gchar *thisfn = "nact_export_format_weak_notify";
189 g_debug( "%s: format=%p (%s), vbox=%p",
190 thisfn, ( void * ) format, G_OBJECT_TYPE_NAME( format ), ( void * ) vbox );
192 g_object_unref( format );
196 * nact_export_format_select:
197 * @container: the #GtkVBox in which the display has been drawn.
198 * @format: the #GQuark which must be used as a default value.
200 * Select the default value.
202 void
203 nact_export_format_select( const GtkWidget *container, GQuark format )
205 void *quark_ptr;
207 quark_ptr = GUINT_TO_POINTER( format );
208 gtk_container_foreach( GTK_CONTAINER( container ), ( GtkCallback ) select_default_iter, quark_ptr );
211 static void
212 select_default_iter( GtkWidget *widget, void *quark_ptr )
214 NAExportFormat *format;
215 GQuark format_quark;
216 GtkRadioButton *button;
218 format_quark = ( GQuark ) GPOINTER_TO_UINT( quark_ptr );
219 format = NA_EXPORT_FORMAT( g_object_get_data( G_OBJECT( widget ), EXPORT_FORMAT_PROP_OBJECT ));
221 if( na_export_format_get_quark( format ) == format_quark ){
222 button = GTK_RADIO_BUTTON( g_object_get_data( G_OBJECT( widget ), EXPORT_FORMAT_PROP_BUTTON ));
223 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
228 * nact_export_format_get_selected:
229 * @container: the #GtkVBox in which the display has been drawn.
231 * Returns: the currently selected value, as a #NAExportFormat object.
233 * The returned #NAExportFormat is owned by #NactExportFormat, and
234 * should not be released by the caller.
236 NAExportFormat *
237 nact_export_format_get_selected( const GtkWidget *container )
239 NAExportFormat *format;
241 format = NULL;
242 gtk_container_foreach( GTK_CONTAINER( container ), ( GtkCallback ) get_selected_iter, &format );
243 g_debug( "nact_export_format_get_selected: format=%p", ( void * ) format );
245 return( format );
248 static void
249 get_selected_iter( GtkWidget *widget, NAExportFormat **format )
251 GtkRadioButton *button;
253 if( !( *format )){
254 button = GTK_RADIO_BUTTON( g_object_get_data( G_OBJECT( widget ), EXPORT_FORMAT_PROP_BUTTON ));
255 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( button ))){
256 *format = NA_EXPORT_FORMAT( g_object_get_data( G_OBJECT( widget ), EXPORT_FORMAT_PROP_OBJECT ));