Updated Spanish translation
[evolution.git] / e-util / e-attachment-dialog.c
blob0f71bc9390c62edbec2b1998bbb1b9b92b8ed943
1 /*
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
11 * for more details.
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)
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
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;
41 enum {
42 PROP_0,
43 PROP_ATTACHMENT
46 G_DEFINE_TYPE (
47 EAttachmentDialog,
48 e_attachment_dialog,
49 GTK_TYPE_DIALOG)
51 static void
52 attachment_dialog_update (EAttachmentDialog *dialog)
54 EAttachment *attachment;
55 GFileInfo *file_info;
56 GtkWidget *widget;
57 const gchar *content_type;
58 const gchar *display_name;
59 gchar *description;
60 gchar *disposition;
61 gchar *type_description = NULL;
62 gboolean sensitive;
63 gboolean active;
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);
71 } else {
72 file_info = NULL;
73 description = NULL;
74 disposition = NULL;
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);
80 } else {
81 content_type = NULL;
82 display_name = NULL;
85 if (content_type != NULL) {
86 gchar *comment;
87 gchar *mime_type;
89 comment = g_content_type_get_description (content_type);
90 mime_type = g_content_type_get_mime_type (content_type);
92 type_description =
93 g_strdup_printf ("%s (%s)", comment, mime_type);
95 g_free (comment);
96 g_free (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);
129 static void
130 attachment_dialog_set_property (GObject *object,
131 guint property_id,
132 const GValue *value,
133 GParamSpec *pspec)
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));
140 return;
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
146 static void
147 attachment_dialog_get_property (GObject *object,
148 guint property_id,
149 GValue *value,
150 GParamSpec *pspec)
152 switch (property_id) {
153 case PROP_ATTACHMENT:
154 g_value_set_object (
155 value, e_attachment_dialog_get_attachment (
156 E_ATTACHMENT_DIALOG (object)));
157 return;
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 static void
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);
199 static void
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);
217 static void
218 attachment_dialog_response (GtkDialog *dialog,
219 gint response_id)
221 EAttachmentDialogPrivate *priv;
222 EAttachment *attachment;
223 GtkToggleButton *button;
224 GFileInfo *file_info;
225 CamelMimePart *mime_part;
226 const gchar *attribute;
227 const gchar *text;
228 gboolean active;
230 if (response_id != GTK_RESPONSE_OK)
231 return;
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");
270 static void
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 (
291 object_class,
292 PROP_ATTACHMENT,
293 g_param_spec_object (
294 "attachment",
295 "Attachment",
296 NULL,
297 E_TYPE_ATTACHMENT,
298 G_PARAM_READWRITE |
299 G_PARAM_CONSTRUCT));
302 static void
303 e_attachment_dialog_init (EAttachmentDialog *dialog)
305 GtkWidget *container;
306 GtkWidget *widget;
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);
329 container = widget;
331 widget = gtk_entry_new ();
332 gtk_entry_set_activates_default (GTK_ENTRY (widget), TRUE);
333 gtk_table_attach (
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);
343 gtk_table_attach (
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);
350 gtk_table_attach (
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);
360 gtk_table_attach (
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);
368 gtk_table_attach (
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);
376 gtk_table_attach (
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"));
383 gtk_table_attach (
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);
390 GtkWidget *
391 e_attachment_dialog_new (GtkWindow *parent,
392 EAttachment *attachment)
394 if (parent != NULL)
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);
404 EAttachment *
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;
412 void
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");