2 * e-attachment-dialog.c
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
25 #include "e-attachment-dialog.h"
27 #include <glib/gi18n.h>
29 #define E_ATTACHMENT_DIALOG_GET_PRIVATE(obj) \
30 (G_TYPE_INSTANCE_GET_PRIVATE \
31 ((obj), E_TYPE_ATTACHMENT_DIALOG, EAttachmentDialogPrivate))
33 struct _EAttachmentDialogPrivate
{
34 EAttachment
*attachment
;
35 GtkWidget
*display_name_entry
;
36 GtkWidget
*description_entry
;
37 GtkWidget
*content_type_label
;
38 GtkWidget
*disposition_checkbox
;
52 attachment_dialog_update (EAttachmentDialog
*dialog
)
54 EAttachment
*attachment
;
57 const gchar
*content_type
;
58 const gchar
*display_name
;
61 gchar
*type_description
= NULL
;
65 attachment
= e_attachment_dialog_get_attachment (dialog
);
67 if (attachment
!= NULL
) {
68 file_info
= e_attachment_ref_file_info (attachment
);
69 description
= e_attachment_dup_description (attachment
);
70 disposition
= e_attachment_dup_disposition (attachment
);
77 if (file_info
!= NULL
) {
78 content_type
= g_file_info_get_content_type (file_info
);
79 display_name
= g_file_info_get_display_name (file_info
);
85 if (content_type
!= NULL
) {
89 comment
= g_content_type_get_description (content_type
);
90 mime_type
= g_content_type_get_mime_type (content_type
);
93 g_strdup_printf ("%s (%s)", comment
, mime_type
);
99 sensitive
= G_IS_FILE_INFO (file_info
);
101 gtk_dialog_set_response_sensitive (
102 GTK_DIALOG (dialog
), GTK_RESPONSE_OK
, sensitive
);
104 widget
= dialog
->priv
->display_name_entry
;
105 gtk_widget_set_sensitive (widget
, sensitive
);
106 if (display_name
!= NULL
)
107 gtk_entry_set_text (GTK_ENTRY (widget
), display_name
);
109 widget
= dialog
->priv
->description_entry
;
110 gtk_widget_set_sensitive (widget
, sensitive
);
111 if (description
!= NULL
)
112 gtk_entry_set_text (GTK_ENTRY (widget
), description
);
114 widget
= dialog
->priv
->content_type_label
;
115 gtk_label_set_text (GTK_LABEL (widget
), type_description
);
117 active
= (g_strcmp0 (disposition
, "inline") == 0);
118 widget
= dialog
->priv
->disposition_checkbox
;
119 gtk_widget_set_sensitive (widget
, sensitive
);
120 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (widget
), active
);
122 g_free (description
);
123 g_free (disposition
);
124 g_free (type_description
);
126 g_clear_object (&file_info
);
130 attachment_dialog_set_property (GObject
*object
,
135 switch (property_id
) {
136 case PROP_ATTACHMENT
:
137 e_attachment_dialog_set_attachment (
138 E_ATTACHMENT_DIALOG (object
),
139 g_value_get_object (value
));
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
147 attachment_dialog_get_property (GObject
*object
,
152 switch (property_id
) {
153 case PROP_ATTACHMENT
:
155 value
, e_attachment_dialog_get_attachment (
156 E_ATTACHMENT_DIALOG (object
)));
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
164 attachment_dialog_dispose (GObject
*object
)
166 EAttachmentDialogPrivate
*priv
;
168 priv
= E_ATTACHMENT_DIALOG_GET_PRIVATE (object
);
170 if (priv
->attachment
!= NULL
) {
171 g_object_unref (priv
->attachment
);
172 priv
->attachment
= NULL
;
175 if (priv
->display_name_entry
!= NULL
) {
176 g_object_unref (priv
->display_name_entry
);
177 priv
->display_name_entry
= NULL
;
180 if (priv
->description_entry
!= NULL
) {
181 g_object_unref (priv
->description_entry
);
182 priv
->description_entry
= NULL
;
185 if (priv
->content_type_label
!= NULL
) {
186 g_object_unref (priv
->content_type_label
);
187 priv
->content_type_label
= NULL
;
190 if (priv
->disposition_checkbox
!= NULL
) {
191 g_object_unref (priv
->disposition_checkbox
);
192 priv
->disposition_checkbox
= NULL
;
195 /* Chain up to parent's dispose() method. */
196 G_OBJECT_CLASS (e_attachment_dialog_parent_class
)->dispose (object
);
200 attachment_dialog_map (GtkWidget
*widget
)
202 GtkWidget
*action_area
;
203 GtkWidget
*content_area
;
205 /* Chain up to parent's map() method. */
206 GTK_WIDGET_CLASS (e_attachment_dialog_parent_class
)->map (widget
);
208 /* XXX Override GtkDialog's broken style property defaults. */
209 action_area
= gtk_dialog_get_action_area (GTK_DIALOG (widget
));
210 content_area
= gtk_dialog_get_content_area (GTK_DIALOG (widget
));
212 gtk_box_set_spacing (GTK_BOX (content_area
), 12);
213 gtk_container_set_border_width (GTK_CONTAINER (action_area
), 0);
214 gtk_container_set_border_width (GTK_CONTAINER (content_area
), 12);
218 attachment_dialog_response (GtkDialog
*dialog
,
221 EAttachmentDialogPrivate
*priv
;
222 EAttachment
*attachment
;
223 GtkToggleButton
*button
;
224 GFileInfo
*file_info
;
225 CamelMimePart
*mime_part
;
226 const gchar
*attribute
;
230 if (response_id
!= GTK_RESPONSE_OK
)
233 priv
= E_ATTACHMENT_DIALOG_GET_PRIVATE (dialog
);
234 g_return_if_fail (E_IS_ATTACHMENT (priv
->attachment
));
235 attachment
= priv
->attachment
;
237 file_info
= e_attachment_ref_file_info (attachment
);
238 g_return_if_fail (G_IS_FILE_INFO (file_info
));
240 mime_part
= e_attachment_ref_mime_part (attachment
);
242 attribute
= G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
;
243 text
= gtk_entry_get_text (GTK_ENTRY (priv
->display_name_entry
));
244 g_file_info_set_attribute_string (file_info
, attribute
, text
);
246 if (mime_part
!= NULL
)
247 camel_mime_part_set_filename (mime_part
, text
);
249 attribute
= G_FILE_ATTRIBUTE_STANDARD_DESCRIPTION
;
250 text
= gtk_entry_get_text (GTK_ENTRY (priv
->description_entry
));
251 g_file_info_set_attribute_string (file_info
, attribute
, text
);
253 if (mime_part
!= NULL
)
254 camel_mime_part_set_description (mime_part
, text
);
256 button
= GTK_TOGGLE_BUTTON (priv
->disposition_checkbox
);
257 active
= gtk_toggle_button_get_active (button
);
258 text
= active
? "inline" : "attachment";
259 e_attachment_set_disposition (attachment
, text
);
261 if (mime_part
!= NULL
)
262 camel_mime_part_set_disposition (mime_part
, text
);
264 g_clear_object (&file_info
);
265 g_clear_object (&mime_part
);
267 g_object_notify (G_OBJECT (attachment
), "file-info");
271 e_attachment_dialog_class_init (EAttachmentDialogClass
*class)
273 GObjectClass
*object_class
;
274 GtkWidgetClass
*widget_class
;
275 GtkDialogClass
*dialog_class
;
277 g_type_class_add_private (class, sizeof (EAttachmentDialogPrivate
));
279 object_class
= G_OBJECT_CLASS (class);
280 object_class
->set_property
= attachment_dialog_set_property
;
281 object_class
->get_property
= attachment_dialog_get_property
;
282 object_class
->dispose
= attachment_dialog_dispose
;
284 widget_class
= GTK_WIDGET_CLASS (class);
285 widget_class
->map
= attachment_dialog_map
;
287 dialog_class
= GTK_DIALOG_CLASS (class);
288 dialog_class
->response
= attachment_dialog_response
;
290 g_object_class_install_property (
293 g_param_spec_object (
303 e_attachment_dialog_init (EAttachmentDialog
*dialog
)
305 GtkWidget
*container
;
308 dialog
->priv
= E_ATTACHMENT_DIALOG_GET_PRIVATE (dialog
);
310 gtk_dialog_add_button (
311 GTK_DIALOG (dialog
), _("_Cancel"), GTK_RESPONSE_CANCEL
);
312 gtk_dialog_add_button (
313 GTK_DIALOG (dialog
), _("_OK"), GTK_RESPONSE_OK
);
314 gtk_window_set_icon_name (
315 GTK_WINDOW (dialog
), "mail-attachment");
316 gtk_window_set_title (
317 GTK_WINDOW (dialog
), _("Attachment Properties"));
319 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_OK
);
321 container
= gtk_dialog_get_content_area (GTK_DIALOG (dialog
));
323 widget
= gtk_table_new (4, 2, FALSE
);
324 gtk_table_set_col_spacings (GTK_TABLE (widget
), 6);
325 gtk_table_set_row_spacings (GTK_TABLE (widget
), 6);
326 gtk_box_pack_start (GTK_BOX (container
), widget
, TRUE
, TRUE
, 0);
327 gtk_widget_show (widget
);
331 widget
= gtk_entry_new ();
332 gtk_entry_set_activates_default (GTK_ENTRY (widget
), TRUE
);
334 GTK_TABLE (container
), widget
,
335 1, 2, 0, 1, GTK_FILL
| GTK_EXPAND
, 0, 0, 0);
336 dialog
->priv
->display_name_entry
= g_object_ref (widget
);
337 gtk_widget_show (widget
);
339 widget
= gtk_label_new_with_mnemonic (_("F_ilename:"));
340 gtk_misc_set_alignment (GTK_MISC (widget
), 1.0, 0.5);
341 gtk_label_set_mnemonic_widget (
342 GTK_LABEL (widget
), dialog
->priv
->display_name_entry
);
344 GTK_TABLE (container
), widget
,
345 0, 1, 0, 1, GTK_FILL
, 0, 0, 0);
346 gtk_widget_show (widget
);
348 widget
= gtk_entry_new ();
349 gtk_entry_set_activates_default (GTK_ENTRY (widget
), TRUE
);
351 GTK_TABLE (container
), widget
,
352 1, 2, 1, 2, GTK_FILL
| GTK_EXPAND
, 0, 0, 0);
353 dialog
->priv
->description_entry
= g_object_ref (widget
);
354 gtk_widget_show (widget
);
356 widget
= gtk_label_new_with_mnemonic (_("_Description:"));
357 gtk_misc_set_alignment (GTK_MISC (widget
), 1.0, 0.5);
358 gtk_label_set_mnemonic_widget (
359 GTK_LABEL (widget
), dialog
->priv
->description_entry
);
361 GTK_TABLE (container
), widget
,
362 0, 1, 1, 2, GTK_FILL
, 0, 0, 0);
363 gtk_widget_show (widget
);
365 widget
= gtk_label_new (NULL
);
366 gtk_label_set_selectable (GTK_LABEL (widget
), TRUE
);
367 gtk_misc_set_alignment (GTK_MISC (widget
), 0.0, 0.5);
369 GTK_TABLE (container
), widget
,
370 1, 2, 2, 3, GTK_FILL
| GTK_EXPAND
, 0, 0, 0);
371 dialog
->priv
->content_type_label
= g_object_ref (widget
);
372 gtk_widget_show (widget
);
374 widget
= gtk_label_new (_("MIME Type:"));
375 gtk_misc_set_alignment (GTK_MISC (widget
), 1.0, 0.5);
377 GTK_TABLE (container
), widget
,
378 0, 1, 2, 3, GTK_FILL
, 0, 0, 0);
379 gtk_widget_show (widget
);
381 widget
= gtk_check_button_new_with_mnemonic (
382 _("_Suggest automatic display of attachment"));
384 GTK_TABLE (container
), widget
,
385 0, 2, 3, 4, GTK_FILL
| GTK_EXPAND
, 0, 0, 0);
386 dialog
->priv
->disposition_checkbox
= g_object_ref (widget
);
387 gtk_widget_show (widget
);
391 e_attachment_dialog_new (GtkWindow
*parent
,
392 EAttachment
*attachment
)
395 g_return_val_if_fail (GTK_IS_WINDOW (parent
), NULL
);
396 if (attachment
!= NULL
)
397 g_return_val_if_fail (E_IS_ATTACHMENT (attachment
), NULL
);
399 return g_object_new (
400 E_TYPE_ATTACHMENT_DIALOG
,
401 "transient-for", parent
, "attachment", attachment
, NULL
);
405 e_attachment_dialog_get_attachment (EAttachmentDialog
*dialog
)
407 g_return_val_if_fail (E_IS_ATTACHMENT_DIALOG (dialog
), NULL
);
409 return dialog
->priv
->attachment
;
413 e_attachment_dialog_set_attachment (EAttachmentDialog
*dialog
,
414 EAttachment
*attachment
)
416 g_return_if_fail (E_IS_ATTACHMENT_DIALOG (dialog
));
418 if (attachment
!= NULL
) {
419 g_return_if_fail (E_IS_ATTACHMENT (attachment
));
420 g_object_ref (attachment
);
423 if (dialog
->priv
->attachment
!= NULL
)
424 g_object_unref (dialog
->priv
->attachment
);
426 dialog
->priv
->attachment
= attachment
;
428 attachment_dialog_update (dialog
);
430 g_object_notify (G_OBJECT (dialog
), "attachment");